PIbd-23. Panina A.D. Lab Work 04 hard #15
@ -0,0 +1,186 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using JewelryStoreContracts.StoragesModels;
|
||||
using JewelryStoreContracts.ViewModels;
|
||||
using JewelryStoreContracts.BindingModels;
|
||||
using JewelryStoreContracts.SearchModels;
|
||||
using JewelryStoreDatabaseImplement.Models;
|
||||
|
||||
namespace JewelryStoreDatabaseImplement.Implements
|
||||
{
|
||||
public class StoreStorage : IStoreStorage
|
||||
{
|
||||
public List<StoreViewModel> GetFullList()
|
||||
{
|
||||
using var context = new JewelryStoreDatabase();
|
||||
return context.Shops.Include(x => x.Jewels).ThenInclude(x => x.Jewel).ToList().
|
||||
Select(x => x.GetViewModel).ToList();
|
||||
}
|
||||
public List<StoreViewModel> GetFilteredList(StoreSearchModel model)
|
||||
{
|
||||
if (string.IsNullOrEmpty(model.StoreName))
|
||||
{
|
||||
return new();
|
||||
}
|
||||
using var context = new JewelryStoreDatabase();
|
||||
return context.Shops.Include(x => x.Jewels).ThenInclude(x => x.Jewel).Where(x => x.ShopName.Contains(model.StoreName)).
|
||||
ToList().Select(x => x.GetViewModel).ToList();
|
||||
}
|
||||
public StoreViewModel? GetElement(StoreSearchModel model)
|
||||
{
|
||||
if (string.IsNullOrEmpty(model.StoreName) && !model.Id.HasValue)
|
||||
{
|
||||
return new();
|
||||
}
|
||||
using var context = new JewelryStoreDatabase();
|
||||
return context.Shops.Include(x => x.Jewels).ThenInclude(x => x.Jewel)
|
||||
.FirstOrDefault(x =>
|
||||
(!string.IsNullOrEmpty(model.StoreName) && x.ShopName == model.StoreName) ||
|
||||
(model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
|
||||
}
|
||||
public StoreViewModel? Insert(StoreBindingModel model)
|
||||
{
|
||||
using var context = new JewelryStoreDatabase();
|
||||
var newShop = Store.Create(context, model);
|
||||
if (newShop == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
context.Shops.Add(newShop);
|
||||
context.SaveChanges();
|
||||
return newShop.GetViewModel;
|
||||
}
|
||||
|
||||
public StoreViewModel? Update(StoreBindingModel model)
|
||||
{
|
||||
using var context = new JewelryStoreDatabase();
|
||||
using var transaction = context.Database.BeginTransaction();
|
||||
try
|
||||
{
|
||||
var shop = context.Shops.FirstOrDefault(x => x.Id == model.Id);
|
||||
if (shop == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
shop.Update(model);
|
||||
context.SaveChanges();
|
||||
shop.UpdateJewels(context, model);
|
||||
transaction.Commit();
|
||||
return shop.GetViewModel;
|
||||
}
|
||||
catch
|
||||
{
|
||||
transaction.Rollback();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public StoreViewModel? Delete(StoreBindingModel model)
|
||||
{
|
||||
using var context = new JewelryStoreDatabase();
|
||||
var shop = context.Shops.Include(x => x.Jewels).FirstOrDefault(x => x.Id == model.Id);
|
||||
if (shop != null)
|
||||
{
|
||||
context.Shops.Remove(shop);
|
||||
context.SaveChanges();
|
||||
return shop.GetViewModel;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public bool RestockingShops(SupplyBindingModel model)
|
||||
{
|
||||
using var context = new JewelryStoreDatabase();
|
||||
var transaction = context.Database.BeginTransaction();
|
||||
var Shops = context.Shops.Include(x => x.Jewels).ThenInclude(x => x.Pizza).ToList().
|
||||
Where(x => x.JewelMaxCount > x.ShopPizzas.Select(x => x.Value.Item2).Sum()).ToList();
|
||||
if (model == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
try
|
||||
{
|
||||
foreach (Store shop in Shops)
|
||||
{
|
||||
int difference = shop.JewelMaxCount - shop.ShopJewels.Select(x => x.Value.Item2).Sum();
|
||||
int refill = Math.Min(difference, model.Count);
|
||||
model.Count -= refill;
|
||||
if (shop.ShopJewels.ContainsKey(model.JewelId))
|
||||
{
|
||||
var datePair = shop.ShopJewels[model.JewelId];
|
||||
datePair.Item2 += refill;
|
||||
shop.ShopJewels[model.JewelId] = datePair;
|
||||
}
|
||||
else
|
||||
{
|
||||
var pizza = context.Jewels.First(x => x.Id == model.JewelId);
|
||||
shop.ShopJewels.Add(model.JewelId, (jewel, refill));
|
||||
}
|
||||
shop.JewelsDictionatyUpdate(context);
|
||||
if (model.Count == 0)
|
||||
{
|
||||
transaction.Commit();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
transaction.Rollback();
|
||||
return false;
|
||||
}
|
||||
catch
|
||||
{
|
||||
transaction.Rollback();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public bool Sale(SupplySearchModel model)
|
||||
{
|
||||
using var context = new JewelryStoreDatabase();
|
||||
var transaction = context.Database.BeginTransaction();
|
||||
try
|
||||
{
|
||||
var shops = context.Shops.Include(x => x.Jewels).ThenInclude(x => x.Jewel).ToList().
|
||||
Where(x => x.ShopPizzas.ContainsKey(model.JewelId.Value)).OrderByDescending(x => x.ShopPizzas[model.JewelId.Value].Item2).ToList();
|
||||
|
||||
foreach (var shop in shops)
|
||||
{
|
||||
int residue = model.Count.Value - shop.ShopPizzas[model.JewelId.Value].Item2;
|
||||
if (residue > 0)
|
||||
{
|
||||
shop.ShopPizzas.Remove(model.JewelId.Value);
|
||||
shop.PizzasDictionatyUpdate(context);
|
||||
context.SaveChanges();
|
||||
model.Count = residue;
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
if (residue == 0)
|
||||
shop.ShopPizzas.Remove(model.JewelId.Value);
|
||||
else
|
||||
{
|
||||
var dataPair = shop.ShopPizzas[model.JewelId.Value];
|
||||
dataPair.Item2 = -residue;
|
||||
shop.ShopPizzas[model.JewelId.Value] = dataPair;
|
||||
}
|
||||
|
||||
shop.JewelsDictionatyUpdate(context);
|
||||
transaction.Commit();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
transaction.Rollback();
|
||||
return false;
|
||||
}
|
||||
catch
|
||||
{
|
||||
transaction.Rollback();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user