using Microsoft.Extensions.Logging; using System.Text.Json; using TheCyclopsContracts.BusinessLogicContracts; using TheCyclopsContracts.DataModels; using TheCyclopsContracts.Exceptions; using TheCyclopsContracts.Extensions; using TheCyclopsContracts.StoragesContracts; namespace TheCyclopsBusinessLogic.Implementations; internal class EmployeeBusinessLogicContract(IEmployeeStorageContract employeeStorageContract, ILogger logger) : IEmployeeBusinessLogicContract { private readonly ILogger _logger = logger; private readonly IEmployeeStorageContract _employeeStorageContract = employeeStorageContract; public List GetAllEmployees(bool onlyActive = true) { _logger.LogInformation("GetAllEmployees params: {onlyActive}", onlyActive); return _employeeStorageContract.GetList(onlyActive) ?? throw new NullListException(); } public List GetAllEmployeesByPost(string postId, bool onlyActive = true) { _logger.LogInformation("GetAllEmployees 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."); } return _employeeStorageContract.GetList(onlyActive, postId) ?? throw new NullListException(); } public List GetAllEmployeesByBirthDate(DateTime fromDate, DateTime toDate, bool onlyActive = true) { _logger.LogInformation("GetAllEmployees params: {onlyActive}, {fromDate}, {toDate}", onlyActive, fromDate, toDate); if (fromDate.IsDateNotOlder(toDate)) { throw new IncorrectDatesException(fromDate, toDate); } return _employeeStorageContract.GetList(onlyActive, fromBirthDate: fromDate, toBirthDate: toDate) ?? throw new NullListException(); } public List GetAllEmployeesByEmploymentDate(DateTime fromDate, DateTime toDate, bool onlyActive = true) { _logger.LogInformation("GetAllEmployees params: {onlyActive}, {fromDate}, {toDate}", onlyActive, fromDate, toDate); if (fromDate.IsDateNotOlder(toDate)) { throw new IncorrectDatesException(fromDate, toDate); } return _employeeStorageContract.GetList(onlyActive, fromEmploymentDate: fromDate, toEmploymentDate: toDate) ?? throw new NullListException(); } public EmployeeDataModel GetEmployeeByData(string data) { _logger.LogInformation("Get element by data: {data}", data); if (data.IsEmpty()) { throw new ArgumentNullException(nameof(data)); } if (data.IsGuid()) { return _employeeStorageContract.GetElementById(data) ?? throw new ElementNotFoundException(data); } return _employeeStorageContract.GetElementByFIO(data) ?? throw new ElementNotFoundException(data); } public void InsertEmployee(EmployeeDataModel employeeDataModel) { _logger.LogInformation("New data: {json}", JsonSerializer.Serialize(employeeDataModel)); ArgumentNullException.ThrowIfNull(employeeDataModel); employeeDataModel.Validate(); _employeeStorageContract.AddElement(employeeDataModel); } public void UpdateEmployee(EmployeeDataModel employeeDataModel) { _logger.LogInformation("Update data: {json}", JsonSerializer.Serialize(employeeDataModel)); ArgumentNullException.ThrowIfNull(employeeDataModel); employeeDataModel.Validate(); _employeeStorageContract.UpdElement(employeeDataModel); } public void DeleteEmployee(string id) { _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"); } _employeeStorageContract.DelElement(id); } }