From fe3e9171e8164d05a1a56dc8ca475a095ccdd021 Mon Sep 17 00:00:00 2001 From: Oleg Shabunov Date: Wed, 17 Apr 2024 01:23:18 +0400 Subject: [PATCH] DatabaseImplement/Models --- .../AutoWorkshopDatabase.cs | 6 +- AutoWorkshopDatabaseImplement/Models/Shop.cs | 123 ++++++++++++++++++ .../Models/ShopRepair.cs | 22 ++++ 3 files changed, 150 insertions(+), 1 deletion(-) create mode 100644 AutoWorkshopDatabaseImplement/Models/Shop.cs create mode 100644 AutoWorkshopDatabaseImplement/Models/ShopRepair.cs diff --git a/AutoWorkshopDatabaseImplement/AutoWorkshopDatabase.cs b/AutoWorkshopDatabaseImplement/AutoWorkshopDatabase.cs index ced4c8f..c14ca57 100644 --- a/AutoWorkshopDatabaseImplement/AutoWorkshopDatabase.cs +++ b/AutoWorkshopDatabaseImplement/AutoWorkshopDatabase.cs @@ -9,7 +9,7 @@ namespace AutoWorkshopDatabaseImplement { if (OptionsBuilder.IsConfigured == false) { - OptionsBuilder.UseNpgsql(@"Host=localhost;Database=AutoWorkshop;Username=postgres;Password=admin"); + OptionsBuilder.UseNpgsql(@"Host=localhost;Database=AutoWorkshop_Hard;Username=postgres;Password=admin"); } base.OnConfiguring(OptionsBuilder); @@ -25,5 +25,9 @@ namespace AutoWorkshopDatabaseImplement public virtual DbSet RepairComponents { set; get; } public virtual DbSet Orders { set; get; } + + public virtual DbSet Shops { get; set; } + + public virtual DbSet ShopRepairs { get; set; } } } diff --git a/AutoWorkshopDatabaseImplement/Models/Shop.cs b/AutoWorkshopDatabaseImplement/Models/Shop.cs new file mode 100644 index 0000000..3ad4006 --- /dev/null +++ b/AutoWorkshopDatabaseImplement/Models/Shop.cs @@ -0,0 +1,123 @@ +using AutoWorkshopContracts.BindingModels; +using AutoWorkshopContracts.ViewModels; +using AutoWorkshopDataModels.Models; +using System.ComponentModel.DataAnnotations.Schema; + +namespace AutoWorkshopDatabaseImplement.Models +{ + public class Shop : IShopModel + { + public int Id { get; set; } + + public string ShopName { get; set; } = string.Empty; + + public string Address { get; set; } = string.Empty; + + public DateTime OpeningDate { get; set; } + + public int RepairsMaxCount { get; set; } + + private Dictionary? _shopRepairs = null; + + public Dictionary ShopRepairs + { + get + { + if (_shopRepairs == null) + { + if (_shopRepairs == null) + { + _shopRepairs = Repairs.ToDictionary(ShopRep => ShopRep.RepairId, ShopRep => (ShopRep.Repair as IRepairModel, ShopRep.Count)); + } + return _shopRepairs; + } + + return _shopRepairs; + } + } + + [ForeignKey("ShopId")] + public List Repairs { get; set; } = new(); + + public static Shop Create(AutoWorkshopDatabase Context, ShopBindingModel Model) + { + return new Shop() + { + Id = Model.Id, + ShopName = Model.ShopName, + Address = Model.Address, + OpeningDate = Model.OpeningDate, + Repairs = Model.ShopRepairs.Select(x => new ShopRepair + { + Repair = Context.Repairs.First(y => y.Id == x.Key), + Count = x.Value.Item2 + }).ToList(), + RepairsMaxCount = Model.RepairsMaxCount + }; + } + + public void Update(ShopBindingModel Model) + { + ShopName = Model.ShopName; + Address = Model.Address; + OpeningDate = Model.OpeningDate; + RepairsMaxCount = Model.RepairsMaxCount; + } + + public ShopViewModel GetViewModel => new() + { + Id = Id, + ShopName = ShopName, + Address = Address, + OpeningDate = OpeningDate, + ShopRepairs = ShopRepairs, + RepairsMaxCount = RepairsMaxCount + }; + + public void UpdateRepairs(AutoWorkshopDatabase Context, ShopBindingModel Model) + { + var ShopRepairs = Context.ShopRepairs + .Where(rec => rec.ShopId == Model.Id) + .ToList(); + + if (ShopRepairs != null && ShopRepairs.Count > 0) + { + Context.ShopRepairs.RemoveRange(ShopRepairs.Where(rec => !Model.ShopRepairs.ContainsKey(rec.RepairId))); + Context.SaveChanges(); + + foreach (var RepairToUpdate in ShopRepairs) + { + RepairToUpdate.Count = Model.ShopRepairs[RepairToUpdate.RepairId].Item2; + Model.ShopRepairs.Remove(RepairToUpdate.RepairId); + } + + Context.SaveChanges(); + } + + var Shop = Context.Shops.First(x => x.Id == Id); + + foreach (var ShopRepair in Model.ShopRepairs) + { + Context.ShopRepairs.Add(new ShopRepair + { + Shop = Shop, + Repair = Context.Repairs.First(x => x.Id == ShopRepair.Key), + Count = ShopRepair.Value.Item2 + }); + + Context.SaveChanges(); + } + + _shopRepairs = null; + } + + public void RepairsDictionatyUpdate(AutoWorkshopDatabase Context) + { + UpdateRepairs(Context, new ShopBindingModel + { + Id = Id, + ShopRepairs = ShopRepairs + }); + } + } +} diff --git a/AutoWorkshopDatabaseImplement/Models/ShopRepair.cs b/AutoWorkshopDatabaseImplement/Models/ShopRepair.cs new file mode 100644 index 0000000..2810e13 --- /dev/null +++ b/AutoWorkshopDatabaseImplement/Models/ShopRepair.cs @@ -0,0 +1,22 @@ +using System.ComponentModel.DataAnnotations; + +namespace AutoWorkshopDatabaseImplement.Models +{ + public class ShopRepair + { + public int Id { get; set; } + + [Required] + public int RepairId { get; set; } + + [Required] + public int ShopId { get; set; } + + [Required] + public int Count { get; set; } + + public virtual Shop Shop { get; set; } = new(); + + public virtual Repair Repair { get; set; } = new(); + } +}