ISEbd-21_Zinovev_V.V._Furni.../FurnitureAssembly/FurnitureAssemblyBusinessLogic/WorkpieceLogic.cs
2024-03-20 19:37:08 +04:00

112 lines
4.2 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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.BusinessLogics
{
public class WorkpieceLogic : IWorkpieceLogic
{
private readonly ILogger _logger;
private readonly IWorkpieceStorage _workpieceStorage;
public WorkpieceLogic(ILogger<WorkpieceLogic> logger, IWorkpieceStorage workpieceStorage)
{
_logger = logger;
_workpieceStorage = workpieceStorage;
}
public List<WorkpieceViewModel>? ReadList(WorkpieceSearchModel? model)
{
_logger.LogInformation("ReadList. WorkpieceName:{WorkpieceName}.Id:{ Id} ", model?.WorkpieceName, model?.Id);
var list = model == null ? _workpieceStorage.GetFullList() : _workpieceStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
return list;
}
public WorkpieceViewModel? ReadElement(WorkpieceSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. WorkpieceName:{WorkpieceName}.Id:{ Id}", model.WorkpieceName, model.Id);
var element = _workpieceStorage.GetElement(model);
if (element == null)
{
_logger.LogWarning("ReadElement element not found");
return null;
}
_logger.LogInformation("ReadElement find. Id:{Id}", element.Id);
return element;
}
public bool Create(WorkpieceBindingModel model)
{
CheckModel(model);
if (_workpieceStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Update(WorkpieceBindingModel model)
{
CheckModel(model);
if (_workpieceStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
public bool Delete(WorkpieceBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id:{Id}", model.Id);
if (_workpieceStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
private void CheckModel(WorkpieceBindingModel model, bool withParams =
true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.WorkpieceName))
{
throw new ArgumentNullException("Нет названия компонента",
nameof(model.WorkpieceName));
}
if (model.Cost <= 0)
{
throw new ArgumentNullException("Цена компонента должна быть больше 0", nameof(model.Cost));
}
_logger.LogInformation("Workpiece. WorkpieceName:{WorkpieceName}.Cost:{ Cost}. Id: { Id}", model.WorkpieceName, model.Cost, model.Id);
var element = _workpieceStorage.GetElement(new WorkpieceSearchModel {WorkpieceName = model.WorkpieceName});
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Компонент с таким названием уже есть");
}
}
}
}