PIbd22_Kuznetsov_A.V._Sewin.../SewingDresses/SewingDressesBusinessLogic/BusinessLogic/ImplementLogic.cs

111 lines
3.6 KiB
C#
Raw Normal View History

2024-05-26 13:40:22 +04:00
using Microsoft.Extensions.Logging;
using SewingDressesContracts.BindingModels;
using SewingDressesContracts.BusinessLogicsContracts;
using SewingDressesContracts.SearchModels;
using SewingDressesContracts.StoragesContracts;
using SewingDressesContracts.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SewingDressesBusinessLogic.BusinessLogic
{
public class ImplementLogic : IImplementLogic
{
private readonly ILogger _logger;
private readonly IImplementStorage _storage;
public ImplementLogic(ILogger<ImplementLogic> logger, IImplementStorage storage)
{
_logger = logger;
_storage = storage;
}
public ImplementViewModel? ReadElement(ImplementSearchModel? model)
{
if (model == null)
throw new ArgumentNullException(nameof(model));
_logger.LogInformation("ReadElement. FIO:{ImplementFIO}. Id:{Id}", model.ImplementFIO, model.Id);
var element = _storage.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<ImplementViewModel>? ReadList(ImplementSearchModel? model)
{
var list = model == null ? _storage.GetFullList() : _storage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
return list;
}
private void CheckModel(ImplementBindingModel? model, bool withParams = true)
{
if (model == null)
throw new ArgumentNullException(nameof(model));
if (!withParams)
return;
if (string.IsNullOrEmpty(model.ImplementFIO))
throw new ArgumentException("Нет ФИО исполнителя", nameof(model.ImplementFIO));
if (string.IsNullOrEmpty(model.Password))
{
throw new ArgumentNullException("Нет пароля клиента", nameof(model.Password));
}
if (model.WorkExperience < 0)
{
throw new ArgumentNullException("Стаж меньше 0", nameof(model.WorkExperience));
}
if (model.Qualification < 0)
{
throw new ArgumentException("Квалификация меньше 0", nameof(model.Qualification));
}
_logger.LogInformation("Implementer. ImplementerFIO:{ImplementerFIO}. Password:{Password}. WorkExperience:{WorkExperience}. Qualification:{Qualification}. Id: {Id} ", model.ImplementFIO, model.Password, model.WorkExperience, model.Qualification, model.Id);
var element = _storage.GetElement(new ImplementSearchModel { ImplementFIO = model.ImplementFIO });
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Исполнитель с этим именем уже существует");
}
}
public bool Create(ImplementBindingModel model)
{
CheckModel(model);
_logger.LogInformation("Insert Imlement");
if (_storage.Insert(model) == null)
{
_logger.LogWarning("Insert Error");
return false;
}
return true;
}
public bool Delete(ImplementBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete Implement Id:{Id}", model.Id);
if (_storage.Delete(model) == null)
{
_logger.LogWarning("Delete Error");
return false;
}
return true;
}
public bool Update(ImplementBindingModel model)
{
CheckModel(model);
_logger.LogInformation("Update Implement Id:{Id}", model.Id);
if (_storage.Update(model) == null)
{
_logger.LogWarning("Update Error");
return false;
}
return true;
}
}
}