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

96 lines
3.2 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 ServiceStationDataModels.Enums;
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 {
private readonly ILogger _logger;
private readonly IWorkStorage _storage;
private WorkLogic(ILogger logger, IWorkStorage storage) {
_logger = logger;
_storage = storage;
}
public bool Create(WorkBindingModel model) {
CheckModel(model);
model.Status = WorkStatus.Принят;
if (_storage.Insert(model) == null) {
_logger.LogInformation("Insert operation failed");
return false;
}
return true;
}
public bool FinishWork(WorkBindingModel model) {
return StatusUpdate(model, WorkStatus.Готов);
}
public bool IssuedWork(WorkBindingModel model) {
return StatusUpdate(model, WorkStatus.Выдан);
}
public bool TakeWorkInWork(WorkBindingModel model) {
return StatusUpdate(model, WorkStatus.Выполняется);
}
public List<WorkViewModel>? ReadList(WorkSearchModel? model = null) {
throw new NotImplementedException();
}
private void CheckModel(WorkBindingModel 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.ReportId).ToString())) {
throw new ArgumentNullException("Нет Id работы", nameof(model.ReportId));
}
if (string.IsNullOrEmpty((model.CategoryWorkId).ToString())) {
throw new ArgumentNullException("Нет Id категории работы", nameof(model.CategoryWorkId));
}
if (model.Price <= 0) {
throw new ArgumentNullException("Цена за работу должна быть больше 0", nameof(model.Price));
}
_logger.LogInformation($"Work. Id:{model.Id}.Price.{model.Price}.Status:{model.Status}.ExecutorId:{model.ExecutorId}" +
$"ReportId:{model.ReportId}.CategoryWordId:{model.CategoryWorkId}");
}
private bool StatusUpdate(WorkBindingModel model, WorkStatus newOrderStatus) {
CheckModel(model, false);
var viewModel = _storage.GetElement(new WorkSearchModel { Id = model.Id });
if (viewModel == null) {
throw new ArgumentNullException(nameof(model));
}
if (viewModel.Status + 1 != newOrderStatus) {
_logger.LogWarning("Status update to " + newOrderStatus.ToString() + " operation failed.");
return false;
}
model.Status = newOrderStatus;
if (_storage.Update(model) == null) {
_logger.LogWarning("Update operarion failed");
return false;
}
return true;
}
}
}