307 lines
11 KiB
C#
307 lines
11 KiB
C#
using AutoMapper;
|
|
using SmallSoftwareContracts.AdapterContracts.OperationResponses;
|
|
using SmallSoftwareContracts.AdapterContracts;
|
|
using SmallSoftwareContracts.BindingModels;
|
|
using SmallSoftwareContracts.BusinessLogicsContracts;
|
|
using SmallSoftwareContracts.DataModels;
|
|
using SmallSoftwareContracts.Exceptions;
|
|
using SmallSoftwareContracts.ViewModels;
|
|
using SmallSoftwareDatabase.Models;
|
|
using static System.Runtime.InteropServices.JavaScript.JSType;
|
|
using System.Text.Json;
|
|
|
|
namespace SmallSoftwareWebApi.Adapters;
|
|
|
|
|
|
public class WorkerAdapter : IWorkerAdapter
|
|
{
|
|
private readonly IWorkerBusinessLogicContract _workerBusinessLogicContract;
|
|
private readonly ILogger _logger;
|
|
private readonly Mapper _mapper;
|
|
private readonly JsonSerializerOptions JsonSerializerOptions = new()
|
|
{
|
|
PropertyNameCaseInsensitive = true
|
|
};
|
|
|
|
public WorkerAdapter(IWorkerBusinessLogicContract
|
|
workerBusinessLogicContract, ILogger<WorkerAdapter> logger)
|
|
{
|
|
_workerBusinessLogicContract = workerBusinessLogicContract;
|
|
_logger = logger;
|
|
var config = new MapperConfiguration(cfg =>
|
|
{
|
|
cfg.CreateMap<WorkerBindingModel, WorkerDataModel>();
|
|
cfg.CreateMap<WorkerDataModel, WorkerViewModel>();
|
|
cfg.CreateMap<WorkerDataModel, WorkerViewModel>().ForMember(x => x.Configuration, x => x.MapFrom(src =>
|
|
JsonSerializer.Serialize(src.ConfigurationModel, JsonSerializerOptions)));
|
|
});
|
|
_mapper = new Mapper(config);
|
|
}
|
|
public WorkerOperationResponse GetList(bool includeDeleted)
|
|
{
|
|
try
|
|
{
|
|
return WorkerOperationResponse.OK([.. _workerBusinessLogicContract.GetAllWorkers(!includeDeleted)
|
|
.Select(x => _mapper.Map<WorkerViewModel>(x))]);
|
|
}
|
|
catch (NullListException)
|
|
{
|
|
_logger.LogError("NullListException");
|
|
return WorkerOperationResponse.NotFound("The list is not initialized");
|
|
}
|
|
catch (StorageException ex)
|
|
{
|
|
_logger.LogError(ex, "StorageException");
|
|
return WorkerOperationResponse
|
|
.InternalServerError($"Error while working with data storage: { ex.InnerException!.Message}");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Exception");
|
|
return
|
|
WorkerOperationResponse.InternalServerError(ex.Message);
|
|
}
|
|
}
|
|
public WorkerOperationResponse GetPostList(string id, bool includeDeleted)
|
|
{
|
|
try
|
|
{
|
|
return WorkerOperationResponse.OK([..
|
|
_workerBusinessLogicContract.GetAllWorkersByPost(id, !includeDeleted).Select(x => _mapper.Map<WorkerViewModel>(x))]);
|
|
}
|
|
catch (ValidationException ex)
|
|
{
|
|
_logger.LogError(ex, "ValidationException");
|
|
return WorkerOperationResponse.BadRequest($"Incorrect data transmitted: { ex.Message}");
|
|
}
|
|
catch (NullListException)
|
|
{
|
|
_logger.LogError("NullListException");
|
|
return WorkerOperationResponse.NotFound("The list is not initialized");
|
|
}
|
|
catch (StorageException ex)
|
|
{
|
|
_logger.LogError(ex, "StorageException");
|
|
return WorkerOperationResponse
|
|
.InternalServerError($"Error while working with data storage: { ex.InnerException!.Message}");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Exception");
|
|
return
|
|
WorkerOperationResponse.InternalServerError(ex.Message);
|
|
}
|
|
}
|
|
public WorkerOperationResponse GetListByBirthDate(DateTime fromDate,
|
|
DateTime toDate, bool includeDeleted)
|
|
{
|
|
try
|
|
{
|
|
return WorkerOperationResponse.OK([..
|
|
_workerBusinessLogicContract.GetAllWorkersByBirthDate(fromDate, toDate,
|
|
!includeDeleted).Select(x => _mapper.Map<WorkerViewModel>(x))]);
|
|
}
|
|
catch (IncorrectDatesException ex)
|
|
{
|
|
_logger.LogError(ex, "IncorrectDatesException");
|
|
return WorkerOperationResponse.BadRequest($"Incorrect dates: { ex.Message}");
|
|
}
|
|
catch (NullListException)
|
|
{
|
|
_logger.LogError("NullListException");
|
|
return WorkerOperationResponse.NotFound("The list is not initialized");
|
|
}
|
|
catch (StorageException ex)
|
|
{
|
|
_logger.LogError(ex, "StorageException");
|
|
return WorkerOperationResponse
|
|
.InternalServerError($"Error while working with data storage: { ex.InnerException!.Message}");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Exception");
|
|
return
|
|
WorkerOperationResponse.InternalServerError(ex.Message);
|
|
}
|
|
}
|
|
public WorkerOperationResponse GetListByEmploymentDate(DateTime fromDate,
|
|
DateTime toDate, bool includeDeleted)
|
|
{
|
|
try
|
|
{
|
|
return WorkerOperationResponse.OK([..
|
|
_workerBusinessLogicContract.GetAllWorkersByEmploymentDate(fromDate, toDate,
|
|
!includeDeleted).Select(x => _mapper.Map<WorkerViewModel>(x))]);
|
|
}
|
|
catch (IncorrectDatesException ex)
|
|
{
|
|
_logger.LogError(ex, "IncorrectDatesException");
|
|
return WorkerOperationResponse.BadRequest($"Incorrect dates: { ex.Message}");
|
|
}
|
|
catch (NullListException)
|
|
{
|
|
_logger.LogError("NullListException");
|
|
return WorkerOperationResponse.NotFound("The list is not initialized");
|
|
}
|
|
catch (StorageException ex)
|
|
{
|
|
_logger.LogError(ex, "StorageException");
|
|
return WorkerOperationResponse
|
|
.InternalServerError($"Error while working with data storage: { ex.InnerException!.Message}");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Exception");
|
|
return
|
|
WorkerOperationResponse.InternalServerError(ex.Message);
|
|
}
|
|
}
|
|
public WorkerOperationResponse GetElement(string data)
|
|
{
|
|
try
|
|
{
|
|
return
|
|
WorkerOperationResponse.OK(_mapper.Map<WorkerViewModel>(_workerBusinessLogicContract.GetWorkerByData(data)));
|
|
}
|
|
catch (ArgumentNullException ex)
|
|
{
|
|
_logger.LogError(ex, "ArgumentNullException");
|
|
return WorkerOperationResponse.BadRequest("Data is empty");
|
|
}
|
|
catch (ValidationException ex)
|
|
{
|
|
_logger.LogError(ex, "ValidationException");
|
|
return WorkerOperationResponse.BadRequest($"Incorrect data transmitted: { ex.Message}");
|
|
}
|
|
catch (ElementNotFoundException ex)
|
|
{
|
|
_logger.LogError(ex, "ElementNotFoundException");
|
|
return WorkerOperationResponse.NotFound($"Not found element by data { data}");
|
|
}
|
|
catch (StorageException ex)
|
|
{
|
|
_logger.LogError(ex, "StorageException");
|
|
return WorkerOperationResponse
|
|
.InternalServerError($"Error while working with data storage: { ex.InnerException!.Message}");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Exception");
|
|
return
|
|
WorkerOperationResponse.InternalServerError(ex.Message);
|
|
}
|
|
}
|
|
public WorkerOperationResponse RegisterWorker(WorkerBindingModel
|
|
workerModel)
|
|
{
|
|
try
|
|
{
|
|
_workerBusinessLogicContract.InsertWorker(_mapper.Map<WorkerDataModel>(workerModel));
|
|
return WorkerOperationResponse.NoContent();
|
|
}
|
|
catch (ArgumentNullException ex)
|
|
{
|
|
_logger.LogError(ex, "ArgumentNullException");
|
|
return WorkerOperationResponse.BadRequest("Data is empty");
|
|
}
|
|
catch (ValidationException ex)
|
|
{
|
|
_logger.LogError(ex, "ValidationException");
|
|
return WorkerOperationResponse.BadRequest($"Incorrect data transmitted: { ex.Message}");
|
|
}
|
|
catch (ElementExistsException ex)
|
|
{
|
|
_logger.LogError(ex, "ElementExistsException");
|
|
return WorkerOperationResponse.BadRequest(ex.Message);
|
|
}
|
|
catch (StorageException ex)
|
|
{
|
|
_logger.LogError(ex, "StorageException");
|
|
|
|
return WorkerOperationResponse
|
|
.BadRequest($"Error while working with data storage: { ex.InnerException!.Message}");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Exception");
|
|
return
|
|
WorkerOperationResponse.InternalServerError(ex.Message);
|
|
}
|
|
}
|
|
public WorkerOperationResponse ChangeWorkerInfo(WorkerBindingModel
|
|
workerModel)
|
|
{
|
|
try
|
|
{
|
|
_workerBusinessLogicContract.UpdateWorker(_mapper.Map<WorkerDataModel>(workerModel));
|
|
return WorkerOperationResponse.NoContent();
|
|
}
|
|
catch (ArgumentNullException ex)
|
|
{
|
|
_logger.LogError(ex, "ArgumentNullException");
|
|
return WorkerOperationResponse.BadRequest("Data is empty");
|
|
}
|
|
catch (ValidationException ex)
|
|
{
|
|
_logger.LogError(ex, "ValidationException");
|
|
return WorkerOperationResponse.BadRequest($"Incorrect data transmitted: { ex.Message}");
|
|
}
|
|
catch (ElementNotFoundException ex)
|
|
{
|
|
_logger.LogError(ex, "ElementNotFoundException");
|
|
return WorkerOperationResponse.BadRequest($"Not found element by Id { workerModel.Id}");
|
|
}
|
|
catch (ElementExistsException ex)
|
|
{
|
|
_logger.LogError(ex, "ElementExistsException");
|
|
return WorkerOperationResponse.BadRequest(ex.Message);
|
|
}
|
|
catch (StorageException ex)
|
|
{
|
|
_logger.LogError(ex, "StorageException");
|
|
return WorkerOperationResponse
|
|
.BadRequest($"Error while working with data storage: { ex.InnerException!.Message}");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Exception");
|
|
return
|
|
WorkerOperationResponse.InternalServerError(ex.Message);
|
|
}
|
|
}
|
|
public WorkerOperationResponse RemoveWorker(string id)
|
|
{
|
|
try
|
|
{
|
|
_workerBusinessLogicContract.DeleteWorker(id);
|
|
return WorkerOperationResponse.NoContent();
|
|
}
|
|
catch (ArgumentNullException ex)
|
|
{
|
|
_logger.LogError(ex, "ArgumentNullException");
|
|
return WorkerOperationResponse.BadRequest("Id is empty");
|
|
}
|
|
catch (ValidationException ex)
|
|
{
|
|
_logger.LogError(ex, "ValidationException");
|
|
return WorkerOperationResponse.BadRequest($"Incorrect data transmitted: { ex.Message}");
|
|
}
|
|
catch (ElementNotFoundException ex)
|
|
{
|
|
_logger.LogError(ex, "ElementNotFoundException");
|
|
return WorkerOperationResponse.BadRequest($"Not found element by id: { id}");
|
|
}
|
|
catch (StorageException ex)
|
|
{
|
|
_logger.LogError(ex, "StorageException");
|
|
return WorkerOperationResponse
|
|
.BadRequest($"Error while working with data storage: { ex.InnerException!.Message}");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Exception");
|
|
return
|
|
WorkerOperationResponse.InternalServerError(ex.Message);
|
|
}
|
|
}
|
|
} |