source: resTools_backend/backend/Data/DataContext.cs

Last change on this file was 13f1472, checked in by Danilo <danilo.najkov@…>, 2 years ago

vip functionallity + menu fields + alergens filtering + google/fb login + email queueing

  • Property mode set to 100644
File size: 4.5 KB
Line 
1using backend.Entities;
2using Microsoft.EntityFrameworkCore;
3
4namespace backend.Data
5{
6 public class DataContext : DbContext
7 {
8 public DataContext(DbContextOptions<DataContext> dbContextOptions) : base(dbContextOptions)
9 {
10 }
11
12 private DbSet<User> users;
13 public DbSet<User> Users
14 {
15 get
16 {
17 if (users == null)
18 {
19 users = Set<User>();
20 }
21
22 return users;
23 }
24 }
25
26 private DbSet<Restaurant> restaurants;
27 public DbSet<Restaurant> Restoraunts
28 {
29 get
30 {
31 if (restaurants == null)
32 {
33 restaurants = Set<Restaurant>();
34 }
35
36 return restaurants;
37 }
38 }
39
40 private DbSet<Reservation> reservations;
41 public DbSet<Reservation> Reservations
42 {
43 get
44 {
45 if (reservations == null)
46 {
47 reservations = Set<Reservation>();
48 }
49
50 return reservations;
51 }
52 }
53
54 private DbSet<MenuItem> menuItems;
55 public DbSet<MenuItem> MenuItems
56 {
57 get
58 {
59 if (menuItems == null)
60 {
61 menuItems = Set<MenuItem>();
62 }
63
64 return menuItems;
65 }
66 }
67
68 private DbSet<Review> reviews;
69 public DbSet<Review> Reviews
70 {
71 get
72 {
73 if (reviews == null)
74 {
75 reviews = Set<Review>();
76 }
77
78 return reviews;
79 }
80 }
81
82 private DbSet<ToDoItem> toDoItems;
83 public DbSet<ToDoItem> ToDoItems
84 {
85 get
86 {
87 if (toDoItems == null)
88 {
89 toDoItems = Set<ToDoItem>();
90 }
91
92 return toDoItems;
93 }
94 }
95
96 private DbSet<QueueItem> queueItems;
97 public DbSet<QueueItem> QueueItems
98 {
99 get
100 {
101 if (queueItems == null)
102 {
103 queueItems = Set<QueueItem>();
104 }
105
106 return queueItems;
107 }
108 }
109
110
111 protected override void OnModelCreating(ModelBuilder modelBuilder)
112 {
113 //
114 // User
115 //
116 modelBuilder.Entity<User>().Property(x => x.Id).IsRequired().ValueGeneratedOnAdd();
117 modelBuilder.Entity<User>()
118 .HasOne(p => p.Restaurant)
119 .WithOne(b => b.Owner);
120
121 //
122 // Restoraunt
123 //
124 modelBuilder.Entity<Restaurant>().Property(x => x.Id).IsRequired().ValueGeneratedOnAdd();
125 modelBuilder.Entity<Restaurant>()
126 .HasOne(p => p.Owner)
127 .WithOne(b => b.Restaurant)
128 .HasForeignKey<Restaurant>(k => k.OwnerFk);
129 modelBuilder.Entity<Restaurant>()
130 .HasMany(p => p.Reservations)
131 .WithOne(b => b.Restaurant);
132 modelBuilder.Entity<Restaurant>()
133 .HasMany(p => p.Reviews)
134 .WithOne(b => b.Restaurant);
135
136 //
137 // Reservation
138 //
139 modelBuilder.Entity<Reservation>().Property(x => x.Id).IsRequired().ValueGeneratedOnAdd();
140 modelBuilder.Entity<Reservation>()
141 .HasOne(p => p.Restaurant)
142 .WithMany(b => b.Reservations);
143
144 //
145 // MenuItem
146 //
147 modelBuilder.Entity<MenuItem>().Property(x => x.Id).IsRequired().ValueGeneratedOnAdd();
148 modelBuilder.Entity<MenuItem>()
149 .HasOne(p => p.Restaurant)
150 .WithMany(b => b.Menu);
151
152 //
153 // Review
154 //
155 modelBuilder.Entity<Review>().Property(x => x.Id).IsRequired().ValueGeneratedOnAdd();
156 modelBuilder.Entity<Review>()
157 .HasOne(p => p.Restaurant)
158 .WithMany(b => b.Reviews);
159 modelBuilder.Entity<Review>()
160 .HasOne(p => p.User);
161
162 //
163 // ToDoItem
164 //
165 modelBuilder.Entity<ToDoItem>().Property(x => x.Id).IsRequired().ValueGeneratedOnAdd();
166 modelBuilder.Entity<ToDoItem>()
167 .HasOne(p => p.LinkedReview);
168
169 modelBuilder.Entity<QueueItem>().Property(x => x.Id).IsRequired().ValueGeneratedOnAdd();
170 }
171 }
172}
Note: See TracBrowser for help on using the repository browser.