Хранилище и метод продажи

This commit is contained in:
prodigygirl 2023-03-12 19:07:21 +04:00
parent 0cfb06ff05
commit c42221ed0d

View File

@ -0,0 +1,175 @@
using FurnitureAssemblyContracts.BindingModels;
using FurnitureAssemblyContracts.SearchModels;
using FurnitureAssemblyContracts.StoragesContracts;
using FurnitureAssemblyContracts.ViewModels;
using FurnitureAssemblyDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FurnitureAssemblyDatabaseImplement.Implements
{
public class ShopStorage : IShopStorage
{
public ShopViewModel? GetElement(ShopSearchModel model)
{
if (string.IsNullOrEmpty(model.ShopName) &&
!model.Id.HasValue)
{
return null;
}
using var context = new FurnitureAssemblyDatabase();
return context.Shops
.Include(x => x.ShopFurnitures)
.ThenInclude(x => x.Furniture)
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.ShopName) &&
x.ShopName == model.ShopName) ||
(model.Id.HasValue && x.Id ==
model.Id))
?.GetViewModel;
}
public List<ShopViewModel> GetFilteredList(ShopSearchModel model)
{
if (string.IsNullOrEmpty(model.ShopName))
{
return new();
}
using var context = new FurnitureAssemblyDatabase();
return context.Shops
.Include(x => x.ShopFurnitures)
.ThenInclude(x => x.Furniture)
.Where(x => x.ShopName.Contains(model.ShopName))
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public List<ShopViewModel> GetFullList()
{
using var context = new FurnitureAssemblyDatabase();
return context.Shops
.Include(x => x.ShopFurnitures)
.ThenInclude(x => x.Furniture)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public ShopViewModel? Delete(ShopBindingModel model)
{
using var context = new FurnitureAssemblyDatabase();
var element = context.Shops.Include(x => x.ShopFurnitures).FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Shops.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
public ShopViewModel? Insert(ShopBindingModel model)
{
using var context = new FurnitureAssemblyDatabase();
using var transaction = context.Database.BeginTransaction();
try
{
var newShop = Shop.Create(context, model);
if (newShop == null)
{
return null;
}
if (GetElement(new ShopSearchModel { ShopName = newShop.ShopName}) != null)
{
throw new Exception("Магазин с таким названием уже есть");
}
context.Shops.Add(newShop);
context.SaveChanges();
transaction.Commit();
return newShop.GetViewModel;
}
catch
{
transaction.Rollback();
throw;
}
}
public bool Sell(FurnitureBindingModel furniture, int count)
{
using var context = new FurnitureAssemblyDatabase();
using var transaction = context.Database.BeginTransaction();
foreach (var shop in context.Shops.Include(x => x.ShopFurnitures).ThenInclude(x => x.Furniture).ToList())
{
if (shop.Furnitures.ContainsKey(furniture.Id))
{
if (shop.Furnitures[furniture.Id].Item2 >= count)
{
shop.Furnitures[furniture.Id] = (shop.Furnitures[furniture.Id].Item1, shop.Furnitures[furniture.Id].Item2 - count);
count = 0;
}
else
{
count -= shop.Furnitures[furniture.Id].Item2;
shop.Furnitures[furniture.Id] = (shop.Furnitures[furniture.Id].Item1, 0);
}
var model = new ShopBindingModel
{
Id = shop.Id,
ShopName = shop.ShopName,
MaxCount = shop.MaxCount,
Furnitures = shop.Furnitures,
Address = shop.Address,
DateOpening = shop.DateOpening
};
shop.Update(model);
shop.UpdateFurnitures(context, model);
if (count == 0)
break;
}
}
if (count == 0)
{
context.SaveChanges();
transaction.Commit();
return true;
}
else
{
transaction.Rollback();
return false;
}
}
public ShopViewModel? Update(ShopBindingModel model)
{
using var context = new FurnitureAssemblyDatabase();
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.UpdateFurnitures(context, model);
transaction.Commit();
return shop.GetViewModel;
}
catch
{
transaction.Rollback();
throw;
}
}
}
}