128 lines
3.8 KiB
C#
128 lines
3.8 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
using SushiBarContracts.BindingModels;
|
|
using SushiBarContracts.SearchModels;
|
|
using SushiBarContracts.StoragesContracts;
|
|
using SushiBarContracts.ViewModels;
|
|
using SushiBarDatabaseImplement.Models;
|
|
using SushiBarDataModels.Models;
|
|
|
|
namespace SushiBarDatabaseImplement.Implements;
|
|
|
|
public class StoreStorage : IStoreStorage
|
|
{
|
|
public List<StoreViewModel> GetFullList()
|
|
{
|
|
using var context = new SushiBarDatabase();
|
|
return context.Stores
|
|
.Include(x => x.StoreSushi)
|
|
.ThenInclude(x => x.Sushi)
|
|
.Select(x => x.GetViewModel)
|
|
.ToList();
|
|
}
|
|
|
|
public List<StoreViewModel> GetFilteredList(StoreSearchModel model)
|
|
{
|
|
if (string.IsNullOrEmpty(model.StoreName))
|
|
{
|
|
return new List<StoreViewModel>();
|
|
}
|
|
using var context = new SushiBarDatabase();
|
|
return context.Stores
|
|
.Include(x => x.StoreSushi)
|
|
.ThenInclude(x => x.Sushi)
|
|
.Select(x => x.GetViewModel)
|
|
.Where(x => x.StoreName.Contains(model.StoreName ?? string.Empty))
|
|
.ToList();
|
|
}
|
|
|
|
public StoreViewModel? GetElement(StoreSearchModel model)
|
|
{
|
|
if (!model.Id.HasValue)
|
|
{
|
|
return null;
|
|
}
|
|
using var context = new SushiBarDatabase();
|
|
return context.Stores
|
|
.Include(x => x.StoreSushi)
|
|
.ThenInclude(x => x.Sushi)
|
|
.FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id)
|
|
?.GetViewModel;
|
|
}
|
|
|
|
public StoreViewModel? Insert(StoreBindingModel model)
|
|
{
|
|
using var context = new SushiBarDatabase();
|
|
using var transaction = context.Database.BeginTransaction();
|
|
try
|
|
{
|
|
var store = Store.Create(context, model);
|
|
if (store == null)
|
|
{
|
|
return null;
|
|
}
|
|
if (context.Stores.Any(x => x.StoreName == store.StoreName))
|
|
{
|
|
throw new Exception("Name already exists!");
|
|
}
|
|
context.Stores.Add(store);
|
|
context.SaveChanges();
|
|
transaction.Commit();
|
|
return store.GetViewModel;
|
|
}
|
|
catch
|
|
{
|
|
transaction.Rollback();
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public StoreViewModel? Update(StoreBindingModel model)
|
|
{
|
|
using var context = new SushiBarDatabase();
|
|
var shop = context.Stores.FirstOrDefault(x => x.Id == model.Id);
|
|
if (shop == null)
|
|
{
|
|
return null;
|
|
}
|
|
shop.Update(model);
|
|
shop.UpdateSushi(context, model);
|
|
context.SaveChanges();
|
|
return shop.GetViewModel;
|
|
}
|
|
|
|
public StoreViewModel? Delete(StoreBindingModel model)
|
|
{
|
|
using var context = new SushiBarDatabase();
|
|
var element = context.Stores.FirstOrDefault(x => x.Id == model.Id);
|
|
if (element == null) return null;
|
|
context.Stores.Remove(element);
|
|
context.SaveChanges();
|
|
return element.GetViewModel;
|
|
}
|
|
|
|
public bool SellSushi(ISushiModel model, int quantity)
|
|
{
|
|
using var context = new SushiBarDatabase();
|
|
using var transaction = context.Database.BeginTransaction();
|
|
foreach (var sp in
|
|
context.StoreSushis.Where(x => x.SushiId == model.Id))
|
|
{
|
|
var res = Math.Min(quantity, sp.Count);
|
|
sp.Count -= res;
|
|
quantity -= res;
|
|
if (sp.Count == 0) context.StoreSushis.Remove(sp);
|
|
if (quantity == 0)
|
|
break;
|
|
}
|
|
if (quantity == 0)
|
|
{
|
|
context.SaveChanges();
|
|
transaction.Commit();
|
|
}
|
|
else
|
|
{
|
|
transaction.Rollback();
|
|
}
|
|
return quantity == 0;
|
|
}
|
|
} |