Files
Pibd-21_Semin_D.A._SmallSof…/SmallSoftwareProject/SmallSoftwareBusinessLogic/Implementations/RequestBusinessLogicContract.cs

109 lines
3.9 KiB
C#

using Microsoft.Extensions.Logging;
using SmallSoftwareContracts.BusinessLogicsContracts;
using SmallSoftwareContracts.DataModels;
using SmallSoftwareContracts.Exceptions;
using SmallSoftwareContracts.Extensions;
using SmallSoftwareContracts.StoragesContracts;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
namespace SmallSoftwareBusinessLogic.Implementations;
internal class RequestBusinessLogicContract(IRequestStorageContract requestStorageContract, ILogger logger) : IRequestBusinessLogicContract
{
private readonly ILogger _logger = logger;
private readonly IRequestStorageContract _requestStorageContract =
requestStorageContract;
public List<RequestDataModel> GetAllRequestsByPeriod(DateTime fromDate, DateTime toDate)
{
_logger.LogInformation("GetAllRequests params: {fromDate}, {toDate}", fromDate, toDate);
if (fromDate.IsDateNotOlder(toDate))
{
throw new IncorrectDatesException(fromDate, toDate);
}
return _requestStorageContract.GetList(fromDate, toDate) ?? throw new NullListException();
}
public List<RequestDataModel> GetAllRequestsByWorkerByPeriod(string workerId, DateTime fromDate, DateTime toDate)
{
_logger.LogInformation("GetAllRequests params: {workerId}, {fromDate}, { toDate} ", workerId, fromDate, toDate);
if (fromDate.IsDateNotOlder(toDate))
{
throw new IncorrectDatesException(fromDate, toDate);
}
if (workerId.IsEmpty())
{
throw new ArgumentNullException(nameof(workerId));
}
if (!workerId.IsGuid())
{
throw new ValidationException("The value in the field workerId is not a unique identifier.");
}
return _requestStorageContract.GetList(fromDate, toDate, workerId:
workerId) ?? throw new NullListException();
}
public List<RequestDataModel> GetAllRequestsBySoftwareByPeriod(string softwareId, DateTime fromDate, DateTime toDate)
{
_logger.LogInformation("GetAllRequests params: {softwareId}, {fromDate}, { toDate} ", softwareId, fromDate, toDate);
if (fromDate.IsDateNotOlder(toDate))
{
throw new IncorrectDatesException(fromDate, toDate);
}
if (softwareId.IsEmpty())
{
throw new ArgumentNullException(nameof(softwareId));
}
if (!softwareId.IsGuid())
{
throw new ValidationException("The value in the field softwareId is not a unique identifier.");
}
return _requestStorageContract.GetList(fromDate, toDate, softwareId:
softwareId) ?? throw new NullListException();
}
public RequestDataModel GetRequestByData(string data)
{
_logger.LogInformation("Get element by data: {data}", data);
if (data.IsEmpty())
{
throw new ArgumentNullException(nameof(data));
}
if (!data.IsGuid())
{
throw new ValidationException("Id is not a unique identifier");
}
return _requestStorageContract.GetElementById(data) ?? throw new ElementNotFoundException(data);
}
public void InsertRequest(RequestDataModel requestDataModel)
{
_logger.LogInformation("New data: {json}",
JsonSerializer.Serialize(requestDataModel));
ArgumentNullException.ThrowIfNull(requestDataModel);
requestDataModel.Validate();
_requestStorageContract.AddElement(requestDataModel);
}
public void CancelRequest(string id)
{
_logger.LogInformation("Cancel by id: {id}", id);
if (id.IsEmpty())
{
throw new ArgumentNullException(nameof(id));
}
if (!id.IsGuid())
{
throw new ValidationException("Id is not a unique identifier");
}
_requestStorageContract.DelElement(id);
}
}