Gismatullin.ISEbd-21.STO.Co.../ServiceStation/ServiceSourceBusinessLogic/BusinessLogic/CatergoryWorkLogic.cs
2024-04-30 22:40:05 +03:00

90 lines
2.8 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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 CatergoryWorkLogic : ICategoryWorkLogic {
private readonly ILogger _logger;
private readonly ICategoryWorkStorage _storage;
public CatergoryWorkLogic(ILogger logger, ICategoryWorkStorage storage) {
_logger = logger;
_storage = storage;
}
public bool Create(CategoryWorkBindingModel model) {
CheckModel(model);
if (_storage.Insert(model) == null) {
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Delete(CategoryWorkBindingModel 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(CategoryWorkBindingModel model) {
CheckModel(model);
if (_storage.Update(model) == null) {
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
public List<CategoryWorkViewModel>? ReadList(CategoryWorkSearchModel? model) {
_logger.LogInformation($"ReadList. Id:{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(CategoryWorkBindingModel model, bool withParams = true) {
if (model == null) {
throw new ArgumentNullException(nameof(model));
}
if (!withParams) {
return;
}
if (string.IsNullOrEmpty((model.Id).ToString())) {
throw new ArgumentNullException("Нет Id категории", nameof(model.Id));
}
if (string.IsNullOrEmpty((model.ExecutorId).ToString())) {
throw new ArgumentNullException("Нет Id исполнителя", nameof(model.ExecutorId));
}
if (string.IsNullOrEmpty(model.Name)) {
throw new ArgumentNullException("Нет названия категории работы", nameof(model.Name));
}
_logger.LogInformation($"CategoryWork. Id:{model.Id}.Name:{model.Name}");
var element = _storage.GetElement(new CategoryWorkSearchModel {
Name = model.Name
});
if (element != null && element.Id != model.Id) {
throw new InvalidOperationException("Категория с таким названием уже есть");
}
}
}
}