161 lines
5.3 KiB
C#
Raw Normal View History

2024-05-24 12:23:50 +04:00
using FurnitureAssemblyContracts.BindingModels;
using FurnitureAssemblyContracts.BusinessLogicsContracts;
using FurnitureAssemblyContracts.SearchModels;
using FurnitureAssemblyContracts.StoragesContracts;
using FurnitureAssemblyContracts.ViewModels;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FurnitureAssemblyBusinessLogic.BussinessLogic
{
// Класс, реализующий логику для изделий
2024-05-24 13:05:47 +04:00
public class UsersLogic : IUsersLogic
2024-05-24 12:23:50 +04:00
{
private readonly ILogger _logger;
2024-05-24 13:05:47 +04:00
private readonly IUsersStorage _furnitureStorage;
2024-05-24 12:23:50 +04:00
// Конструктор
2024-05-24 13:05:47 +04:00
public UsersLogic(ILogger<UsersLogic> logger, IUsersStorage furnitureStorage)
2024-05-24 12:23:50 +04:00
{
_logger = logger;
_furnitureStorage = furnitureStorage;
}
// Вывод отфильтрованного списка
2024-05-24 13:05:47 +04:00
public List<FurnitureViewModel>? ReadList(UsersSearchModel? model)
2024-05-24 12:23:50 +04:00
{
2024-05-24 13:05:47 +04:00
_logger.LogInformation("ReadList. UsersName: {UsersName}. Id:{Id}", model?.UsersName, model?.Id);
2024-05-24 12:23:50 +04:00
// list хранит весь список в случае, если model пришло со значением null на вход метода
var list = model == null ? _furnitureStorage.GetFullList() : _furnitureStorage.GetFilteredList(model);
if(list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
return list;
}
// Вывод конкретного изделия
2024-05-24 13:05:47 +04:00
public FurnitureViewModel? ReadElement(UsersSearchModel model)
2024-05-24 12:23:50 +04:00
{
if(model == null)
{
throw new ArgumentNullException(nameof(model));
}
2024-05-24 13:05:47 +04:00
_logger.LogInformation("ReadElement. UsersName: {UsersName}. Id:{Id}", model.UsersName, model.Id);
2024-05-24 12:23:50 +04:00
var element = _furnitureStorage.GetElement(model);
if(element == null)
{
_logger.LogWarning("ReadElement element not found");
return null;
}
_logger.LogInformation("Readelement find. Id:{Id}", model.Id);
return element;
}
// Создание изделия
2024-05-24 13:05:47 +04:00
public bool Create(UsersBindingModel model)
2024-05-24 12:23:50 +04:00
{
CheckModel(model);
if(_furnitureStorage.Insert(model) == null)
{
_logger.LogWarning("Create operation failed");
return false;
}
return true;
}
// Обновление изделия
2024-05-24 13:05:47 +04:00
public bool Update(UsersBindingModel model)
2024-05-24 12:23:50 +04:00
{
CheckModel(model);
if(_furnitureStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
// Удаление изделия
2024-05-24 13:05:47 +04:00
public bool Delete(UsersBindingModel model)
2024-05-24 12:23:50 +04:00
{
CheckModel(model, false);
_logger.LogInformation("Delete, Id:{Id}", model.Id);
if(_furnitureStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
// Проверка входного аргумента для методов Insert, Update и Delete
2024-05-24 13:05:47 +04:00
private void CheckModel(UsersBindingModel model, bool withParams = true)
2024-05-24 12:23:50 +04:00
{
if(model == null)
{
throw new ArgumentNullException(nameof(model));
}
// При удалении параметру withParams передаём false
if (!withParams)
{
return;
}
// Проверка на наличие названия изделия
2024-05-24 13:05:47 +04:00
if(string.IsNullOrEmpty(model.UsersName))
2024-05-24 12:23:50 +04:00
{
2024-05-24 13:05:47 +04:00
throw new ArgumentNullException("Нет названия изделия", nameof(model.UsersName));
2024-05-24 12:23:50 +04:00
}
// Проверка на наличие нормальной цены у изделия
if(model.Price <= 0)
{
throw new ArgumentNullException("Цена изделия должна быть больше 0", nameof(model.Price));
}
2024-05-24 13:05:47 +04:00
_logger.LogInformation("Furniture. UsersName:{UsersName}. Price:{Price}. Id:{Id}",
model.UsersName, model.Price, model.Id);
2024-05-24 12:23:50 +04:00
// Проверка на наличие такого же изделия в списке
2024-05-24 13:05:47 +04:00
var element = _furnitureStorage.GetElement(new UsersSearchModel
2024-05-24 12:23:50 +04:00
{
2024-05-24 13:05:47 +04:00
UsersName = model.UsersName,
2024-05-24 12:23:50 +04:00
});
// Если элемент найден и его Id не совпадает с Id объекта, переданного на вход
if(element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Изделие с таким названием уже есть");
}
}
}
}