forked from slavaxom9k/PIBD-23_Fomichev_V.S._MagicCarpet
120 lines
4.4 KiB
C#
120 lines
4.4 KiB
C#
using AutoMapper;
|
|
using MagicCarpetContracts.AdapterContracts.OperationResponses;
|
|
using MagicCarpetContracts.AdapterContracts;
|
|
using MagicCarpetContracts.BuisnessLogicContracts;
|
|
using MagicCarpetContracts.Exceptions;
|
|
using MagicCarpetContracts.ViewModels;
|
|
using MagicCarpetContracts.DataModels;
|
|
|
|
namespace MagicCarpetWebApi.Adapters;
|
|
|
|
public class SalaryAdapter : ISalaryAdapter
|
|
{
|
|
private readonly ISalaryBusinessLogicContract _salaryBusinessLogicContract;
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
private readonly Mapper _mapper;
|
|
|
|
public SalaryAdapter(ISalaryBusinessLogicContract salaryBusinessLogicContract, ILogger<SalaryAdapter> logger)
|
|
{
|
|
_salaryBusinessLogicContract = salaryBusinessLogicContract;
|
|
_logger = logger;
|
|
var config = new MapperConfiguration(cfg =>
|
|
{
|
|
cfg.CreateMap<SalaryDataModel, SalaryViewModel>();
|
|
});
|
|
_mapper = new Mapper(config);
|
|
}
|
|
|
|
public SalaryOperationResponse GetListByPeriod(DateTime fromDate, DateTime toDate)
|
|
{
|
|
try
|
|
{
|
|
return SalaryOperationResponse.OK([.. _salaryBusinessLogicContract.GetAllSalariesByPeriod(fromDate, toDate).Select(x => _mapper.Map<SalaryViewModel>(x))]);
|
|
}
|
|
catch (ValidationException ex)
|
|
{
|
|
_logger.LogError(ex, "ValidationException");
|
|
return SalaryOperationResponse.BadRequest($"Incorrect data transmitted: {ex.Message}");
|
|
}
|
|
catch (IncorrectDatesException ex)
|
|
{
|
|
_logger.LogError(ex, "IncorrectDatesException");
|
|
return SalaryOperationResponse.BadRequest($"Incorrect dates: {ex.Message}");
|
|
}
|
|
catch (NullListException)
|
|
{
|
|
_logger.LogError("NullListException");
|
|
return SalaryOperationResponse.NotFound("The list is not initialized");
|
|
}
|
|
catch (StorageException ex)
|
|
{
|
|
_logger.LogError(ex, "StorageException");
|
|
return SalaryOperationResponse.InternalServerError($"Error while working with data storage: {ex.InnerException!.Message}");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Exception");
|
|
return SalaryOperationResponse.InternalServerError(ex.Message);
|
|
}
|
|
}
|
|
|
|
public SalaryOperationResponse GetListByPeriodByEmployee(DateTime fromDate, DateTime toDate, string workerId)
|
|
{
|
|
try
|
|
{
|
|
return SalaryOperationResponse.OK([.. _salaryBusinessLogicContract.GetAllSalariesByPeriodByEmployee(fromDate, toDate, workerId).Select(x => _mapper.Map<SalaryViewModel>(x))]);
|
|
}
|
|
catch (ValidationException ex)
|
|
{
|
|
_logger.LogError(ex, "ValidationException");
|
|
return SalaryOperationResponse.BadRequest($"Incorrect data transmitted: {ex.Message}");
|
|
}
|
|
catch (IncorrectDatesException ex)
|
|
{
|
|
_logger.LogError(ex, "IncorrectDatesException");
|
|
return SalaryOperationResponse.BadRequest($"Incorrect dates: {ex.Message}");
|
|
}
|
|
catch (NullListException)
|
|
{
|
|
_logger.LogError("NullListException");
|
|
return SalaryOperationResponse.NotFound("The list is not initialized");
|
|
}
|
|
catch (StorageException ex)
|
|
{
|
|
_logger.LogError(ex, "StorageException");
|
|
return SalaryOperationResponse.InternalServerError($"Error while working with data storage: {ex.InnerException!.Message}");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Exception");
|
|
return SalaryOperationResponse.InternalServerError(ex.Message);
|
|
}
|
|
}
|
|
|
|
public SalaryOperationResponse CalculateSalary(DateTime date)
|
|
{
|
|
try
|
|
{
|
|
_salaryBusinessLogicContract.CalculateSalaryByMounth(date);
|
|
return SalaryOperationResponse.NoContent();
|
|
}
|
|
catch (NullListException)
|
|
{
|
|
_logger.LogError("NullListException");
|
|
return SalaryOperationResponse.NotFound("The list is not initialized");
|
|
}
|
|
catch (StorageException ex)
|
|
{
|
|
_logger.LogError(ex, "StorageException");
|
|
return SalaryOperationResponse.InternalServerError($"Error while working with data storage: {ex.InnerException!.Message}");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Exception");
|
|
return SalaryOperationResponse.InternalServerError(ex.Message);
|
|
}
|
|
}
|
|
}
|