add real implementations

This commit is contained in:
Ivan Gutorov 2025-02-16 21:31:02 +04:00
parent 3a1b867c29
commit e69e5b7f67
5 changed files with 267 additions and 18 deletions

View File

@ -1,7 +1,10 @@
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using System.Text.Json;
using TheCyclopsContracts.BusinessLogicContracts; using TheCyclopsContracts.BusinessLogicContracts;
using TheCyclopsContracts.DataModels; using TheCyclopsContracts.DataModels;
using TheCyclopsContracts.Enums; using TheCyclopsContracts.Enums;
using TheCyclopsContracts.Exceptions;
using TheCyclopsContracts.Extensions;
using TheCyclopsContracts.StoragesContracts; using TheCyclopsContracts.StoragesContracts;
namespace TheCyclopsBusinessLogic.Implementations; namespace TheCyclopsBusinessLogic.Implementations;
@ -11,25 +14,53 @@ internal class ComponentBusinessLogicContract(IComponentStorageContract componen
private readonly ILogger _logger = logger; private readonly ILogger _logger = logger;
private readonly IComponentStorageContract _componentStorageContract = componentStorageContract; private readonly IComponentStorageContract _componentStorageContract = componentStorageContract;
public List<ComponentDataModel> GetAllComponents(bool onlyActive = true) public List<ComponentDataModel> GetAllComponents(bool onlyActive)
{ {
return []; _logger.LogInformation("GetAllComponents params: {onlyActive}", onlyActive);
return _componentStorageContract.GetList(onlyActive) ?? throw new NullListException();
} }
public ComponentDataModel GetComponentByData(string data) public ComponentDataModel GetComponentByData(string data)
{ {
return new ComponentDataModel("", "", ComponentType.None, 0, false); _logger.LogInformation("Get element by data: {data}", data);
if (data.IsEmpty())
{
throw new ArgumentNullException(nameof(data));
}
if (data.IsGuid())
{
return _componentStorageContract.GetElementById(data) ?? throw new ElementNotFoundException(data);
}
return _componentStorageContract.GetElementByName(data) ?? throw new ElementNotFoundException(data);
} }
public void InsertComponent(ComponentDataModel componentDataModel) public void InsertComponent(ComponentDataModel componentDataModel)
{ {
_logger.LogInformation("New data: {json}", JsonSerializer.Serialize(componentDataModel));
ArgumentNullException.ThrowIfNull(componentDataModel);
componentDataModel.Validate();
_componentStorageContract.AddElement(componentDataModel);
} }
public void UpdateComponent(ComponentDataModel componentDataModel) public void UpdateComponent(ComponentDataModel componentDataModel)
{ {
_logger.LogInformation("Update data: {json}", JsonSerializer.Serialize(componentDataModel));
ArgumentNullException.ThrowIfNull(componentDataModel);
componentDataModel.Validate();
_componentStorageContract.UpdElement(componentDataModel);
} }
public void DeleteComponent(string id) public void DeleteComponent(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");
}
_componentStorageContract.DelElement(id);
} }
} }

View File

@ -1,6 +1,9 @@
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using System.Text.Json;
using TheCyclopsContracts.BusinessLogicContracts; using TheCyclopsContracts.BusinessLogicContracts;
using TheCyclopsContracts.DataModels; using TheCyclopsContracts.DataModels;
using TheCyclopsContracts.Exceptions;
using TheCyclopsContracts.Extensions;
using TheCyclopsContracts.StoragesContracts; using TheCyclopsContracts.StoragesContracts;
namespace TheCyclopsBusinessLogic.Implementations; namespace TheCyclopsBusinessLogic.Implementations;
@ -12,38 +15,85 @@ internal class EmployeeBusinessLogicContract(IEmployeeStorageContract employeeSt
public List<EmployeeDataModel> GetAllEmployees(bool onlyActive = true) public List<EmployeeDataModel> GetAllEmployees(bool onlyActive = true)
{ {
return []; _logger.LogInformation("GetAllEmployees params: {onlyActive}", onlyActive);
return _employeeStorageContract.GetList(onlyActive) ?? throw new NullListException();
} }
public List<EmployeeDataModel> GetAllEmployeesByPost(string postId, bool onlyActive = true) public List<EmployeeDataModel> GetAllEmployeesByPost(string postId, bool onlyActive = true)
{ {
return []; _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<EmployeeDataModel> GetAllEmployeesByBirthDate(DateTime fromDate, DateTime toDate, bool onlyActive = true) public List<EmployeeDataModel> GetAllEmployeesByBirthDate(DateTime fromDate, DateTime toDate, bool onlyActive = true)
{ {
return []; _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<EmployeeDataModel> GetAllEmployeesByEmploymentDate(DateTime fromDate, DateTime toDate, bool onlyActive = true) public List<EmployeeDataModel> GetAllEmployeesByEmploymentDate(DateTime fromDate, DateTime toDate, bool onlyActive = true)
{ {
return []; _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) public EmployeeDataModel GetEmployeeByData(string data)
{ {
return new EmployeeDataModel("", "", "", "", DateTime.Now, DateTime.Now, false); _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) 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) 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) 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);
} }
} }

View File

@ -1,6 +1,9 @@
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using System.Text.Json;
using TheCyclopsContracts.BusinessLogicContracts; using TheCyclopsContracts.BusinessLogicContracts;
using TheCyclopsContracts.DataModels; using TheCyclopsContracts.DataModels;
using TheCyclopsContracts.Exceptions;
using TheCyclopsContracts.Extensions;
using TheCyclopsContracts.StoragesContracts; using TheCyclopsContracts.StoragesContracts;
namespace TheCyclopsBusinessLogic.Implementations; namespace TheCyclopsBusinessLogic.Implementations;
@ -10,31 +13,112 @@ internal class InstallationBusinessLogicContract(IInstallationStorageContract in
private readonly ILogger _logger = logger; private readonly ILogger _logger = logger;
private readonly IInstallationStorageContract _installationStorageContract = installationStorageContract; private readonly IInstallationStorageContract _installationStorageContract = installationStorageContract;
//public List<InstallationDataModel> GetAllInstallationsByPeriod(DateTime fromDate, DateTime toDate)
//{
// return [];
//}
//public List<InstallationDataModel> GetAllInstallationsByEmployeeByPeriod(string employeeId, DateTime fromDate, DateTime toDate)
//{
// return [];
//}
//public List<InstallationDataModel> GetAllInstallationsByComponentByPeriod(string componentId, DateTime fromDate, DateTime toDate)
//{
// return [];
//}
//public InstallationDataModel GetInstallationByData(string data)
//{
// return new InstallationDataModel("", "", DateTime.Now, 0, 0, false, []);
//}
//public void InsertInstallation(InstallationDataModel installationDataModel)
//{
//}
//public void CancelInstallation(string id)
//{
//}
public List<InstallationDataModel> GetAllInstallationsByPeriod(DateTime fromDate, DateTime toDate) public List<InstallationDataModel> GetAllInstallationsByPeriod(DateTime fromDate, DateTime toDate)
{ {
return []; _logger.LogInformation("GetAllInstallations params: {fromDate}, {toDate}", fromDate, toDate);
if (fromDate.IsDateNotOlder(toDate))
{
throw new IncorrectDatesException(fromDate, toDate);
}
return _installationStorageContract.GetList(fromDate, toDate) ?? throw new NullListException();
} }
public List<InstallationDataModel> GetAllInstallationsByEmployeeByPeriod(string employeeId, DateTime fromDate, DateTime toDate) public List<InstallationDataModel> GetAllInstallationsByEmployeeByPeriod(string employeeId, DateTime fromDate, DateTime toDate)
{ {
return []; _logger.LogInformation("GetAllInstallations params: {employeeId}, {fromDate}, {toDate}", employeeId, fromDate, toDate);
if (fromDate.IsDateNotOlder(toDate))
{
throw new IncorrectDatesException(fromDate, toDate);
}
if (employeeId.IsEmpty())
{
throw new ArgumentNullException(nameof(employeeId));
}
if (!employeeId.IsGuid())
{
throw new ValidationException("The value in the field employeeId is not a unique identifier.");
}
return _installationStorageContract.GetList(fromDate, toDate, employeeId: employeeId) ?? throw new NullListException();
} }
public List<InstallationDataModel> GetAllInstallationsByComponentByPeriod(string componentId, DateTime fromDate, DateTime toDate) public List<InstallationDataModel> GetAllInstallationsByComponentByPeriod(string componentId, DateTime fromDate, DateTime toDate)
{ {
return []; _logger.LogInformation("GetAllInstallations params: {componentId}, {fromDate}, {toDate}", componentId, fromDate, toDate);
if (fromDate.IsDateNotOlder(toDate))
{
throw new IncorrectDatesException(fromDate, toDate);
}
if (componentId.IsEmpty())
{
throw new ArgumentNullException(nameof(componentId));
}
if (!componentId.IsGuid())
{
throw new ValidationException("The value in the field componentId is not a unique identifier.");
}
return _installationStorageContract.GetList(fromDate, toDate, componentId: componentId) ?? throw new NullListException();
} }
public InstallationDataModel GetInstallationByData(string data) public InstallationDataModel GetInstallationByData(string data)
{ {
return new InstallationDataModel("", "", DateTime.Now, 0, 0, false, []); _logger.LogInformation("Get element by data: {data}", data);
if (data.IsEmpty())
{
throw new ArgumentNullException(nameof(data));
}
if (!data.IsGuid())
{
throw new ValidationException("Id is not a unique identifier");
}
return _installationStorageContract.GetElementById(data) ?? throw new ElementNotFoundException(data);
} }
public void InsertInstallation(InstallationDataModel installationDataModel) public void InsertInstallation(InstallationDataModel installationDataModel)
{ {
_logger.LogInformation("New data: {json}", JsonSerializer.Serialize(installationDataModel));
ArgumentNullException.ThrowIfNull(installationDataModel);
installationDataModel.Validate();
_installationStorageContract.AddElement(installationDataModel);
} }
public void CancelInstallation(string id) public void CancelInstallation(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");
}
_installationStorageContract.DelElement(id);
} }
} }

View File

@ -1,7 +1,10 @@
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using System.Text.Json;
using TheCyclopsContracts.BusinessLogicContracts; using TheCyclopsContracts.BusinessLogicContracts;
using TheCyclopsContracts.DataModels; using TheCyclopsContracts.DataModels;
using TheCyclopsContracts.Enums; using TheCyclopsContracts.Enums;
using TheCyclopsContracts.Exceptions;
using TheCyclopsContracts.Extensions;
using TheCyclopsContracts.StoragesContracts; using TheCyclopsContracts.StoragesContracts;
namespace TheCyclopsBusinessLogic.Implementations; namespace TheCyclopsBusinessLogic.Implementations;
@ -11,34 +14,81 @@ internal class PostBusinessLogicContract(IPostStorageContract postStorageContrac
private readonly ILogger _logger = logger; private readonly ILogger _logger = logger;
private readonly IPostStorageContract _postStorageContract = postStorageContract; private readonly IPostStorageContract _postStorageContract = postStorageContract;
public List<PostDataModel> GetAllPosts(bool onlyActive) public List<PostDataModel> GetAllPosts(bool onlyActive = true)
{ {
return []; _logger.LogInformation("GetAllPosts params: {onlyActive}", onlyActive);
return _postStorageContract.GetList(onlyActive) ?? throw new NullListException();
} }
public List<PostDataModel> GetAllDataOfPost(string postId) public List<PostDataModel> GetAllDataOfPost(string postId)
{ {
return []; _logger.LogInformation("GetAllDataOfPost for {postId}", postId);
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 _postStorageContract.GetPostWithHistory(postId) ?? throw new NullListException();
} }
public PostDataModel GetPostByData(string data) public PostDataModel GetPostByData(string data)
{ {
return new PostDataModel("", "", PostType.None, 0, false, DateTime.Now); _logger.LogInformation("Get element by data: {data}", data);
if (data.IsEmpty())
{
throw new ArgumentNullException(nameof(data));
}
if (data.IsGuid())
{
return _postStorageContract.GetElementById(data) ?? throw new ElementNotFoundException(data);
}
return _postStorageContract.GetElementByName(data) ?? throw new ElementNotFoundException(data);
} }
public void InsertPost(PostDataModel postDataModel) public void InsertPost(PostDataModel postDataModel)
{ {
_logger.LogInformation("New data: {json}", JsonSerializer.Serialize(postDataModel));
ArgumentNullException.ThrowIfNull(postDataModel);
postDataModel.Validate();
_postStorageContract.AddElement(postDataModel);
} }
public void UpdatePost(PostDataModel postDataModel) public void UpdatePost(PostDataModel postDataModel)
{ {
_logger.LogInformation("Update data: {json}", JsonSerializer.Serialize(postDataModel));
ArgumentNullException.ThrowIfNull(postDataModel);
postDataModel.Validate();
_postStorageContract.UpdElement(postDataModel);
} }
public void DeletePost(string id) public void DeletePost(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");
}
_postStorageContract.DelElement(id);
} }
public void RestorePost(string id) public void RestorePost(string id)
{ {
_logger.LogInformation("Restore by id: {id}", id);
if (id.IsEmpty())
{
throw new ArgumentNullException(nameof(id));
}
if (!id.IsGuid())
{
throw new ValidationException("Id is not a unique identifier");
}
_postStorageContract.ResElement(id);
} }
} }

View File

@ -1,6 +1,8 @@
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using TheCyclopsContracts.BusinessLogicContracts; using TheCyclopsContracts.BusinessLogicContracts;
using TheCyclopsContracts.DataModels; using TheCyclopsContracts.DataModels;
using TheCyclopsContracts.Exceptions;
using TheCyclopsContracts.Extensions;
using TheCyclopsContracts.StoragesContracts; using TheCyclopsContracts.StoragesContracts;
namespace TheCyclopsBusinessLogic.Implementations; namespace TheCyclopsBusinessLogic.Implementations;
@ -18,15 +20,47 @@ internal class SalaryBusinessLogicContract(ISalaryStorageContract salaryStorageC
public List<SalaryDataModel> GetAllSalariesByPeriod(DateTime fromDate, DateTime toDate) public List<SalaryDataModel> GetAllSalariesByPeriod(DateTime fromDate, DateTime toDate)
{ {
return []; _logger.LogInformation("GetAllSalaries params: {fromDate}, {toDate}", fromDate, toDate);
if (fromDate.IsDateNotOlder(toDate))
{
throw new IncorrectDatesException(fromDate, toDate);
}
return _salaryStorageContract.GetList(fromDate, toDate) ?? throw new NullListException();
} }
public List<SalaryDataModel> GetAllSalariesByPeriodByEmployee(DateTime fromDate, DateTime toDate, string employeeId) public List<SalaryDataModel> GetAllSalariesByPeriodByEmployee(DateTime fromDate, DateTime toDate, string employeeId)
{ {
return []; if (fromDate.IsDateNotOlder(toDate))
{
throw new IncorrectDatesException(fromDate, toDate);
}
if (employeeId.IsEmpty())
{
throw new ArgumentNullException(nameof(employeeId));
}
if (!employeeId.IsGuid())
{
throw new ValidationException("The value in the field employeeId is not a unique identifier.");
}
_logger.LogInformation("GetAllSalaries params: {fromDate}, {toDate}, {employeeId}", fromDate, toDate, employeeId);
return _salaryStorageContract.GetList(fromDate, toDate, employeeId) ?? throw new NullListException();
} }
public void CalculateSalaryByMounth(DateTime date) public void CalculateSalaryByMounth(DateTime date)
{ {
_logger.LogInformation("CalculateSalaryByMounth: {date}", date);
var startDate = new DateTime(date.Year, date.Month, 1);
var finishDate = new DateTime(date.Year, date.Month, DateTime.DaysInMonth(date.Year, date.Month));
var employees = _employeeStorageContract.GetList() ?? throw new NullListException();
foreach (var employee in employees)
{
var installations = _installationStorageContract.GetList(startDate, finishDate, employeeId: employee.Id)?.Sum(x => x.InstallationPrice) ??
throw new NullListException();
var post = _postStorageContract.GetElementById(employee.PostId) ??
throw new NullListException();
var salary = post.Salary + installations * 0.1;
_logger.LogDebug("The employee {employeeId} was paid a salary of {salary}", employee.Id, salary);
_salaryStorageContract.AddElement(new SalaryDataModel(employee.Id, finishDate, salary));
}
} }
} }