реализованны все модели БД для сущности Работник

This commit is contained in:
Николай 2023-04-01 16:27:37 +04:00
parent a7cebe62b7
commit e948a1c60f
2 changed files with 131 additions and 11 deletions

View File

@ -33,7 +33,7 @@ namespace HardwareShopDatabaseImplement
public virtual DbSet<Order> Orders { set; get; }
public virtual DbSet<Purchase> Purchase { set; get; }
public virtual DbSet<Purchase> Purchases { set; get; }
public virtual DbSet<PurchaseBuild> PurchasesBuilds { set; get; }

View File

@ -1,8 +1,9 @@
using HardwareShopDataModels.Enums;
using HardwareShopContracts.BindingModels;
using HardwareShopContracts.ViewModels;
using HardwareShopDataModels.Enums;
using HardwareShopDataModels.Models;
using HardwareShopDataModels.Models;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations.Schema;
using System.Diagnostics;
namespace HardwareShopDatabaseImplement.Models
{
@ -20,18 +21,137 @@ namespace HardwareShopDatabaseImplement.Models
public virtual User User { get; set; }
[NotMapped]
public Dictionary<int, (IBuildModel, int)>? PurchaseBuilds { get; set; }
[NotMapped]
public Dictionary<int, (IGoodModel, int)> PurchaseGoods { get; set; } = new();
[ForeignKey("PurchaseId")]
public virtual List<PurchaseBuild>? Builds { get; set; }
public Dictionary<int, (IBuildModel, int)>? _purchaseBuilds = null;
[NotMapped]
public Dictionary<int, (IBuildModel, int)>? PurchaseBuilds
{
get
{
if (_purchaseBuilds == null)
{
_purchaseBuilds = Builds.ToDictionary(recPB => recPB.BuildID, recPB => (recPB.Build as IBuildModel, recPB.Count));
}
return _purchaseBuilds;
}
}
[ForeignKey("PurchaseId")]
public virtual List<PurchaseGood> Goods { get; set; } = new();
public Dictionary<int, (IGoodModel, int)>? _purchaseGoods = null;
[NotMapped]
public Dictionary<int, (IGoodModel, int)>? PurchaseGoods
{
get
{
if (_purchaseGoods == null)
{
_purchaseGoods = Goods.ToDictionary(recPG => recPG.GoodID, recPG => (recPG.Good as IGoodModel, recPG.Count));
}
return _purchaseGoods;
}
}
public static Purchase Create(HardwareShopDatabase context, PurchaseBindingModel model)
{
return new Purchase()
{
Id = model.Id,
Sum = model.Sum,
PurchaseStatus = model.PurchaseStatus,
DatePurchase = model.DatePurchase,
UserID = model.UserID,
Builds = model.PurchaseBuilds.Select(x => new PurchaseBuild
{
Build = context.Builds.First(y => y.Id == x.Key),
Count = x.Value.Item2
}).ToList(),
Goods = model.PurchaseGoods.Select(x => new PurchaseGood
{
Good = context.Goods.First(y => y.Id == x.Key),
Count = x.Value.Item2
}).ToList()
};
}
public void Update(PurchaseBindingModel model)
{
PurchaseStatus = model.PurchaseStatus;
DatePurchase = model.DatePurchase;
}
public PurchaseViewModel GetViewModel => new()
{
Id = Id,
Sum = Sum,
PurchaseStatus = PurchaseStatus,
DatePurchase = DatePurchase,
UserID = UserID,
UserEmail = User.Email,
PurchaseBuilds = PurchaseBuilds,
PurchaseGoods = PurchaseGoods
};
public void UpdateBuilds(HardwareShopDatabase context, PurchaseBindingModel model)
{
var purchaseBuilds = context.PurchasesBuilds.Where(rec => rec.PurchaseID == model.Id).ToList();
if (purchaseBuilds != null && purchaseBuilds.Count > 0)
{
context.PurchasesBuilds.RemoveRange(purchaseBuilds.Where(rec => !model.PurchaseBuilds.ContainsKey(rec.BuildID)));
context.SaveChanges();
foreach (var updateBuild in purchaseBuilds)
{
updateBuild.Count = model.PurchaseBuilds[updateBuild.BuildID].Item2;
model.PurchaseBuilds.Remove(updateBuild.BuildID);
}
context.SaveChanges();
}
var purchase = context.Purchases.First(x => x.Id == Id);
foreach (var dc in model.PurchaseBuilds)
{
context.PurchasesBuilds.Add(new PurchaseBuild
{
Purchase = purchase,
Build = context.Builds.First(x => x.Id == dc.Key),
Count = dc.Value.Item2
});
context.SaveChanges();
}
_purchaseBuilds = null;
}
public void UpdateGoods(HardwareShopDatabase context, PurchaseBindingModel model)
{
var purchaseGoods = context.PurchasesGoods.Where(rec => rec.PurchaseID == model.Id).ToList();
if (purchaseGoods != null && purchaseGoods.Count > 0)
{ // удалили те в бд, которых нет в модели
context.PurchasesGoods.RemoveRange(purchaseGoods.Where(rec => !model.PurchaseGoods.ContainsKey(rec.GoodID)));
context.SaveChanges();
// обновили количество у существующих записей
foreach (var updateGood in purchaseGoods)
{
updateGood.Count = model.PurchaseGoods[updateGood.GoodID].Item2;
model.PurchaseGoods.Remove(updateGood.GoodID);
}
context.SaveChanges();
}
var purchase = context.Purchases.First(x => x.Id == Id);
//добавляем в бд блюда которые есть в моделе, но ещё нет в бд
foreach (var dc in model.PurchaseGoods)
{
context.PurchasesGoods.Add(new PurchaseGood
{
Purchase = purchase,
Good = context.Goods.First(x => x.Id == dc.Key),
Count = dc.Value.Item2
});
context.SaveChanges();
}
_purchaseGoods = null;
}
}
}