genre added

This commit is contained in:
2024-03-17 14:26:03 +01:00
Unverified
parent 4659c6c9c5
commit 26bbbf80c2
10 changed files with 381 additions and 3 deletions

View File

@@ -13,7 +13,11 @@ namespace WatchIt.Database.Model
#region PUBLIC METHODS
public static void Build<T>(ModelBuilder builder) where T : class, IEntity<T> => Build<T>(builder.Entity<T>());
public static void Build<T>(EntityTypeBuilder<T> builder) where T : class, IEntity<T> => T.Build(builder);
public static void Build<T>(EntityTypeBuilder<T> builder) where T : class, IEntity<T>
{
T.Build(builder);
builder.HasData(T.InsertData());
}
#endregion
}

View File

@@ -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<Genre>
{
#region PROPERTIES
public short Id { get; set; }
public string Name { get; set; }
public string? Description { get; set; }
#endregion
#region PUBLIC METHODS
static void IEntity<Genre>.Build(EntityTypeBuilder<Genre> 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<Genre> IEntity<Genre>.InsertData() => new List<Genre>
{
new Genre { Id = 1, Name = "Comedy" },
new Genre { Id = 2, Name = "Thriller" },
new Genre { Id = 3, Name = "Horror" },
};
#endregion
}
}

View File

@@ -13,6 +13,7 @@ namespace WatchIt.Database.Model
#region METHODS
static abstract void Build(EntityTypeBuilder<T> builder);
static virtual IEnumerable<T> InsertData() => Array.Empty<T>();
#endregion
}

View File

@@ -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<Account> Accounts { get; set; }
public virtual DbSet<AccountProfilePicture> AccountProfilePictures { get; set; }
// Genre
public virtual DbSet<Genre> Genres { get; set; }
#endregion
@@ -41,6 +45,9 @@ namespace WatchIt.Database
// Account
EntityBuilder.Build<Account>(modelBuilder);
EntityBuilder.Build<AccountProfilePicture>(modelBuilder);
// Genre
EntityBuilder.Build<Genre>(modelBuilder);
}
#endregion

View File

@@ -12,7 +12,7 @@ using WatchIt.Database;
namespace WatchIt.Database.Migrations
{
[DbContext(typeof(DatabaseContext))]
[Migration("20240317125055_Migration1")]
[Migration("20240317131418_Migration1")]
partial class Migration1
{
/// <inheritdoc />
@@ -119,6 +119,31 @@ namespace WatchIt.Database.Migrations
b.ToTable("AccountProfilePictures");
});
modelBuilder.Entity("WatchIt.Database.Model.Genre.Genre", b =>
{
b.Property<short>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("smallint");
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<short>("Id"));
b.Property<string>("Description")
.HasMaxLength(1000)
.HasColumnType("character varying(1000)");
b.Property<string>("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")

View File

@@ -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<short>(type: "smallint", nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
Name = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: false),
Description = table.Column<string>(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);
}
/// <inheritdoc />
@@ -79,6 +99,9 @@ namespace WatchIt.Database.Migrations
migrationBuilder.DropTable(
name: "Accounts");
migrationBuilder.DropTable(
name: "Genres");
migrationBuilder.DropTable(
name: "AccountProfilePictures");
}

View File

@@ -0,0 +1,183 @@
// <auto-generated />
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
{
/// <inheritdoc />
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<long>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("bigint");
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<long>("Id"));
b.Property<Guid>("AccountProfilePictureId")
.HasColumnType("uuid");
b.Property<DateTimeOffset>("CreationDate")
.ValueGeneratedOnAdd()
.HasColumnType("timestamp with time zone")
.HasDefaultValueSql("now()");
b.Property<string>("Description")
.IsRequired()
.HasMaxLength(1000)
.HasColumnType("character varying(1000)");
b.Property<string>("Email")
.IsRequired()
.HasMaxLength(320)
.HasColumnType("character varying(320)");
b.Property<bool>("IsAdmin")
.HasColumnType("boolean");
b.Property<DateTimeOffset>("LastActive")
.ValueGeneratedOnAdd()
.HasColumnType("timestamp with time zone")
.HasDefaultValueSql("now()");
b.Property<string>("LeftSalt")
.IsRequired()
.HasMaxLength(20)
.HasColumnType("character varying(20)");
b.Property<byte[]>("Password")
.IsRequired()
.HasMaxLength(1000)
.HasColumnType("bytea");
b.Property<string>("RightSalt")
.IsRequired()
.HasMaxLength(20)
.HasColumnType("character varying(20)");
b.Property<string>("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<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("uuid");
b.Property<byte[]>("Image")
.IsRequired()
.HasMaxLength(-1)
.HasColumnType("bytea");
b.Property<string>("MimeType")
.IsRequired()
.HasMaxLength(50)
.HasColumnType("character varying(50)");
b.Property<DateTimeOffset>("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<short>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("smallint");
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<short>("Id"));
b.Property<string>("Description")
.HasMaxLength(1000)
.HasColumnType("character varying(1000)");
b.Property<string>("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
}
}
}

View File

@@ -0,0 +1,45 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
#pragma warning disable CA1814 // Prefer jagged arrays over multidimensional
namespace WatchIt.Database.Migrations
{
/// <inheritdoc />
public partial class Migration2 : Migration
{
/// <inheritdoc />
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" }
});
}
/// <inheritdoc />
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);
}
}
}

View File

@@ -116,6 +116,48 @@ namespace WatchIt.Database.Migrations
b.ToTable("AccountProfilePictures");
});
modelBuilder.Entity("WatchIt.Database.Model.Genre.Genre", b =>
{
b.Property<short>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("smallint");
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<short>("Id"));
b.Property<string>("Description")
.HasMaxLength(1000)
.HasColumnType("character varying(1000)");
b.Property<string>("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")

View File

@@ -26,7 +26,6 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\WatchIt.Database\WatchIt.Database.csproj" />
<ProjectReference Include="..\WatchIt.Database\WatchIt.Database\WatchIt.Database.csproj" />
</ItemGroup>