lab3 start

This commit is contained in:
parap 2023-04-23 20:30:20 +04:00
parent d1b3d29ab0
commit f1ff090bc1
4 changed files with 276 additions and 0 deletions

View File

@ -0,0 +1,152 @@
using PizzeriaContracts.BindingModels;
using PizzeriaContracts.SearchModels;
using PizzeriaContracts.StoragesContracts;
using PizzeriaContracts.ViewModels;
using PizzeriaDatabaseImplement.models;
using Microsoft.EntityFrameworkCore;
using PizzeriaDataModels;
namespace PizzeriaDatabaseImplement.Implements
{
public class ShopStorage : IShopStorage
{
public List<ShopViewModel> GetFullList()
{
using var context = new PizzeriaDatabase();
return context.Shops
.Include(x => x.Pizzas)
.ThenInclude(x => x.Pizza)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public List<ShopViewModel> GetFilteredList(ShopSearchModel model)
{
if (string.IsNullOrEmpty(model.ShopName))
{
return new();
}
using var context = new PizzeriaDatabase();
return context.Shops
.Include(x => x.Pizzas)
.ThenInclude(x => x.Pizza)
.Where(x => x.ShopName.Contains(model.ShopName))
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public ShopViewModel? GetElement(ShopSearchModel model)
{
if (string.IsNullOrEmpty(model.ShopName) &&
!model.Id.HasValue)
{
return null;
}
using var context = new PizzeriaDatabase();
return context.Shops
.Include(x => x.Pizzas)
.ThenInclude(x => x.Pizza)
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.ShopName) &&
x.ShopName == model.ShopName) ||
(model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
}
public ShopViewModel? Insert(ShopBindingModel model)
{
using var context = new PizzeriaDatabase();
var newShop = Shop.Create(context, model);
if (context.Shops.Any(x => x.ShopName == newShop.ShopName))
{
throw new Exception("Магазин с таким названием уже существует");
}
if (newShop == null)
{
return null;
}
context.Shops.Add(newShop);
context.SaveChanges();
return newShop.GetViewModel;
}
public ShopViewModel? Update(ShopBindingModel model)
{
using var context = new PizzeriaDatabase();
using var transaction = context.Database.BeginTransaction();
try
{
var shop = context.Shops.FirstOrDefault(rec =>
rec.Id == model.Id);
if (shop == null)
{
return null;
}
shop.Update(model);
context.SaveChanges();
shop.UpdatePizzas(context, model);
transaction.Commit();
return shop.GetViewModel;
}
catch
{
transaction.Rollback();
throw;
}
}
public ShopViewModel? Delete(ShopBindingModel model)
{
using var context = new PizzeriaDatabase();
var element = context.Shops
.Include(x => x.Pizzas)
.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Shops.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
public bool TrySellPizza(IPizzaModel pizza, int count)
{
using var context = new PizzeriaDatabase();
using var transaction = context.Database.BeginTransaction();
foreach (var shopPizzas in context.ShopPizzas.Where(x => x.PizzaId == pizza.Id))
{
var min = Math.Min(count, shopPizzas.Count);
shopPizzas.Count -= min;
count -= min;
if (count <= 0)
{
break;
}
}
if (count == 0)
{
context.SaveChanges();
transaction.Commit();
}
else
{
transaction.Rollback();
}
if (count > 0)
{
return false;
}
return true;
}
public bool TryRestoreShops(IPizzaModel pizza, int count)
{
throw new NotImplementedException();
}
}
}

View File

@ -15,7 +15,9 @@ namespace PizzeriaDatabaseImplement
} }
public virtual DbSet<Component> Components { set; get; } public virtual DbSet<Component> Components { set; get; }
public virtual DbSet<Pizza> Pizzas { set; get; } public virtual DbSet<Pizza> Pizzas { set; get; }
public virtual DbSet<Shop> Shops { set; get; }
public virtual DbSet<PizzaComponent> PizzaComponents { set; get; } public virtual DbSet<PizzaComponent> PizzaComponents { set; get; }
public virtual DbSet<ShopPizza> ShopPizzas { set; get; }
public virtual DbSet<Order> Orders { set; get; } public virtual DbSet<Order> Orders { set; get; }
} }
} }

View File

@ -0,0 +1,105 @@
using PizzeriaContracts.BindingModels;
using PizzeriaContracts.ViewModels;
using PizzeriaDataModels;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Diagnostics;
namespace PizzeriaDatabaseImplement.models
{
public class Shop : IShopModel
{
[Required]
public int Id { get; set; }
[Required]
public string ShopName { get; set; } = string.Empty;
[Required]
public string Addres { get; set; } = string.Empty;
[Required]
public DateTime OpenTime { get; set; }
[Required]
public int Capacity { get; set; }
private Dictionary<int, (IPizzaModel, int)>? _shopPizzas = null;
[NotMapped]
public Dictionary<int, (IPizzaModel, int)> ShopPizzas
{
get
{
if (_shopPizzas == null)
{
_shopPizzas = Pizzas.ToDictionary(x => x.PizzaId, x => (x.Pizza as IPizzaModel, x.Count));
}
return _shopPizzas;
}
}
[ForeignKey("ShopId")]
public virtual List<ShopPizza> Pizzas { get; set; } = new();
public static Shop Create(PizzeriaDatabase context, ShopBindingModel model)
{
return new Shop()
{
Id = model.Id,
ShopName = model.ShopName,
Addres = model.Addres,
OpenTime = model.OpenTime,
Capacity = model.Capacity,
Pizzas = model.ShopPizzas.Select(x => new
ShopPizza
{
Pizza = context.Pizzas.First(y => y.Id == x.Key),
Count = x.Value.Item2
}).ToList()
};
}
public void Update(ShopBindingModel model)
{
if (model == null)
{
return;
}
ShopName = model.ShopName;
Addres = model.Addres;
OpenTime = model.OpenTime;
Capacity = model.Capacity;
}
public ShopViewModel GetViewModel => new()
{
Id = Id,
ShopName = ShopName,
Addres = Addres,
OpenTime = OpenTime,
Capacity = Capacity,
ShopPizzas = ShopPizzas
};
public void UpdatePizzas(PizzeriaDatabase context, ShopBindingModel model)
{
var shopPizzas = context.ShopPizzas.Where(rec => rec.ShopId == model.Id).ToList();
if (shopPizzas != null && shopPizzas.Count > 0)
{ // удалили те, которых нет в модели
context.ShopPizzas.RemoveRange(shopPizzas.Where(rec
=> !model.ShopPizzas.ContainsKey(rec.PizzaId)));
context.SaveChanges();
// обновили количество у существующих записей
foreach (var updatePizza in shopPizzas)
{
updatePizza.Count =
model.ShopPizzas[updatePizza.PizzaId].Item2;
model.ShopPizzas.Remove(updatePizza.PizzaId);
}
context.SaveChanges();
}
var shop = context.Shops.First(x => x.Id == Id);
foreach (var pc in model.ShopPizzas)
{
context.ShopPizzas.Add(new ShopPizza
{
Shop = shop,
Pizza = context.Pizzas.First(x => x.Id == pc.Key),
Count = pc.Value.Item2
});
context.SaveChanges();
}
_shopPizzas = null;
}
}
}

View File

@ -0,0 +1,17 @@
using System.ComponentModel.DataAnnotations;
namespace PizzeriaDatabaseImplement.models
{
public class ShopPizza
{
public int Id { get; set; }
[Required]
public int PizzaId { get; set; }
[Required]
public int ShopId { get; set; }
[Required]
public int Count { get; set; }
public virtual Shop Shop { get; set; } = new();
public virtual Pizza Pizza { get; set; } = new();
}
}