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

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

View File

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