PIbd-22-Stroev-V.M.-Plumbin.../PlumbingRepair/PlumbingRepairBusinessLogic/BusinessLogics/WorkLogic.cs

119 lines
4.2 KiB
C#
Raw Normal View History

2024-02-14 11:14:37 +04:00
using Microsoft.Extensions.Logging;
using PlumbingRepairContracts.BindingModels;
using PlumbingRepairContracts.BusinessLogicsContracts;
using PlumbingRepairContracts.SearchModels;
using PlumbingRepairContracts.StoragesContracts;
using PlumbingRepairContracts.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PlumbingRepairBusinessLogic.BusinessLogics
{
public class WorkLogic : IWorkLogic
{
private readonly ILogger _logger;
private readonly IWorkStorage _workStorage;
public WorkLogic(ILogger<WorkLogic> logger, IWorkStorage
workStorage)
{
_logger = logger;
_workStorage = workStorage;
}
public List<WorkViewModel>? ReadList(WorkSearchModel? model)
{
_logger.LogInformation("ReadList. WorkName: {WorkName}.Id:{Id} ",
model?.WorkName, model?.Id);
var list = model == null ? _workStorage.GetFullList() :
_workStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
return list;
}
public WorkViewModel? ReadElement(WorkSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. WorkName:{WorkName}.Id:{Id}",
model.WorkName, model.Id);
var element = _workStorage.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(WorkBindingModel model)
{
CheckModel(model);
if (_workStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Update(WorkBindingModel model)
{
CheckModel(model);
if (_workStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
public bool Delete(WorkBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id:{Id}", model.Id);
if (_workStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
private void CheckModel(WorkBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.WorkName))
{
throw new ArgumentNullException("Нет названия работы",
nameof(model.WorkName));
}
if (model.Price <= 0)
{
throw new ArgumentNullException("Цена работы должна быть больше 0", nameof(model.Price));
}
_logger.LogInformation("Work. WorkName:{WorkName}.Price:{Price}. Id: {Id}",
model.WorkName, model.Price, model.Id);
var element = _workStorage.GetElement(new WorkSearchModel
{
WorkName = model.WorkName
});
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Работа с таким названием уже есть");
}
}
}
}