ЧУДО СЛУЧИЛОСЬ!!!!!!!!!!!! МИЛЛИОН ТРАССИРОВКИ!!!!!!!!!

This commit is contained in:
Кашин Максим 2023-04-23 22:41:14 +04:00
parent edab2c3099
commit 181366a9e3
2 changed files with 26 additions and 35 deletions

View File

@ -72,26 +72,16 @@ namespace PrecastConcretePlantDatabaseImplement.Implements
public ShopViewModel? Update(ShopBindingModel model)
{
using var context = new PrecastConcretePlantDataBase();
using var transaction = context.Database.BeginTransaction();
try
{
var shop = context.Shops.FirstOrDefault(rec => rec.Id == model.Id);
var shop = context.Shops.FirstOrDefault(x => x.Id == model.Id);
if (shop == null)
{
return null;
}
shop.Update(model);
context.SaveChanges();
shop.UpdateReinforcedies(context, model);
transaction.Commit();
context.SaveChanges();
return shop.GetViewModel;
}
catch
{
transaction.Rollback();
throw;
}
}
public ShopViewModel? Delete(ShopBindingModel model)
{

View File

@ -58,10 +58,13 @@ namespace PrecastConcretePlantDatabaseImplement.Models
public void Update(ShopBindingModel model)
{
if (model == null)
{
return;
}
Name = model.Name;
Address = model.Address;
DateOpening = model.DateOpening;
ReinforcedMaxCount = model.ReinforcedMaxCount;
}
public ShopViewModel GetViewModel => new()
@ -76,30 +79,28 @@ namespace PrecastConcretePlantDatabaseImplement.Models
public void UpdateReinforcedies(PrecastConcretePlantDataBase context, ShopBindingModel model)
{
var shopReinforcedies = context.ShopReinforcedies.Where(rec => rec.ShopId == model.Id).ToList();
var shopReinforcedies = context.ShopReinforcedies
.Where(rec => rec.ShopId == model.Id)
.ToList();
if (shopReinforcedies != null && shopReinforcedies.Count > 0)
{
context.ShopReinforcedies.RemoveRange(shopReinforcedies.Where(rec => !model.ShopReinforcedies.ContainsKey(rec.ReinforcedId)));
context.SaveChanges();
foreach (var updateReinforced in shopReinforcedies)
context.ShopReinforcedies
.RemoveRange(shopReinforcedies
.Where(rec => !model.ShopReinforcedies
.ContainsKey(rec.ReinforcedId)));
foreach (var updateReinforced in shopReinforcedies.Where(x => model.ShopReinforcedies.ContainsKey(x.ReinforcedId)))
{
updateReinforced.Count = model.ShopReinforcedies[updateReinforced.ReinforcedId].Item2;
model.ShopReinforcedies.Remove(updateReinforced.ReinforcedId);
}
context.SaveChanges();
}
var shop = context.Shops.First(x => x.Id == Id);
foreach (var st in model.ShopReinforcedies)
var shop = context.Shops.First(x => x.Id == model.Id);
shop.Reinforcedies.AddRange(model.ShopReinforcedies.Select(x => new ShopReinforced
{
context.ShopReinforcedies.Add(new ShopReinforced
{
Shop = shop,
Reinforced = context.Reinforcedies.First(x => x.Id == st.Key),
Count = st.Value.Item2
});
Reinforced = context.Reinforcedies.First(y => y.Id == x.Key),
Count = x.Value.Item2,
}).Except(shopReinforcedies ?? new()));
context.SaveChanges();
}
_shopReinforcedies = null;
}
}