-
Notifications
You must be signed in to change notification settings - Fork 0
/
AppDb.cs
82 lines (79 loc) · 4.07 KB
/
AppDb.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
using EnergyScanApi.Models;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using MySqlConnector;
using System;
using System.Linq;
namespace EnergyScanApi
{
public class AppDb : DbContext
{
public MySqlConnection Connection { get; }
public AppDb(DbContextOptions options, IConfiguration configuration) : base(options)
{
if (configuration["Database:insertCorePolicys"].ToString().Equals("true"))
{
//Database.Migrate();
if (Policies.FirstOrDefault(p => p.Name.Equals("read")) == null)
{
Policies.Add(new Policy() { Name = "read", Id = Guid.NewGuid().ToString(), CreationDate = DateTime.Now, ChangedLast = DateTime.Now });
}
if (Policies.FirstOrDefault(p => p.Name.Equals("create")) == null)
{
Policies.Add(new Policy() { Name = "create", Id = Guid.NewGuid().ToString(), CreationDate = DateTime.Now, ChangedLast = DateTime.Now });
}
if (Policies.FirstOrDefault(p => p.Name.Equals("update")) == null)
{
Policies.Add(new Policy() { Name = "update", Id = Guid.NewGuid().ToString(), CreationDate = DateTime.Now, ChangedLast = DateTime.Now });
}
if (Policies.FirstOrDefault(p => p.Name.Equals("delete")) == null)
{
Policies.Add(new Policy() { Name = "delete", Id = Guid.NewGuid().ToString(), CreationDate = DateTime.Now, ChangedLast = DateTime.Now });
}
if (Policies.FirstOrDefault(p => p.Name.Equals("usermanager")) == null)
{
Policies.Add(new Policy() { Name = "usermanager", Id = Guid.NewGuid().ToString(), CreationDate = DateTime.Now, ChangedLast = DateTime.Now });
}
if (Policies.FirstOrDefault(p => p.Name.Equals("requestmanager")) == null)
{
Policies.Add(new Policy() { Name = "requestmanager", Id = Guid.NewGuid().ToString(), CreationDate = DateTime.Now, ChangedLast = DateTime.Now });
}
if (Policies.FirstOrDefault(p => p.Name.Equals("policymanager")) == null)
{
Policies.Add(new Policy() { Name = "policymanager", Id = Guid.NewGuid().ToString(), CreationDate = DateTime.Now, ChangedLast = DateTime.Now });
}
if (Policies.FirstOrDefault(p => p.Name.Equals("groupmanager")) == null)
{
Policies.Add(new Policy() { Name = "groupmanager", Id = Guid.NewGuid().ToString(), CreationDate = DateTime.Now, ChangedLast = DateTime.Now });
}
SaveChanges();
SettingsHelpers.AddOrUpdateAppSetting("Database:insertCorePolicys", "false");
}
}
protected override void OnModelCreating(ModelBuilder builder)
{
builder.Entity<User>()
.HasIndex(u => u.Username)
.IsUnique();
builder.Entity<User>()
.HasIndex(u => u.Email)
.IsUnique();
}
public DbSet<Barcode> Barcodes { get; set; }
public DbSet<BarcodeCan> BarcodeCans { get; set; }
public DbSet<Can> Cans { get; set; }
public DbSet<ChangeRequest> ChangeRequests { get; set; }
public DbSet<Country> Countries { get; set; }
public DbSet<Group> Groups { get; set; }
public DbSet<Image> Images { get; set; }
public DbSet<ImageCan> ImageCans { get; set; }
public DbSet<Manufacturer> Manufacturers { get; set; }
public DbSet<Policy> Policies { get; set; }
public DbSet<Status> Statuses { get; set; }
public DbSet<Tag> Tags { get; set; }
public DbSet<TagCan> TagCans { get; set; }
public DbSet<Taste> Tastes { get; set; }
public DbSet<User> Users { get; set; }
public DbSet<UserPolicy> UserPolicies { get; set; }
}
}