144 lines
4.6 KiB
C#
144 lines
4.6 KiB
C#
using FurnitureAssemblyContracts.BindingModels;
|
|
using FurnitureAssemblyContracts.SearchModels;
|
|
using FurnitureAssemblyContracts.StoragesContracts;
|
|
using FurnitureAssemblyContracts.ViewModels;
|
|
using FurnitureAssemblyFileImplement.Models;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace FurnitureAssemFileImplement.Implements
|
|
{
|
|
public class ShopStorage : IShopStorage
|
|
{
|
|
private readonly DataFileSingleton source;
|
|
public ShopStorage()
|
|
{
|
|
source = DataFileSingleton.GetInstance();
|
|
}
|
|
|
|
public List<ShopViewModel> GetFullList()
|
|
{
|
|
return source.Shops.Select(x => x.GetViewModel).ToList();
|
|
}
|
|
|
|
public List<ShopViewModel> GetFilteredList(ShopSearchModel model)
|
|
{
|
|
if (string.IsNullOrEmpty(model.ShopName))
|
|
{
|
|
return new();
|
|
}
|
|
|
|
return source.Shops.Where(x => x.ShopName.Equals(model.ShopName)).Select(x => x.GetViewModel).ToList();
|
|
}
|
|
|
|
public ShopViewModel? GetElement(ShopSearchModel model)
|
|
{
|
|
if (string.IsNullOrEmpty(model.ShopName) && !model.Id.HasValue)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
return source.Shops.FirstOrDefault(x =>
|
|
(model.Id.HasValue && x.Id == model.Id) || (!string.IsNullOrEmpty(model.ShopName) && x.ShopName == model.ShopName))?.GetViewModel;
|
|
}
|
|
|
|
public ShopViewModel? Insert(ShopBindingModel model)
|
|
{
|
|
model.Id = source.Shops.Count > 0 ? source.Shops.Max(x => x.Id) + 1 : 1;
|
|
|
|
var newShop = Shop.Create(model);
|
|
if (newShop == null)
|
|
{
|
|
return null;
|
|
}
|
|
source.Shops.Add(newShop);
|
|
source.SaveShops();
|
|
return newShop.GetViewModel;
|
|
}
|
|
|
|
public ShopViewModel? Update(ShopBindingModel model)
|
|
{
|
|
var shop = source.Shops.FirstOrDefault(x => x.Id == model.Id);
|
|
if (shop == null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
shop.Update(model);
|
|
source.SaveShops();
|
|
return shop.GetViewModel;
|
|
}
|
|
|
|
public ShopViewModel? Delete(ShopBindingModel model)
|
|
{
|
|
var element = source.Shops.FirstOrDefault(x => x.Id == model.Id);
|
|
if (element != null)
|
|
{
|
|
source.Shops.Remove(element);
|
|
source.SaveShops();
|
|
return element.GetViewModel;
|
|
}
|
|
return null;
|
|
}
|
|
private int GetCountFurnitureAllShops(FurnitureBindingModel furniture)
|
|
{
|
|
int count = 0;
|
|
foreach (var shop in source.Shops)
|
|
{
|
|
if (shop.Furnitures.ContainsKey(furniture.Id))
|
|
{
|
|
count += shop.Furnitures[furniture.Id].Item2;
|
|
}
|
|
}
|
|
return count;
|
|
}
|
|
|
|
|
|
public bool Sell(FurnitureBindingModel furniture, int count)
|
|
{
|
|
if (GetCountFurnitureAllShops(furniture) < count)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
|
|
foreach (var shop in source.Shops)
|
|
{
|
|
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);
|
|
shop.Update(new ShopBindingModel
|
|
{
|
|
Id = shop.Id,
|
|
ShopName = shop.ShopName,
|
|
MaxCount = shop.MaxCount,
|
|
Furnitures = shop.Furnitures,
|
|
Address = shop.Address,
|
|
DateOpening = shop.DateOpening,
|
|
});
|
|
break;
|
|
}
|
|
count -= shop.Furnitures[furniture.Id].Item2;
|
|
shop.Furnitures[furniture.Id] = (shop.Furnitures[furniture.Id].Item1, 0);
|
|
shop.Update(new ShopBindingModel
|
|
{
|
|
Id = shop.Id,
|
|
ShopName = shop.ShopName,
|
|
MaxCount = shop.MaxCount,
|
|
Furnitures = shop.Furnitures,
|
|
Address = shop.Address,
|
|
DateOpening = shop.DateOpening,
|
|
});
|
|
}
|
|
}
|
|
source.SaveShops();
|
|
return true;
|
|
}
|
|
}
|
|
}
|