161 lines
4.3 KiB
C#
161 lines
4.3 KiB
C#
using SushiBarContracts.BindingModels;
|
|
using SushiBarContracts.SearchModels;
|
|
using SushiBarContracts.StoragesContracts;
|
|
using SushiBarContracts.ViewModels;
|
|
using SushiBarDataModels.Models;
|
|
using SushiBarFileImplement.Models;
|
|
|
|
namespace SushiBarFileImplement.Implements;
|
|
|
|
public class StoreStorage : IStoreStorage
|
|
{
|
|
private readonly DataFileSingleton _singleton;
|
|
|
|
public StoreStorage()
|
|
{
|
|
_singleton = DataFileSingleton.GetInstance();
|
|
}
|
|
|
|
public List<StoreViewModel> GetFullList()
|
|
{
|
|
return _singleton.Stores
|
|
.Select(x => x.GetViewModel)
|
|
.ToList();
|
|
}
|
|
|
|
public List<StoreViewModel> GetFilteredList(StoreSearchModel model)
|
|
{
|
|
if (!model.Id.HasValue)
|
|
{
|
|
return new List<StoreViewModel>();
|
|
}
|
|
return _singleton.Stores
|
|
.Where(x => x.Id == model.Id)
|
|
.Select(x => x.GetViewModel)
|
|
.ToList();
|
|
}
|
|
|
|
public StoreViewModel? GetElement(StoreSearchModel model)
|
|
{
|
|
if (!model.Id.HasValue)
|
|
{
|
|
return null;
|
|
}
|
|
return _singleton.Stores
|
|
.FirstOrDefault(x => x.Id == model.Id)
|
|
?.GetViewModel;
|
|
}
|
|
|
|
public StoreViewModel? Insert(StoreBindingModel model)
|
|
{
|
|
model.Id = _singleton.Stores.Count > 0 ? _singleton.Stores.Max(x => x.Id) + 1 : 1;
|
|
var store = Store.Create(model);
|
|
if (store == null)
|
|
{
|
|
return null;
|
|
}
|
|
_singleton.Stores.Add(store);
|
|
_singleton.SaveStores();
|
|
return store.GetViewModel;
|
|
}
|
|
|
|
public StoreViewModel? Update(StoreBindingModel model)
|
|
{
|
|
var store = _singleton.Stores.FirstOrDefault(x => x.Id == model.Id);
|
|
if (store == null)
|
|
{
|
|
return null;
|
|
}
|
|
store.Update(model);
|
|
_singleton.SaveStores();
|
|
return store.GetViewModel;
|
|
}
|
|
|
|
public StoreViewModel? Delete(StoreBindingModel model)
|
|
{
|
|
var element = _singleton.Stores.FirstOrDefault(x => x.Id == model.Id);
|
|
if (element == null) return null;
|
|
|
|
_singleton.Stores.Remove(element);
|
|
_singleton.SaveStores();
|
|
return element.GetViewModel;
|
|
}
|
|
|
|
public bool SellSushi(ISushiModel model, int quantity)
|
|
{
|
|
var sushi = _singleton.Sushis.FirstOrDefault(x => x.Id == model.Id);
|
|
int countStore = quantity;
|
|
|
|
if (sushi is null)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
foreach (var store in _singleton.Stores)
|
|
{
|
|
foreach (var valueTuple in store.Sushis.Values)
|
|
{
|
|
if (valueTuple.Item1.Id == sushi.Id)
|
|
{
|
|
quantity -= valueTuple.Item2;
|
|
}
|
|
|
|
if (quantity <= 0)
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (quantity > 0)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
quantity = countStore;
|
|
foreach (var shop in _singleton.Stores)
|
|
{
|
|
var sushis = shop.Sushis;
|
|
|
|
for (var j =1; j < sushis.Count + 1; j++)
|
|
{
|
|
if (sushis[j].Item1.Id == sushi.Id)
|
|
{
|
|
while (sushis[j].Item2 > 0 && quantity > 0)
|
|
{
|
|
var tempItem2 = sushis[j].Item2;
|
|
tempItem2--;
|
|
quantity--;
|
|
sushis[j] = (sushis[j].Item1, tempItem2);
|
|
}
|
|
}
|
|
|
|
if (quantity > 0) continue;
|
|
shop.Update(new StoreBindingModel()
|
|
{
|
|
Id = shop.Id,
|
|
StoreName = shop.StoreName,
|
|
StoreAddress = shop.StoreAddress,
|
|
OpeningDate = shop.OpeningDate,
|
|
maxSushi = shop.maxSushi,
|
|
Sushis = sushis
|
|
});
|
|
_singleton.SaveStores();
|
|
return true;
|
|
}
|
|
|
|
shop.Update(new StoreBindingModel()
|
|
{
|
|
Id = shop.Id,
|
|
StoreName = shop.StoreName,
|
|
StoreAddress = shop.StoreAddress,
|
|
OpeningDate = shop.OpeningDate,
|
|
maxSushi = shop.maxSushi,
|
|
Sushis = sushis
|
|
});
|
|
_singleton.SaveStores();
|
|
}
|
|
|
|
return quantity <= 0;
|
|
}
|
|
} |