112 lines
3.6 KiB
C#
112 lines
3.6 KiB
C#
using DinerContracts.BindingModels;
|
||
using DinerContracts.BusinessLogicsContracts;
|
||
using DinerContracts.SearchModels;
|
||
using DinerContracts.StoragesContracts;
|
||
using DinerContracts.ViewModels;
|
||
using Microsoft.Extensions.Logging;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Reflection.Metadata.Ecma335;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
|
||
namespace DineryBusinessLogic.BusinessLogic {
|
||
public class ImplementerLogic : IImplementerLogic {
|
||
|
||
private readonly ILogger _logger;
|
||
private readonly IImplementerStorage _storage;
|
||
|
||
public ImplementerLogic(ILogger<ImplementerLogic> logger, IImplementerStorage storage) {
|
||
_logger = logger;
|
||
_storage = storage;
|
||
}
|
||
|
||
public bool Create(ImplementerBindingModel model) {
|
||
CheckModel(model);
|
||
if (_storage.Insert(model) == null) {
|
||
_logger.LogWarning("Insert operation failed");
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
public bool Delete(ImplementerBindingModel model) {
|
||
CheckModel(model, false);
|
||
_logger.LogInformation($"Delete. ID:{model.ID}");
|
||
if (_storage.Delete(model) == null) {
|
||
_logger.LogWarning("Delete operation failed");
|
||
return false;
|
||
}
|
||
return false;
|
||
}
|
||
|
||
public bool Update(ImplementerBindingModel model) {
|
||
CheckModel(model);
|
||
if (_storage.Update(model) == null) {
|
||
_logger.LogWarning("Update operation failde");
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
public ImplementerViewModel? ReadElement(ImplementerSearchModel model) {
|
||
if (model == null) {
|
||
throw new ArgumentNullException(nameof(model));
|
||
}
|
||
_logger.LogInformation($"ReadElement. FIO:{model.ImplementerFIO}.ID:{model.ID}");
|
||
var element = _storage.GetElement(model);
|
||
if (element == null) {
|
||
_logger.LogWarning("ReadElement. element not fount");
|
||
return null;
|
||
}
|
||
_logger.LogInformation($"ReadElement. find.ID:{element.ID}");
|
||
return element;
|
||
}
|
||
|
||
public List<ImplementerViewModel>? ReadList(ImplementerSearchModel? model) {
|
||
_logger.LogInformation($"ReadList. ImplementerID:{model?.ID}");
|
||
var list = model == null ? _storage.GetFullList() : _storage.GetFilteredList(model);
|
||
if (list == null) {
|
||
_logger.LogWarning("ReadList. return null list");
|
||
return null;
|
||
}
|
||
_logger.LogInformation($"ReadList. Count:{list.Count}");
|
||
return list;
|
||
}
|
||
|
||
|
||
private void CheckModel(ImplementerBindingModel model, bool withParams = true) {
|
||
if (model == null) {
|
||
throw new ArgumentNullException(nameof(model));
|
||
}
|
||
if (!withParams) {
|
||
return;
|
||
}
|
||
if (string.IsNullOrEmpty(model.ImplementerFIO)) {
|
||
throw new ArgumentNullException("Нет ФИО исполнителя", nameof(model.ImplementerFIO));
|
||
}
|
||
if (string.IsNullOrEmpty(model.Password)) {
|
||
throw new ArgumentNullException("Нет пароля");
|
||
}
|
||
if (string.IsNullOrEmpty((model.WorkExperience).ToString())) {
|
||
throw new ArgumentNullException("Не указан стаж");
|
||
}
|
||
if (string.IsNullOrEmpty((model.Qualification).ToString())) {
|
||
throw new ArgumentNullException("Нет квалификации");
|
||
}
|
||
if (string.IsNullOrEmpty((model.ID).ToString())) {
|
||
throw new ArgumentNullException("Нет ID пользователя", nameof(model.ID));
|
||
}
|
||
_logger.LogInformation($"Implementer. ID:{model.ID}.ImplementerFIO:{model.ImplementerFIO}.WorkExperience:{model.WorkExperience}." +
|
||
$"Qualification:{model.Qualification}");
|
||
var element = _storage.GetElement(new ImplementerSearchModel {
|
||
ImplementerFIO = model.ImplementerFIO
|
||
});
|
||
if (element != null && element.ImplementerFIO == model.ImplementerFIO) {
|
||
throw new InvalidOperationException("Исполнитель с таким ФИО уже есть");
|
||
}
|
||
}
|
||
}
|
||
}
|