106 lines
4.1 KiB
C#
106 lines
4.1 KiB
C#
using Microsoft.Extensions.Logging;
|
|
using ServiceStationContracts.BindingModels;
|
|
using ServiceStationContracts.BusinessLogic;
|
|
using ServiceStationContracts.SearchModels;
|
|
using ServiceStationContracts.ViewModels;
|
|
using ServiceStationContracts.StorageContracts;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace ServiceStationBusinessLogic.BusinessLogic
|
|
{
|
|
public class WorkLogic : IWorkLogic {
|
|
|
|
private readonly ILogger _logger;
|
|
private readonly IWorkStorage _storage;
|
|
|
|
public WorkLogic(ILogger<IWorkLogic> logger, IWorkStorage storage) {
|
|
_logger = logger;
|
|
_storage = storage;
|
|
}
|
|
|
|
public bool Create(WorkBindingModel model) {
|
|
CheckModel(model);
|
|
if (_storage.Insert(model) == null) {
|
|
_logger.LogInformation("Insert operation failed");
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
public bool Update(WorkBindingModel model) {
|
|
CheckModel(model);
|
|
if (_storage.Update(model) == null) {
|
|
_logger.LogInformation("Insert operation failed");
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
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.TaskByWorkId.ToString())) {
|
|
throw new ArgumentNullException("Нет номера задачи", nameof(model.TaskByWorkId));
|
|
}
|
|
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.TaskByWorkId}." +
|
|
$"ClientList.Count:{model.ClientList.Count}");
|
|
}
|
|
}
|
|
}
|