source: ChapterX.Infrastructure/Data/DataContext/ApplicationDbContext.cs@ 99c1e45

main
Last change on this file since 99c1e45 was 99c1e45, checked in by kikisrbinoska <srbinoskakristina07@…>, 11 days ago

Fixed writer section and admin management

  • Property mode set to 100644
File size: 15.5 KB
Line 
1using ChapterX.Application.Abstractions;
2using ChapterX.Domain.Entities;
3using Microsoft.EntityFrameworkCore;
4using Microsoft.EntityFrameworkCore.Storage;
5
6namespace 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<Notify> Notifies { get; init; }
22 public DbSet<AISuggestion> AISuggestions { get; init; }
23 public DbSet<Admin> Admins { get; init; }
24 public DbSet<Writer> Writers { get; init; }
25 public DbSet<RegularUser> RegularUsers { get; init; }
26 public DbSet<HasGenre> HasGenres { get; init; }
27 public DbSet<NeedApproval> NeedApprovals { get; init; }
28 public DbSet<Status> Statuses { get; init; }
29 public DbSet<ContentType> ContentTypes { get; init; }
30 public DbSet<SuggestionType> SuggestionTypes { get; init; }
31 public DbSet<Roles> Roles { get; init; }
32 public DbSet<PermissionLevel> PermissionLevels { get; init; }
33
34 public Task<IDbContextTransaction> BeginTransactionAsync(CancellationToken cancellationToken = default)
35 => Database.BeginTransactionAsync(cancellationToken);
36
37 protected override void OnModelCreating(ModelBuilder modelBuilder)
38 {
39 base.OnModelCreating(modelBuilder);
40
41 // USERS
42 modelBuilder.Entity<User>(e =>
43 {
44 e.ToTable("users");
45 e.HasKey(x => x.Id);
46 e.Property(x => x.Id).HasColumnName("user_id");
47 e.Property(x => x.Username).HasColumnName("username");
48 e.Property(x => x.Email).HasColumnName("email");
49 e.Property(x => x.Name).HasColumnName("user_name");
50 e.Property(x => x.Surname).HasColumnName("surname");
51 e.Property(x => x.Password).HasColumnName("password");
52 e.Property(x => x.CreatedAt).HasColumnName("user_created_at");
53 e.Property(x => x.UpdatedAt).HasColumnName("user_updated_at");
54 });
55
56 // ADMINS
57 modelBuilder.Entity<Admin>(e =>
58 {
59 e.ToTable("admins");
60 e.HasKey(x => x.Id);
61 e.Property(x => x.Id).HasColumnName("user_id");
62 e.HasOne(x => x.User).WithOne(u => u.Admin).HasForeignKey<Admin>(x => x.Id);
63 });
64
65 // REGULAR_USER
66 modelBuilder.Entity<RegularUser>(e =>
67 {
68 e.ToTable("regular_user");
69 e.HasKey(x => x.Id);
70 e.Property(x => x.Id).HasColumnName("user_id");
71 e.HasOne(x => x.User).WithOne(u => u.RegularUser).HasForeignKey<RegularUser>(x => x.Id);
72 });
73
74 // WRITER
75 modelBuilder.Entity<Writer>(e =>
76 {
77 e.ToTable("writer");
78 e.HasKey(x => x.Id);
79 e.Property(x => x.Id).HasColumnName("user_id");
80 e.HasOne(x => x.User).WithOne(u => u.Writer).HasForeignKey<Writer>(x => x.Id);
81 });
82
83 // STORY
84 modelBuilder.Entity<Story>(e =>
85 {
86 e.ToTable("story");
87 e.HasKey(x => x.Id);
88 e.Property(x => x.Id).HasColumnName("story_id");
89 e.Property(x => x.MatureContent).HasColumnName("mature_content");
90 e.Property(x => x.Title).HasColumnName("title");
91 e.Property(x => x.ShortDescription).HasColumnName("short_description");
92 e.Property(x => x.Image).HasColumnName("image");
93 e.Property(x => x.Content).HasColumnName("story_content");
94 e.Property(x => x.UserId).HasColumnName("user_id");
95 e.Property(x => x.CreatedAt).HasColumnName("story_created_at");
96 e.Property(x => x.UpdatedAt).HasColumnName("story_updated_at");
97 e.HasOne(x => x.Writer).WithMany(w => w.Stories).HasForeignKey(x => x.UserId);
98 });
99
100 // STATUS
101 modelBuilder.Entity<Status>(e =>
102 {
103 e.ToTable("status");
104 e.HasKey(x => new { x.StoryId, x.StatusValue });
105 e.Ignore(x => x.Id);
106 e.Property(x => x.StoryId).HasColumnName("story_id");
107 e.Property(x => x.StatusValue).HasColumnName("status");
108 e.HasOne(x => x.Story).WithMany(s => s.Statuses).HasForeignKey(x => x.StoryId);
109 });
110
111 // CHAPTER
112 modelBuilder.Entity<Chapter>(e =>
113 {
114 e.ToTable("chapter");
115 e.HasKey(x => x.Id);
116 e.Property(x => x.Id).HasColumnName("chapter_id");
117 e.Property(x => x.Number).HasColumnName("chapter_number");
118 e.Property(x => x.Name).HasColumnName("chapter_name");
119 e.Property(x => x.Title).HasColumnName("title");
120 e.Property(x => x.Content).HasColumnName("chapter_content");
121 e.Property(x => x.WordCount).HasColumnName("word_count");
122 e.Property(x => x.Rating).HasColumnName("rating");
123 e.Property(x => x.PublishedAt).HasColumnName("published_at");
124 e.Property(x => x.ViewCount).HasColumnName("view_count");
125 e.Property(x => x.StoryId).HasColumnName("story_id");
126 e.Property(x => x.CreatedAt).HasColumnName("chapter_created_at");
127 e.Property(x => x.UpdatedAt).HasColumnName("chapter_updated_at");
128 e.HasOne(x => x.Story).WithMany(s => s.Chapters).HasForeignKey(x => x.StoryId);
129 });
130
131 // GENRE
132 modelBuilder.Entity<Genre>(e =>
133 {
134 e.ToTable("genre");
135 e.HasKey(x => x.Id);
136 e.Property(x => x.Id).HasColumnName("genre_id");
137 e.Property(x => x.Name).HasColumnName("genre_name");
138 });
139
140 // HAS_GENRE
141 modelBuilder.Entity<HasGenre>(e =>
142 {
143 e.ToTable("has_genre");
144 e.HasKey(x => new { x.StoryId, x.GenreId });
145 e.Ignore(x => x.Id);
146 e.Property(x => x.StoryId).HasColumnName("story_id");
147 e.Property(x => x.GenreId).HasColumnName("genre_id");
148 e.HasOne(x => x.Story).WithMany(s => s.HasGenres).HasForeignKey(x => x.StoryId);
149 e.HasOne(x => x.Genre).WithMany(g => g.HasGenres).HasForeignKey(x => x.GenreId);
150 });
151
152 // LIKES
153 modelBuilder.Entity<Likes>(e =>
154 {
155 e.ToTable("likes");
156 e.HasKey(x => new { x.UserId, x.StoryId });
157 e.Ignore(x => x.Id);
158 e.Property(x => x.UserId).HasColumnName("user_id");
159 e.Property(x => x.StoryId).HasColumnName("story_id");
160 e.Property(x => x.CreatedAt).HasColumnName("like_created_at");
161 e.HasOne(x => x.User).WithMany(u => u.Likes).HasForeignKey(x => x.UserId);
162 e.HasOne(x => x.Story).WithMany(s => s.Likes).HasForeignKey(x => x.StoryId);
163 });
164
165 // COMMENT
166 modelBuilder.Entity<Comment>(e =>
167 {
168 e.ToTable("comment");
169 e.HasKey(x => x.Id);
170 e.Property(x => x.Id).HasColumnName("comment_id");
171 e.Property(x => x.Content).HasColumnName("comment_content");
172 e.Property(x => x.UserId).HasColumnName("user_id");
173 e.Property(x => x.StoryId).HasColumnName("story_id");
174 e.Property(x => x.CreatedAt).HasColumnName("comment_created_at");
175 e.Property(x => x.UpdatedAt).HasColumnName("comment_updated_at");
176 e.HasOne(x => x.User).WithMany(u => u.Comments).HasForeignKey(x => x.UserId);
177 e.HasOne(x => x.Story).WithMany(s => s.Comments).HasForeignKey(x => x.StoryId);
178 });
179
180 // READING_LIST
181 modelBuilder.Entity<ReadingList>(e =>
182 {
183 e.ToTable("reading_list");
184 e.HasKey(x => x.Id);
185 e.Property(x => x.Id).HasColumnName("list_id");
186 e.Property(x => x.Name).HasColumnName("list_name");
187 e.Property(x => x.Content).HasColumnName("list_content");
188 e.Property(x => x.IsPublic).HasColumnName("is_public");
189 e.Property(x => x.UserId).HasColumnName("user_id");
190 e.Property(x => x.CreatedAt).HasColumnName("list_created_at");
191 e.Property(x => x.UpdatedAt).HasColumnName("list_updated_at");
192 e.HasOne(x => x.User).WithMany(u => u.ReadingLists).HasForeignKey(x => x.UserId);
193 });
194
195 // READING_LIST_ITEMS
196 modelBuilder.Entity<ReadingListItems>(e =>
197 {
198 e.ToTable("reading_list_items");
199 e.HasKey(x => new { x.ListId, x.StoryId });
200 e.Ignore(x => x.Id);
201 e.Property(x => x.ListId).HasColumnName("list_id");
202 e.Property(x => x.StoryId).HasColumnName("story_id");
203 e.Property(x => x.AddedAt).HasColumnName("added_at");
204 e.HasOne(x => x.ReadingList).WithMany(r => r.ReadingListItems).HasForeignKey(x => x.ListId);
205 e.HasOne(x => x.Story).WithMany(s => s.ReadingListItems).HasForeignKey(x => x.StoryId);
206 });
207
208 // NOTIFICATION
209 modelBuilder.Entity<Notification>(e =>
210 {
211 e.ToTable("notification");
212 e.HasKey(x => x.Id);
213 e.Property(x => x.Id).HasColumnName("notification_id");
214 e.Property(x => x.Content).HasColumnName("notification_content");
215 e.Property(x => x.IsRead).HasColumnName("is_read");
216 e.Property(x => x.CreatedAt).HasColumnName("notification_created_at");
217 e.Property(x => x.RecipientUserId).HasColumnName("recipient_user_id");
218 e.Property(x => x.Type).HasColumnName("type");
219 e.Property(x => x.Link).HasColumnName("link");
220 });
221
222 // CONTENT_TYPE
223 modelBuilder.Entity<ContentType>(e =>
224 {
225 e.ToTable("content_type");
226 e.HasKey(x => new { x.NotificationId, x.ContentTypeValue });
227 e.Ignore(x => x.Id);
228 e.Property(x => x.NotificationId).HasColumnName("notification_id");
229 e.Property(x => x.ContentTypeValue).HasColumnName("content_type");
230 e.HasOne(x => x.Notification).WithMany(n => n.ContentTypes).HasForeignKey(x => x.NotificationId);
231 });
232
233 // NOTIFY
234 modelBuilder.Entity<Notify>(e =>
235 {
236 e.ToTable("notify");
237 e.HasKey(x => new { x.UserId, x.StoryId, x.NotificationId });
238 e.Ignore(x => x.Id);
239 e.Property(x => x.UserId).HasColumnName("user_id");
240 e.Property(x => x.StoryId).HasColumnName("story_id");
241 e.Property(x => x.NotificationId).HasColumnName("notification_id");
242 e.HasOne(x => x.User).WithMany(u => u.Notifies).HasForeignKey(x => x.UserId);
243 e.HasOne(x => x.Story).WithMany(s => s.Notifies).HasForeignKey(x => x.StoryId);
244 e.HasOne(x => x.Notification).WithMany(n => n.Notifies).HasForeignKey(x => x.NotificationId);
245 });
246
247 // AI_SUGGESTION
248 modelBuilder.Entity<AISuggestion>(e =>
249 {
250 e.ToTable("ai_suggestion");
251 e.HasKey(x => x.Id);
252 e.Property(x => x.Id).HasColumnName("suggestion_id");
253 e.Property(x => x.OriginalText).HasColumnName("original_text");
254 e.Property(x => x.SuggestedText).HasColumnName("suggested_text");
255 e.Property(x => x.Accepted).HasColumnName("accepted");
256 e.Property(x => x.CreatedAt).HasColumnName("suggestion_created_at");
257 e.Property(x => x.AppliedAt).HasColumnName("applied_at");
258 e.Property(x => x.StoryId).HasColumnName("story_id");
259 e.HasOne(x => x.Story).WithMany(s => s.AISuggestions).HasForeignKey(x => x.StoryId);
260 });
261
262 // SUGGESTION_TYPE
263 modelBuilder.Entity<SuggestionType>(e =>
264 {
265 e.ToTable("suggestion_type");
266 e.HasKey(x => new { x.SuggestionId, x.SuggestionTypeValue });
267 e.Ignore(x => x.Id);
268 e.Property(x => x.SuggestionId).HasColumnName("suggestion_id");
269 e.Property(x => x.SuggestionTypeValue).HasColumnName("suggestion_type");
270 e.HasOne(x => x.AISuggestion).WithMany(a => a.SuggestionTypes).HasForeignKey(x => x.SuggestionId);
271 });
272
273 // NEED_APPROVAL
274 modelBuilder.Entity<NeedApproval>(e =>
275 {
276 e.ToTable("need_approval");
277 e.HasKey(x => new { x.SuggestionId, x.StoryId, x.ChapterId });
278 e.Ignore(x => x.Id);
279 e.Property(x => x.SuggestionId).HasColumnName("suggestion_id");
280 e.Property(x => x.StoryId).HasColumnName("story_id");
281 e.Property(x => x.ChapterId).HasColumnName("chapter_id");
282 e.HasOne(x => x.AISuggestion).WithMany(a => a.NeedApprovals).HasForeignKey(x => x.SuggestionId);
283 e.HasOne(x => x.Story).WithMany(s => s.NeedApprovals).HasForeignKey(x => x.StoryId);
284 e.HasOne(x => x.Chapter).WithMany(c => c.NeedApprovals).HasForeignKey(x => x.ChapterId);
285 });
286
287 // COLLABORATION
288 modelBuilder.Entity<Collaboration>(e =>
289 {
290 e.ToTable("collaboration");
291 e.HasKey(x => new { x.UserId, x.StoryId });
292 e.Ignore(x => x.Id);
293 e.Property(x => x.UserId).HasColumnName("user_id");
294 e.Property(x => x.StoryId).HasColumnName("story_id");
295 e.Property(x => x.CreatedAt).HasColumnName("collab_created_at");
296 e.HasOne(x => x.User).WithMany(u => u.Collaborations).HasForeignKey(x => x.UserId);
297 e.HasOne(x => x.Story).WithMany(s => s.Collaborations).HasForeignKey(x => x.StoryId);
298 });
299
300 // ROLES
301 modelBuilder.Entity<Roles>(e =>
302 {
303 e.ToTable("roles");
304 e.HasKey(x => new { x.UserId, x.StoryId, x.RoleValue });
305 e.Ignore(x => x.Id);
306 e.Property(x => x.UserId).HasColumnName("user_id");
307 e.Property(x => x.StoryId).HasColumnName("story_id");
308 e.Property(x => x.RoleValue).HasColumnName("roles");
309 });
310
311 // PERMISSION_LEVEL
312 modelBuilder.Entity<PermissionLevel>(e =>
313 {
314 e.ToTable("permission_level");
315 e.HasKey(x => new { x.UserId, x.StoryId, x.Level });
316 e.Ignore(x => x.Id);
317 e.Property(x => x.UserId).HasColumnName("user_id");
318 e.Property(x => x.StoryId).HasColumnName("story_id");
319 e.Property(x => x.Level).HasColumnName("permission_level");
320 });
321 }
322 }
323}
Note: See TracBrowser for help on using the repository browser.