Files

185 lines
5.0 KiB
C#
Raw Permalink Normal View History

2023-11-21 12:25:31 +01:00
using System;
2023-11-22 09:34:06 +01:00
using System.Net.Http;
2023-11-21 12:25:31 +01:00
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.InMemory;
using WD7UVN_HFT_2023241.Models;
namespace WD7UVN_HFT_2023241.Repository
{
2023-11-22 11:39:36 +01:00
public static class Database
{
2023-11-22 14:02:02 +01:00
public static CompanyDbContext Context = new CompanyDbContext();
2023-11-22 11:39:36 +01:00
}
2023-11-21 12:25:31 +01:00
public class CompanyDbContext : DbContext
{
2023-11-21 12:33:46 +01:00
//Tables
2023-11-21 12:25:31 +01:00
public DbSet<Customer> Customers { get; set; }
public DbSet<Employee> Employees { get; set; }
public DbSet<MaintainerTeam> Maintainers { get; set; }
public DbSet<Service> Services { get; set; }
2023-11-21 12:33:46 +01:00
public CompanyDbContext()
{
this.Database.EnsureCreated();
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
optionsBuilder
.UseInMemoryDatabase("company");
2023-11-22 09:34:06 +01:00
}
}
2023-11-22 09:11:15 +01:00
2023-11-22 09:34:06 +01:00
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
2023-11-22 09:11:15 +01:00
2023-11-22 09:34:06 +01:00
modelBuilder.Entity<Customer>(customer => customer
.HasOne<Service>()
.WithMany()
.HasForeignKey(customer => customer.SERVICE_ID)
2024-04-29 22:57:16 +02:00
.OnDelete(DeleteBehavior.SetNull));
2023-11-22 09:11:15 +01:00
2023-11-22 14:02:02 +01:00
modelBuilder.Entity<Service>(service => service
.HasOne<MaintainerTeam>()
.WithMany()
.HasForeignKey(service => service.MAINTAINER_ID)
.OnDelete(DeleteBehavior.SetNull));
modelBuilder.Entity<MaintainerTeam>(team => team
.HasOne<Employee>()
.WithMany()
.HasForeignKey(team => team.LEADER_ID)
.OnDelete(DeleteBehavior.SetNull));
modelBuilder.Entity<Employee>(emp => emp
.HasOne<Employee>()
.WithMany()
.HasForeignKey(emp => emp.MANAGER_ID)
.OnDelete(DeleteBehavior.SetNull));
2024-04-29 22:57:16 +02:00
modelBuilder.Entity<Employee>(emp => emp
.HasOne<MaintainerTeam>()
.WithMany()
.HasForeignKey(emp => emp.MAINTAINER_ID)
.OnDelete(DeleteBehavior.SetNull));
2023-11-22 09:34:06 +01:00
//loading test values
modelBuilder.Entity<Customer>().HasData(
new Customer{
2023-12-10 18:01:22 +01:00
NAME = "Szemed Fénye Optika Kft.",
ID = 1,
EMAIL = "info@szemedfenye.hu",
PHONE = "+36 30 123 4567",
SERVICE_ID = 1
},
new Customer{
NAME = "DoBox Logisztika Kft.",
ID = 2,
EMAIL = "info@dobox.hu",
PHONE = "+36 50 234 5678",
SERVICE_ID = 2
}
2023-11-22 09:34:06 +01:00
);
2023-11-22 09:11:15 +01:00
2023-11-22 09:34:06 +01:00
modelBuilder.Entity<Service>().HasData(
new Service{
2023-12-10 18:01:22 +01:00
NAME = "Microsoft Exchange",
ID = 1,
ACCOUNT = "admin:password123",
NOTES = "Currently migrating to EXOnline",
SERVICE_DOMAIN = "admin.exchange.intranet.szemedfenye.hu",
PORT = 443,
IP = "10.42.567.3",
VERSION = "2016",
2023-12-10 18:01:22 +01:00
MAINTAINER_ID = 1
},
new Service{
NAME = "OpenLDAP",
ID = 2,
ACCOUNT = "ldapadmin:verystrongpassword",
NOTES = "OpenLDAP directory access protocol on Linux, over SSL",
SERVICE_DOMAIN = "conf.ldap.intranet.dobox.hu",
PORT = 636,
IP = "66.254.114.41",
MAINTAINER_ID = 2,
VERSION = "2.4.57"
2023-12-10 18:01:22 +01:00
}
2023-11-22 09:34:06 +01:00
);
2023-11-22 09:11:15 +01:00
2023-11-22 09:34:06 +01:00
modelBuilder.Entity<MaintainerTeam>().HasData(
new MaintainerTeam{
2023-12-10 18:01:22 +01:00
ID = 1,
NAME = "Microsoft Team",
EMAIL = "microsoft@ourcompany.hu",
LEADER_ID = 2
},
new MaintainerTeam{
ID = 2,
NAME = "Linux Team",
LEADER_ID = 4,
EMAIL = "linux@ourcompany.hu"
}
2023-11-22 09:34:06 +01:00
);
2023-11-22 09:11:15 +01:00
2023-11-22 09:34:06 +01:00
modelBuilder.Entity<Employee>().HasData(
new Employee{
2023-12-10 18:01:22 +01:00
ID = 1,
NAME = "Gipsz Jakab",
EMAIL = "gipsz.jakab@ourcompany.hu",
PHONE = "+36 20 345 6789",
MANAGER_ID = 2,
MAINTAINER_ID = 1
},
2023-11-22 09:34:06 +01:00
new Employee{
2023-12-10 18:01:22 +01:00
NAME = "Nagy Krisztina",
ID = 2,
MAINTAINER_ID = 1,
EMAIL = "nagy.krisztina@ourcompany.hu",
PHONE = "+36 30 987 6543"
},
2023-11-21 12:33:46 +01:00
2023-11-22 09:34:06 +01:00
new Employee{
2023-12-10 18:01:22 +01:00
NAME = "Székely Csaba",
ID = 3,
PHONE = "+36 50 8766 5432",
EMAIL = "szekely.csaba@ourcompany.hu",
MANAGER_ID = 2,
MAINTAINER_ID = 1
},
new Employee{
NAME = "Marik Tamás",
ID = 4,
PHONE = "+36 20 345 6780",
EMAIL = "marik.tamas@ourcompany.hu",
MAINTAINER_ID = 2,
},
new Employee{
NAME = "Dávid András",
ID = 5,
PHONE = "+36 51 865 2876",
EMAIL = "david.andras@ourcompany.hu",
MANAGER_ID = 4,
MAINTAINER_ID = 2
},
new Employee{
NAME = "Steiner Zsófia",
ID = 6,
PHONE = "+36 20 756 8635",
MANAGER_ID = 4,
MAINTAINER_ID = 2
}
2023-11-22 09:34:06 +01:00
);
2023-11-21 12:33:46 +01:00
}
2023-11-21 12:25:31 +01:00
}
}