доделал

This commit is contained in:
goldfest 2024-05-06 13:12:15 +04:00
parent 8029254363
commit 509b29b85d
4 changed files with 10 additions and 17 deletions

View File

@ -102,7 +102,6 @@ namespace TravelCompany.Forms
Adress = textBoxAdress.Text, Adress = textBoxAdress.Text,
OpeningDate = dateTimeOpen.Value, OpeningDate = dateTimeOpen.Value,
TravelMaxCount = (int)numericUpTravelMaxCount.Value, TravelMaxCount = (int)numericUpTravelMaxCount.Value,
ShopTravels = null
}; };
var operationResult = _id.HasValue ? _logic.Update(model) : _logic.Create(model); var operationResult = _id.HasValue ? _logic.Update(model) : _logic.Create(model);
if (!operationResult) if (!operationResult)

View File

@ -12,7 +12,7 @@ using TravelCompanyDatabaseImplement;
namespace TravelCompanyDatabaseImplement.Migrations namespace TravelCompanyDatabaseImplement.Migrations
{ {
[DbContext(typeof(TravelCompanyDataBase))] [DbContext(typeof(TravelCompanyDataBase))]
[Migration("20240503064033_Initial-Create")] [Migration("20240506091037_Initial-Create")]
partial class InitialCreate partial class InitialCreate
{ {
/// <inheritdoc /> /// <inheritdoc />

View File

@ -72,35 +72,29 @@ namespace TravelCompanyDatabaseImplement.Models
ShopTravels = ShopTravels, ShopTravels = ShopTravels,
TravelMaxCount = TravelMaxCount TravelMaxCount = TravelMaxCount
}; };
public void UpdateTravels(TravelCompanyDataBase context, ShopBindingModel model) public void UpdateTravels(TravelCompanyDataBase context, ShopBindingModel model)
{ {
if (model.ShopTravels == null) var ShopTravels = context.ShopTravels.Where(rec => rec.ShopId == model.Id).ToList();
return; if (ShopTravels != null && ShopTravels.Count > 0)
var shopTravels = context.ShopTravels.Where(rec =>
rec.ShopId == model.Id).ToList();
if (shopTravels != null && shopTravels.Count > 0)
{ {
context.ShopTravels.RemoveRange(shopTravels.Where(rec context.ShopTravels.RemoveRange(ShopTravels.Where(rec => !model.ShopTravels.ContainsKey(rec.TravelId)));
=> !model.ShopTravels.ContainsKey(rec.TravelId)));
context.SaveChanges(); context.SaveChanges();
foreach (var updateTravel in shopTravels) ShopTravels = context.ShopTravels.Where(rec => rec.ShopId == model.Id).ToList();
foreach (var updateTravel in ShopTravels)
{ {
updateTravel.Count = updateTravel.Count = model.ShopTravels[updateTravel.TravelId].Item2;
model.ShopTravels[updateTravel.TravelId].Item2;
model.ShopTravels.Remove(updateTravel.TravelId); model.ShopTravels.Remove(updateTravel.TravelId);
} }
context.SaveChanges(); context.SaveChanges();
} }
var shop = context.Shops.First(x => x.Id == Id); var shop = context.Shops.First(x => x.Id == Id);
foreach (var pc in model.ShopTravels) foreach (var ar in model.ShopTravels)
{ {
context.ShopTravels.Add(new ShopTravels context.ShopTravels.Add(new ShopTravels
{ {
Shop = shop, Shop = shop,
Travel = context.Travels.First(x => x.Id == pc.Key), Travel = context.Travels.First(x => x.Id == ar.Key),
Count = pc.Value.Item2 Count = ar.Value.Item2
}); });
context.SaveChanges(); context.SaveChanges();
} }