From 26bbbf80c286988a2711f75a4acb3999697f7ddf Mon Sep 17 00:00:00 2001 From: Mateusz Skoczek Date: Sun, 17 Mar 2024 14:26:03 +0100 Subject: [PATCH] genre added --- .../WatchIt.Database.Model/EntityBuilder.cs | 6 +- .../WatchIt.Database.Model/Genre/Genre.cs | 49 +++++ .../WatchIt.Database.Model/IEntity.cs | 1 + .../WatchIt.Database/DatabaseContext.cs | 7 + ... => 20240317131418_Migration1.Designer.cs} | 27 ++- ...ation1.cs => 20240317131418_Migration1.cs} | 23 +++ .../20240317132500_Migration2.Designer.cs | 183 ++++++++++++++++++ .../Migrations/20240317132500_Migration2.cs | 45 +++++ .../DatabaseContextModelSnapshot.cs | 42 ++++ WatchIt/WatchIt.csproj | 1 - 10 files changed, 381 insertions(+), 3 deletions(-) create mode 100644 WatchIt.Database/WatchIt.Database.Model/Genre/Genre.cs rename WatchIt.Database/WatchIt.Database/Migrations/{20240317125055_Migration1.Designer.cs => 20240317131418_Migration1.Designer.cs} (84%) rename WatchIt.Database/WatchIt.Database/Migrations/{20240317125055_Migration1.cs => 20240317131418_Migration1.cs} (80%) create mode 100644 WatchIt.Database/WatchIt.Database/Migrations/20240317132500_Migration2.Designer.cs create mode 100644 WatchIt.Database/WatchIt.Database/Migrations/20240317132500_Migration2.cs diff --git a/WatchIt.Database/WatchIt.Database.Model/EntityBuilder.cs b/WatchIt.Database/WatchIt.Database.Model/EntityBuilder.cs index 5d58bd5..b1f7c24 100644 --- a/WatchIt.Database/WatchIt.Database.Model/EntityBuilder.cs +++ b/WatchIt.Database/WatchIt.Database.Model/EntityBuilder.cs @@ -13,7 +13,11 @@ namespace WatchIt.Database.Model #region PUBLIC METHODS public static void Build(ModelBuilder builder) where T : class, IEntity => Build(builder.Entity()); - public static void Build(EntityTypeBuilder builder) where T : class, IEntity => T.Build(builder); + public static void Build(EntityTypeBuilder builder) where T : class, IEntity + { + T.Build(builder); + builder.HasData(T.InsertData()); + } #endregion } diff --git a/WatchIt.Database/WatchIt.Database.Model/Genre/Genre.cs b/WatchIt.Database/WatchIt.Database.Model/Genre/Genre.cs new file mode 100644 index 0000000..fd0bb91 --- /dev/null +++ b/WatchIt.Database/WatchIt.Database.Model/Genre/Genre.cs @@ -0,0 +1,49 @@ +using Microsoft.EntityFrameworkCore.Metadata.Builders; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace WatchIt.Database.Model.Genre +{ + public class Genre : IEntity + { + #region PROPERTIES + + public short Id { get; set; } + public string Name { get; set; } + public string? Description { get; set; } + + #endregion + + + + #region PUBLIC METHODS + + static void IEntity.Build(EntityTypeBuilder builder) + { + builder.HasKey(x => x.Id); + builder.HasIndex(x => x.Id) + .IsUnique(); + builder.Property(x => x.Id) + .IsRequired(); + + builder.Property(x => x.Name) + .HasMaxLength(100) + .IsRequired(); + + builder.Property(x => x.Description) + .HasMaxLength(1000); + } + + static IEnumerable IEntity.InsertData() => new List + { + new Genre { Id = 1, Name = "Comedy" }, + new Genre { Id = 2, Name = "Thriller" }, + new Genre { Id = 3, Name = "Horror" }, + }; + + #endregion + } +} diff --git a/WatchIt.Database/WatchIt.Database.Model/IEntity.cs b/WatchIt.Database/WatchIt.Database.Model/IEntity.cs index de27755..ff6afc1 100644 --- a/WatchIt.Database/WatchIt.Database.Model/IEntity.cs +++ b/WatchIt.Database/WatchIt.Database.Model/IEntity.cs @@ -13,6 +13,7 @@ namespace WatchIt.Database.Model #region METHODS static abstract void Build(EntityTypeBuilder builder); + static virtual IEnumerable InsertData() => Array.Empty(); #endregion } diff --git a/WatchIt.Database/WatchIt.Database/DatabaseContext.cs b/WatchIt.Database/WatchIt.Database/DatabaseContext.cs index 8634160..24b2871 100644 --- a/WatchIt.Database/WatchIt.Database/DatabaseContext.cs +++ b/WatchIt.Database/WatchIt.Database/DatabaseContext.cs @@ -7,6 +7,7 @@ using System.Text; using System.Threading.Tasks; using WatchIt.Database.Model; using WatchIt.Database.Model.Account; +using WatchIt.Database.Model.Genre; namespace WatchIt.Database { @@ -28,6 +29,9 @@ namespace WatchIt.Database public virtual DbSet Accounts { get; set; } public virtual DbSet AccountProfilePictures { get; set; } + // Genre + public virtual DbSet Genres { get; set; } + #endregion @@ -41,6 +45,9 @@ namespace WatchIt.Database // Account EntityBuilder.Build(modelBuilder); EntityBuilder.Build(modelBuilder); + + // Genre + EntityBuilder.Build(modelBuilder); } #endregion diff --git a/WatchIt.Database/WatchIt.Database/Migrations/20240317125055_Migration1.Designer.cs b/WatchIt.Database/WatchIt.Database/Migrations/20240317131418_Migration1.Designer.cs similarity index 84% rename from WatchIt.Database/WatchIt.Database/Migrations/20240317125055_Migration1.Designer.cs rename to WatchIt.Database/WatchIt.Database/Migrations/20240317131418_Migration1.Designer.cs index 7745a9f..04c538b 100644 --- a/WatchIt.Database/WatchIt.Database/Migrations/20240317125055_Migration1.Designer.cs +++ b/WatchIt.Database/WatchIt.Database/Migrations/20240317131418_Migration1.Designer.cs @@ -12,7 +12,7 @@ using WatchIt.Database; namespace WatchIt.Database.Migrations { [DbContext(typeof(DatabaseContext))] - [Migration("20240317125055_Migration1")] + [Migration("20240317131418_Migration1")] partial class Migration1 { /// @@ -119,6 +119,31 @@ namespace WatchIt.Database.Migrations b.ToTable("AccountProfilePictures"); }); + modelBuilder.Entity("WatchIt.Database.Model.Genre.Genre", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("smallint"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Description") + .HasMaxLength(1000) + .HasColumnType("character varying(1000)"); + + b.Property("Name") + .IsRequired() + .HasMaxLength(100) + .HasColumnType("character varying(100)"); + + b.HasKey("Id"); + + b.HasIndex("Id") + .IsUnique(); + + b.ToTable("Genres"); + }); + modelBuilder.Entity("WatchIt.Database.Model.Account.Account", b => { b.HasOne("WatchIt.Database.Model.Account.AccountProfilePicture", "AccountProfilePicture") diff --git a/WatchIt.Database/WatchIt.Database/Migrations/20240317125055_Migration1.cs b/WatchIt.Database/WatchIt.Database/Migrations/20240317131418_Migration1.cs similarity index 80% rename from WatchIt.Database/WatchIt.Database/Migrations/20240317125055_Migration1.cs rename to WatchIt.Database/WatchIt.Database/Migrations/20240317131418_Migration1.cs index e68d4a5..3767c44 100644 --- a/WatchIt.Database/WatchIt.Database/Migrations/20240317125055_Migration1.cs +++ b/WatchIt.Database/WatchIt.Database/Migrations/20240317131418_Migration1.cs @@ -26,6 +26,20 @@ namespace WatchIt.Database.Migrations table.PrimaryKey("PK_AccountProfilePictures", x => x.Id); }); + migrationBuilder.CreateTable( + name: "Genres", + columns: table => new + { + Id = table.Column(type: "smallint", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + Name = table.Column(type: "character varying(100)", maxLength: 100, nullable: false), + Description = table.Column(type: "character varying(1000)", maxLength: 1000, nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_Genres", x => x.Id); + }); + migrationBuilder.CreateTable( name: "Accounts", columns: table => new @@ -71,6 +85,12 @@ namespace WatchIt.Database.Migrations table: "Accounts", column: "Id", unique: true); + + migrationBuilder.CreateIndex( + name: "IX_Genres_Id", + table: "Genres", + column: "Id", + unique: true); } /// @@ -79,6 +99,9 @@ namespace WatchIt.Database.Migrations migrationBuilder.DropTable( name: "Accounts"); + migrationBuilder.DropTable( + name: "Genres"); + migrationBuilder.DropTable( name: "AccountProfilePictures"); } diff --git a/WatchIt.Database/WatchIt.Database/Migrations/20240317132500_Migration2.Designer.cs b/WatchIt.Database/WatchIt.Database/Migrations/20240317132500_Migration2.Designer.cs new file mode 100644 index 0000000..e46d82e --- /dev/null +++ b/WatchIt.Database/WatchIt.Database/Migrations/20240317132500_Migration2.Designer.cs @@ -0,0 +1,183 @@ +// +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; +using WatchIt.Database; + +#nullable disable + +namespace WatchIt.Database.Migrations +{ + [DbContext(typeof(DatabaseContext))] + [Migration("20240317132500_Migration2")] + partial class Migration2 + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "8.0.3") + .HasAnnotation("Relational:MaxIdentifierLength", 63); + + NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); + + modelBuilder.Entity("WatchIt.Database.Model.Account.Account", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("bigint"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("AccountProfilePictureId") + .HasColumnType("uuid"); + + b.Property("CreationDate") + .ValueGeneratedOnAdd() + .HasColumnType("timestamp with time zone") + .HasDefaultValueSql("now()"); + + b.Property("Description") + .IsRequired() + .HasMaxLength(1000) + .HasColumnType("character varying(1000)"); + + b.Property("Email") + .IsRequired() + .HasMaxLength(320) + .HasColumnType("character varying(320)"); + + b.Property("IsAdmin") + .HasColumnType("boolean"); + + b.Property("LastActive") + .ValueGeneratedOnAdd() + .HasColumnType("timestamp with time zone") + .HasDefaultValueSql("now()"); + + b.Property("LeftSalt") + .IsRequired() + .HasMaxLength(20) + .HasColumnType("character varying(20)"); + + b.Property("Password") + .IsRequired() + .HasMaxLength(1000) + .HasColumnType("bytea"); + + b.Property("RightSalt") + .IsRequired() + .HasMaxLength(20) + .HasColumnType("character varying(20)"); + + b.Property("Username") + .IsRequired() + .HasMaxLength(50) + .HasColumnType("character varying(50)"); + + b.HasKey("Id"); + + b.HasIndex("AccountProfilePictureId") + .IsUnique(); + + b.HasIndex("Id") + .IsUnique(); + + b.ToTable("Accounts"); + }); + + modelBuilder.Entity("WatchIt.Database.Model.Account.AccountProfilePicture", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("Image") + .IsRequired() + .HasMaxLength(-1) + .HasColumnType("bytea"); + + b.Property("MimeType") + .IsRequired() + .HasMaxLength(50) + .HasColumnType("character varying(50)"); + + b.Property("UploadDate") + .ValueGeneratedOnAdd() + .HasColumnType("timestamp with time zone") + .HasDefaultValueSql("now()"); + + b.HasKey("Id"); + + b.HasIndex("Id") + .IsUnique(); + + b.ToTable("AccountProfilePictures"); + }); + + modelBuilder.Entity("WatchIt.Database.Model.Genre.Genre", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("smallint"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Description") + .HasMaxLength(1000) + .HasColumnType("character varying(1000)"); + + b.Property("Name") + .IsRequired() + .HasMaxLength(100) + .HasColumnType("character varying(100)"); + + b.HasKey("Id"); + + b.HasIndex("Id") + .IsUnique(); + + b.ToTable("Genres"); + + b.HasData( + new + { + Id = (short)1, + Name = "Comedy" + }, + new + { + Id = (short)2, + Name = "Thriller" + }, + new + { + Id = (short)3, + Name = "Horror" + }); + }); + + modelBuilder.Entity("WatchIt.Database.Model.Account.Account", b => + { + b.HasOne("WatchIt.Database.Model.Account.AccountProfilePicture", "AccountProfilePicture") + .WithOne("Account") + .HasForeignKey("WatchIt.Database.Model.Account.Account", "AccountProfilePictureId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("AccountProfilePicture"); + }); + + modelBuilder.Entity("WatchIt.Database.Model.Account.AccountProfilePicture", b => + { + b.Navigation("Account") + .IsRequired(); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/WatchIt.Database/WatchIt.Database/Migrations/20240317132500_Migration2.cs b/WatchIt.Database/WatchIt.Database/Migrations/20240317132500_Migration2.cs new file mode 100644 index 0000000..552a5fe --- /dev/null +++ b/WatchIt.Database/WatchIt.Database/Migrations/20240317132500_Migration2.cs @@ -0,0 +1,45 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +#pragma warning disable CA1814 // Prefer jagged arrays over multidimensional + +namespace WatchIt.Database.Migrations +{ + /// + public partial class Migration2 : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.InsertData( + table: "Genres", + columns: new[] { "Id", "Description", "Name" }, + values: new object[,] + { + { (short)1, null, "Comedy" }, + { (short)2, null, "Thriller" }, + { (short)3, null, "Horror" } + }); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DeleteData( + table: "Genres", + keyColumn: "Id", + keyValue: (short)1); + + migrationBuilder.DeleteData( + table: "Genres", + keyColumn: "Id", + keyValue: (short)2); + + migrationBuilder.DeleteData( + table: "Genres", + keyColumn: "Id", + keyValue: (short)3); + } + } +} diff --git a/WatchIt.Database/WatchIt.Database/Migrations/DatabaseContextModelSnapshot.cs b/WatchIt.Database/WatchIt.Database/Migrations/DatabaseContextModelSnapshot.cs index 5dbbac4..0104648 100644 --- a/WatchIt.Database/WatchIt.Database/Migrations/DatabaseContextModelSnapshot.cs +++ b/WatchIt.Database/WatchIt.Database/Migrations/DatabaseContextModelSnapshot.cs @@ -116,6 +116,48 @@ namespace WatchIt.Database.Migrations b.ToTable("AccountProfilePictures"); }); + modelBuilder.Entity("WatchIt.Database.Model.Genre.Genre", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("smallint"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Description") + .HasMaxLength(1000) + .HasColumnType("character varying(1000)"); + + b.Property("Name") + .IsRequired() + .HasMaxLength(100) + .HasColumnType("character varying(100)"); + + b.HasKey("Id"); + + b.HasIndex("Id") + .IsUnique(); + + b.ToTable("Genres"); + + b.HasData( + new + { + Id = (short)1, + Name = "Comedy" + }, + new + { + Id = (short)2, + Name = "Thriller" + }, + new + { + Id = (short)3, + Name = "Horror" + }); + }); + modelBuilder.Entity("WatchIt.Database.Model.Account.Account", b => { b.HasOne("WatchIt.Database.Model.Account.AccountProfilePicture", "AccountProfilePicture") diff --git a/WatchIt/WatchIt.csproj b/WatchIt/WatchIt.csproj index c41500f..df189f9 100644 --- a/WatchIt/WatchIt.csproj +++ b/WatchIt/WatchIt.csproj @@ -26,7 +26,6 @@ -