additional class for store

This commit is contained in:
Viltskaa 2023-02-13 15:35:09 +04:00
parent ae4eef9254
commit 665aaab1a6
5 changed files with 354 additions and 2 deletions

View File

@ -0,0 +1,178 @@
using Microsoft.Extensions.Logging;
using SushiBarContracts.BindingModels;
using SushiBarContracts.BusinessLogicsContracts;
using SushiBarContracts.SearchModels;
using SushiBarContracts.StoragesContracts;
using SushiBarContracts.ViewModels;
using SushiBarDataModels.Models;
namespace SushiBarBusinessLogic.BusinessLogics
{
public class StoreLogic : IStoreLogic
{
private readonly ILogger _logger;
private readonly IStoreStorage _storeStorage;
public StoreLogic(ILogger<StoreLogic> logger, IStoreStorage storeStorage)
{
_logger = logger;
_storeStorage = storeStorage;
}
public bool AddPackage(StoreSearchModel model, ISushiModel sushi, int quantity)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (quantity <= 0)
{
throw new ArgumentException("Quantity must be more then zero", nameof(quantity));
}
_logger.LogInformation("AddPackageInStore. StoreName:{StoreName}.Id:{ Id}", model.StoreName, model.Id);
var element = _storeStorage.GetElement(model);
if (element == null)
{
_logger.LogWarning("AddPackageInStore element not found");
return false;
}
_logger.LogInformation("AddPackageInStore find. Id:{Id}", element.Id);
if (element.Sushis.TryGetValue(sushi.Id, out var pair))
{
element.Sushis[sushi.Id] = (sushi, quantity + pair.Item2);
_logger.LogInformation("AddPackageInStore. Has been added {quantity} {package} in {StoreName}", quantity, sushi.SushiName, element.StoreName);
}
else
{
element.Sushis[sushi.Id] = (sushi, quantity);
_logger.LogInformation("AddPastryInShop. Has been added {quantity} new Package {package} in {StoreName}", quantity, sushi.SushiName, element.StoreName);
}
_storeStorage.Update(new()
{
Id = element.Id,
StoreAdress = element.StoreAdress,
StoreName = element.StoreName,
OpeningDate = element.OpeningDate,
Sushis = element.Sushis
});
return true;
}
public bool Create(StoreBindingModel model)
{
CheckModel(model);
model.Sushis = new();
if (_storeStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Delete(StoreBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id:{Id}", model.Id);
if (_storeStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
public StoreViewModel? ReadElement(StoreSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. StoreName:{StoreName}.Id:{ Id}", model.StoreName, model.Id);
var element = _storeStorage.GetElement(model);
if (element == null)
{
_logger.LogWarning("ReadElement element not found");
return null;
}
_logger.LogInformation("ReadElement find. Id:{Id}", element.Id);
return element;
}
public List<StoreViewModel>? ReadList(StoreSearchModel? model)
{
_logger.LogInformation("ReadList. StoreName:{StoreName}.Id:{ Id} ", model?.StoreName, model?.Id);
var list = (model == null) ? _storeStorage.GetFullList() : _storeStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
return list;
}
public bool Update(StoreBindingModel model)
{
CheckModel(model, false);
if (string.IsNullOrEmpty(model.StoreName))
{
throw new ArgumentNullException("Store name is empty!", nameof(model.StoreName));
}
if (_storeStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
private void CheckModel(StoreBindingModel model, bool withParams)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.StoreName))
{
throw new ArgumentNullException("Store name is empty!", nameof(model.StoreName));
}
_logger.LogInformation("Store. StoreName:{0}.StoreAdress:{1}. Id: {2}", model.StoreName, model.StoreAdress, model.Id);
var element = _storeStorage.GetElement(new StoreSearchModel
{
StoreName = model.StoreName
});
if (element != null && element.Id != model.Id && element.StoreName == model.StoreName)
{
throw new InvalidOperationException("This name of store is already exists!");
}
}
}
}

View File

@ -2,8 +2,8 @@
{
public interface IStoreModel : IId
{
public string StoreName { get; set; }
public string StoreAdress { get; set; }
public string StoreName { get; }
public string StoreAdress { get; }
DateTime OpeningDate { get; }
Dictionary<int, (ISushiModel, int)> Sushis { get; }
}

View File

@ -8,11 +8,13 @@ namespace SushibarListImplement
public List<Component> Components { get; set; }
public List<Order> Orders { get; set; }
public List<Sushi> Sushi { get; set; }
public List<Store> Stores { get; set; }
private DataListSingleton()
{
Components = new List<Component>();
Orders = new List<Order>();
Sushi = new List<Sushi>();
Stores = new List<Store>();
}
public static DataListSingleton GetInstance()
{

View File

@ -0,0 +1,119 @@
using SushiBarContracts.BindingModels;
using SushiBarContracts.SearchModels;
using SushiBarContracts.StoragesContracts;
using SushiBarContracts.ViewModels;
using SushibarListImplement.Models;
namespace SushibarListImplement.Implements
{
public class StoreStorage : IStoreStorage
{
private readonly DataListSingleton _source;
public StoreStorage()
{
_source = DataListSingleton.GetInstance();
}
public StoreViewModel? Delete(StoreBindingModel model)
{
for (int i = 0; i < _source.Stores.Count; ++i)
{
if (_source.Stores[i].Id == model.Id)
{
var element = _source.Stores[i];
_source.Stores.RemoveAt(i);
return element.GetViewModel;
}
}
return null;
}
public StoreViewModel? GetElement(StoreSearchModel model)
{
if (string.IsNullOrEmpty(model.StoreName) && !model.Id.HasValue)
{
return null;
}
foreach (var store in _source.Stores)
{
if ((!string.IsNullOrEmpty(model.StoreName) && store.StoreName == model.StoreName) || (model.Id.HasValue && store.Id == model.Id))
{
return store.GetViewModel;
}
}
return null;
}
public List<StoreViewModel> GetFilteredList(StoreSearchModel model)
{
var result = new List<StoreViewModel>();
if (string.IsNullOrEmpty(model.StoreName))
{
return result;
}
foreach (var store in _source.Stores)
{
if (store.StoreName.Contains(model.StoreName))
{
result.Add(store.GetViewModel);
}
}
return result;
}
public List<StoreViewModel> GetFullList()
{
var result = new List<StoreViewModel>();
foreach (var store in _source.Stores)
{
result.Add(store.GetViewModel);
}
return result;
}
public StoreViewModel? Insert(StoreBindingModel model)
{
model.Id = 1;
foreach (var store in _source.Stores)
{
if (model.Id <= store.Id)
{
model.Id = store.Id + 1;
}
}
var newStore = Store.Create(model);
if (newStore == null)
{
return null;
}
_source.Stores.Add(newStore);
return newStore.GetViewModel;
}
public StoreViewModel? Update(StoreBindingModel model)
{
foreach (var store in _source.Stores)
{
if (store.Id == model.Id)
{
store.Update(model);
return store.GetViewModel;
}
}
return null;
}
}
}
}

View File

@ -0,0 +1,53 @@
using SushiBarContracts.BindingModels;
using SushiBarContracts.ViewModels;
using SushiBarDataModels.Models;
namespace SushibarListImplement.Models
{
public class Store : IStoreModel
{
public string StoreName { get; private set; } = string.Empty;
public string StoreAdress { get; private set; } = string.Empty;
public DateTime OpeningDate { get; private set; }
public Dictionary<int, (ISushiModel, int)> Sushis { get; private set; } = new();
public int Id { get; private set; }
public static Store? Create(StoreBindingModel? model)
{
if (model == null)
{
return null;
}
return new Store()
{
Id = model.Id,
StoreName = model.StoreName,
StoreAdress = model.StoreAdress,
OpeningDate = model.OpeningDate,
Sushis = new()
};
}
public void Update(StoreBindingModel? model)
{
if (model == null)
{
return;
}
StoreName = model.StoreName;
StoreAdress = model.StoreAdress;
OpeningDate = model.OpeningDate;
Sushis = model.Sushis;
}
public StoreViewModel GetViewModel => new()
{
Id = Id,
StoreName = StoreName,
StoreAdress = StoreAdress,
OpeningDate = OpeningDate,
Sushis = Sushis
};
}
}
}