Refactoring, database structure changed

This commit is contained in:
2025-03-03 00:56:32 +01:00
Unverified
parent d3805ef3db
commit c603c41c0b
913 changed files with 21764 additions and 32775 deletions

View File

@@ -0,0 +1,40 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using WatchIt.Database.Model.Accounts;
namespace WatchIt.Database.Configuration.Accounts;
public class AccountBackgroundPictureConfiguration : IEntityTypeConfiguration<AccountBackgroundPicture>
{
#region PUBLIC METHODS
public void Configure(EntityTypeBuilder<AccountBackgroundPicture> builder)
{
builder.ToTable("AccountBackgroundPictures", "accounts");
// Account
builder.HasKey(x => x.AccountId);
builder.HasIndex(x => x.AccountId)
.IsUnique();
builder.HasOne(x => x.Account)
.WithOne(x => x.BackgroundPicture)
.HasForeignKey<AccountBackgroundPicture>(x => x.AccountId)
.IsRequired();
builder.Property(x => x.AccountId)
.IsRequired();
// Background
builder.HasOne(x => x.Background)
.WithMany(x => x.BackgroundUsages)
.HasForeignKey(x => x.BackgroundId)
.IsRequired();
builder.Property(x => x.BackgroundId)
.IsRequired();
// Version
builder.Property(b => b.Version)
.IsRowVersion();
}
#endregion
}

View File

@@ -0,0 +1,98 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using WatchIt.Database.Model.Accounts;
namespace WatchIt.Database.Configuration.Accounts;
public class AccountConfiguration : IEntityTypeConfiguration<Account>
{
#region PUBLIC METHODS
public void Configure(EntityTypeBuilder<Account> builder)
{
builder.ToTable("Accounts", "accounts");
// Id
builder.HasKey(x => x.Id);
builder.HasIndex(x => x.Id)
.IsUnique();
builder.Property(x => x.Id)
.IsRequired()
.UseIdentityAlwaysColumn();
// Username
builder.Property(x => x.Username)
.HasMaxLength(50)
.IsRequired();
// Email
builder.Property(x => x.Email)
.HasMaxLength(320)
.IsRequired();
// Password
builder.Property(x => x.Password)
.HasMaxLength(1000)
.IsRequired();
// Left salt
builder.Property(x => x.LeftSalt)
.HasMaxLength(20)
.IsRequired();
// Right salt
builder.Property(x => x.RightSalt)
.HasMaxLength(20)
.IsRequired();
// Is admin
builder.Property(x => x.IsAdmin)
.IsRequired()
.HasDefaultValue(false);
// Join date
builder.Property(x => x.JoinDate)
.IsRequired()
.HasDefaultValueSql("now()");
// Active date
builder.Property(x => x.ActiveDate)
.IsRequired()
.HasDefaultValueSql("now()");
// Description
builder.Property(x => x.Description)
.HasMaxLength(1000);
// Gender
builder.HasOne(x => x.Gender)
.WithMany(x => x.Accounts)
.HasForeignKey(x => x.GenderId);
builder.Property(x => x.GenderId);
// Version
builder.Property(b => b.Version)
.IsRowVersion();
#region Navigation
// AccountFollow
builder.HasMany(x => x.Follows)
.WithMany(x => x.Followers)
.UsingEntity<AccountFollow>(
x => x.HasOne<Account>(y => y.Followed)
.WithMany(y => y.FollowersRelationshipObjects)
.HasForeignKey(y => y.FollowedId)
.IsRequired(),
x => x.HasOne<Account>(y => y.Follower)
.WithMany(y => y.FollowsRelationshipObjects)
.HasForeignKey(y => y.FollowerId)
.IsRequired()
);
#endregion
}
#endregion
}

View File

@@ -0,0 +1,32 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using WatchIt.Database.Model.Accounts;
namespace WatchIt.Database.Configuration.Accounts;
public class AccountFollowConfiguration : IEntityTypeConfiguration<AccountFollow>
{
#region PUBLIC METHODS
public void Configure(EntityTypeBuilder<AccountFollow> builder)
{
builder.ToTable("AccountFollows", "accounts");
builder.HasKey(x => new { x.FollowerId, x.FollowedId });
// Follower
// FK configured in AccountConfiguration
builder.Property(x => x.FollowerId)
.IsRequired();
// Followed
// FK configured in AccountConfiguration
builder.Property(x => x.FollowedId)
.IsRequired();
// Version
builder.Property(b => b.Version)
.IsRowVersion();
}
#endregion
}

View File

@@ -0,0 +1,35 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using WatchIt.Database.Model.Accounts;
namespace WatchIt.Database.Configuration.Accounts;
public class AccountProfilePictureConfiguration : ImageEntityConfiguration<AccountProfilePicture>
{
#region PUBLIC METHODS
public override void Configure(EntityTypeBuilder<AccountProfilePicture> builder)
{
builder.ToTable($"AccountProfilePictures", "accounts");
// Account
builder.HasKey(x => x.AccountId);
builder.HasIndex(x => x.AccountId)
.IsUnique();
builder.HasOne(x => x.Account)
.WithOne(x => x.ProfilePicture)
.HasForeignKey<AccountProfilePicture>(x => x.AccountId)
.IsRequired();
builder.Property(x => x.AccountId)
.IsRequired();
// Generic properties
base.Configure(builder);
// Version
builder.Property(b => b.Version)
.IsRowVersion();
}
#endregion
}

View File

@@ -0,0 +1,44 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using WatchIt.Database.Model.Accounts;
namespace WatchIt.Database.Configuration.Accounts;
public class AccountRefreshTokenConfiguration : IEntityTypeConfiguration<AccountRefreshToken>
{
#region PUBLIC METHODS
public void Configure(EntityTypeBuilder<AccountRefreshToken> builder)
{
builder.ToTable("AccountRefreshTokens", "accounts");
// Id
builder.HasKey(x => x.Token);
builder.HasIndex(x => x.Token)
.IsUnique();
builder.Property(x => x.Token)
.IsRequired();
// Account
builder.HasOne(x => x.Account)
.WithMany(x => x.RefreshTokens)
.HasForeignKey(x => x.AccountId)
.IsRequired();
builder.Property(x => x.AccountId)
.IsRequired();
// Expiration date
builder.Property(x => x.ExpirationDate)
.IsRequired();
// Is extendable
builder.Property(x => x.IsExtendable)
.IsRequired();
// Version
builder.Property(b => b.Version)
.IsRowVersion();
}
#endregion
}

View File

@@ -0,0 +1,34 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using WatchIt.Database.Model.Genders;
namespace WatchIt.Database.Configuration.Genders;
public class GenderConfiguration : IEntityTypeConfiguration<Gender>
{
#region PUBLIC METHODS
public void Configure(EntityTypeBuilder<Gender> builder)
{
builder.ToTable("Genders", "genders");
// Id
builder.HasKey(x => x.Id);
builder.HasIndex(x => x.Id)
.IsUnique();
builder.Property(x => x.Id)
.IsRequired()
.UseIdentityAlwaysColumn();
// Name
builder.Property(x => x.Name)
.HasMaxLength(100)
.IsRequired();
// Version
builder.Property(b => b.Version)
.IsRowVersion();
}
#endregion
}

View File

@@ -0,0 +1,34 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using WatchIt.Database.Model.Genres;
namespace WatchIt.Database.Configuration.Genres;
public class GenreConfiguration : IEntityTypeConfiguration<Genre>
{
#region PUBLIC METHODS
public void Configure(EntityTypeBuilder<Genre> builder)
{
builder.ToTable("Genres", "genres");
// Id
builder.HasKey(x => x.Id);
builder.HasIndex(x => x.Id)
.IsUnique();
builder.Property(x => x.Id)
.IsRequired()
.UseIdentityAlwaysColumn();
// Name
builder.Property(x => x.Name)
.HasMaxLength(100)
.IsRequired();
// Version
builder.Property(b => b.Version)
.IsRowVersion();
}
#endregion
}

View File

@@ -0,0 +1,30 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using WatchIt.Database.Model;
namespace WatchIt.Database.Configuration;
public abstract class ImageEntityConfiguration<T> : IEntityTypeConfiguration<T> where T : class, IImageEntity
{
#region PUBLIC METHODS
public virtual void Configure(EntityTypeBuilder<T> builder)
{
// Image
builder.Property(x => x.Image)
.HasMaxLength(-1)
.IsRequired();
// MimeType
builder.Property(x => x.MimeType)
.HasMaxLength(50)
.IsRequired();
// UploadDate
builder.Property(x => x.UploadDate)
.IsRequired()
.HasDefaultValueSql("now()");
}
#endregion
}

View File

@@ -0,0 +1,90 @@
using System.ComponentModel.DataAnnotations.Schema;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using WatchIt.Database.Model.Media;
namespace WatchIt.Database.Configuration.Media;
public class MediumConfiguration : IEntityTypeConfiguration<Medium>
{
#region PUBLIC METHODS
public void Configure(EntityTypeBuilder<Medium> builder)
{
builder.ToTable("Media", "media");
builder.HasDiscriminator<MediumType>("Type")
.HasValue<MediumMovie>(MediumType.Movie)
.HasValue<MediumSeries>(MediumType.Series);
// Id
builder.HasKey(x => x.Id);
builder.HasIndex(x => x.Id)
.IsUnique();
builder.Property(x => x.Id)
.IsRequired()
.UseIdentityAlwaysColumn();
// Type
builder.Property(x => x.Type)
.IsRequired();
// Title
builder.Property(x => x.Title)
.HasMaxLength(250)
.IsRequired();
// Original title
builder.Property(x => x.OriginalTitle)
.HasMaxLength(250);
// Description
builder.Property(x => x.Description)
.HasMaxLength(1000);
// Duration
builder.Property(x => x.Duration);
// Release date
builder.Property(x => x.ReleaseDate);
// Version
builder.Property(b => b.Version)
.IsRowVersion();
#region Navigation
// MediumGenre
builder.HasMany(x => x.Genres)
.WithMany(x => x.Media)
.UsingEntity<MediumGenre>(
x => x.HasOne(y => y.Genre)
.WithMany(y => y.MediaRelationObjects)
.HasForeignKey(y => y.GenreId)
.IsRequired(),
x => x.HasOne(y => y.Medium)
.WithMany(y => y.GenresRelationshipObjects)
.HasForeignKey(y => y.MediumId)
.IsRequired()
);
// MediumRating
builder.HasMany(x => x.RatedBy)
.WithMany(x => x.MediaRated)
.UsingEntity<MediumRating>(
x => x.HasOne(y => y.Account)
.WithMany(y => y.MediaRatings)
.HasForeignKey(y => y.AccountId)
.IsRequired(),
x => x.HasOne(y => y.Medium)
.WithMany(y => y.Ratings)
.HasForeignKey(y => y.MediumId)
.IsRequired()
);
#endregion
}
#endregion
}

View File

@@ -0,0 +1,32 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using WatchIt.Database.Model.Media;
namespace WatchIt.Database.Configuration.Media;
public class MediumGenreConfiguration : IEntityTypeConfiguration<MediumGenre>
{
#region PUBLIC METHODS
public void Configure(EntityTypeBuilder<MediumGenre> builder)
{
builder.ToTable("MediumGenres", "media");
builder.HasKey(x => new { x.GenreId, x.MediumId });
// Medium
// FK configured in MediumConfiguration
builder.Property(x => x.MediumId)
.IsRequired();
// Genre
// FK configured in MediumConfiguration
builder.Property(x => x.GenreId)
.IsRequired();
// Version
builder.Property(b => b.Version)
.IsRowVersion();
}
#endregion
}

View File

@@ -0,0 +1,19 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using WatchIt.Database.Model.Media;
namespace WatchIt.Database.Configuration.Media;
public class MediumMovieConfiguration : IEntityTypeConfiguration<MediumMovie>
{
#region PUBLIC METHODS
public void Configure(EntityTypeBuilder<MediumMovie> builder)
{
// Budget
builder.Property(x => x.Budget)
.HasColumnType("money");
}
#endregion
}

View File

@@ -0,0 +1,35 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using WatchIt.Database.Model.Media;
namespace WatchIt.Database.Configuration.Media;
public class MediumPictureConfiguration : ImageEntityConfiguration<MediumPicture>
{
#region PUBLIC METHODS
public override void Configure(EntityTypeBuilder<MediumPicture> builder)
{
builder.ToTable("MediumPictures", "media");
// Medium
builder.HasKey(x => x.MediumId);
builder.HasIndex(x => x.MediumId)
.IsUnique();
builder.HasOne(x => x.Medium)
.WithOne(x => x.Picture)
.HasForeignKey<MediumPicture>(x => x.MediumId)
.IsRequired();
builder.Property(x => x.MediumId)
.IsRequired();
// Version
builder.Property(b => b.Version)
.IsRowVersion();
// Generic properties
base.Configure(builder);
}
#endregion
}

View File

@@ -0,0 +1,31 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using WatchIt.Database.Model.Accounts;
using WatchIt.Database.Model.Media;
namespace WatchIt.Database.Configuration.Media;
public class MediumRatingConfiguration : RatingEntityConfiguration<MediumRating>
{
#region PUBLIC METHODS
public override void Configure(EntityTypeBuilder<MediumRating> builder)
{
builder.ToTable("MediumRatings", "media");
builder.HasKey(x => new { x.AccountId, x.MediumId });
// Medium
// FK configured in MediumConfiguration
builder.Property(x => x.MediumId)
.IsRequired();
// Version
builder.Property(b => b.Version)
.IsRowVersion();
// Generic properties
base.Configure(builder);
}
#endregion
}

View File

@@ -0,0 +1,20 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using WatchIt.Database.Model.Media;
namespace WatchIt.Database.Configuration.Media;
public class MediumSeriesConfiguration : IEntityTypeConfiguration<MediumSeries>
{
#region PUBLIC METHODS
public void Configure(EntityTypeBuilder<MediumSeries> builder)
{
// Has ended
builder.Property(x => x.HasEnded)
.IsRequired()
.HasDefaultValue(false);
}
#endregion
}

View File

@@ -0,0 +1,33 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using WatchIt.Database.Model.Media;
namespace WatchIt.Database.Configuration.Media;
public class MediumViewCountConfiguration : ViewCountEntityConfiguration<MediumViewCount>
{
#region PUBLIC METHODS
public override void Configure(EntityTypeBuilder<MediumViewCount> builder)
{
builder.ToTable("MediumViewCounts", "media");
builder.HasKey(x => new { x.MediumId, x.Date });
// Medium
builder.HasOne(x => x.Medium)
.WithMany(x => x.ViewCounts)
.HasForeignKey(x => x.MediumId)
.IsRequired();
builder.Property(x => x.MediumId)
.IsRequired();
// Version
builder.Property(b => b.Version)
.IsRowVersion();
// Generic properties
base.Configure(builder);
}
#endregion
}

View File

@@ -0,0 +1,54 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using WatchIt.Database.Model.People;
namespace WatchIt.Database.Configuration.People;
public class PersonConfiguration : IEntityTypeConfiguration<Person>
{
#region PUBLIC METHODS
public void Configure(EntityTypeBuilder<Person> builder)
{
builder.ToTable("People", "people");
// Id
builder.HasKey(x => x.Id);
builder.HasIndex(x => x.Id)
.IsUnique();
builder.Property(x => x.Id)
.IsRequired()
.UseIdentityAlwaysColumn();
// Name
builder.Property(x => x.Name)
.HasMaxLength(100)
.IsRequired();
// Full name
builder.Property(x => x.FullName)
.HasMaxLength(200);
// Description
builder.Property(x => x.Description)
.HasMaxLength(1000);
// Birth date
builder.Property(x => x.BirthDate);
// Death date
builder.Property(x => x.DeathDate);
// Gender
builder.HasOne(x => x.Gender)
.WithMany(x => x.People)
.HasForeignKey(x => x.GenderId);
builder.Property(x => x.GenderId);
// Version
builder.Property(b => b.Version)
.IsRowVersion();
}
#endregion
}

View File

@@ -0,0 +1,36 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using WatchIt.Database.Model.Media;
using WatchIt.Database.Model.People;
namespace WatchIt.Database.Configuration.People;
public class PersonPictureConfiguration : ImageEntityConfiguration<PersonPicture>
{
#region PUBLIC METHODS
public override void Configure(EntityTypeBuilder<PersonPicture> builder)
{
builder.ToTable("PersonPictures", "people");
// Person
builder.HasKey(x => x.PersonId);
builder.HasIndex(x => x.PersonId)
.IsUnique();
builder.HasOne(x => x.Person)
.WithOne(x => x.Picture)
.HasForeignKey<PersonPicture>(x => x.PersonId)
.IsRequired();
builder.Property(x => x.PersonId)
.IsRequired();
// Version
builder.Property(b => b.Version)
.IsRowVersion();
// Generic properties
base.Configure(builder);
}
#endregion
}

View File

@@ -0,0 +1,33 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using WatchIt.Database.Model.People;
namespace WatchIt.Database.Configuration.People;
public class PersonViewCountConfiguration : ViewCountEntityConfiguration<PersonViewCount>
{
#region PUBLIC METHODS
public override void Configure(EntityTypeBuilder<PersonViewCount> builder)
{
builder.ToTable("PersonViewCounts", "people");
builder.HasKey(x => new { x.PersonId, x.Date });
// Medium
builder.HasOne(x => x.Person)
.WithMany(x => x.ViewCounts)
.HasForeignKey(x => x.PersonId)
.IsRequired();
builder.Property(x => x.PersonId)
.IsRequired();
// Version
builder.Property(b => b.Version)
.IsRowVersion();
// Generic properties
base.Configure(builder);
}
#endregion
}

View File

@@ -0,0 +1,54 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using WatchIt.Database.Converters;
using WatchIt.Database.Model.Photos;
namespace WatchIt.Database.Configuration.Photos;
public class PhotoBackgroundConfiguration : IEntityTypeConfiguration<PhotoBackground>
{
#region PUBLIC METHODS
public void Configure(EntityTypeBuilder<PhotoBackground> builder)
{
builder.ToTable("PhotoBackground", "photos");
// Id
builder.HasKey(x => x.Id);
builder.HasIndex(x => x.Id)
.IsUnique();
builder.Property(x => x.Id)
.IsRequired();
// Photo
builder.HasOne(x => x.Photo)
.WithOne(x => x.Background)
.HasForeignKey<PhotoBackground>(x => x.PhotoId)
.IsRequired();
builder.HasIndex(x => x.PhotoId)
.IsUnique();
builder.Property(x => x.PhotoId)
.IsRequired();
// Is universal
builder.Property(x => x.IsUniversal)
.IsRequired()
.HasDefaultValue(false);
// First gradient color
builder.Property(x => x.FirstGradientColor)
.HasConversion<ColorToByteArrayConverter>()
.IsRequired();
// Second gradient color
builder.Property(x => x.SecondGradientColor)
.HasConversion<ColorToByteArrayConverter>()
.IsRequired();
// Version
builder.Property(b => b.Version)
.IsRowVersion();
}
#endregion
}

View File

@@ -0,0 +1,39 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using WatchIt.Database.Model.Photos;
namespace WatchIt.Database.Configuration.Photos;
public class PhotoConfiguration : ImageEntityConfiguration<Photo>
{
#region PUBLIC METHODS
public override void Configure(EntityTypeBuilder<Photo> builder)
{
builder.ToTable("Photos", "photos");
// Id
builder.HasKey(x => x.Id);
builder.HasIndex(x => x.Id)
.IsUnique();
builder.Property(x => x.Id)
.IsRequired();
// Medium
builder.HasOne(x => x.Medium)
.WithMany(x => x.Photos)
.HasForeignKey(x => x.MediumId)
.IsRequired();
builder.Property(x => x.MediumId)
.IsRequired();
// Version
builder.Property(b => b.Version)
.IsRowVersion();
// Generic properties
base.Configure(builder);
}
#endregion
}

View File

@@ -0,0 +1,30 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using WatchIt.Database.Model;
using WatchIt.Database.Model.Accounts;
namespace WatchIt.Database.Configuration;
public abstract class RatingEntityConfiguration<T> : IEntityTypeConfiguration<T> where T : class, IRatingEntity
{
#region PUBLIC METHODS
public virtual void Configure(EntityTypeBuilder<T> builder)
{
// Account
// You have to configure FK and PK by yourself
builder.Property(x => x.AccountId)
.IsRequired();
// Rating
builder.Property(x => x.Rating)
.IsRequired();
// Date
builder.Property(x => x.Date)
.IsRequired()
.HasDefaultValueSql("now()");
}
#endregion
}

View File

@@ -0,0 +1,28 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using WatchIt.Database.Model.Roles;
namespace WatchIt.Database.Configuration.Roles;
public class RoleActorConfiguration : IEntityTypeConfiguration<RoleActor>
{
#region PUBLIC METHODS
public void Configure(EntityTypeBuilder<RoleActor> builder)
{
// Actor type
builder.HasOne(x => x.ActorType)
.WithMany(x => x.Roles)
.HasForeignKey(x => x.ActorTypeId)
.IsRequired();
builder.Property(x => x.ActorTypeId)
.IsRequired();
// Name
builder.Property(x => x.Name)
.HasMaxLength(100)
.IsRequired();
}
#endregion
}

View File

@@ -0,0 +1,34 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using WatchIt.Database.Model.Roles;
namespace WatchIt.Database.Configuration.Roles;
public class RoleActorTypeConfiguration : IEntityTypeConfiguration<RoleActorType>
{
#region PUBLIC METHODS
public void Configure(EntityTypeBuilder<RoleActorType> builder)
{
builder.ToTable("RoleActorTypes", "roles");
// Id
builder.HasKey(x => x.Id);
builder.HasIndex(x => x.Id)
.IsUnique();
builder.Property(x => x.Id)
.IsRequired()
.UseIdentityAlwaysColumn();
// Name
builder.Property(x => x.Name)
.HasMaxLength(100)
.IsRequired();
// Version
builder.Property(b => b.Version)
.IsRowVersion();
}
#endregion
}

View File

@@ -0,0 +1,70 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using WatchIt.Database.Model.Roles;
namespace WatchIt.Database.Configuration.Roles;
public class RoleConfiguration : IEntityTypeConfiguration<Role>
{
#region PUBLIC METHODS
public void Configure(EntityTypeBuilder<Role> builder)
{
builder.ToTable("Roles", "roles");
builder.HasDiscriminator<RoleType>("Type")
.HasValue<RoleActor>(RoleType.Actor)
.HasValue<RoleCreator>(RoleType.Creator);
// Id
builder.HasKey(x => x.Id);
builder.HasIndex(x => x.Id)
.IsUnique();
builder.Property(x => x.Id)
.IsRequired();
// Type
builder.Property(x => x.Type)
.IsRequired();
// Medium
builder.HasOne(x => x.Medium)
.WithMany(x => x.Roles)
.HasForeignKey(x => x.MediumId)
.IsRequired();
builder.Property(x => x.MediumId)
.IsRequired();
// Person
builder.HasOne(x => x.Person)
.WithMany(x => x.Roles)
.HasForeignKey(x => x.PersonId)
.IsRequired();
builder.Property(x => x.PersonId)
.IsRequired();
// Version
builder.Property(b => b.Version)
.IsRowVersion();
#region Navigation
// RoleRating
builder.HasMany(x => x.RatedBy)
.WithMany(x => x.RolesRated)
.UsingEntity<RoleRating>(
x => x.HasOne(y => y.Account)
.WithMany(y => y.RolesRatings)
.HasForeignKey(y => y.AccountId)
.IsRequired(),
x => x.HasOne(y => y.Role)
.WithMany(y => y.Ratings)
.HasForeignKey(y => y.RoleId)
.IsRequired()
);
#endregion
}
#endregion
}

View File

@@ -0,0 +1,23 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using WatchIt.Database.Model.Roles;
namespace WatchIt.Database.Configuration.Roles;
public class RoleCreatorConfiguration : IEntityTypeConfiguration<RoleCreator>
{
#region PUBLIC METHODS
public void Configure(EntityTypeBuilder<RoleCreator> builder)
{
// Creator type
builder.HasOne(x => x.CreatorType)
.WithMany(x => x.Roles)
.HasForeignKey(x => x.CreatorTypeId)
.IsRequired();
builder.Property(x => x.CreatorTypeId)
.IsRequired();
}
#endregion
}

View File

@@ -0,0 +1,34 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using WatchIt.Database.Model.Roles;
namespace WatchIt.Database.Configuration.Roles;
public class RoleCreatorTypeConfiguration : IEntityTypeConfiguration<RoleCreatorType>
{
#region PUBLIC METHODS
public void Configure(EntityTypeBuilder<RoleCreatorType> builder)
{
builder.ToTable("RoleCreatorTypes", "roles");
// Id
builder.HasKey(x => x.Id);
builder.HasIndex(x => x.Id)
.IsUnique();
builder.Property(x => x.Id)
.IsRequired()
.UseIdentityAlwaysColumn();
// Name
builder.Property(x => x.Name)
.HasMaxLength(100)
.IsRequired();
// Version
builder.Property(b => b.Version)
.IsRowVersion();
}
#endregion
}

View File

@@ -0,0 +1,30 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using WatchIt.Database.Model.Roles;
namespace WatchIt.Database.Configuration.Roles;
public class RoleRatingConfiguration : RatingEntityConfiguration<RoleRating>
{
#region PUBLIC METHODS
public override void Configure(EntityTypeBuilder<RoleRating> builder)
{
builder.ToTable("RoleRatings", "roles");
builder.HasKey(x => new { x.AccountId, x.RoleId });
// Role
// FK configured in RoleConfiguration
builder.Property(x => x.RoleId)
.IsRequired();
// Version
builder.Property(b => b.Version)
.IsRowVersion();
// Generic properties
base.Configure(builder);
}
#endregion
}

View File

@@ -0,0 +1,25 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using WatchIt.Database.Model;
namespace WatchIt.Database.Configuration;
public abstract class ViewCountEntityConfiguration<T> : IEntityTypeConfiguration<T> where T : class, IViewCountEntity
{
#region PUBLIC METHODS
public virtual void Configure(EntityTypeBuilder<T> builder)
{
// Date
// You have to configure PK by yourself
builder.Property(x => x.Date)
.IsRequired()
.HasDefaultValueSql("now()");
// View count
builder.Property(x => x.ViewCount)
.IsRequired();
}
#endregion
}