Merge branch 'Lab3_Base' into Lab3_Hard
This commit is contained in:
commit
2ae833bcd5
@ -79,7 +79,7 @@ namespace ConfectioneryDatabaseImplement.Models
|
|||||||
.ContainsKey(rec.ComponentId)));
|
.ContainsKey(rec.ComponentId)));
|
||||||
context.SaveChanges();
|
context.SaveChanges();
|
||||||
// обновили количество у существующих записей
|
// обновили количество у существующих записей
|
||||||
foreach (var updateComponent in pastryComponents)
|
foreach (var updateComponent in pastryComponents.Where(x => model.PastryComponents.ContainsKey(x.ComponentId)))
|
||||||
{
|
{
|
||||||
updateComponent.Count = model.PastryComponents[updateComponent.ComponentId].Item2;
|
updateComponent.Count = model.PastryComponents[updateComponent.ComponentId].Item2;
|
||||||
model.PastryComponents.Remove(updateComponent.ComponentId);
|
model.PastryComponents.Remove(updateComponent.ComponentId);
|
||||||
|
@ -47,6 +47,8 @@ namespace ConfectioneryDatabaseImplement.Implements
|
|||||||
}
|
}
|
||||||
using var context = new ConfectioneryDatabase();
|
using var context = new ConfectioneryDatabase();
|
||||||
return context.Pastries
|
return context.Pastries
|
||||||
|
.Include(x => x.Components)
|
||||||
|
.ThenInclude(x => x.Component)
|
||||||
.Select(x => x.GetViewModel)
|
.Select(x => x.GetViewModel)
|
||||||
.Where(x => x.PastryName.Contains(model.PastryName))
|
.Where(x => x.PastryName.Contains(model.PastryName))
|
||||||
.ToList();
|
.ToList();
|
||||||
@ -56,6 +58,8 @@ namespace ConfectioneryDatabaseImplement.Implements
|
|||||||
{
|
{
|
||||||
using var context = new ConfectioneryDatabase();
|
using var context = new ConfectioneryDatabase();
|
||||||
return context.Pastries
|
return context.Pastries
|
||||||
|
.Include(x => x.Components)
|
||||||
|
.ThenInclude(x => x.Component)
|
||||||
.Select(x => x.GetViewModel)
|
.Select(x => x.GetViewModel)
|
||||||
.ToList();
|
.ToList();
|
||||||
}
|
}
|
||||||
@ -68,26 +72,33 @@ namespace ConfectioneryDatabaseImplement.Implements
|
|||||||
{
|
{
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
newPastry = context.Pastries.Add(newPastry).Entity;
|
context.Pastries.Add(newPastry);
|
||||||
context.SaveChanges();
|
context.SaveChanges();
|
||||||
model.Id = newPastry.Id;
|
|
||||||
newPastry.UpdateComponents(context, model);
|
|
||||||
return newPastry.GetViewModel;
|
return newPastry.GetViewModel;
|
||||||
}
|
}
|
||||||
|
|
||||||
public PastryViewModel? Update(PastryBindingModel model)
|
public PastryViewModel? Update(PastryBindingModel model)
|
||||||
{
|
{
|
||||||
using var context = new ConfectioneryDatabase();
|
using var context = new ConfectioneryDatabase();
|
||||||
var pastry = context.Pastries.FirstOrDefault(x => x.Id == model.Id);
|
using var transaction = context.Database.BeginTransaction();
|
||||||
if (pastry == null)
|
try
|
||||||
{
|
{
|
||||||
return null;
|
var pastry = context.Pastries.FirstOrDefault(x => x.Id == model.Id);
|
||||||
|
if (pastry == null)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
pastry.Update(model);
|
||||||
|
context.SaveChanges();
|
||||||
|
pastry.UpdateComponents(context, model);
|
||||||
|
transaction.Commit();
|
||||||
|
return pastry.GetViewModel;
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
transaction.Rollback();
|
||||||
|
throw;
|
||||||
}
|
}
|
||||||
pastry.Update(model);
|
|
||||||
pastry.UpdateComponents(context, model);
|
|
||||||
context.Update(pastry);
|
|
||||||
context.SaveChanges();
|
|
||||||
return pastry.GetViewModel;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user