PIbd-23_Sergunov_M.A._SUBD/BookShop/BookShopBusinessLogic/BusinessLogics/BookLogic.cs

130 lines
4.3 KiB
C#
Raw Normal View History

using BookShopContracts.BindingModels;
using BookShopContracts.BusinessLogicsContracts;
using BookShopContracts.SearchModels;
using BookShopContracts.StoragesContracts;
using BookShopContracts.ViewModels;
using Microsoft.Extensions.Logging;
namespace BookShopBusinessLogic.BusinessLogics
{
public class BookLogic : IBookLogic
{
private readonly ILogger _logger;
private readonly IBookStorage _bookStorage;
public BookLogic(ILogger<BookLogic> logger, IBookStorage bookStorage)
{
_logger = logger;
_bookStorage = bookStorage;
}
public bool Create(BookBindingModel model)
{
CheckModel(model);
if (_bookStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Delete(BookBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id: {Id}", model.Id);
if (_bookStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
public BookViewModel? ReadElement(BookSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. BookName: {BookName}. Id: {Id}.", model.BookName, model.Id);
var element = _bookStorage.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<BookViewModel>? ReadList(BookSearchModel? model)
{
_logger.LogInformation("ReadList. BookName: {BookName}. Id: {Id}.", model?.BookName, model?.Id);
var list = model == null ? _bookStorage.GetFullList() : _bookStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count: {Count}", list.Count);
return list;
}
public string TestInsertList(int num)
{
throw new NotImplementedException();
}
public string TestReadList(int num)
{
return _bookStorage.TestReadList(num);
}
public bool Update(BookBindingModel model)
{
CheckModel(model);
if (_bookStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
private void CheckModel(BookBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.BookName))
{
throw new ArgumentNullException("Нет названия книги", nameof(model.BookName));
}
if (model.Count < 0)
{
throw new ArgumentNullException("Количество не может быть меньше 0", nameof(model.BookName));
}
if (model.Cost <= 0)
{
throw new ArgumentNullException("Стоимость не может быть меньше 0", nameof(model.BookName));
}
_logger.LogInformation("Book. BookName: {BookName}. Cost: {Cost}. Id: {Id}.", model.BookName, model.Cost, model.Id);
var element = _bookStorage.GetElement(new BookSearchModel
{
BookName = model.BookName
});
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Книга с таким названием уже есть");
}
}
}
}