DatabaseImplement/Models

This commit is contained in:
ShabOl 2024-04-17 01:23:18 +04:00
parent abf3aeca23
commit fe3e9171e8
3 changed files with 150 additions and 1 deletions

View File

@ -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<RepairComponent> RepairComponents { set; get; }
public virtual DbSet<Order> Orders { set; get; }
public virtual DbSet<Shop> Shops { get; set; }
public virtual DbSet<ShopRepair> ShopRepairs { get; set; }
}
}

View File

@ -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<int, (IRepairModel, int)>? _shopRepairs = null;
public Dictionary<int, (IRepairModel, int)> 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<ShopRepair> 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
});
}
}
}

View File

@ -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();
}
}