| [877c13c] | 1 | using ChapterX.Application.Abstractions;
|
|---|
| 2 | using ChapterX.Domain.Entities;
|
|---|
| 3 | using Microsoft.EntityFrameworkCore;
|
|---|
| [d300631] | 4 | using Microsoft.EntityFrameworkCore.Storage;
|
|---|
| [877c13c] | 5 |
|
|---|
| 6 | namespace ChapterX.Infrastructure.Data.DataContext
|
|---|
| 7 | {
|
|---|
| 8 | public sealed class ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
|
|---|
| 9 | : DbContext(options), IApplicationDbContext
|
|---|
| 10 | {
|
|---|
| 11 | public DbSet<User> Users { get; init; }
|
|---|
| 12 | public DbSet<Story> Stories { get; init; }
|
|---|
| 13 | public DbSet<Chapter> Chapters { get; init; }
|
|---|
| 14 | public DbSet<Comment> Comments { get; init; }
|
|---|
| 15 | public DbSet<Genre> Genres { get; init; }
|
|---|
| 16 | public DbSet<ReadingList> ReadingLists { get; init; }
|
|---|
| 17 | public DbSet<ReadingListItems> ReadingListItems { get; init; }
|
|---|
| 18 | public DbSet<Notification> Notifications { get; init; }
|
|---|
| 19 | public DbSet<Likes> Likes { get; init; }
|
|---|
| 20 | public DbSet<Collaboration> Collaborations { get; init; }
|
|---|
| 21 | public DbSet<AISuggestion> AISuggestions { get; init; }
|
|---|
| 22 | public DbSet<Admin> Admins { get; init; }
|
|---|
| 23 | public DbSet<Writer> Writers { get; init; }
|
|---|
| 24 | public DbSet<RegularUser> RegularUsers { get; init; }
|
|---|
| 25 | public DbSet<HasGenre> HasGenres { get; init; }
|
|---|
| 26 | public DbSet<NeedApproval> NeedApprovals { get; init; }
|
|---|
| 27 |
|
|---|
| [d300631] | 28 | public Task<IDbContextTransaction> BeginTransactionAsync(CancellationToken cancellationToken = default)
|
|---|
| 29 | => Database.BeginTransactionAsync(cancellationToken);
|
|---|
| 30 |
|
|---|
| [877c13c] | 31 | protected override void OnModelCreating(ModelBuilder modelBuilder)
|
|---|
| 32 | {
|
|---|
| 33 | base.OnModelCreating(modelBuilder);
|
|---|
| 34 |
|
|---|
| 35 | // USERS
|
|---|
| 36 | modelBuilder.Entity<User>(e =>
|
|---|
| 37 | {
|
|---|
| 38 | e.ToTable("users");
|
|---|
| 39 | e.HasKey(x => x.Id);
|
|---|
| 40 | e.Property(x => x.Id).HasColumnName("user_id");
|
|---|
| 41 | e.Property(x => x.Username).HasColumnName("username");
|
|---|
| 42 | e.Property(x => x.Email).HasColumnName("email");
|
|---|
| [d300631] | 43 | e.Property(x => x.Name).HasColumnName("user_name");
|
|---|
| [877c13c] | 44 | e.Property(x => x.Surname).HasColumnName("surname");
|
|---|
| 45 | e.Property(x => x.Password).HasColumnName("password");
|
|---|
| [d300631] | 46 | e.Property(x => x.CreatedAt).HasColumnName("user_created_at");
|
|---|
| 47 | e.Property(x => x.UpdatedAt).HasColumnName("user_updated_at");
|
|---|
| [877c13c] | 48 | });
|
|---|
| 49 |
|
|---|
| 50 | // ADMINS
|
|---|
| 51 | modelBuilder.Entity<Admin>(e =>
|
|---|
| 52 | {
|
|---|
| 53 | e.ToTable("admins");
|
|---|
| 54 | e.HasKey(x => x.Id);
|
|---|
| 55 | e.Property(x => x.Id).HasColumnName("user_id");
|
|---|
| [e882b92] | 56 | e.Property(x => x.AssignedAt).HasColumnName("assigned_at");
|
|---|
| [877c13c] | 57 | e.HasOne(x => x.User).WithOne(u => u.Admin).HasForeignKey<Admin>(x => x.Id);
|
|---|
| 58 | });
|
|---|
| 59 |
|
|---|
| 60 | // REGULAR_USER
|
|---|
| 61 | modelBuilder.Entity<RegularUser>(e =>
|
|---|
| 62 | {
|
|---|
| 63 | e.ToTable("regular_user");
|
|---|
| 64 | e.HasKey(x => x.Id);
|
|---|
| 65 | e.Property(x => x.Id).HasColumnName("user_id");
|
|---|
| [e882b92] | 66 | e.Property(x => x.JoinedAt).HasColumnName("joined_at");
|
|---|
| [877c13c] | 67 | e.HasOne(x => x.User).WithOne(u => u.RegularUser).HasForeignKey<RegularUser>(x => x.Id);
|
|---|
| 68 | });
|
|---|
| 69 |
|
|---|
| 70 | // WRITER
|
|---|
| 71 | modelBuilder.Entity<Writer>(e =>
|
|---|
| 72 | {
|
|---|
| 73 | e.ToTable("writer");
|
|---|
| 74 | e.HasKey(x => x.Id);
|
|---|
| 75 | e.Property(x => x.Id).HasColumnName("user_id");
|
|---|
| [e882b92] | 76 | e.Property(x => x.Bio).HasColumnName("bio");
|
|---|
| [877c13c] | 77 | e.HasOne(x => x.User).WithOne(u => u.Writer).HasForeignKey<Writer>(x => x.Id);
|
|---|
| 78 | });
|
|---|
| 79 |
|
|---|
| 80 | // STORY
|
|---|
| 81 | modelBuilder.Entity<Story>(e =>
|
|---|
| 82 | {
|
|---|
| 83 | e.ToTable("story");
|
|---|
| 84 | e.HasKey(x => x.Id);
|
|---|
| 85 | e.Property(x => x.Id).HasColumnName("story_id");
|
|---|
| 86 | e.Property(x => x.MatureContent).HasColumnName("mature_content");
|
|---|
| [99c1e45] | 87 | e.Property(x => x.Title).HasColumnName("title");
|
|---|
| [877c13c] | 88 | e.Property(x => x.ShortDescription).HasColumnName("short_description");
|
|---|
| 89 | e.Property(x => x.Image).HasColumnName("image");
|
|---|
| [d300631] | 90 | e.Property(x => x.Content).HasColumnName("story_content");
|
|---|
| [e882b92] | 91 | e.Property(x => x.Status).HasColumnName("status");
|
|---|
| [877c13c] | 92 | e.Property(x => x.UserId).HasColumnName("user_id");
|
|---|
| [d300631] | 93 | e.Property(x => x.CreatedAt).HasColumnName("story_created_at");
|
|---|
| 94 | e.Property(x => x.UpdatedAt).HasColumnName("story_updated_at");
|
|---|
| [877c13c] | 95 | e.HasOne(x => x.Writer).WithMany(w => w.Stories).HasForeignKey(x => x.UserId);
|
|---|
| 96 | });
|
|---|
| 97 |
|
|---|
| 98 | // CHAPTER
|
|---|
| 99 | modelBuilder.Entity<Chapter>(e =>
|
|---|
| 100 | {
|
|---|
| 101 | e.ToTable("chapter");
|
|---|
| 102 | e.HasKey(x => x.Id);
|
|---|
| 103 | e.Property(x => x.Id).HasColumnName("chapter_id");
|
|---|
| 104 | e.Property(x => x.Number).HasColumnName("chapter_number");
|
|---|
| 105 | e.Property(x => x.Name).HasColumnName("chapter_name");
|
|---|
| 106 | e.Property(x => x.Title).HasColumnName("title");
|
|---|
| [d300631] | 107 | e.Property(x => x.Content).HasColumnName("chapter_content");
|
|---|
| [877c13c] | 108 | e.Property(x => x.WordCount).HasColumnName("word_count");
|
|---|
| 109 | e.Property(x => x.Rating).HasColumnName("rating");
|
|---|
| 110 | e.Property(x => x.PublishedAt).HasColumnName("published_at");
|
|---|
| 111 | e.Property(x => x.ViewCount).HasColumnName("view_count");
|
|---|
| 112 | e.Property(x => x.StoryId).HasColumnName("story_id");
|
|---|
| [d300631] | 113 | e.Property(x => x.CreatedAt).HasColumnName("chapter_created_at");
|
|---|
| 114 | e.Property(x => x.UpdatedAt).HasColumnName("chapter_updated_at");
|
|---|
| [877c13c] | 115 | e.HasOne(x => x.Story).WithMany(s => s.Chapters).HasForeignKey(x => x.StoryId);
|
|---|
| 116 | });
|
|---|
| 117 |
|
|---|
| 118 | // GENRE
|
|---|
| 119 | modelBuilder.Entity<Genre>(e =>
|
|---|
| 120 | {
|
|---|
| 121 | e.ToTable("genre");
|
|---|
| 122 | e.HasKey(x => x.Id);
|
|---|
| 123 | e.Property(x => x.Id).HasColumnName("genre_id");
|
|---|
| [d300631] | 124 | e.Property(x => x.Name).HasColumnName("genre_name");
|
|---|
| [877c13c] | 125 | });
|
|---|
| 126 |
|
|---|
| 127 | // HAS_GENRE
|
|---|
| 128 | modelBuilder.Entity<HasGenre>(e =>
|
|---|
| 129 | {
|
|---|
| 130 | e.ToTable("has_genre");
|
|---|
| 131 | e.HasKey(x => new { x.StoryId, x.GenreId });
|
|---|
| 132 | e.Ignore(x => x.Id);
|
|---|
| 133 | e.Property(x => x.StoryId).HasColumnName("story_id");
|
|---|
| 134 | e.Property(x => x.GenreId).HasColumnName("genre_id");
|
|---|
| 135 | e.HasOne(x => x.Story).WithMany(s => s.HasGenres).HasForeignKey(x => x.StoryId);
|
|---|
| 136 | e.HasOne(x => x.Genre).WithMany(g => g.HasGenres).HasForeignKey(x => x.GenreId);
|
|---|
| 137 | });
|
|---|
| 138 |
|
|---|
| 139 | // LIKES
|
|---|
| 140 | modelBuilder.Entity<Likes>(e =>
|
|---|
| 141 | {
|
|---|
| 142 | e.ToTable("likes");
|
|---|
| 143 | e.HasKey(x => new { x.UserId, x.StoryId });
|
|---|
| 144 | e.Ignore(x => x.Id);
|
|---|
| 145 | e.Property(x => x.UserId).HasColumnName("user_id");
|
|---|
| 146 | e.Property(x => x.StoryId).HasColumnName("story_id");
|
|---|
| [e882b92] | 147 | e.Property(x => x.LikedAt).HasColumnName("liked_at");
|
|---|
| [877c13c] | 148 | e.HasOne(x => x.User).WithMany(u => u.Likes).HasForeignKey(x => x.UserId);
|
|---|
| 149 | e.HasOne(x => x.Story).WithMany(s => s.Likes).HasForeignKey(x => x.StoryId);
|
|---|
| 150 | });
|
|---|
| 151 |
|
|---|
| 152 | // COMMENT
|
|---|
| 153 | modelBuilder.Entity<Comment>(e =>
|
|---|
| 154 | {
|
|---|
| 155 | e.ToTable("comment");
|
|---|
| 156 | e.HasKey(x => x.Id);
|
|---|
| 157 | e.Property(x => x.Id).HasColumnName("comment_id");
|
|---|
| [d300631] | 158 | e.Property(x => x.Content).HasColumnName("comment_content");
|
|---|
| [877c13c] | 159 | e.Property(x => x.UserId).HasColumnName("user_id");
|
|---|
| 160 | e.Property(x => x.StoryId).HasColumnName("story_id");
|
|---|
| [d300631] | 161 | e.Property(x => x.CreatedAt).HasColumnName("comment_created_at");
|
|---|
| 162 | e.Property(x => x.UpdatedAt).HasColumnName("comment_updated_at");
|
|---|
| [877c13c] | 163 | e.HasOne(x => x.User).WithMany(u => u.Comments).HasForeignKey(x => x.UserId);
|
|---|
| 164 | e.HasOne(x => x.Story).WithMany(s => s.Comments).HasForeignKey(x => x.StoryId);
|
|---|
| 165 | });
|
|---|
| 166 |
|
|---|
| 167 | // READING_LIST
|
|---|
| 168 | modelBuilder.Entity<ReadingList>(e =>
|
|---|
| 169 | {
|
|---|
| 170 | e.ToTable("reading_list");
|
|---|
| 171 | e.HasKey(x => x.Id);
|
|---|
| 172 | e.Property(x => x.Id).HasColumnName("list_id");
|
|---|
| [d300631] | 173 | e.Property(x => x.Name).HasColumnName("list_name");
|
|---|
| 174 | e.Property(x => x.Content).HasColumnName("list_content");
|
|---|
| [877c13c] | 175 | e.Property(x => x.IsPublic).HasColumnName("is_public");
|
|---|
| 176 | e.Property(x => x.UserId).HasColumnName("user_id");
|
|---|
| [0b502c2] | 177 | e.Property(x => x.CreatedAt).HasColumnName("list_created_at");
|
|---|
| 178 | e.Property(x => x.UpdatedAt).HasColumnName("list_updated_at");
|
|---|
| [877c13c] | 179 | e.HasOne(x => x.User).WithMany(u => u.ReadingLists).HasForeignKey(x => x.UserId);
|
|---|
| 180 | });
|
|---|
| 181 |
|
|---|
| 182 | // READING_LIST_ITEMS
|
|---|
| 183 | modelBuilder.Entity<ReadingListItems>(e =>
|
|---|
| 184 | {
|
|---|
| 185 | e.ToTable("reading_list_items");
|
|---|
| 186 | e.HasKey(x => new { x.ListId, x.StoryId });
|
|---|
| 187 | e.Ignore(x => x.Id);
|
|---|
| 188 | e.Property(x => x.ListId).HasColumnName("list_id");
|
|---|
| 189 | e.Property(x => x.StoryId).HasColumnName("story_id");
|
|---|
| 190 | e.Property(x => x.AddedAt).HasColumnName("added_at");
|
|---|
| 191 | e.HasOne(x => x.ReadingList).WithMany(r => r.ReadingListItems).HasForeignKey(x => x.ListId);
|
|---|
| 192 | e.HasOne(x => x.Story).WithMany(s => s.ReadingListItems).HasForeignKey(x => x.StoryId);
|
|---|
| 193 | });
|
|---|
| 194 |
|
|---|
| 195 | // NOTIFICATION
|
|---|
| 196 | modelBuilder.Entity<Notification>(e =>
|
|---|
| 197 | {
|
|---|
| 198 | e.ToTable("notification");
|
|---|
| 199 | e.HasKey(x => x.Id);
|
|---|
| 200 | e.Property(x => x.Id).HasColumnName("notification_id");
|
|---|
| [d300631] | 201 | e.Property(x => x.Content).HasColumnName("notification_content");
|
|---|
| [e882b92] | 202 | e.Property(x => x.ContentType).HasColumnName("content_type");
|
|---|
| [877c13c] | 203 | e.Property(x => x.IsRead).HasColumnName("is_read");
|
|---|
| [73b69b2] | 204 | e.Property(x => x.Link).HasColumnName("link");
|
|---|
| [877c13c] | 205 | e.Property(x => x.UserId).HasColumnName("user_id");
|
|---|
| 206 | e.Property(x => x.StoryId).HasColumnName("story_id");
|
|---|
| [e882b92] | 207 | e.Property(x => x.CreatedAt).HasColumnName("notification_created_at");
|
|---|
| 208 | e.HasOne(x => x.User).WithMany(u => u.Notifications).HasForeignKey(x => x.UserId);
|
|---|
| 209 | e.HasOne(x => x.Story).WithMany(s => s.Notifications).HasForeignKey(x => x.StoryId)
|
|---|
| 210 | .IsRequired(false).OnDelete(DeleteBehavior.SetNull);
|
|---|
| [877c13c] | 211 | });
|
|---|
| 212 |
|
|---|
| 213 | // AI_SUGGESTION
|
|---|
| 214 | modelBuilder.Entity<AISuggestion>(e =>
|
|---|
| 215 | {
|
|---|
| 216 | e.ToTable("ai_suggestion");
|
|---|
| 217 | e.HasKey(x => x.Id);
|
|---|
| 218 | e.Property(x => x.Id).HasColumnName("suggestion_id");
|
|---|
| 219 | e.Property(x => x.OriginalText).HasColumnName("original_text");
|
|---|
| 220 | e.Property(x => x.SuggestedText).HasColumnName("suggested_text");
|
|---|
| [e882b92] | 221 | e.Property(x => x.SuggestionType).HasColumnName("suggestion_type");
|
|---|
| [877c13c] | 222 | e.Property(x => x.Accepted).HasColumnName("accepted");
|
|---|
| [d300631] | 223 | e.Property(x => x.CreatedAt).HasColumnName("suggestion_created_at");
|
|---|
| [877c13c] | 224 | e.Property(x => x.AppliedAt).HasColumnName("applied_at");
|
|---|
| 225 | e.Property(x => x.StoryId).HasColumnName("story_id");
|
|---|
| 226 | e.HasOne(x => x.Story).WithMany(s => s.AISuggestions).HasForeignKey(x => x.StoryId);
|
|---|
| 227 | });
|
|---|
| 228 |
|
|---|
| 229 | // NEED_APPROVAL
|
|---|
| 230 | modelBuilder.Entity<NeedApproval>(e =>
|
|---|
| 231 | {
|
|---|
| 232 | e.ToTable("need_approval");
|
|---|
| 233 | e.HasKey(x => new { x.SuggestionId, x.StoryId, x.ChapterId });
|
|---|
| 234 | e.Ignore(x => x.Id);
|
|---|
| 235 | e.Property(x => x.SuggestionId).HasColumnName("suggestion_id");
|
|---|
| 236 | e.Property(x => x.StoryId).HasColumnName("story_id");
|
|---|
| 237 | e.Property(x => x.ChapterId).HasColumnName("chapter_id");
|
|---|
| 238 | e.HasOne(x => x.AISuggestion).WithMany(a => a.NeedApprovals).HasForeignKey(x => x.SuggestionId);
|
|---|
| 239 | e.HasOne(x => x.Story).WithMany(s => s.NeedApprovals).HasForeignKey(x => x.StoryId);
|
|---|
| 240 | e.HasOne(x => x.Chapter).WithMany(c => c.NeedApprovals).HasForeignKey(x => x.ChapterId);
|
|---|
| 241 | });
|
|---|
| 242 |
|
|---|
| 243 | // COLLABORATION
|
|---|
| 244 | modelBuilder.Entity<Collaboration>(e =>
|
|---|
| 245 | {
|
|---|
| 246 | e.ToTable("collaboration");
|
|---|
| 247 | e.HasKey(x => new { x.UserId, x.StoryId });
|
|---|
| 248 | e.Ignore(x => x.Id);
|
|---|
| 249 | e.Property(x => x.UserId).HasColumnName("user_id");
|
|---|
| 250 | e.Property(x => x.StoryId).HasColumnName("story_id");
|
|---|
| [e882b92] | 251 | e.Property(x => x.Role).HasColumnName("role");
|
|---|
| 252 | e.Property(x => x.PermissionLevel).HasColumnName("permission_level");
|
|---|
| [d300631] | 253 | e.Property(x => x.CreatedAt).HasColumnName("collab_created_at");
|
|---|
| [877c13c] | 254 | e.HasOne(x => x.User).WithMany(u => u.Collaborations).HasForeignKey(x => x.UserId);
|
|---|
| 255 | e.HasOne(x => x.Story).WithMany(s => s.Collaborations).HasForeignKey(x => x.StoryId);
|
|---|
| 256 | });
|
|---|
| 257 | }
|
|---|
| 258 | }
|
|---|
| 259 | }
|
|---|