source: ChapterX.Infrastructure/Data/DataContext/ApplicationDbContext.cs@ 0b502c2

main
Last change on this file since 0b502c2 was 0b502c2, checked in by kikisrbinoska <srbinoskakristina07@…>, 12 days ago

Fixed user profile and reading lists

  • 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.ShortDescription).HasColumnName("short_description");
91 e.Property(x => x.Image).HasColumnName("image");
92 e.Property(x => x.Content).HasColumnName("story_content");
93 e.Property(x => x.UserId).HasColumnName("user_id");
94 e.Property(x => x.CreatedAt).HasColumnName("story_created_at");
95 e.Property(x => x.UpdatedAt).HasColumnName("story_updated_at");
96 e.HasOne(x => x.Writer).WithMany(w => w.Stories).HasForeignKey(x => x.UserId);
97 });
98
99 // STATUS
100 modelBuilder.Entity<Status>(e =>
101 {
102 e.ToTable("status");
103 e.HasKey(x => new { x.StoryId, x.StatusValue });
104 e.Ignore(x => x.Id);
105 e.Property(x => x.StoryId).HasColumnName("story_id");
106 e.Property(x => x.StatusValue).HasColumnName("status");
107 e.HasOne(x => x.Story).WithMany(s => s.Statuses).HasForeignKey(x => x.StoryId);
108 });
109
110 // CHAPTER
111 modelBuilder.Entity<Chapter>(e =>
112 {
113 e.ToTable("chapter");
114 e.HasKey(x => x.Id);
115 e.Property(x => x.Id).HasColumnName("chapter_id");
116 e.Property(x => x.Number).HasColumnName("chapter_number");
117 e.Property(x => x.Name).HasColumnName("chapter_name");
118 e.Property(x => x.Title).HasColumnName("title");
119 e.Property(x => x.Content).HasColumnName("chapter_content");
120 e.Property(x => x.WordCount).HasColumnName("word_count");
121 e.Property(x => x.Rating).HasColumnName("rating");
122 e.Property(x => x.PublishedAt).HasColumnName("published_at");
123 e.Property(x => x.ViewCount).HasColumnName("view_count");
124 e.Property(x => x.StoryId).HasColumnName("story_id");
125 e.Property(x => x.CreatedAt).HasColumnName("chapter_created_at");
126 e.Property(x => x.UpdatedAt).HasColumnName("chapter_updated_at");
127 e.HasOne(x => x.Story).WithMany(s => s.Chapters).HasForeignKey(x => x.StoryId);
128 });
129
130 // GENRE
131 modelBuilder.Entity<Genre>(e =>
132 {
133 e.ToTable("genre");
134 e.HasKey(x => x.Id);
135 e.Property(x => x.Id).HasColumnName("genre_id");
136 e.Property(x => x.Name).HasColumnName("genre_name");
137 });
138
139 // HAS_GENRE
140 modelBuilder.Entity<HasGenre>(e =>
141 {
142 e.ToTable("has_genre");
143 e.HasKey(x => new { x.StoryId, x.GenreId });
144 e.Ignore(x => x.Id);
145 e.Property(x => x.StoryId).HasColumnName("story_id");
146 e.Property(x => x.GenreId).HasColumnName("genre_id");
147 e.HasOne(x => x.Story).WithMany(s => s.HasGenres).HasForeignKey(x => x.StoryId);
148 e.HasOne(x => x.Genre).WithMany(g => g.HasGenres).HasForeignKey(x => x.GenreId);
149 });
150
151 // LIKES
152 modelBuilder.Entity<Likes>(e =>
153 {
154 e.ToTable("likes");
155 e.HasKey(x => new { x.UserId, x.StoryId });
156 e.Ignore(x => x.Id);
157 e.Property(x => x.UserId).HasColumnName("user_id");
158 e.Property(x => x.StoryId).HasColumnName("story_id");
159 e.Property(x => x.CreatedAt).HasColumnName("like_created_at");
160 e.HasOne(x => x.User).WithMany(u => u.Likes).HasForeignKey(x => x.UserId);
161 e.HasOne(x => x.Story).WithMany(s => s.Likes).HasForeignKey(x => x.StoryId);
162 });
163
164 // COMMENT
165 modelBuilder.Entity<Comment>(e =>
166 {
167 e.ToTable("comment");
168 e.HasKey(x => x.Id);
169 e.Property(x => x.Id).HasColumnName("comment_id");
170 e.Property(x => x.Content).HasColumnName("comment_content");
171 e.Property(x => x.UserId).HasColumnName("user_id");
172 e.Property(x => x.StoryId).HasColumnName("story_id");
173 e.Property(x => x.CreatedAt).HasColumnName("comment_created_at");
174 e.Property(x => x.UpdatedAt).HasColumnName("comment_updated_at");
175 e.HasOne(x => x.User).WithMany(u => u.Comments).HasForeignKey(x => x.UserId);
176 e.HasOne(x => x.Story).WithMany(s => s.Comments).HasForeignKey(x => x.StoryId);
177 });
178
179 // READING_LIST
180 modelBuilder.Entity<ReadingList>(e =>
181 {
182 e.ToTable("reading_list");
183 e.HasKey(x => x.Id);
184 e.Property(x => x.Id).HasColumnName("list_id");
185 e.Property(x => x.Name).HasColumnName("list_name");
186 e.Property(x => x.Content).HasColumnName("list_content");
187 e.Property(x => x.IsPublic).HasColumnName("is_public");
188 e.Property(x => x.UserId).HasColumnName("user_id");
189 e.Property(x => x.CreatedAt).HasColumnName("list_created_at");
190 e.Property(x => x.UpdatedAt).HasColumnName("list_updated_at");
191 e.HasOne(x => x.User).WithMany(u => u.ReadingLists).HasForeignKey(x => x.UserId);
192 });
193
194 // READING_LIST_ITEMS
195 modelBuilder.Entity<ReadingListItems>(e =>
196 {
197 e.ToTable("reading_list_items");
198 e.HasKey(x => new { x.ListId, x.StoryId });
199 e.Ignore(x => x.Id);
200 e.Property(x => x.ListId).HasColumnName("list_id");
201 e.Property(x => x.StoryId).HasColumnName("story_id");
202 e.Property(x => x.AddedAt).HasColumnName("added_at");
203 e.HasOne(x => x.ReadingList).WithMany(r => r.ReadingListItems).HasForeignKey(x => x.ListId);
204 e.HasOne(x => x.Story).WithMany(s => s.ReadingListItems).HasForeignKey(x => x.StoryId);
205 });
206
207 // NOTIFICATION
208 modelBuilder.Entity<Notification>(e =>
209 {
210 e.ToTable("notification");
211 e.HasKey(x => x.Id);
212 e.Property(x => x.Id).HasColumnName("notification_id");
213 e.Property(x => x.Content).HasColumnName("notification_content");
214 e.Property(x => x.IsRead).HasColumnName("is_read");
215 e.Property(x => x.CreatedAt).HasColumnName("notification_created_at");
216 e.Property(x => x.RecipientUserId).HasColumnName("recipient_user_id");
217 e.Property(x => x.Type).HasColumnName("type");
218 e.Property(x => x.Link).HasColumnName("link");
219 });
220
221 // CONTENT_TYPE
222 modelBuilder.Entity<ContentType>(e =>
223 {
224 e.ToTable("content_type");
225 e.HasKey(x => new { x.NotificationId, x.ContentTypeValue });
226 e.Ignore(x => x.Id);
227 e.Property(x => x.NotificationId).HasColumnName("notification_id");
228 e.Property(x => x.ContentTypeValue).HasColumnName("content_type");
229 e.HasOne(x => x.Notification).WithMany(n => n.ContentTypes).HasForeignKey(x => x.NotificationId);
230 });
231
232 // NOTIFY
233 modelBuilder.Entity<Notify>(e =>
234 {
235 e.ToTable("notify");
236 e.HasKey(x => new { x.UserId, x.StoryId, x.NotificationId });
237 e.Ignore(x => x.Id);
238 e.Property(x => x.UserId).HasColumnName("user_id");
239 e.Property(x => x.StoryId).HasColumnName("story_id");
240 e.Property(x => x.NotificationId).HasColumnName("notification_id");
241 e.HasOne(x => x.User).WithMany(u => u.Notifies).HasForeignKey(x => x.UserId);
242 e.HasOne(x => x.Story).WithMany(s => s.Notifies).HasForeignKey(x => x.StoryId);
243 e.HasOne(x => x.Notification).WithMany(n => n.Notifies).HasForeignKey(x => x.NotificationId);
244 });
245
246 // AI_SUGGESTION
247 modelBuilder.Entity<AISuggestion>(e =>
248 {
249 e.ToTable("ai_suggestion");
250 e.HasKey(x => x.Id);
251 e.Property(x => x.Id).HasColumnName("suggestion_id");
252 e.Property(x => x.OriginalText).HasColumnName("original_text");
253 e.Property(x => x.SuggestedText).HasColumnName("suggested_text");
254 e.Property(x => x.Accepted).HasColumnName("accepted");
255 e.Property(x => x.CreatedAt).HasColumnName("suggestion_created_at");
256 e.Property(x => x.AppliedAt).HasColumnName("applied_at");
257 e.Property(x => x.StoryId).HasColumnName("story_id");
258 e.HasOne(x => x.Story).WithMany(s => s.AISuggestions).HasForeignKey(x => x.StoryId);
259 });
260
261 // SUGGESTION_TYPE
262 modelBuilder.Entity<SuggestionType>(e =>
263 {
264 e.ToTable("suggestion_type");
265 e.HasKey(x => new { x.SuggestionId, x.SuggestionTypeValue });
266 e.Ignore(x => x.Id);
267 e.Property(x => x.SuggestionId).HasColumnName("suggestion_id");
268 e.Property(x => x.SuggestionTypeValue).HasColumnName("suggestion_type");
269 e.HasOne(x => x.AISuggestion).WithMany(a => a.SuggestionTypes).HasForeignKey(x => x.SuggestionId);
270 });
271
272 // NEED_APPROVAL
273 modelBuilder.Entity<NeedApproval>(e =>
274 {
275 e.ToTable("need_approval");
276 e.HasKey(x => new { x.SuggestionId, x.StoryId, x.ChapterId });
277 e.Ignore(x => x.Id);
278 e.Property(x => x.SuggestionId).HasColumnName("suggestion_id");
279 e.Property(x => x.StoryId).HasColumnName("story_id");
280 e.Property(x => x.ChapterId).HasColumnName("chapter_id");
281 e.HasOne(x => x.AISuggestion).WithMany(a => a.NeedApprovals).HasForeignKey(x => x.SuggestionId);
282 e.HasOne(x => x.Story).WithMany(s => s.NeedApprovals).HasForeignKey(x => x.StoryId);
283 e.HasOne(x => x.Chapter).WithMany(c => c.NeedApprovals).HasForeignKey(x => x.ChapterId);
284 });
285
286 // COLLABORATION
287 modelBuilder.Entity<Collaboration>(e =>
288 {
289 e.ToTable("collaboration");
290 e.HasKey(x => new { x.UserId, x.StoryId });
291 e.Ignore(x => x.Id);
292 e.Property(x => x.UserId).HasColumnName("user_id");
293 e.Property(x => x.StoryId).HasColumnName("story_id");
294 e.Property(x => x.CreatedAt).HasColumnName("collab_created_at");
295 e.HasOne(x => x.User).WithMany(u => u.Collaborations).HasForeignKey(x => x.UserId);
296 e.HasOne(x => x.Story).WithMany(s => s.Collaborations).HasForeignKey(x => x.StoryId);
297 });
298
299 // ROLES
300 modelBuilder.Entity<Roles>(e =>
301 {
302 e.ToTable("roles");
303 e.HasKey(x => new { x.UserId, x.StoryId, x.RoleValue });
304 e.Ignore(x => x.Id);
305 e.Property(x => x.UserId).HasColumnName("user_id");
306 e.Property(x => x.StoryId).HasColumnName("story_id");
307 e.Property(x => x.RoleValue).HasColumnName("roles");
308 });
309
310 // PERMISSION_LEVEL
311 modelBuilder.Entity<PermissionLevel>(e =>
312 {
313 e.ToTable("permission_level");
314 e.HasKey(x => new { x.UserId, x.StoryId, x.Level });
315 e.Ignore(x => x.Id);
316 e.Property(x => x.UserId).HasColumnName("user_id");
317 e.Property(x => x.StoryId).HasColumnName("story_id");
318 e.Property(x => x.Level).HasColumnName("permission_level");
319 });
320 }
321 }
322}
Note: See TracBrowser for help on using the repository browser.