From e69e5b7f67d8af5db6a1f07d64436c692552ae6e Mon Sep 17 00:00:00 2001 From: Ivan Gutorov Date: Sun, 16 Feb 2025 21:31:02 +0400 Subject: [PATCH] add real implementations --- .../ComponentBusinessLogicContract.cs | 37 +++++++- .../EmployeeBusinessLogicContract.cs | 60 +++++++++++- .../InstallationBusinessLogicContract.cs | 92 ++++++++++++++++++- .../PostBusinessLogicContract.cs | 58 +++++++++++- .../SalaryBusinessLogicContract.cs | 38 +++++++- 5 files changed, 267 insertions(+), 18 deletions(-) diff --git a/TheCyclopsProject/TheCyclopsBusinessLogic/Implementations/ComponentBusinessLogicContract.cs b/TheCyclopsProject/TheCyclopsBusinessLogic/Implementations/ComponentBusinessLogicContract.cs index 8c11d7a..7c5fa94 100644 --- a/TheCyclopsProject/TheCyclopsBusinessLogic/Implementations/ComponentBusinessLogicContract.cs +++ b/TheCyclopsProject/TheCyclopsBusinessLogic/Implementations/ComponentBusinessLogicContract.cs @@ -1,7 +1,10 @@ using Microsoft.Extensions.Logging; +using System.Text.Json; using TheCyclopsContracts.BusinessLogicContracts; using TheCyclopsContracts.DataModels; using TheCyclopsContracts.Enums; +using TheCyclopsContracts.Exceptions; +using TheCyclopsContracts.Extensions; using TheCyclopsContracts.StoragesContracts; namespace TheCyclopsBusinessLogic.Implementations; @@ -11,25 +14,53 @@ internal class ComponentBusinessLogicContract(IComponentStorageContract componen private readonly ILogger _logger = logger; private readonly IComponentStorageContract _componentStorageContract = componentStorageContract; - public List GetAllComponents(bool onlyActive = true) + public List GetAllComponents(bool onlyActive) { - return []; + _logger.LogInformation("GetAllComponents params: {onlyActive}", onlyActive); + return _componentStorageContract.GetList(onlyActive) ?? throw new NullListException(); } 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) { + _logger.LogInformation("New data: {json}", JsonSerializer.Serialize(componentDataModel)); + ArgumentNullException.ThrowIfNull(componentDataModel); + componentDataModel.Validate(); + _componentStorageContract.AddElement(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) { + _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); } } diff --git a/TheCyclopsProject/TheCyclopsBusinessLogic/Implementations/EmployeeBusinessLogicContract.cs b/TheCyclopsProject/TheCyclopsBusinessLogic/Implementations/EmployeeBusinessLogicContract.cs index f751d04..d6545da 100644 --- a/TheCyclopsProject/TheCyclopsBusinessLogic/Implementations/EmployeeBusinessLogicContract.cs +++ b/TheCyclopsProject/TheCyclopsBusinessLogic/Implementations/EmployeeBusinessLogicContract.cs @@ -1,6 +1,9 @@ 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; @@ -12,38 +15,85 @@ internal class EmployeeBusinessLogicContract(IEmployeeStorageContract employeeSt public List GetAllEmployees(bool onlyActive = true) { - return []; + _logger.LogInformation("GetAllEmployees params: {onlyActive}", onlyActive); + return _employeeStorageContract.GetList(onlyActive) ?? throw new NullListException(); } public List 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 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 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) { - 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) { + _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); } } diff --git a/TheCyclopsProject/TheCyclopsBusinessLogic/Implementations/InstallationBusinessLogicContract.cs b/TheCyclopsProject/TheCyclopsBusinessLogic/Implementations/InstallationBusinessLogicContract.cs index ea93dcb..f17e5a0 100644 --- a/TheCyclopsProject/TheCyclopsBusinessLogic/Implementations/InstallationBusinessLogicContract.cs +++ b/TheCyclopsProject/TheCyclopsBusinessLogic/Implementations/InstallationBusinessLogicContract.cs @@ -1,6 +1,9 @@ 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; @@ -10,31 +13,112 @@ internal class InstallationBusinessLogicContract(IInstallationStorageContract in private readonly ILogger _logger = logger; private readonly IInstallationStorageContract _installationStorageContract = installationStorageContract; + //public List GetAllInstallationsByPeriod(DateTime fromDate, DateTime toDate) + //{ + // return []; + //} + + //public List GetAllInstallationsByEmployeeByPeriod(string employeeId, DateTime fromDate, DateTime toDate) + //{ + // return []; + //} + + //public List 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 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 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 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) { - 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) { + _logger.LogInformation("New data: {json}", JsonSerializer.Serialize(installationDataModel)); + ArgumentNullException.ThrowIfNull(installationDataModel); + installationDataModel.Validate(); + _installationStorageContract.AddElement(installationDataModel); } 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); } } diff --git a/TheCyclopsProject/TheCyclopsBusinessLogic/Implementations/PostBusinessLogicContract.cs b/TheCyclopsProject/TheCyclopsBusinessLogic/Implementations/PostBusinessLogicContract.cs index 57cd823..9fd6bd9 100644 --- a/TheCyclopsProject/TheCyclopsBusinessLogic/Implementations/PostBusinessLogicContract.cs +++ b/TheCyclopsProject/TheCyclopsBusinessLogic/Implementations/PostBusinessLogicContract.cs @@ -1,7 +1,10 @@ using Microsoft.Extensions.Logging; +using System.Text.Json; using TheCyclopsContracts.BusinessLogicContracts; using TheCyclopsContracts.DataModels; using TheCyclopsContracts.Enums; +using TheCyclopsContracts.Exceptions; +using TheCyclopsContracts.Extensions; using TheCyclopsContracts.StoragesContracts; namespace TheCyclopsBusinessLogic.Implementations; @@ -11,34 +14,81 @@ internal class PostBusinessLogicContract(IPostStorageContract postStorageContrac private readonly ILogger _logger = logger; private readonly IPostStorageContract _postStorageContract = postStorageContract; - public List GetAllPosts(bool onlyActive) + public List GetAllPosts(bool onlyActive = true) { - return []; + _logger.LogInformation("GetAllPosts params: {onlyActive}", onlyActive); + return _postStorageContract.GetList(onlyActive) ?? throw new NullListException(); } public List 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) { - 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) { + _logger.LogInformation("New data: {json}", JsonSerializer.Serialize(postDataModel)); + ArgumentNullException.ThrowIfNull(postDataModel); + postDataModel.Validate(); + _postStorageContract.AddElement(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) { + _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) { + _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); } } diff --git a/TheCyclopsProject/TheCyclopsBusinessLogic/Implementations/SalaryBusinessLogicContract.cs b/TheCyclopsProject/TheCyclopsBusinessLogic/Implementations/SalaryBusinessLogicContract.cs index 8cde41a..9b8015b 100644 --- a/TheCyclopsProject/TheCyclopsBusinessLogic/Implementations/SalaryBusinessLogicContract.cs +++ b/TheCyclopsProject/TheCyclopsBusinessLogic/Implementations/SalaryBusinessLogicContract.cs @@ -1,6 +1,8 @@ using Microsoft.Extensions.Logging; using TheCyclopsContracts.BusinessLogicContracts; using TheCyclopsContracts.DataModels; +using TheCyclopsContracts.Exceptions; +using TheCyclopsContracts.Extensions; using TheCyclopsContracts.StoragesContracts; namespace TheCyclopsBusinessLogic.Implementations; @@ -18,15 +20,47 @@ internal class SalaryBusinessLogicContract(ISalaryStorageContract salaryStorageC public List 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 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) { + _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)); + } } }