129 lines
3.9 KiB
C#
129 lines
3.9 KiB
C#
using ComputersShopContracts.BindingModels;
|
|
using ComputersShopContracts.SearchModels;
|
|
using ComputersShopContracts.StoragesContracts;
|
|
using ComputersShopContracts.ViewModels;
|
|
using ComputersShopDataModels.Models;
|
|
using ComputersShopFileImplement.Models;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace ComputersShopFileImplement.Implements
|
|
{
|
|
public class ShopStorage : IShopStorage
|
|
{
|
|
private readonly DataFileSingleton source;
|
|
|
|
public ShopStorage()
|
|
{
|
|
source = DataFileSingleton.GetInstance();
|
|
}
|
|
public ShopViewModel? Delete(ShopBindingModel model)
|
|
{
|
|
var shop = source.Shops.FirstOrDefault(x => x.Id == model.Id);
|
|
if (shop != null)
|
|
{
|
|
source.Shops.Remove(shop);
|
|
source.SaveShops();
|
|
return shop.GetViewModel;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public ShopViewModel? GetElement(ShopSearchModel model)
|
|
{
|
|
if (string.IsNullOrEmpty(model.ShopName) && !model.Id.HasValue)
|
|
{
|
|
return null;
|
|
}
|
|
return source.Shops
|
|
.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();
|
|
}
|
|
return source.Shops
|
|
.Where(x => x.ShopName.Contains(model.ShopName))
|
|
.Select(x => x.GetViewModel)
|
|
.ToList();
|
|
}
|
|
|
|
public List<ShopViewModel> GetFullList()
|
|
{
|
|
return source.Shops.Select(x => x.GetViewModel).ToList();
|
|
}
|
|
|
|
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 bool SellComputers(IComputerModel model, int quantity)
|
|
{
|
|
int hasCount = source.Shops
|
|
.SelectMany(x => x.Computers)
|
|
.Where(pair => pair.Key == model.Id)
|
|
.Sum(pair => pair.Value.Item2);
|
|
|
|
if (hasCount < quantity)
|
|
return false;
|
|
|
|
source.Shops.ForEach(x =>
|
|
{
|
|
if (x.Computers.TryGetValue(model.Id, out var pair))
|
|
{
|
|
if (quantity >= pair.Item2)
|
|
{
|
|
quantity -= pair.Item2;
|
|
x.Computers[model.Id] = (model, 0);
|
|
}
|
|
else
|
|
{
|
|
x.Computers[model.Id] = (model, pair.Item2 - quantity);
|
|
quantity = 0;
|
|
}
|
|
|
|
x.Update(new ShopBindingModel
|
|
{
|
|
Id = x.Id,
|
|
ShopAddress = x.ShopAddress,
|
|
Capacity = x.Capacity,
|
|
DateOpening = x.DateOpening,
|
|
ShopName = x.ShopName,
|
|
Computers = x.Computers
|
|
});
|
|
}
|
|
});
|
|
|
|
source.SaveShops();
|
|
return true;
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|
|
}
|