77 lines
2.6 KiB
C#
77 lines
2.6 KiB
C#
using Microsoft.Extensions.Logging;
|
|
using PimpMyRide.BusinessLogicsContracts;
|
|
using PimpMyRide.DataModels;
|
|
using PimpMyRide.Enums;
|
|
using PimpMyRide.Exceptions;
|
|
using PimpMyRide.Extensions;
|
|
using PimpMyRide.StoragesContracts;
|
|
using System.Text.Json;
|
|
|
|
namespace PimpMyRideBusinessLogic.Implementations;
|
|
|
|
internal class ServiceBusinessLogicContract(IServiceStorageContracts serviceStorageContracts, ILogger logger) : IServiceBusinessLogicContract
|
|
{
|
|
private readonly ILogger _logger = logger;
|
|
private readonly IServiceStorageContracts _serviceStorageContract = serviceStorageContracts;
|
|
|
|
public List<ServiceDataModel> GetAllServices()
|
|
{
|
|
_logger.LogInformation("GetAllServices");
|
|
return _serviceStorageContract.GetList() ?? throw new NullListException();
|
|
}
|
|
|
|
public List<ServiceDataModel> GetAllServicesByWork(string workerId, WorkType workType)
|
|
{
|
|
_logger.LogInformation("GetAllCars params: {workerId}, {workType}", workerId, workType);
|
|
if (workerId.IsEmpty())
|
|
{
|
|
throw new ArgumentNullException(nameof(workerId));
|
|
}
|
|
if (!workerId.IsGuid())
|
|
{
|
|
throw new ValidationException("The value in the field workerId is not an unique identifier.");
|
|
}
|
|
if (workType == WorkType.None)
|
|
{
|
|
throw new ArgumentNullException(nameof(workType));
|
|
}
|
|
return _serviceStorageContract.GetList(workerId, workType) ?? throw new NullListException();
|
|
}
|
|
|
|
public ServiceDataModel GetServiceByData(string data)
|
|
{
|
|
_logger.LogInformation("Get element by data: {data}", data);
|
|
if (data.IsEmpty())
|
|
{
|
|
throw new ArgumentNullException(nameof(data));
|
|
}
|
|
if (data.IsGuid())
|
|
{
|
|
return _serviceStorageContract.GetElementById(data) ?? throw new ElementNotFoundException(data);
|
|
}
|
|
throw new ValidationException("The value in the field data is not an unique identifier.");
|
|
}
|
|
|
|
public void InsertService(ServiceDataModel serviceDataModel)
|
|
{
|
|
_logger.LogInformation("New data: {json}", JsonSerializer.Serialize(serviceDataModel));
|
|
ArgumentNullException.ThrowIfNull(serviceDataModel);
|
|
serviceDataModel.Validate();
|
|
_serviceStorageContract.AddElement(serviceDataModel);
|
|
}
|
|
|
|
public void CancelService(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");
|
|
}
|
|
_serviceStorageContract.DelElement(id);
|
|
}
|
|
}
|