diff --git a/CarShowroom/CarShowroomDataModels/AbstractModels/ICar.cs b/CarShowroom/CarShowroomDataModels/AbstractModels/ICar.cs index ff2ce4f..b60ba59 100644 --- a/CarShowroom/CarShowroomDataModels/AbstractModels/ICar.cs +++ b/CarShowroom/CarShowroomDataModels/AbstractModels/ICar.cs @@ -10,7 +10,7 @@ namespace CarShowroomContracts.AbstractModels public interface ICar : IId { string Color { get; } - DateTime RealiseDate { get; } + DateTime ReleaseDate { get; } int ModelId { get; } } } diff --git a/CarShowroom/CarShowroomDataModels/AbstractModels/ISale.cs b/CarShowroom/CarShowroomDataModels/AbstractModels/ISale.cs index 03da69f..e96bc53 100644 --- a/CarShowroom/CarShowroomDataModels/AbstractModels/ISale.cs +++ b/CarShowroom/CarShowroomDataModels/AbstractModels/ISale.cs @@ -11,8 +11,8 @@ namespace CarShowroomContracts.AbstractModels { DateTime SaleTime { get; } int Cost { get; } - int ClientId { get; } - int EmployeeId { get; } + int? ClientId { get; } + int? EmployeeId { get; } List CarIds { get; } List ServiceIds { get; } } diff --git a/CarShowroom/CarShowroomDataModels/Dtos/CarDto.cs b/CarShowroom/CarShowroomDataModels/Dtos/CarDto.cs index 1c0422c..1423b95 100644 --- a/CarShowroom/CarShowroomDataModels/Dtos/CarDto.cs +++ b/CarShowroom/CarShowroomDataModels/Dtos/CarDto.cs @@ -11,13 +11,13 @@ namespace CarShowroomContracts.Dtos { public int Id { get; set; } public string Color { get; set; } - public DateTime RealiseDate { get; set; } + public DateTime ReleaseDate { get; set; } public int ModelId { get; set; } public CarDto(ICar model) { Id = model.Id; Color = model.Color; - RealiseDate = model.RealiseDate; + ReleaseDate = model.ReleaseDate; ModelId = model.ModelId; } } diff --git a/CarShowroom/CarShowroomDataModels/Dtos/SaleDto.cs b/CarShowroom/CarShowroomDataModels/Dtos/SaleDto.cs index d11ae97..a7d4f10 100644 --- a/CarShowroom/CarShowroomDataModels/Dtos/SaleDto.cs +++ b/CarShowroom/CarShowroomDataModels/Dtos/SaleDto.cs @@ -12,8 +12,8 @@ namespace CarShowroomDataModels.Dtos public int Id { get; set; } public DateTime SaleTime { get; set; } public int Cost { get; set; } - public int ClientId { get; set; } - public int EmployeeId { get; set; } + public int? ClientId { get; set; } + public int? EmployeeId { get; set; } public List CarIds { get; set; } = new(); public List ServiceIds { get; set; } = new(); public SaleDto(ISale model) diff --git a/CarShowroom/CarShowroomDataModels/Views/CarView.cs b/CarShowroom/CarShowroomDataModels/Views/CarView.cs index e771c3f..d827811 100644 --- a/CarShowroom/CarShowroomDataModels/Views/CarView.cs +++ b/CarShowroom/CarShowroomDataModels/Views/CarView.cs @@ -6,7 +6,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace CarShowroomContracts.Dtos +namespace CarShowroomContracts.Views { public class CarView : ICar { @@ -15,7 +15,7 @@ namespace CarShowroomContracts.Dtos [DisplayName("Цвет")] public string Color { get; set; } [DisplayName("Дата производства")] - public DateTime RealiseDate { get; set; } + public DateTime ReleaseDate { get; set; } public int ModelId { get; set; } [DisplayName("Модель")] public string ModelName { get; set; } = string.Empty; @@ -27,7 +27,7 @@ namespace CarShowroomContracts.Dtos { Id = model.Id; Color = model.Color; - RealiseDate = model.RealiseDate; + ReleaseDate = model.ReleaseDate; ModelId = model.ModelId; } } diff --git a/CarShowroom/CarShowroomDataModels/Views/ClientView.cs b/CarShowroom/CarShowroomDataModels/Views/ClientView.cs index b0738aa..4d7e47f 100644 --- a/CarShowroom/CarShowroomDataModels/Views/ClientView.cs +++ b/CarShowroom/CarShowroomDataModels/Views/ClientView.cs @@ -6,7 +6,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace CarShowroomContracts.Dtos +namespace CarShowroomContracts.Views { public class ClientView : IClient { diff --git a/CarShowroom/CarShowroomDataModels/Views/EmployeeView.cs b/CarShowroom/CarShowroomDataModels/Views/EmployeeView.cs index 5431043..62c5da7 100644 --- a/CarShowroom/CarShowroomDataModels/Views/EmployeeView.cs +++ b/CarShowroom/CarShowroomDataModels/Views/EmployeeView.cs @@ -6,7 +6,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace CarShowroomContracts.Dtos +namespace CarShowroomContracts.Views { public class EmployeeView : IEmployee { diff --git a/CarShowroom/CarShowroomDataModels/Views/MakeView.cs b/CarShowroom/CarShowroomDataModels/Views/MakeView.cs index 6873fe0..28258ae 100644 --- a/CarShowroom/CarShowroomDataModels/Views/MakeView.cs +++ b/CarShowroom/CarShowroomDataModels/Views/MakeView.cs @@ -6,7 +6,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace CarShowroomContracts.Dtos +namespace CarShowroomContracts.Views { public class MakeView : IMake { diff --git a/CarShowroom/CarShowroomDataModels/Views/ModelView.cs b/CarShowroom/CarShowroomDataModels/Views/ModelView.cs index 32c85ef..ca00972 100644 --- a/CarShowroom/CarShowroomDataModels/Views/ModelView.cs +++ b/CarShowroom/CarShowroomDataModels/Views/ModelView.cs @@ -6,7 +6,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace CarShowroomContracts.Dtos +namespace CarShowroomContracts.Views { public class ModelView : IModel { diff --git a/CarShowroom/CarShowroomDataModels/Views/SaleView.cs b/CarShowroom/CarShowroomDataModels/Views/SaleView.cs index 7cda64c..ab847ef 100644 --- a/CarShowroom/CarShowroomDataModels/Views/SaleView.cs +++ b/CarShowroom/CarShowroomDataModels/Views/SaleView.cs @@ -1,5 +1,6 @@ using CarShowroomContracts.AbstractModels; using CarShowroomContracts.Dtos; +using CarShowroomContracts.Views; using System; using System.Collections.Generic; using System.ComponentModel; @@ -7,9 +8,9 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace CarShowroomDataModels.Dtos +namespace CarShowroomDataModels.Views { - internal class SaleView : ISale + public class SaleView : ISale { [DisplayName("Номер продажи")] public int Id { get; set; } @@ -17,10 +18,10 @@ namespace CarShowroomDataModels.Dtos public DateTime SaleTime { get; set; } [DisplayName("Сумма")] public int Cost { get; set; } - public int ClientId { get; set; } + public int? ClientId { get; set; } [DisplayName("Клиент")] public string ClientName { get; set; } = string.Empty; - public int EmployeeId { get; set; } + public int? EmployeeId { get; set; } [DisplayName("Сотрудник")] public string EmployeeName { get; set; } = string.Empty; public List CarIds diff --git a/CarShowroom/CarShowroomDataModels/Views/ServiceView.cs b/CarShowroom/CarShowroomDataModels/Views/ServiceView.cs index dafcf1b..b3e312c 100644 --- a/CarShowroom/CarShowroomDataModels/Views/ServiceView.cs +++ b/CarShowroom/CarShowroomDataModels/Views/ServiceView.cs @@ -5,7 +5,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace CarShowroomContracts.Dtos +namespace CarShowroomContracts.Views { public class ServiceView : IService { diff --git a/CarShowroom/CarShowroomDatabaseStorage/CarShowroomDatabase.cs b/CarShowroom/CarShowroomDatabaseStorage/CarShowroomDatabase.cs new file mode 100644 index 0000000..bc09ec4 --- /dev/null +++ b/CarShowroom/CarShowroomDatabaseStorage/CarShowroomDatabase.cs @@ -0,0 +1,86 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Azure.Core; +using Azure; +using CarShowroomDatabaseStorage.Entities; +using Microsoft.EntityFrameworkCore; + +namespace CarShowroomDatabaseStorage +{ + public class CarShowroomDatabase : DbContext + { + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + if (optionsBuilder.IsConfigured == false) + { + optionsBuilder.UseNpgsql("Host=localhost;Port=5433;Database=CarShowroomDatabase;Username=postgres;Password=postgres"); + //ptionsBuilder.UseSqlServer(@"Data Source=localhost\SQLEXPRESS;Initial Catalog=CarShowroomDatabase;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); + } + base.OnConfiguring(optionsBuilder); + } + + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + //composite primary keys + modelBuilder.Entity().HasKey(sc => new { sc.SaleId, sc.CarId }); + modelBuilder.Entity().HasKey(sc => new { sc.SaleId, sc.ServiceId }); + //default values + modelBuilder.Entity().Property(c => c.Name).HasDefaultValue("empty_string"); + modelBuilder.Entity().Property(c => c.Password).HasDefaultValue("empty_string"); + modelBuilder.Entity().Property(e => e.Name).HasDefaultValue("empty_string"); + modelBuilder.Entity().Property(e => e.Password).HasDefaultValue("empty_string"); + modelBuilder.Entity().Property(c => c.Color).HasDefaultValue("empty_string"); + modelBuilder.Entity().Property(c => c.Cost).HasDefaultValue(-1); + modelBuilder.Entity().Property(c => c.Price).HasDefaultValue(-1); + modelBuilder.Entity().Property(c => c.Cost).HasDefaultValue(-1); + //on delete actions + modelBuilder.Entity() + .HasOne(m => m.Make) + .WithMany(m => m.Models) + .OnDelete(DeleteBehavior.Restrict); + modelBuilder.Entity() + .HasOne(m => m.Model) + .WithMany(m => m.Cars) + .OnDelete(DeleteBehavior.Restrict); + modelBuilder.Entity() + .HasOne(m => m.Client) + .WithMany(m => m.Sales) + .OnDelete(DeleteBehavior.SetNull); + modelBuilder.Entity() + .HasOne(m => m.Employee) + .WithMany(m => m.Sales) + .OnDelete(DeleteBehavior.SetNull); + modelBuilder.Entity() + .HasOne(m => m.Sale) + .WithMany(m => m.SaleServices) + .OnDelete(DeleteBehavior.Restrict); + modelBuilder.Entity() + .HasOne(m => m.Sale) + .WithMany(m => m.SaleCars) + .OnDelete(DeleteBehavior.Restrict); + modelBuilder.Entity() + .HasOne(m => m.Service) + .WithMany(m => m.SaleServices) + .OnDelete(DeleteBehavior.Cascade); + modelBuilder.Entity() + .HasOne(m => m.Car) + .WithMany(m => m.SaleCars) + .OnDelete(DeleteBehavior.Cascade); + base.OnModelCreating(modelBuilder); + } + + public virtual DbSet Makes { set; get; } + public virtual DbSet Models { set; get; } + public virtual DbSet Cars { set; get; } + public virtual DbSet Services { set; get; } + public virtual DbSet Employees { set; get; } + public virtual DbSet Clients { set; get; } + public virtual DbSet Sales { set; get; } + public virtual DbSet SaleCars { set; get; } + public virtual DbSet SaleServices { set; get; } + } +} diff --git a/CarShowroom/CarShowroomDatabaseStorage/CarShowroomDatabaseStorage.csproj b/CarShowroom/CarShowroomDatabaseStorage/CarShowroomDatabaseStorage.csproj index c00b1c4..48bd433 100644 --- a/CarShowroom/CarShowroomDatabaseStorage/CarShowroomDatabaseStorage.csproj +++ b/CarShowroom/CarShowroomDatabaseStorage/CarShowroomDatabaseStorage.csproj @@ -6,6 +6,16 @@ enable + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + diff --git a/CarShowroom/CarShowroomDatabaseStorage/Class1.cs b/CarShowroom/CarShowroomDatabaseStorage/Class1.cs deleted file mode 100644 index 43b60ec..0000000 --- a/CarShowroom/CarShowroomDatabaseStorage/Class1.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace CarShowroomDatabaseStorage -{ - public class Class1 - { - - } -} diff --git a/CarShowroom/CarShowroomDatabaseStorage/Entities/Car.cs b/CarShowroom/CarShowroomDatabaseStorage/Entities/Car.cs index 23e9249..60da6b4 100644 --- a/CarShowroom/CarShowroomDatabaseStorage/Entities/Car.cs +++ b/CarShowroom/CarShowroomDatabaseStorage/Entities/Car.cs @@ -1,24 +1,66 @@ using CarShowroomContracts.AbstractModels; +using CarShowroomContracts.Views; using System; using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; using System.Linq; using System.Text; using System.Threading.Tasks; namespace CarShowroomDatabaseStorage.Entities { + [Table("car")] public class Car : ICar { - public int Id { get; set; } - public string Color { get; set; } - public DateTime RealiseDate { get; set; } - public int ModelId { get; set; } - public Car(ICar car) + [Column("car_id")] + public int Id { get; private set; } + [Required] + [Column("car_color")] + [MaxLength(50)] + public string Color { get; private set; } = string.Empty; + [Required] + [Column("car_releasedate")] + public DateTime ReleaseDate { get; private set; } + [Required] + [Column("car_model_id")] + public int ModelId { get; private set; } + public virtual Model? Model { get; set; } + public virtual List SaleCars { get; set; } = new(); + + private Car(){} + + private Car(ICar car) { Id = car.Id; Color = car.Color; - RealiseDate = DateTime.Now; + ReleaseDate = DateTime.Now; ModelId = car.ModelId; } + + public static Car? Create(ICar car) + { + if (car == null) + return null; + return new Car(car); + } + + public void Update(ICar car) + { + if (car == null) + return; + Color = car.Color; + ReleaseDate = car.ReleaseDate; + ModelId = car.ModelId; + } + + public CarView GetCarView() + { + CarView car = new CarView(this); + car.ModelPrice = Model?.Price ?? 0; + car.ModelName = Model?.Name ?? string.Empty; + car.MakeName = Model?.Make?.Name ?? string.Empty; + return car; + } } } diff --git a/CarShowroom/CarShowroomDatabaseStorage/Entities/Client.cs b/CarShowroom/CarShowroomDatabaseStorage/Entities/Client.cs index 8788df2..5d15bc1 100644 --- a/CarShowroom/CarShowroomDatabaseStorage/Entities/Client.cs +++ b/CarShowroom/CarShowroomDatabaseStorage/Entities/Client.cs @@ -1,24 +1,66 @@ -using CarShowroomDataModels.Models; +using CarShowroomContracts.AbstractModels; +using CarShowroomContracts.Views; +using CarShowroomDataModels.Models; +using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; using System.Linq; using System.Text; using System.Threading.Tasks; namespace CarShowroomDatabaseStorage.Entities { + [Table("client")] + [Index(nameof(Email), IsUnique = true)] public class Client : IClient { - public int Id { get; set; } - public string Name { get; set; } - public string Email { get; set; } - public string Password { get; set; } - public Client(IClient client) + [Column("client_id")] + public int Id { get; private set; } + [Required] + [Column("client_name")] + [MaxLength(50)] + public string Name { get; private set; } = string.Empty; + [Required] + [Column("client_email")] + [MaxLength(40)] + public string Email { get; private set; } = string.Empty; + [Required] + [Column("client_password")] + [MaxLength(32)] + public string Password { get; private set; } = string.Empty; + public virtual List Sales { get; set; } = new(); + + private Client() { } + + private Client(IClient client) { Id = client.Id; Name = client.Name; Email = client.Email; Password = client.Password; } + + public static Client? Create(IClient client) + { + if (client == null) + return null; + return new Client(client); + } + + public void Update(IClient client) + { + if (client == null) + return; + Name = client.Name; + Email = client.Email; + Password = client.Password; + } + + public ClientView GetClientView() + { + return new ClientView(this); + } } } diff --git a/CarShowroom/CarShowroomDatabaseStorage/Entities/Employee.cs b/CarShowroom/CarShowroomDatabaseStorage/Entities/Employee.cs index acc8f24..1eb0760 100644 --- a/CarShowroom/CarShowroomDatabaseStorage/Entities/Employee.cs +++ b/CarShowroom/CarShowroomDatabaseStorage/Entities/Employee.cs @@ -1,24 +1,65 @@ -using CarShowroomDataModels.Models; +using CarShowroomContracts.Views; +using CarShowroomDataModels.Models; +using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; using System.Linq; using System.Text; using System.Threading.Tasks; namespace CarShowroomDatabaseStorage.Entities { + [Table("employee")] + [Index(nameof(Email), IsUnique = true)] public class Employee : IEmployee { - public int Id { get; set; } - public string Name { get; set; } - public string Email { get; set; } - public string Password { get; set; } - public Employee(IEmployee employee) + [Column("employee_id")] + public int Id { get; private set; } + [Required] + [Column("employee_name")] + [MaxLength(50)] + public string Name { get; private set; } = string.Empty; + [Required] + [Column("employee_email")] + [MaxLength(40)] + public string Email { get; private set; } = string.Empty; + [Required] + [Column("employee_password")] + [MaxLength(32)] + public string Password { get; private set; } = string.Empty; + public virtual List Sales { get; set; } = new(); + + private Employee() { } + + private Employee(IEmployee employee) { Id = employee.Id; Name = employee.Name; Email = employee.Email; Password = employee.Password; } + + public static Employee? Create(IEmployee employee) + { + if (employee == null) + return null; + return new Employee(employee); + } + + public void Update(IEmployee employee) + { + if(employee == null) + return; + Name = employee.Name; + Email = employee.Email; + Password = employee.Password; + } + + public EmployeeView GetEmployeeView() + { + return new EmployeeView(this); + } } } diff --git a/CarShowroom/CarShowroomDatabaseStorage/Entities/Make.cs b/CarShowroom/CarShowroomDatabaseStorage/Entities/Make.cs index 3d6ec5e..f9a3abb 100644 --- a/CarShowroom/CarShowroomDatabaseStorage/Entities/Make.cs +++ b/CarShowroom/CarShowroomDatabaseStorage/Entities/Make.cs @@ -1,20 +1,53 @@ using CarShowroomContracts.AbstractModels; +using CarShowroomContracts.Views; +using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; using System.Linq; using System.Text; using System.Threading.Tasks; namespace CarShowroomDatabaseStorage.Entities { + [Table("make")] + [Index(nameof(Name), IsUnique = true)] public class Make : IMake { - public int Id { get; set; } - public string Name { get; set; } - public Make(IMake make) + [Column("make_id")] + public int Id { get; private set; } + [Required] + [Column("make_name")] + [MaxLength(50)] + public string Name { get; private set; } = string.Empty; + public virtual List Models { get; set; } = new(); + + private Make() { } + + private Make(IMake make) { Id = make.Id; Name = make.Name; } + + public static Make? Create(IMake make) + { + if (make == null) + return null; + return new Make(make); + } + + public void Update(IMake make) + { + if (make == null) + return; + Name = make.Name; + } + + public MakeView GetView() + { + return new MakeView(this); + } } } diff --git a/CarShowroom/CarShowroomDatabaseStorage/Entities/Model.cs b/CarShowroom/CarShowroomDatabaseStorage/Entities/Model.cs index cbb7e2d..adcf50f 100644 --- a/CarShowroom/CarShowroomDatabaseStorage/Entities/Model.cs +++ b/CarShowroom/CarShowroomDatabaseStorage/Entities/Model.cs @@ -1,24 +1,66 @@ using CarShowroomContracts.AbstractModels; +using CarShowroomContracts.Views; +using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; using System.Linq; using System.Text; using System.Threading.Tasks; namespace CarShowroomDatabaseStorage.Entities { + [Table("model")] + [Index(nameof(Name), IsUnique = true)] public class Model : IModel { - public int Id { get; set; } - public string Name { get; set; } - public int Price { get; set; } - public int MakeId { get; set; } - public Model(IModel model) + [Column("model_id")] + public int Id { get; private set; } + [Required] + [Column("model_name")] + [MaxLength(50)] + public string Name { get; private set; } = string.Empty; + [Required] + [Column("model_price")] + public int Price { get; private set; } + [Required] + [Column("model_make_id")] + public int MakeId { get; private set; } + public virtual Make? Make { get; set; } + public virtual List Cars { get; set; } = new(); + + private Model() { } + + private Model(IModel model) { Id = model.Id; Name = model.Name; Price = model.Price; MakeId = model.MakeId; } + + public static Model? Create(IModel model) + { + if (model == null) + return null; + return new Model(model); + } + + public void Update(IModel model) + { + if (model == null) + return; + Name = model.Name; + Price = model.Price; + MakeId = model.MakeId; + } + + public ModelView GetModelView() + { + ModelView model = new ModelView(this); + model.MakeName = Make?.Name ?? string.Empty; + return model; + } } } diff --git a/CarShowroom/CarShowroomDatabaseStorage/Entities/Sale.cs b/CarShowroom/CarShowroomDatabaseStorage/Entities/Sale.cs index d11178e..67aa616 100644 --- a/CarShowroom/CarShowroomDatabaseStorage/Entities/Sale.cs +++ b/CarShowroom/CarShowroomDatabaseStorage/Entities/Sale.cs @@ -1,30 +1,94 @@ using CarShowroomContracts.AbstractModels; +using CarShowroomContracts.Views; +using CarShowroomDataModels.Views; using System; using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; using System.Linq; using System.Text; using System.Threading.Tasks; namespace CarShowroomDatabaseStorage.Entities { + [Table("sale")] public class Sale : ISale { - public int Id { get; set; } - public DateTime SaleTime { get; set; } - public int Cost { get; set; } - public int ClientId { get; set; } - public int EmployeeId { get; set; } - public List CarIds { get; set; } - public List ServiceIds { get; set; } - public Sale(ISale sale) + [Column("sale_id")] + public int Id { get; private set; } + [Required] + [Column("sale_time")] + public DateTime SaleTime { get; private set; } + [Required] + [Column("sale_cost")] + public int Cost { get; private set; } + [Column("sale_client_id")] + public int? ClientId { get; private set; } + public virtual Client? Client { get; set; } + [Column("sale_employee_id")] + public int? EmployeeId { get; private set; } + public virtual Employee? Employee { get; set; } + public virtual List Cars { get; set; } = new(); + public virtual List SaleCars { get; set; } = new(); + [NotMapped] + public List CarIds + { + get + { + return Cars.Select(c => c.Id).ToList(); + } + } + public virtual List Services { get; set; } = new(); + public virtual List SaleServices { get; set; } = new(); + [NotMapped] + public List ServiceIds + { + get + { + return Services.Select(s => s.Id).ToList(); + } + } + + private Sale() { } + + private Sale(ISale sale) { Id = sale.Id; SaleTime = sale.SaleTime; Cost = sale.Cost; ClientId = sale.ClientId; EmployeeId = sale.EmployeeId; - CarIds = sale.CarIds; - ServiceIds = sale.ServiceIds; + } + + public static Sale? Create(ISale sale) + { + if (sale == null) + { + return null; + } + return new Sale(sale); + } + + public void Update(ISale sale) + { + if (sale == null) + { + return; + } + SaleTime = sale.SaleTime; + Cost = sale.Cost; + ClientId = sale.ClientId; + EmployeeId = sale.EmployeeId; + } + + public SaleView GetSaleView() + { + SaleView sale = new SaleView(this); + sale.ClientName = Client?.Name ?? string.Empty; + sale.EmployeeName = Employee?.Name ?? string.Empty; + sale.Services = Services.Select(s => s.GetServiceView()).ToList(); + sale.Cars = Cars.Select(c => c.GetCarView()).ToList(); + return sale; } } } diff --git a/CarShowroom/CarShowroomDatabaseStorage/Entities/SaleCar.cs b/CarShowroom/CarShowroomDatabaseStorage/Entities/SaleCar.cs new file mode 100644 index 0000000..a71c6d5 --- /dev/null +++ b/CarShowroom/CarShowroomDatabaseStorage/Entities/SaleCar.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations.Schema; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CarShowroomDatabaseStorage.Entities +{ + [Table("sale_car")] + public class SaleCar + { + [Column("sale_id")] + public int SaleId { get; set; } + public virtual Sale? Sale { get; set; } + [Column("car_id")] + public int CarId { get; set; } + public virtual Car? Car { get; set; } + } +} diff --git a/CarShowroom/CarShowroomDatabaseStorage/Entities/SaleService.cs b/CarShowroom/CarShowroomDatabaseStorage/Entities/SaleService.cs new file mode 100644 index 0000000..4e89e04 --- /dev/null +++ b/CarShowroom/CarShowroomDatabaseStorage/Entities/SaleService.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CarShowroomDatabaseStorage.Entities +{ + [Table("sale_service")] + public class SaleService + { + [Column("sale_id")] + public int SaleId { get; set; } + public virtual Sale? Sale { get; set; } + [Column("service_id")] + public int ServiceId { get; set; } + public virtual Service? Service { get; set; } + } +} diff --git a/CarShowroom/CarShowroomDatabaseStorage/Entities/Service.cs b/CarShowroom/CarShowroomDatabaseStorage/Entities/Service.cs index 3f03e6b..6b16842 100644 --- a/CarShowroom/CarShowroomDatabaseStorage/Entities/Service.cs +++ b/CarShowroom/CarShowroomDatabaseStorage/Entities/Service.cs @@ -1,22 +1,59 @@ using CarShowroomContracts.AbstractModels; +using CarShowroomContracts.Views; +using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; using System.Linq; +using System.Runtime.ConstrainedExecution; using System.Text; using System.Threading.Tasks; namespace CarShowroomDatabaseStorage.Entities { - internal class Service : IService + [Table("service")] + [Index(nameof(Name), IsUnique = true)] + public class Service : IService { - public int Id { get; set; } - public string Name { get; set; } - public int Cost { get; set; } - public Service(IService service) + [Column("service_id")] + public int Id { get; private set; } + [Required] + [Column("service_name")] + [MaxLength(50)] + public string Name { get; private set; } = string.Empty; + [Required] + [Column("service_cost")] + public int Cost { get; private set; } + public virtual List SaleServices { get; set; } = new(); + + private Service() { } + + private Service(IService service) { Id = service.Id; Name = service.Name; Cost = service.Cost; } + + public static Service? Create(IService service) + { + if (service == null) + return null; + return new Service(service); + } + + public void Update(IService service) + { + if (service == null) + return; + Name = service.Name; + Cost = service.Cost; + } + + public ServiceView GetServiceView() + { + return new ServiceView(this); + } } } diff --git a/CarShowroom/CarShowroomDatabaseStorage/Migrations/20240505145552_InitialCreate.Designer.cs b/CarShowroom/CarShowroomDatabaseStorage/Migrations/20240505145552_InitialCreate.Designer.cs new file mode 100644 index 0000000..659aa60 --- /dev/null +++ b/CarShowroom/CarShowroomDatabaseStorage/Migrations/20240505145552_InitialCreate.Designer.cs @@ -0,0 +1,436 @@ +// +using System; +using CarShowroomDatabaseStorage; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +#nullable disable + +namespace CarShowroomDatabaseStorage.Migrations +{ + [DbContext(typeof(CarShowroomDatabase))] + [Migration("20240505145552_InitialCreate")] + partial class InitialCreate + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.18") + .HasAnnotation("Relational:MaxIdentifierLength", 63); + + NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); + + modelBuilder.Entity("CarShowroomDatabaseStorage.Entities.Car", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer") + .HasColumnName("car_id"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Color") + .IsRequired() + .ValueGeneratedOnAdd() + .HasMaxLength(50) + .HasColumnType("character varying(50)") + .HasDefaultValue("empty_string") + .HasColumnName("car_color"); + + b.Property("ModelId") + .HasColumnType("integer") + .HasColumnName("car_model_id"); + + b.Property("ReleaseDate") + .HasColumnType("timestamp with time zone") + .HasColumnName("car_releasedate"); + + b.Property("SaleId") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("ModelId"); + + b.HasIndex("SaleId"); + + b.ToTable("car"); + }); + + modelBuilder.Entity("CarShowroomDatabaseStorage.Entities.Client", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer") + .HasColumnName("client_id"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Email") + .IsRequired() + .HasMaxLength(40) + .HasColumnType("character varying(40)") + .HasColumnName("client_email"); + + b.Property("Name") + .IsRequired() + .ValueGeneratedOnAdd() + .HasMaxLength(50) + .HasColumnType("character varying(50)") + .HasDefaultValue("empty_string") + .HasColumnName("client_name"); + + b.Property("Password") + .IsRequired() + .ValueGeneratedOnAdd() + .HasMaxLength(32) + .HasColumnType("character varying(32)") + .HasDefaultValue("empty_string") + .HasColumnName("client_password"); + + b.HasKey("Id"); + + b.HasIndex("Email") + .IsUnique(); + + b.ToTable("client"); + }); + + modelBuilder.Entity("CarShowroomDatabaseStorage.Entities.Employee", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer") + .HasColumnName("employee_id"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Email") + .IsRequired() + .HasMaxLength(40) + .HasColumnType("character varying(40)") + .HasColumnName("employee_email"); + + b.Property("Name") + .IsRequired() + .ValueGeneratedOnAdd() + .HasMaxLength(50) + .HasColumnType("character varying(50)") + .HasDefaultValue("empty_string") + .HasColumnName("employee_name"); + + b.Property("Password") + .IsRequired() + .ValueGeneratedOnAdd() + .HasMaxLength(32) + .HasColumnType("character varying(32)") + .HasDefaultValue("empty_string") + .HasColumnName("employee_password"); + + b.HasKey("Id"); + + b.HasIndex("Email") + .IsUnique(); + + b.ToTable("employee"); + }); + + modelBuilder.Entity("CarShowroomDatabaseStorage.Entities.Make", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer") + .HasColumnName("make_id"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Name") + .IsRequired() + .HasMaxLength(50) + .HasColumnType("character varying(50)") + .HasColumnName("make_name"); + + b.HasKey("Id"); + + b.HasIndex("Name") + .IsUnique(); + + b.ToTable("make"); + }); + + modelBuilder.Entity("CarShowroomDatabaseStorage.Entities.Model", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer") + .HasColumnName("model_id"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("MakeId") + .HasColumnType("integer") + .HasColumnName("model_make_id"); + + b.Property("Name") + .IsRequired() + .HasMaxLength(50) + .HasColumnType("character varying(50)") + .HasColumnName("model_name"); + + b.Property("Price") + .ValueGeneratedOnAdd() + .HasColumnType("integer") + .HasDefaultValue(-1) + .HasColumnName("model_price"); + + b.HasKey("Id"); + + b.HasIndex("MakeId"); + + b.HasIndex("Name") + .IsUnique(); + + b.ToTable("model"); + }); + + modelBuilder.Entity("CarShowroomDatabaseStorage.Entities.Sale", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer") + .HasColumnName("sale_id"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("ClientId") + .HasColumnType("integer") + .HasColumnName("sale_client_id"); + + b.Property("Cost") + .ValueGeneratedOnAdd() + .HasColumnType("integer") + .HasDefaultValue(-1) + .HasColumnName("sale_cost"); + + b.Property("EmployeeId") + .HasColumnType("integer") + .HasColumnName("sale_employee_id"); + + b.Property("SaleTime") + .HasColumnType("timestamp with time zone") + .HasColumnName("sale_time"); + + b.HasKey("Id"); + + b.HasIndex("ClientId"); + + b.HasIndex("EmployeeId"); + + b.ToTable("sale"); + }); + + modelBuilder.Entity("CarShowroomDatabaseStorage.Entities.SaleCar", b => + { + b.Property("SaleId") + .HasColumnType("integer") + .HasColumnName("sale_id"); + + b.Property("CarId") + .HasColumnType("integer") + .HasColumnName("car_id"); + + b.HasKey("SaleId", "CarId"); + + b.HasIndex("CarId"); + + b.ToTable("sale_car"); + }); + + modelBuilder.Entity("CarShowroomDatabaseStorage.Entities.SaleService", b => + { + b.Property("SaleId") + .HasColumnType("integer") + .HasColumnName("sale_id"); + + b.Property("ServiceId") + .HasColumnType("integer") + .HasColumnName("service_id"); + + b.HasKey("SaleId", "ServiceId"); + + b.HasIndex("ServiceId"); + + b.ToTable("sale_service"); + }); + + modelBuilder.Entity("CarShowroomDatabaseStorage.Entities.Service", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer") + .HasColumnName("service_id"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Cost") + .ValueGeneratedOnAdd() + .HasColumnType("integer") + .HasDefaultValue(-1) + .HasColumnName("service_cost"); + + b.Property("Name") + .IsRequired() + .HasMaxLength(50) + .HasColumnType("character varying(50)") + .HasColumnName("service_name"); + + b.Property("SaleId") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("Name") + .IsUnique(); + + b.HasIndex("SaleId"); + + b.ToTable("service"); + }); + + modelBuilder.Entity("CarShowroomDatabaseStorage.Entities.Car", b => + { + b.HasOne("CarShowroomDatabaseStorage.Entities.Model", "Model") + .WithMany("Cars") + .HasForeignKey("ModelId") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.HasOne("CarShowroomDatabaseStorage.Entities.Sale", null) + .WithMany("Cars") + .HasForeignKey("SaleId"); + + b.Navigation("Model"); + }); + + modelBuilder.Entity("CarShowroomDatabaseStorage.Entities.Model", b => + { + b.HasOne("CarShowroomDatabaseStorage.Entities.Make", "Make") + .WithMany("Models") + .HasForeignKey("MakeId") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.Navigation("Make"); + }); + + modelBuilder.Entity("CarShowroomDatabaseStorage.Entities.Sale", b => + { + b.HasOne("CarShowroomDatabaseStorage.Entities.Client", "Client") + .WithMany("Sales") + .HasForeignKey("ClientId") + .OnDelete(DeleteBehavior.SetNull); + + b.HasOne("CarShowroomDatabaseStorage.Entities.Employee", "Employee") + .WithMany("Sales") + .HasForeignKey("EmployeeId") + .OnDelete(DeleteBehavior.SetNull); + + b.Navigation("Client"); + + b.Navigation("Employee"); + }); + + modelBuilder.Entity("CarShowroomDatabaseStorage.Entities.SaleCar", b => + { + b.HasOne("CarShowroomDatabaseStorage.Entities.Car", "Car") + .WithMany("SaleCars") + .HasForeignKey("CarId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("CarShowroomDatabaseStorage.Entities.Sale", "Sale") + .WithMany("SaleCars") + .HasForeignKey("SaleId") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.Navigation("Car"); + + b.Navigation("Sale"); + }); + + modelBuilder.Entity("CarShowroomDatabaseStorage.Entities.SaleService", b => + { + b.HasOne("CarShowroomDatabaseStorage.Entities.Sale", "Sale") + .WithMany("SaleServices") + .HasForeignKey("SaleId") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.HasOne("CarShowroomDatabaseStorage.Entities.Service", "Service") + .WithMany("SaleServices") + .HasForeignKey("ServiceId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Sale"); + + b.Navigation("Service"); + }); + + modelBuilder.Entity("CarShowroomDatabaseStorage.Entities.Service", b => + { + b.HasOne("CarShowroomDatabaseStorage.Entities.Sale", null) + .WithMany("Services") + .HasForeignKey("SaleId"); + }); + + modelBuilder.Entity("CarShowroomDatabaseStorage.Entities.Car", b => + { + b.Navigation("SaleCars"); + }); + + modelBuilder.Entity("CarShowroomDatabaseStorage.Entities.Client", b => + { + b.Navigation("Sales"); + }); + + modelBuilder.Entity("CarShowroomDatabaseStorage.Entities.Employee", b => + { + b.Navigation("Sales"); + }); + + modelBuilder.Entity("CarShowroomDatabaseStorage.Entities.Make", b => + { + b.Navigation("Models"); + }); + + modelBuilder.Entity("CarShowroomDatabaseStorage.Entities.Model", b => + { + b.Navigation("Cars"); + }); + + modelBuilder.Entity("CarShowroomDatabaseStorage.Entities.Sale", b => + { + b.Navigation("Cars"); + + b.Navigation("SaleCars"); + + b.Navigation("SaleServices"); + + b.Navigation("Services"); + }); + + modelBuilder.Entity("CarShowroomDatabaseStorage.Entities.Service", b => + { + b.Navigation("SaleServices"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/CarShowroom/CarShowroomDatabaseStorage/Migrations/20240505145552_InitialCreate.cs b/CarShowroom/CarShowroomDatabaseStorage/Migrations/20240505145552_InitialCreate.cs new file mode 100644 index 0000000..91cc743 --- /dev/null +++ b/CarShowroom/CarShowroomDatabaseStorage/Migrations/20240505145552_InitialCreate.cs @@ -0,0 +1,304 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +#nullable disable + +namespace CarShowroomDatabaseStorage.Migrations +{ + /// + public partial class InitialCreate : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "client", + columns: table => new + { + client_id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + client_name = table.Column(type: "character varying(50)", maxLength: 50, nullable: false, defaultValue: "empty_string"), + client_email = table.Column(type: "character varying(40)", maxLength: 40, nullable: false), + client_password = table.Column(type: "character varying(32)", maxLength: 32, nullable: false, defaultValue: "empty_string") + }, + constraints: table => + { + table.PrimaryKey("PK_client", x => x.client_id); + }); + + migrationBuilder.CreateTable( + name: "employee", + columns: table => new + { + employee_id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + employee_name = table.Column(type: "character varying(50)", maxLength: 50, nullable: false, defaultValue: "empty_string"), + employee_email = table.Column(type: "character varying(40)", maxLength: 40, nullable: false), + employee_password = table.Column(type: "character varying(32)", maxLength: 32, nullable: false, defaultValue: "empty_string") + }, + constraints: table => + { + table.PrimaryKey("PK_employee", x => x.employee_id); + }); + + migrationBuilder.CreateTable( + name: "make", + columns: table => new + { + make_id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + make_name = table.Column(type: "character varying(50)", maxLength: 50, nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_make", x => x.make_id); + }); + + migrationBuilder.CreateTable( + name: "sale", + columns: table => new + { + sale_id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + sale_time = table.Column(type: "timestamp with time zone", nullable: false), + sale_cost = table.Column(type: "integer", nullable: false, defaultValue: -1), + sale_client_id = table.Column(type: "integer", nullable: true), + sale_employee_id = table.Column(type: "integer", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_sale", x => x.sale_id); + table.ForeignKey( + name: "FK_sale_client_sale_client_id", + column: x => x.sale_client_id, + principalTable: "client", + principalColumn: "client_id", + onDelete: ReferentialAction.SetNull); + table.ForeignKey( + name: "FK_sale_employee_sale_employee_id", + column: x => x.sale_employee_id, + principalTable: "employee", + principalColumn: "employee_id", + onDelete: ReferentialAction.SetNull); + }); + + migrationBuilder.CreateTable( + name: "model", + columns: table => new + { + model_id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + model_name = table.Column(type: "character varying(50)", maxLength: 50, nullable: false), + model_price = table.Column(type: "integer", nullable: false, defaultValue: -1), + model_make_id = table.Column(type: "integer", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_model", x => x.model_id); + table.ForeignKey( + name: "FK_model_make_model_make_id", + column: x => x.model_make_id, + principalTable: "make", + principalColumn: "make_id", + onDelete: ReferentialAction.Restrict); + }); + + migrationBuilder.CreateTable( + name: "service", + columns: table => new + { + service_id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + service_name = table.Column(type: "character varying(50)", maxLength: 50, nullable: false), + service_cost = table.Column(type: "integer", nullable: false, defaultValue: -1), + SaleId = table.Column(type: "integer", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_service", x => x.service_id); + table.ForeignKey( + name: "FK_service_sale_SaleId", + column: x => x.SaleId, + principalTable: "sale", + principalColumn: "sale_id"); + }); + + migrationBuilder.CreateTable( + name: "car", + columns: table => new + { + car_id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + car_color = table.Column(type: "character varying(50)", maxLength: 50, nullable: false, defaultValue: "empty_string"), + car_releasedate = table.Column(type: "timestamp with time zone", nullable: false), + car_model_id = table.Column(type: "integer", nullable: false), + SaleId = table.Column(type: "integer", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_car", x => x.car_id); + table.ForeignKey( + name: "FK_car_model_car_model_id", + column: x => x.car_model_id, + principalTable: "model", + principalColumn: "model_id", + onDelete: ReferentialAction.Restrict); + table.ForeignKey( + name: "FK_car_sale_SaleId", + column: x => x.SaleId, + principalTable: "sale", + principalColumn: "sale_id"); + }); + + migrationBuilder.CreateTable( + name: "sale_service", + columns: table => new + { + sale_id = table.Column(type: "integer", nullable: false), + service_id = table.Column(type: "integer", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_sale_service", x => new { x.sale_id, x.service_id }); + table.ForeignKey( + name: "FK_sale_service_sale_sale_id", + column: x => x.sale_id, + principalTable: "sale", + principalColumn: "sale_id", + onDelete: ReferentialAction.Restrict); + table.ForeignKey( + name: "FK_sale_service_service_service_id", + column: x => x.service_id, + principalTable: "service", + principalColumn: "service_id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "sale_car", + columns: table => new + { + sale_id = table.Column(type: "integer", nullable: false), + car_id = table.Column(type: "integer", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_sale_car", x => new { x.sale_id, x.car_id }); + table.ForeignKey( + name: "FK_sale_car_car_car_id", + column: x => x.car_id, + principalTable: "car", + principalColumn: "car_id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_sale_car_sale_sale_id", + column: x => x.sale_id, + principalTable: "sale", + principalColumn: "sale_id", + onDelete: ReferentialAction.Restrict); + }); + + migrationBuilder.CreateIndex( + name: "IX_car_car_model_id", + table: "car", + column: "car_model_id"); + + migrationBuilder.CreateIndex( + name: "IX_car_SaleId", + table: "car", + column: "SaleId"); + + migrationBuilder.CreateIndex( + name: "IX_client_client_email", + table: "client", + column: "client_email", + unique: true); + + migrationBuilder.CreateIndex( + name: "IX_employee_employee_email", + table: "employee", + column: "employee_email", + unique: true); + + migrationBuilder.CreateIndex( + name: "IX_make_make_name", + table: "make", + column: "make_name", + unique: true); + + migrationBuilder.CreateIndex( + name: "IX_model_model_make_id", + table: "model", + column: "model_make_id"); + + migrationBuilder.CreateIndex( + name: "IX_model_model_name", + table: "model", + column: "model_name", + unique: true); + + migrationBuilder.CreateIndex( + name: "IX_sale_sale_client_id", + table: "sale", + column: "sale_client_id"); + + migrationBuilder.CreateIndex( + name: "IX_sale_sale_employee_id", + table: "sale", + column: "sale_employee_id"); + + migrationBuilder.CreateIndex( + name: "IX_sale_car_car_id", + table: "sale_car", + column: "car_id"); + + migrationBuilder.CreateIndex( + name: "IX_sale_service_service_id", + table: "sale_service", + column: "service_id"); + + migrationBuilder.CreateIndex( + name: "IX_service_SaleId", + table: "service", + column: "SaleId"); + + migrationBuilder.CreateIndex( + name: "IX_service_service_name", + table: "service", + column: "service_name", + unique: true); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "sale_car"); + + migrationBuilder.DropTable( + name: "sale_service"); + + migrationBuilder.DropTable( + name: "car"); + + migrationBuilder.DropTable( + name: "service"); + + migrationBuilder.DropTable( + name: "model"); + + migrationBuilder.DropTable( + name: "sale"); + + migrationBuilder.DropTable( + name: "make"); + + migrationBuilder.DropTable( + name: "client"); + + migrationBuilder.DropTable( + name: "employee"); + } + } +} diff --git a/CarShowroom/CarShowroomDatabaseStorage/Migrations/CarShowroomDatabaseModelSnapshot.cs b/CarShowroom/CarShowroomDatabaseStorage/Migrations/CarShowroomDatabaseModelSnapshot.cs new file mode 100644 index 0000000..3d8a044 --- /dev/null +++ b/CarShowroom/CarShowroomDatabaseStorage/Migrations/CarShowroomDatabaseModelSnapshot.cs @@ -0,0 +1,433 @@ +// +using System; +using CarShowroomDatabaseStorage; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +#nullable disable + +namespace CarShowroomDatabaseStorage.Migrations +{ + [DbContext(typeof(CarShowroomDatabase))] + partial class CarShowroomDatabaseModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.18") + .HasAnnotation("Relational:MaxIdentifierLength", 63); + + NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); + + modelBuilder.Entity("CarShowroomDatabaseStorage.Entities.Car", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer") + .HasColumnName("car_id"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Color") + .IsRequired() + .ValueGeneratedOnAdd() + .HasMaxLength(50) + .HasColumnType("character varying(50)") + .HasDefaultValue("empty_string") + .HasColumnName("car_color"); + + b.Property("ModelId") + .HasColumnType("integer") + .HasColumnName("car_model_id"); + + b.Property("ReleaseDate") + .HasColumnType("timestamp with time zone") + .HasColumnName("car_releasedate"); + + b.Property("SaleId") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("ModelId"); + + b.HasIndex("SaleId"); + + b.ToTable("car"); + }); + + modelBuilder.Entity("CarShowroomDatabaseStorage.Entities.Client", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer") + .HasColumnName("client_id"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Email") + .IsRequired() + .HasMaxLength(40) + .HasColumnType("character varying(40)") + .HasColumnName("client_email"); + + b.Property("Name") + .IsRequired() + .ValueGeneratedOnAdd() + .HasMaxLength(50) + .HasColumnType("character varying(50)") + .HasDefaultValue("empty_string") + .HasColumnName("client_name"); + + b.Property("Password") + .IsRequired() + .ValueGeneratedOnAdd() + .HasMaxLength(32) + .HasColumnType("character varying(32)") + .HasDefaultValue("empty_string") + .HasColumnName("client_password"); + + b.HasKey("Id"); + + b.HasIndex("Email") + .IsUnique(); + + b.ToTable("client"); + }); + + modelBuilder.Entity("CarShowroomDatabaseStorage.Entities.Employee", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer") + .HasColumnName("employee_id"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Email") + .IsRequired() + .HasMaxLength(40) + .HasColumnType("character varying(40)") + .HasColumnName("employee_email"); + + b.Property("Name") + .IsRequired() + .ValueGeneratedOnAdd() + .HasMaxLength(50) + .HasColumnType("character varying(50)") + .HasDefaultValue("empty_string") + .HasColumnName("employee_name"); + + b.Property("Password") + .IsRequired() + .ValueGeneratedOnAdd() + .HasMaxLength(32) + .HasColumnType("character varying(32)") + .HasDefaultValue("empty_string") + .HasColumnName("employee_password"); + + b.HasKey("Id"); + + b.HasIndex("Email") + .IsUnique(); + + b.ToTable("employee"); + }); + + modelBuilder.Entity("CarShowroomDatabaseStorage.Entities.Make", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer") + .HasColumnName("make_id"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Name") + .IsRequired() + .HasMaxLength(50) + .HasColumnType("character varying(50)") + .HasColumnName("make_name"); + + b.HasKey("Id"); + + b.HasIndex("Name") + .IsUnique(); + + b.ToTable("make"); + }); + + modelBuilder.Entity("CarShowroomDatabaseStorage.Entities.Model", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer") + .HasColumnName("model_id"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("MakeId") + .HasColumnType("integer") + .HasColumnName("model_make_id"); + + b.Property("Name") + .IsRequired() + .HasMaxLength(50) + .HasColumnType("character varying(50)") + .HasColumnName("model_name"); + + b.Property("Price") + .ValueGeneratedOnAdd() + .HasColumnType("integer") + .HasDefaultValue(-1) + .HasColumnName("model_price"); + + b.HasKey("Id"); + + b.HasIndex("MakeId"); + + b.HasIndex("Name") + .IsUnique(); + + b.ToTable("model"); + }); + + modelBuilder.Entity("CarShowroomDatabaseStorage.Entities.Sale", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer") + .HasColumnName("sale_id"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("ClientId") + .HasColumnType("integer") + .HasColumnName("sale_client_id"); + + b.Property("Cost") + .ValueGeneratedOnAdd() + .HasColumnType("integer") + .HasDefaultValue(-1) + .HasColumnName("sale_cost"); + + b.Property("EmployeeId") + .HasColumnType("integer") + .HasColumnName("sale_employee_id"); + + b.Property("SaleTime") + .HasColumnType("timestamp with time zone") + .HasColumnName("sale_time"); + + b.HasKey("Id"); + + b.HasIndex("ClientId"); + + b.HasIndex("EmployeeId"); + + b.ToTable("sale"); + }); + + modelBuilder.Entity("CarShowroomDatabaseStorage.Entities.SaleCar", b => + { + b.Property("SaleId") + .HasColumnType("integer") + .HasColumnName("sale_id"); + + b.Property("CarId") + .HasColumnType("integer") + .HasColumnName("car_id"); + + b.HasKey("SaleId", "CarId"); + + b.HasIndex("CarId"); + + b.ToTable("sale_car"); + }); + + modelBuilder.Entity("CarShowroomDatabaseStorage.Entities.SaleService", b => + { + b.Property("SaleId") + .HasColumnType("integer") + .HasColumnName("sale_id"); + + b.Property("ServiceId") + .HasColumnType("integer") + .HasColumnName("service_id"); + + b.HasKey("SaleId", "ServiceId"); + + b.HasIndex("ServiceId"); + + b.ToTable("sale_service"); + }); + + modelBuilder.Entity("CarShowroomDatabaseStorage.Entities.Service", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer") + .HasColumnName("service_id"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Cost") + .ValueGeneratedOnAdd() + .HasColumnType("integer") + .HasDefaultValue(-1) + .HasColumnName("service_cost"); + + b.Property("Name") + .IsRequired() + .HasMaxLength(50) + .HasColumnType("character varying(50)") + .HasColumnName("service_name"); + + b.Property("SaleId") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("Name") + .IsUnique(); + + b.HasIndex("SaleId"); + + b.ToTable("service"); + }); + + modelBuilder.Entity("CarShowroomDatabaseStorage.Entities.Car", b => + { + b.HasOne("CarShowroomDatabaseStorage.Entities.Model", "Model") + .WithMany("Cars") + .HasForeignKey("ModelId") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.HasOne("CarShowroomDatabaseStorage.Entities.Sale", null) + .WithMany("Cars") + .HasForeignKey("SaleId"); + + b.Navigation("Model"); + }); + + modelBuilder.Entity("CarShowroomDatabaseStorage.Entities.Model", b => + { + b.HasOne("CarShowroomDatabaseStorage.Entities.Make", "Make") + .WithMany("Models") + .HasForeignKey("MakeId") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.Navigation("Make"); + }); + + modelBuilder.Entity("CarShowroomDatabaseStorage.Entities.Sale", b => + { + b.HasOne("CarShowroomDatabaseStorage.Entities.Client", "Client") + .WithMany("Sales") + .HasForeignKey("ClientId") + .OnDelete(DeleteBehavior.SetNull); + + b.HasOne("CarShowroomDatabaseStorage.Entities.Employee", "Employee") + .WithMany("Sales") + .HasForeignKey("EmployeeId") + .OnDelete(DeleteBehavior.SetNull); + + b.Navigation("Client"); + + b.Navigation("Employee"); + }); + + modelBuilder.Entity("CarShowroomDatabaseStorage.Entities.SaleCar", b => + { + b.HasOne("CarShowroomDatabaseStorage.Entities.Car", "Car") + .WithMany("SaleCars") + .HasForeignKey("CarId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("CarShowroomDatabaseStorage.Entities.Sale", "Sale") + .WithMany("SaleCars") + .HasForeignKey("SaleId") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.Navigation("Car"); + + b.Navigation("Sale"); + }); + + modelBuilder.Entity("CarShowroomDatabaseStorage.Entities.SaleService", b => + { + b.HasOne("CarShowroomDatabaseStorage.Entities.Sale", "Sale") + .WithMany("SaleServices") + .HasForeignKey("SaleId") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.HasOne("CarShowroomDatabaseStorage.Entities.Service", "Service") + .WithMany("SaleServices") + .HasForeignKey("ServiceId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Sale"); + + b.Navigation("Service"); + }); + + modelBuilder.Entity("CarShowroomDatabaseStorage.Entities.Service", b => + { + b.HasOne("CarShowroomDatabaseStorage.Entities.Sale", null) + .WithMany("Services") + .HasForeignKey("SaleId"); + }); + + modelBuilder.Entity("CarShowroomDatabaseStorage.Entities.Car", b => + { + b.Navigation("SaleCars"); + }); + + modelBuilder.Entity("CarShowroomDatabaseStorage.Entities.Client", b => + { + b.Navigation("Sales"); + }); + + modelBuilder.Entity("CarShowroomDatabaseStorage.Entities.Employee", b => + { + b.Navigation("Sales"); + }); + + modelBuilder.Entity("CarShowroomDatabaseStorage.Entities.Make", b => + { + b.Navigation("Models"); + }); + + modelBuilder.Entity("CarShowroomDatabaseStorage.Entities.Model", b => + { + b.Navigation("Cars"); + }); + + modelBuilder.Entity("CarShowroomDatabaseStorage.Entities.Sale", b => + { + b.Navigation("Cars"); + + b.Navigation("SaleCars"); + + b.Navigation("SaleServices"); + + b.Navigation("Services"); + }); + + modelBuilder.Entity("CarShowroomDatabaseStorage.Entities.Service", b => + { + b.Navigation("SaleServices"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/ERD/car-showroom_2.vpp b/ERD/car-showroom_2.vpp index 105f441..c7362fc 100644 Binary files a/ERD/car-showroom_2.vpp and b/ERD/car-showroom_2.vpp differ diff --git a/ERD/car-showroom_2.vpp.bak_004d b/ERD/car-showroom_2.vpp.bak_004d new file mode 100644 index 0000000..d5289dd Binary files /dev/null and b/ERD/car-showroom_2.vpp.bak_004d differ diff --git a/ERD/car-showroom_2.vpp.bak_005d b/ERD/car-showroom_2.vpp.bak_005d new file mode 100644 index 0000000..6e2355b Binary files /dev/null and b/ERD/car-showroom_2.vpp.bak_005d differ diff --git a/ERD/car-showroom_2.vpp.bak_006d b/ERD/car-showroom_2.vpp.bak_006d new file mode 100644 index 0000000..7679232 Binary files /dev/null and b/ERD/car-showroom_2.vpp.bak_006d differ