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

106 lines
4.0 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 WorkLogic : IWorkLogic {
2024-04-30 23:40:05 +04:00
private readonly ILogger _logger;
private readonly IWorkStorage _storage;
2024-08-12 17:52:19 +04:00
private WorkLogic(ILogger<IWorkLogic> logger, IWorkStorage storage) {
2024-04-30 23:40:05 +04:00
_logger = logger;
_storage = storage;
}
2024-08-12 17:52:19 +04:00
public bool Create(WorkBindingModel model) {
CheckModel(model);
if (_storage.Insert(model) == null) {
_logger.LogInformation("Insert operation failed");
return false;
}
return true;
}
2024-04-30 23:40:05 +04:00
2024-08-12 17:52:19 +04:00
public bool Update(WorkBindingModel model) {
CheckModel(model);
if (_storage.Update(model) == null) {
_logger.LogInformation("Insert operation failed");
return false;
}
return true;
}
2024-04-30 23:40:05 +04:00
2024-08-12 17:52:19 +04:00
public bool Delete(WorkBindingModel model) {
CheckModel(model, false);
_logger.LogInformation($"Delete. Id:{model.Id}");
if (_storage.Delete(model) == null) {
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
2024-04-30 23:40:05 +04:00
2024-08-12 17:52:19 +04:00
public WorkViewModel? ReadElement(WorkSearchModel model) {
if (model == null) {
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation($"ReadElement.ID:{model.Id}");
var element = _storage.GetElement(model);
if (element == null) {
_logger.LogWarning("ReadElement.Element not found");
return null;
}
_logger.LogInformation($"ReadElement.Find.Id:{element.Id}");
return element;
}
2024-04-30 23:40:05 +04:00
2024-08-12 17:52:19 +04:00
public List<WorkViewModel>? ReadList(WorkSearchModel 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;
}
2024-04-30 23:40:05 +04:00
2024-08-12 17:52:19 +04:00
private void CheckModel(WorkBindingModel model, bool withParams = true) {
if (string.IsNullOrEmpty(model.Id.ToString())) {
throw new ArgumentNullException("Нет номера работы", nameof(model.Id));
}
if (!withParams) {
return;
}
if (string.IsNullOrEmpty(model.Date.ToString())) {
model.Date = DateTime.Now;
}
if (string.IsNullOrEmpty(model.Price.ToString())) {
throw new ArgumentNullException("Стоимость работы должна быть боль 0", nameof(model.Price));
}
if (string.IsNullOrEmpty(model.ExecutorId.ToString())) {
throw new ArgumentNullException("Нет номера исполнителя", nameof(model.ExecutorId));
}
if (string.IsNullOrEmpty(model.TaskId.ToString())) {
throw new ArgumentNullException("Нет номера задачи", nameof(model.TaskId));
}
if (model.ClientList.Count <= 0) {
throw new ArgumentException("Работа не может быть без клиента", nameof(model.ClientList));
}
_logger.LogInformation($"Work.Id:{model.Id}.Date:{model.Date}.Price:{model.Price}.ExecutorId:{model.ExecutorId}.TaskId:{model.TaskId}." +
$"ClientList.Count:{model.ClientList.Count}");
}
}
2024-04-30 23:40:05 +04:00
}