2025-02-21 00:57:12 +04:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using System;
|
2025-02-17 22:42:11 +04:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
2025-02-21 00:57:12 +04:00
|
|
|
|
using System.Text.Json;
|
|
|
|
|
using System.Text.RegularExpressions;
|
2025-02-17 22:42:11 +04:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using TheButcherShopContracts.BusinessLogicContracts;
|
|
|
|
|
using TheButcherShopContracts.DataModels;
|
2025-02-21 00:57:12 +04:00
|
|
|
|
using TheButcherShopContracts.Exceptions;
|
|
|
|
|
using TheButcherShopContracts.Extensions;
|
|
|
|
|
using TheButcherShopContracts.StoragesContracts;
|
2025-02-17 22:42:11 +04:00
|
|
|
|
|
|
|
|
|
namespace ButcherShopBusinessLogic.Implementations;
|
|
|
|
|
|
2025-02-21 00:57:12 +04:00
|
|
|
|
internal class WorkerBusinessLogicContract(IWorkerStorageContract workerStorageContract, ILogger logger) : IWorkerBusinessLogicContract
|
2025-02-17 22:42:11 +04:00
|
|
|
|
{
|
2025-02-21 00:57:12 +04:00
|
|
|
|
private readonly ILogger _logger = logger;
|
|
|
|
|
private readonly IWorkerStorageContract _workerStorageContract = workerStorageContract;
|
|
|
|
|
|
|
|
|
|
//5 из 5 пройдено
|
2025-02-17 22:42:11 +04:00
|
|
|
|
public List<WorkerDataModel> GetAllWorkers(bool onlyActive = true)
|
|
|
|
|
{
|
2025-02-21 00:57:12 +04:00
|
|
|
|
_logger.LogInformation("GetAllWorkers params: {onlyActive}", onlyActive);
|
|
|
|
|
return _workerStorageContract.GetList(onlyActive) ?? throw new NullListException();
|
2025-02-17 22:42:11 +04:00
|
|
|
|
}
|
2025-02-21 00:57:12 +04:00
|
|
|
|
|
|
|
|
|
//6 из 6 пройдено
|
|
|
|
|
public List<WorkerDataModel> GetAllWorkersByPost(string postId, bool onlyActive = true)
|
2025-02-17 22:42:11 +04:00
|
|
|
|
{
|
2025-02-21 00:57:12 +04:00
|
|
|
|
_logger.LogInformation("GetAllWorkers params: {postId}, {onlyActive},", postId, onlyActive);
|
|
|
|
|
if (postId.IsEmpty())
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(postId));
|
|
|
|
|
}
|
|
|
|
|
if (!postId.IsGuid())
|
|
|
|
|
{
|
|
|
|
|
throw new ValidationException("The value in the field postId is not a unique identifier.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (Regex.IsMatch(postId, @"^\+7[\s-]?\d{3}[\s-]?\d{3}[\s-]?\d{2}[\s-]?\d{2}$"))
|
|
|
|
|
{
|
|
|
|
|
return new List<WorkerDataModel>
|
|
|
|
|
{
|
|
|
|
|
_workerStorageContract.GetElementByPhoneNumber(postId) ?? throw new ElementNotFoundException(postId)
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return _workerStorageContract.GetList(onlyActive, postId) ?? throw new NullListException();
|
2025-02-17 22:42:11 +04:00
|
|
|
|
}
|
2025-02-21 00:57:12 +04:00
|
|
|
|
|
|
|
|
|
//5 из 5 пройдено
|
|
|
|
|
public List<WorkerDataModel> GetAllWorkersByBirthDate(DateTime fromDate, DateTime toDate, bool onlyActive = true)
|
2025-02-17 22:42:11 +04:00
|
|
|
|
{
|
2025-02-21 00:57:12 +04:00
|
|
|
|
_logger.LogInformation("GetAllWorkers params: {onlyActive}, {fromDate}, {toDate}", onlyActive, fromDate, toDate);
|
|
|
|
|
if (fromDate.IsDateNotOlder(toDate))
|
|
|
|
|
{
|
|
|
|
|
throw new IncorrectDatesException(fromDate, toDate);
|
|
|
|
|
}
|
|
|
|
|
return _workerStorageContract.GetList(onlyActive, fromBirthDate: fromDate, toBirthDate: toDate) ?? throw new NullListException();
|
2025-02-17 22:42:11 +04:00
|
|
|
|
}
|
2025-02-21 00:57:12 +04:00
|
|
|
|
|
|
|
|
|
//5 из 5 пройдено
|
|
|
|
|
public List<WorkerDataModel> GetAllWorkersByEmploymentDate(DateTime fromDate, DateTime toDate, bool onlyActive = true)
|
2025-02-17 22:42:11 +04:00
|
|
|
|
{
|
2025-02-21 00:57:12 +04:00
|
|
|
|
_logger.LogInformation("GetAllWorkers params: {onlyActive}, {fromDate}, {toDate}", onlyActive, fromDate, toDate);
|
|
|
|
|
if (fromDate.IsDateNotOlder(toDate))
|
|
|
|
|
{
|
|
|
|
|
throw new IncorrectDatesException(fromDate, toDate);
|
|
|
|
|
}
|
|
|
|
|
return _workerStorageContract.GetList(onlyActive, fromEmploymentDate: fromDate, toEmploymentDate: toDate) ?? throw new NullListException();
|
2025-02-17 22:42:11 +04:00
|
|
|
|
}
|
2025-02-21 00:57:12 +04:00
|
|
|
|
|
|
|
|
|
//5 из 8 пройдено
|
2025-02-17 22:42:11 +04:00
|
|
|
|
public WorkerDataModel GetWorkerByData(string data)
|
|
|
|
|
{
|
2025-02-21 00:57:12 +04:00
|
|
|
|
_logger.LogInformation("Get element by data: {data}", data);
|
|
|
|
|
if (data.IsEmpty())
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(data));
|
|
|
|
|
}
|
|
|
|
|
if (data.IsGuid())
|
|
|
|
|
{
|
|
|
|
|
return _workerStorageContract.GetElementById(data) ?? throw new ElementNotFoundException(data);
|
|
|
|
|
}
|
|
|
|
|
if (Regex.IsMatch(data, @"^\+7[\s-]?\d{3}[\s-]?\d{3}[\s-]?\d{2}[\s-]?\d{2}$"))
|
|
|
|
|
{
|
|
|
|
|
return _workerStorageContract.GetElementByPhoneNumber(data) ?? throw new ElementNotFoundException(data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return _workerStorageContract.GetElementByFIO(data) ?? throw new ElementNotFoundException(data);
|
2025-02-17 22:42:11 +04:00
|
|
|
|
}
|
2025-02-21 00:57:12 +04:00
|
|
|
|
|
|
|
|
|
//5 из 5 пройдено
|
2025-02-17 22:42:11 +04:00
|
|
|
|
public void InsertWorker(WorkerDataModel workerDataModel)
|
|
|
|
|
{
|
2025-02-21 00:57:12 +04:00
|
|
|
|
_logger.LogInformation("New data: {json}", JsonSerializer.Serialize(workerDataModel));
|
|
|
|
|
ArgumentNullException.ThrowIfNull(workerDataModel);
|
|
|
|
|
workerDataModel.Validate();
|
|
|
|
|
_workerStorageContract.AddElement(workerDataModel);
|
2025-02-17 22:42:11 +04:00
|
|
|
|
}
|
2025-02-21 00:57:12 +04:00
|
|
|
|
|
|
|
|
|
//5 из 5 пройдено
|
2025-02-17 22:42:11 +04:00
|
|
|
|
public void UpdateWorker(WorkerDataModel workerDataModel)
|
|
|
|
|
{
|
2025-02-21 00:57:12 +04:00
|
|
|
|
_logger.LogInformation("Update data: {json}", JsonSerializer.Serialize(workerDataModel));
|
|
|
|
|
ArgumentNullException.ThrowIfNull(workerDataModel);
|
|
|
|
|
workerDataModel.Validate();
|
|
|
|
|
_workerStorageContract.UpdElement(workerDataModel);
|
2025-02-17 22:42:11 +04:00
|
|
|
|
}
|
2025-02-21 00:57:12 +04:00
|
|
|
|
|
|
|
|
|
//5 из 5 пройдено
|
2025-02-17 22:42:11 +04:00
|
|
|
|
public void DeleteWorker(string id)
|
|
|
|
|
{
|
2025-02-21 00:57:12 +04:00
|
|
|
|
_logger.LogInformation("Delete by id: {id}", id);
|
|
|
|
|
if (id.IsEmpty())
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(id));
|
|
|
|
|
}
|
|
|
|
|
if (!id.IsGuid())
|
|
|
|
|
{
|
|
|
|
|
throw new ValidationException("Id is not a unique identifier");
|
|
|
|
|
}
|
|
|
|
|
_workerStorageContract.DelElement(id);
|
2025-02-17 22:42:11 +04:00
|
|
|
|
}
|
|
|
|
|
}
|