source: ChapterX.Infrastructure/Data/DataContext/ApplicationDbContext.cs@ 877c13c

main
Last change on this file since 877c13c was 877c13c, checked in by kikisrbinoska <srbinoskakristina07@…>, 4 months ago

Added files

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