Small changes to the DB migration

pull/3528/head
tidusjar 5 years ago
parent c065235776
commit d7cdf68a86

@ -1,6 +1,6 @@
using System; using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations; using Microsoft.EntityFrameworkCore.Migrations;
using System;
namespace Ombi.Store.Migrations.OmbiMySql namespace Ombi.Store.Migrations.OmbiMySql
{ {
@ -8,14 +8,34 @@ namespace Ombi.Store.Migrations.OmbiMySql
{ {
protected override void Up(MigrationBuilder migrationBuilder) protected override void Up(MigrationBuilder migrationBuilder)
{ {
migrationBuilder.Sql(@"CREATE TABLE `MobileDevices` ( // migrationBuilder.Sql(@"CREATE TABLE `MobileDevices` (
`Id` int NOT NULL AUTO_INCREMENT, // `Id` int NOT NULL AUTO_INCREMENT,
`Token` longtext CHARACTER SET utf8mb4 NULL, // `Token` longtext CHARACTER SET utf8mb4 NULL,
`UserId` varchar(255) COLLATE utf8mb4_bin NOT NULL, // `UserId` varchar(255) COLLATE utf8mb4_bin NOT NULL,
`AddedAt` datetime(6) NOT NULL, // `AddedAt` datetime(6) NOT NULL,
CONSTRAINT `PK_MobileDevices` PRIMARY KEY (`Id`), // CONSTRAINT `PK_MobileDevices` PRIMARY KEY (`Id`),
CONSTRAINT `FK_MobileDevices_AspNetUsers_UserId` FOREIGN KEY (`UserId`) REFERENCES `AspNetUsers` (`Id`) ON DELETE RESTRICT // CONSTRAINT `FK_MobileDevices_AspNetUsers_UserId` FOREIGN KEY (`UserId`) REFERENCES `AspNetUsers` (`Id`) ON DELETE RESTRICT
);"); //);");
migrationBuilder.CreateTable(
name: "MobileDevices",
columns: table => new
{
Id = table.Column<int>(nullable: false).Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn),
Token = table.Column<string>(maxLength: 256, nullable: true),
UserId = table.Column<string>(maxLength: 256, nullable: false),
AddedAt = table.Column<DateTime>(maxLength: 256, nullable: false),
},
constraints: table =>
{
table.PrimaryKey("PK_MobileDevices", x => x.Id);
table.ForeignKey(
name: "FK_MobileDevices_AspNetUsers_UserId",
column: x => x.UserId,
principalTable: "AspNetUsers",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
});
migrationBuilder.CreateIndex( migrationBuilder.CreateIndex(

@ -39,6 +39,10 @@ namespace Ombi.Controllers.V2
var username = User.Identity.Name.ToUpper(); var username = User.Identity.Name.ToUpper();
var user = await _userManager.Users.FirstOrDefaultAsync(x => x.NormalizedUserName == username); var user = await _userManager.Users.FirstOrDefaultAsync(x => x.NormalizedUserName == username);
if (user == null)
{
return Ok();
}
// Check if we already have this notification id // Check if we already have this notification id
var alreadyExists = await _mobileDevices.GetAll().AnyAsync(x => x.Token == body.Token && x.UserId == user.Id); var alreadyExists = await _mobileDevices.GetAll().AnyAsync(x => x.Token == body.Token && x.UserId == user.Id);
@ -46,8 +50,14 @@ namespace Ombi.Controllers.V2
{ {
return Ok(); return Ok();
} }
// Ensure we don't have too many already for this user
var tokens = await _mobileDevices.GetAll().Where(x => x.UserId == user.Id).OrderBy(x => x.AddedAt).ToListAsync();
if (tokens.Count() > 5)
{
var toDelete = tokens.Take(tokens.Count() - 5);
await _mobileDevices.DeleteRange(toDelete);
}
// let's add it
await _mobileDevices.Add(new MobileDevices await _mobileDevices.Add(new MobileDevices
{ {
Token = body.Token, Token = body.Token,

Loading…
Cancel
Save