Gismatullin.ISEbd-21.STO.Co.../ServiceStation/ServiceSourceBusinessLogic/BusinessLogic/ExecutorLogic.cs

104 lines
3.3 KiB
C#
Raw Normal View History

2024-04-30 23:40:05 +04:00
using Microsoft.Extensions.Logging;
using ServiceStationContracts.BindingModels;
using ServiceStationContracts.BusinessLogicContracts;
using ServiceStationContracts.SearchModels;
using ServiceStationContracts.ViewModels;
using ServiceStationsContracts.StorageContracts;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ServiceSourceBusinessLogic.BusinessLogic {
public class ExecutorLogic : IExecutorLogic {
private readonly ILogger _logger;
private readonly IExecutorStorage _storage;
2024-08-12 17:52:19 +04:00
public ExecutorLogic(ILogger<IExecutorLogic> logger, IExecutorStorage storage) {
2024-04-30 23:40:05 +04:00
_logger = logger;
_storage = storage;
}
public bool Create(ExecutorBindingModel model) {
CheckModel(model);
if (_storage.Insert(model) == null) {
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Delete(ExecutorBindingModel model) {
CheckModel(model, false);
_logger.LogInformation($"Delete.Id:{model.Id}");
if (_storage.Delete(model) == null) {
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
public bool Update(ExecutorBindingModel model) {
CheckModel(model);
if (_storage.Update(model) == null) {
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
public ExecutorViewModel? ReadElement(ExecutorSearchModel model) {
if (model == null) {
throw new ArgumentNullException(nameof(model));
}
2024-08-12 17:52:19 +04:00
_logger.LogInformation($"ReadElement.Id:{model.Id}.Email:{model.Email}");
2024-04-30 23:40:05 +04:00
var element = _storage.GetElement(model);
if (element == null) {
2024-08-12 17:52:19 +04:00
_logger.LogWarning("ReadElement.Element not fount");
2024-04-30 23:40:05 +04:00
return null;
}
2024-08-12 17:52:19 +04:00
_logger.LogInformation($"ReadElement.Find.Id:{element.Id}");
2024-04-30 23:40:05 +04:00
return element;
}
2024-08-12 17:52:19 +04:00
public List<ExecutorViewModel>? ReadList() {
var list = _storage.GetFullList();
2024-04-30 23:40:05 +04:00
if (list == null) {
2024-08-12 17:52:19 +04:00
_logger.LogWarning("ReadList return null list");
2024-04-30 23:40:05 +04:00
return null;
}
_logger.LogInformation($"ReadList.Count:{list.Count}");
return list;
}
private void CheckModel(ExecutorBindingModel model, bool withParams = true) {
2024-08-12 17:52:19 +04:00
if (string.IsNullOrEmpty(model.Id.ToString())) {
throw new ArgumentNullException("Нет номера исполнителя", nameof(model.Id));
2024-04-30 23:40:05 +04:00
}
if (!withParams) {
return;
}
if (string.IsNullOrEmpty((model.Id).ToString())) {
throw new ArgumentNullException("Нет Id исполнителя", nameof(model.Id));
}
if (string.IsNullOrEmpty(model.Email)) {
2024-08-12 17:52:19 +04:00
throw new ArgumentNullException("Нет почты исполнителя", nameof(model.Email));
2024-04-30 23:40:05 +04:00
}
if (string.IsNullOrEmpty(model.Password)) {
2024-08-12 17:52:19 +04:00
throw new ArgumentNullException("Нет пароля исполнителя", nameof(model.Password));
2024-04-30 23:40:05 +04:00
}
2024-08-12 17:52:19 +04:00
if (string.IsNullOrEmpty(model.FIO)) {
throw new ArgumentNullException("Нет ФИО исполнителя", nameof(model.FIO));
2024-04-30 23:40:05 +04:00
}
2024-08-12 17:52:19 +04:00
_logger.LogInformation($"Executor.Id:{model.Id}.Email:{model.Email}.Password:{model.Password}.FIO:{model.FIO}");
2024-04-30 23:40:05 +04:00
var element = _storage.GetElement(new ExecutorSearchModel { Email = model.Email });
if (element != null && element.Id != model.Id) {
2024-08-12 17:52:19 +04:00
throw new InvalidOperationException("Такая почта уже занята");
2024-04-30 23:40:05 +04:00
}
}
}
}