Files
Pibd-21_Semin_D.A._SmallSof…/SmallSoftwareProject/SmallSoftwareBusinessLogic/Implementations/RequestBusinessLogicContract.cs
2025-04-24 00:06:05 +04:00

112 lines
4.2 KiB
C#

using Microsoft.Extensions.Localization;
using Microsoft.Extensions.Logging;
using SmallSoftwareContracts.BusinessLogicsContracts;
using SmallSoftwareContracts.DataModels;
using SmallSoftwareContracts.Exceptions;
using SmallSoftwareContracts.Extensions;
using SmallSoftwareContracts.Resources;
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, IStringLocalizer<Messages> localizer, ILogger logger) : IRequestBusinessLogicContract
{
private readonly ILogger _logger = logger;
private readonly IRequestStorageContract _requestStorageContract =
requestStorageContract;
private readonly IStringLocalizer<Messages> _localizer = localizer;
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, _localizer);
}
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, _localizer);
}
if (workerId.IsEmpty())
{
throw new ArgumentNullException(nameof(workerId));
}
if (!workerId.IsGuid())
{
throw new ValidationException(string.Format(localizer["ValidationExceptionMessageNotAId"], "WorkerId"));
}
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, _localizer);
}
if (softwareId.IsEmpty())
{
throw new ArgumentNullException(nameof(softwareId));
}
if (!softwareId.IsGuid())
{
throw new ValidationException(string.Format(localizer["ValidationExceptionMessageNotAId"], "SoftwareId"));
}
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(string.Format(localizer["ValidationExceptionMessageNotAId"], "Id"));
}
return _requestStorageContract.GetElementById(data) ?? throw new ElementNotFoundException(data, _localizer);
}
public void InsertRequest(RequestDataModel requestDataModel)
{
_logger.LogInformation("New data: {json}",
JsonSerializer.Serialize(requestDataModel));
ArgumentNullException.ThrowIfNull(requestDataModel);
requestDataModel.Validate(_localizer);
_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(string.Format(localizer["ValidationExceptionMessageNotAId"], "Id"));
}
_requestStorageContract.DelElement(id);
}
}