using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using MyHN.Domain;
namespace MyHN.Infrastructure.EntityTypes
{
public class LinkEntityType : IEntityTypeConfiguration
{
public void Configure(EntityTypeBuilder builder)
{
builder.ToTable("links");
builder.HasKey(o => o.Id);
builder.Property(o => o.Url).IsRequired().HasMaxLength(500);
builder.Property(o => o.CreatedAt).IsRequired();
builder.HasOne()
.WithMany()
.HasForeignKey(o => o.CreatedBy)
.IsRequired()
.OnDelete(DeleteBehavior.Cascade);
builder.OwnsMany(o => o.Votes, vote =>
{
vote.ToTable("link_votes");
vote.WithOwner().HasForeignKey("LinkId");
vote.HasOne()
.WithMany()
.HasForeignKey(o => o.CreatedBy)
.IsRequired()
.OnDelete(DeleteBehavior.Cascade);
vote.HasKey("LinkId", nameof(Vote.CreatedBy));
vote.Property(o => o.Direction).IsRequired();
vote.Property(o => o.CreatedAt).IsRequired();
});
}
}
}