PIbd-21_KozyrevSS_SewingDre.../SewingDresses/SewingDressesBusinessLogic/BusinessLogic/ImplementLogic.cs

78 lines
2.8 KiB
C#
Raw Normal View History

2024-04-18 20:04:28 +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
{
2024-04-18 21:00:03 +04:00
public class ImplementLogic : IImplementLogic
2024-04-18 20:04:28 +04:00
{
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("Исполнитель с этим именем уже существует");
}
}
}
2024-04-18 20:24:26 +04:00
}