diff --git a/TheSoftBedProject/SoftBedBusinessLogic/Implementations/CollectBusinessLogicContract.cs b/TheSoftBedProject/SoftBedBusinessLogic/Implementations/CollectBusinessLogicContract.cs index f395ef0..c5f2d03 100644 --- a/TheSoftBedProject/SoftBedBusinessLogic/Implementations/CollectBusinessLogicContract.cs +++ b/TheSoftBedProject/SoftBedBusinessLogic/Implementations/CollectBusinessLogicContract.cs @@ -1,31 +1,82 @@ -using SoftBedContracts.BusinessLogicsContracts; +using Microsoft.Extensions.Logging; +using SoftBedContracts.BusinessLogicsContracts; using SoftBedContracts.DataModels; +using SoftBedContracts.Exceptions; +using SoftBedContracts.Extensions; +using SoftBedContracts.StoragesContracts; +using System.Text.Json; namespace SoftBedBusinessLogic.Implementations; -internal class CollectBusinessLogicContract : ICollectBusinessLogicContract +internal class CollectBusinessLogicContract(ICollectStorageContract collectStorageContract, ILogger logger) : ICollectBusinessLogicContract { - public List GetAllCollectByPeriod(DateTime fromDate, DateTime toDate) + private readonly ILogger _logger = logger; + private readonly ICollectStorageContract _collectStorageContract = collectStorageContract; + public List GetAllCollectsByPeriod(DateTime fromDate, DateTime toDate) { - return []; + _logger.LogInformation("GetAllCollects params: {fromDate}, {toDate}", fromDate, toDate); + if (fromDate.IsDateNotOlder(toDate)) + { + throw new IncorrectDatesException(fromDate, toDate); + } + return _collectStorageContract.GetList(fromDate, toDate) ?? throw new NullListException(); } - public List GetAllCollectByWorkerByPeriod(string workerId, DateTime fromDate, DateTime toDate) + public List GetAllCollectsByWorkerByPeriod(string workerId, DateTime fromDate, DateTime toDate) { - return []; + _logger.LogInformation("GetAllCollects params: {workerId}, {fromDate}, {toDate}", workerId, fromDate, toDate); + if (fromDate.IsDateNotOlder(toDate)) + { + throw new IncorrectDatesException(fromDate, toDate); + } + if (workerId.IsEmpty()) + { + throw new ArgumentNullException(nameof(workerId)); + } + if (!workerId.IsGuid()) + { + throw new ValidationException("The value in the field workerId is not a unique identifier."); + } + return _collectStorageContract.GetList(fromDate, toDate, workerId: workerId) ?? throw new NullListException(); } public List GetAllCollectsByFurnitureByPeriod(string furnitureId, DateTime fromDate, DateTime toDate) { - return []; + _logger.LogInformation("GetAllCollects params: {buyerId}, {fromDate}, {toDate}", furnitureId, fromDate, toDate); + if (fromDate.IsDateNotOlder(toDate)) + { + throw new IncorrectDatesException(fromDate, toDate); + } + if (furnitureId.IsEmpty()) + { + throw new ArgumentNullException(nameof(furnitureId)); + } + if (!furnitureId.IsGuid()) + { + throw new ValidationException("The value in the field buyerId is not a unique identifier."); + } + return _collectStorageContract.GetList(fromDate, toDate, furnitureId: furnitureId) ?? throw new NullListException(); } public CollectDataModel GetCollectByData(string data) { - return new("", "", "", 0); + _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 _collectStorageContract.GetElementById(data) ?? throw new ElementNotFoundException(data); } public void InsertCollect(CollectDataModel collectDataModel) { + _logger.LogInformation("New data: {json}", JsonSerializer.Serialize(collectDataModel)); + ArgumentNullException.ThrowIfNull(collectDataModel); + collectDataModel.Validate(); + _collectStorageContract.AddElement(collectDataModel); } } diff --git a/TheSoftBedProject/SoftBedBusinessLogic/Implementations/FurnitureBusinessLogicContract.cs b/TheSoftBedProject/SoftBedBusinessLogic/Implementations/FurnitureBusinessLogicContract.cs index 52b730e..4f096b7 100644 --- a/TheSoftBedProject/SoftBedBusinessLogic/Implementations/FurnitureBusinessLogicContract.cs +++ b/TheSoftBedProject/SoftBedBusinessLogic/Implementations/FurnitureBusinessLogicContract.cs @@ -1,35 +1,91 @@ -using SoftBedContracts.BusinessLogicsContracts; +using Microsoft.Extensions.Logging; +using SoftBedContracts.BusinessLogicsContracts; using SoftBedContracts.DataModels; using SoftBedContracts.Enums; +using SoftBedContracts.Exceptions; +using SoftBedContracts.Extensions; +using SoftBedContracts.StoragesContracts; +using System.Text.Json; namespace SoftBedBusinessLogic.Implementations; -internal class FurnitureBusinessLogicContract : IFurnitureBusinessLogicContract +internal class FurnitureBusinessLogicContract(IFurnitureStorageContract furnitureStorageContract, ILogger logger) : IFurnitureBusinessLogicContract { - + private readonly ILogger _logger = logger; + private readonly IFurnitureStorageContract _furnitureStorageContract = furnitureStorageContract; public List GetAllFurniture(bool onlyActive = true) { - return []; + _logger.LogInformation("GetAllFurniture params: {onlyActive}", onlyActive); + return _furnitureStorageContract.GetList(onlyActive) ?? throw new NullListException(); + } + public List GetAllFurnitureByWorkPieces(string workPiecesId, bool onlyActive = true) + { + _logger.LogInformation("GetAllSales params: {workPiecesId}", workPiecesId); + if (workPiecesId.IsEmpty()) + { + throw new ArgumentNullException(nameof(workPiecesId)); + } + if (!workPiecesId.IsGuid()) + { + throw new ValidationException("The value in the field workPiecesId is not a unique identifier."); + } + return _furnitureStorageContract.GetList(onlyActive, workPiecesId) ?? throw new NullListException(); } public List GetFurnitureHistoryByFurniture(string furnitureId) { - return []; + _logger.LogInformation("GetFurnitureHistoryByFurniture for {furnitureId}", furnitureId); + if (furnitureId.IsEmpty()) + { + throw new ArgumentNullException(nameof(furnitureId)); + } + if (!furnitureId.IsGuid()) + { + throw new ValidationException("The value in the field furnitureId is not a unique identifier."); + } + return _furnitureStorageContract.GetHistoryByFurnitureId(furnitureId) ?? throw new NullListException(); } - + public FurnitureDataModel GetFurnitureByData(string data) { - return new("", "", FurnitureType.None, 0, false, []); + _logger.LogInformation("Get element by data: {data}", data); + if (data.IsEmpty()) + { + throw new ArgumentNullException(nameof(data)); + } + if (data.IsGuid()) + { + return _furnitureStorageContract.GetElementById(data) ?? throw new ElementNotFoundException(data); + } + return _furnitureStorageContract.GetElementByName(data) ?? throw new ElementNotFoundException(data); } public void InsertFurniture(FurnitureDataModel furnitureDataModel) { + _logger.LogInformation("New data: {json}", JsonSerializer.Serialize(furnitureDataModel)); + ArgumentNullException.ThrowIfNull(furnitureDataModel); + furnitureDataModel.Validate(); + _furnitureStorageContract.AddElement(furnitureDataModel); } public void UpdateFurniture(FurnitureDataModel furnitureDataModel) { + _logger.LogInformation("Update data: {json}", JsonSerializer.Serialize(furnitureDataModel)); + ArgumentNullException.ThrowIfNull(furnitureDataModel); + furnitureDataModel.Validate(); + _furnitureStorageContract.UpdElement(furnitureDataModel); } public void DeleteFurniture(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"); + } + _furnitureStorageContract.DelElement(id); } } diff --git a/TheSoftBedProject/SoftBedBusinessLogic/Implementations/PostBusinessLogicContract.cs b/TheSoftBedProject/SoftBedBusinessLogic/Implementations/PostBusinessLogicContract.cs index f2d4559..a9b44f5 100644 --- a/TheSoftBedProject/SoftBedBusinessLogic/Implementations/PostBusinessLogicContract.cs +++ b/TheSoftBedProject/SoftBedBusinessLogic/Implementations/PostBusinessLogicContract.cs @@ -1,40 +1,93 @@ -using SoftBedContracts.BusinessLogicsContracts; +using Microsoft.Extensions.Logging; +using SoftBedContracts.BusinessLogicsContracts; using SoftBedContracts.DataModels; -using SoftBedContracts.Enums; +using SoftBedContracts.Exceptions; +using SoftBedContracts.Extensions; +using SoftBedContracts.StoragesContracts; +using System.Text.Json; namespace SoftBedBusinessLogic.Implementations; -internal class PostBusinessLogicContract : IPostBusinessLogicContract +internal class PostBusinessLogicContract(IPostStorageContract postStorageContract, ILogger logger) : IPostBusinessLogicContract { + private readonly ILogger _logger = logger; + private readonly IPostStorageContract _postStorageContract = postStorageContract; + + public List GetAllPosts(bool onlyActive = true) + { + _logger.LogInformation("GetAllPosts params: {onlyActive}", onlyActive); + return _postStorageContract.GetList(onlyActive) ?? throw new NullListException(); + } public List GetAllDataOfPost(string postId) { - return []; - } - - public List GetAllPosts(bool onlyActive) - { - 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("", "", PostType.None, 0, true, 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 RestorePost(string id) - { + _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/TheSoftBedProject/SoftBedBusinessLogic/Implementations/SalaryBusinessLogicContract.cs b/TheSoftBedProject/SoftBedBusinessLogic/Implementations/SalaryBusinessLogicContract.cs index def0cac..bf5d0ac 100644 --- a/TheSoftBedProject/SoftBedBusinessLogic/Implementations/SalaryBusinessLogicContract.cs +++ b/TheSoftBedProject/SoftBedBusinessLogic/Implementations/SalaryBusinessLogicContract.cs @@ -1,21 +1,64 @@ -using SoftBedContracts.BusinessLogicsContracts; +using Microsoft.Extensions.Logging; +using SoftBedContracts.BusinessLogicsContracts; using SoftBedContracts.DataModels; +using SoftBedContracts.Exceptions; +using SoftBedContracts.Extensions; +using SoftBedContracts.StoragesContracts; namespace SoftBedBusinessLogic.Implementations; -internal class SalaryBusinessLogicContract : ISalaryBusinessLogicContract +internal class SalaryBusinessLogicContract(ISalaryStorageContract salaryStorageContract, + ICollectStorageContract collectStorageContract, IPostStorageContract postStorageContract, IWorkerStorageContract workerStorageContract, ILogger logger) : ISalaryBusinessLogicContract { - public void CalculateSalaryByMonth(DateTime date) - { - } + private readonly ILogger _logger = logger; + private readonly ISalaryStorageContract _salaryStorageContract = salaryStorageContract; + private readonly ICollectStorageContract _collectStorageContract = collectStorageContract; + private readonly IPostStorageContract _postStorageContract = postStorageContract; + private readonly IWorkerStorageContract _workerStorageContract = workerStorageContract; 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 GetAllSalariesByPeriodByWorker(DateTime fromDate, DateTime toDate, string workerId) { - return []; + if (fromDate.IsDateNotOlder(toDate)) + { + throw new IncorrectDatesException(fromDate, toDate); + } + if (workerId.IsEmpty()) + { + throw new ArgumentNullException(nameof(workerId)); + } + if (!workerId.IsGuid()) + { + throw new ValidationException("The value in the field workerId is not a unique identifier."); + } + _logger.LogInformation("GetAllSalaries params: {fromDate}, {toDate}, {workerId}", fromDate, toDate, workerId); + return _salaryStorageContract.GetList(fromDate, toDate, workerId) ?? throw new NullListException(); } -} + + public void CalculateSalaryByMonth(DateTime date) + { + _logger.LogInformation("CalculateSalaryByMonth: {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 workers = _workerStorageContract.GetList() ?? throw new NullListException(); + foreach (var worker in workers) + { + var collects = _collectStorageContract.GetList(startDate, finishDate, workerId: worker.Id)?.Sum(x => x.Sum) ?? + throw new NullListException(); + var post = _postStorageContract.GetElementById(worker.PostId) ?? + throw new NullListException(); + var salary = post.Salary + collects * 0.1; + _logger.LogDebug("The employee {workerId} was paid a salary of {salary}", worker.Id, salary); + _salaryStorageContract.AddElement(new SalaryDataModel(worker.Id, finishDate, salary)); + } + } +} \ No newline at end of file diff --git a/TheSoftBedProject/SoftBedBusinessLogic/Implementations/WorkPieceBusinessLogicContract.cs b/TheSoftBedProject/SoftBedBusinessLogic/Implementations/WorkPieceBusinessLogicContract.cs index 75876d6..46fa074 100644 --- a/TheSoftBedProject/SoftBedBusinessLogic/Implementations/WorkPieceBusinessLogicContract.cs +++ b/TheSoftBedProject/SoftBedBusinessLogic/Implementations/WorkPieceBusinessLogicContract.cs @@ -1,30 +1,69 @@ -using SoftBedContracts.BusinessLogicsContracts; +using Microsoft.Extensions.Logging; +using SoftBedContracts.BusinessLogicsContracts; using SoftBedContracts.DataModels; +using SoftBedContracts.Exceptions; +using SoftBedContracts.Extensions; +using SoftBedContracts.StoragesContracts; +using System.Text.Json; +using System.Text.RegularExpressions; namespace SoftBedBusinessLogic.Implementations; -internal class WorkPieceBusinessLogicContract : IWorkPieceBusinessLogicContract +internal class WorkPieceBusinessLogicContract(IWorkPieceStorageContract workPieceStorageContract, ILogger logger) : IWorkPieceBusinessLogicContract { + private readonly ILogger _logger = logger; + private IWorkPieceStorageContract _workPieceStorageContract = workPieceStorageContract; public List GetAllWorkPieces() { - return []; + _logger.LogInformation("GetAllWorkPieces"); + return _workPieceStorageContract.GetList() ?? throw new NullListException(); } public WorkPieceDataModel GetWorkPiecesByData(string data) { - return new("", "", 0); + _logger.LogInformation("Get element by data: {data}", data); + if (data.IsEmpty()) + { + throw new ArgumentNullException(nameof(data)); + } + if (data.IsGuid()) + { + return _workPieceStorageContract.GetElementById(data) ?? throw new ElementNotFoundException(data); + } + if (Regex.IsMatch(data, @"^\d+x\d+$")) + { + return _workPieceStorageContract.GetElementBySize(data) ?? throw new ElementNotFoundException(data); + } + return _workPieceStorageContract.GetElementBySize(data) ?? throw new ElementNotFoundException(data); } - public void InsertPost(WorkPieceDataModel workPieceDataModel) + public void InsertWorkPiece(WorkPieceDataModel workPieceDataModel) { + _logger.LogInformation("New data: {json}", JsonSerializer.Serialize(workPieceDataModel)); + ArgumentNullException.ThrowIfNull(workPieceDataModel); + workPieceDataModel.Validate(); + _workPieceStorageContract.AddElement(workPieceDataModel); } - public void UpdatePost(WorkPieceDataModel workPieceDataModel) + public void UpdateWorkPiece(WorkPieceDataModel workPieceDataModel) { + _logger.LogInformation("Update data: {json}", JsonSerializer.Serialize(workPieceDataModel)); + ArgumentNullException.ThrowIfNull(workPieceDataModel); + workPieceDataModel.Validate(); + _workPieceStorageContract.UpdElement(workPieceDataModel); } - public void DeletePost(string id) + public void DeleteWorkPiece(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"); + } + _workPieceStorageContract.DelElement(id); } - } diff --git a/TheSoftBedProject/SoftBedBusinessLogic/Implementations/WorkerBusinessLogicContract.cs b/TheSoftBedProject/SoftBedBusinessLogic/Implementations/WorkerBusinessLogicContract.cs index 71e46ca..9cfa1db 100644 --- a/TheSoftBedProject/SoftBedBusinessLogic/Implementations/WorkerBusinessLogicContract.cs +++ b/TheSoftBedProject/SoftBedBusinessLogic/Implementations/WorkerBusinessLogicContract.cs @@ -1,43 +1,99 @@ -using SoftBedContracts.BusinessLogicsContracts; +using Microsoft.Extensions.Logging; +using SoftBedContracts.BusinessLogicsContracts; using SoftBedContracts.DataModels; +using SoftBedContracts.Exceptions; +using SoftBedContracts.Extensions; +using SoftBedContracts.StoragesContracts; +using System.Text.Json; namespace SoftBedBusinessLogic.Implementations; -internal class WorkerBusinessLogicContract : IWorkerBusinessLogicContract +internal class WorkerBusinessLogicContract(IWorkerStorageContract workerStorageContract, ILogger logger) : IWorkerBusinessLogicContract { + private readonly ILogger _logger = logger; + private readonly IWorkerStorageContract _workerStorageContract = workerStorageContract; + public List GetAllWorkers() { - return []; - } - - public List GetAllWorkersByBirthDate(DateTime fromDate, DateTime toDate) - { - return []; - } - - public List GetAllWorkersByEmploymentDate(DateTime fromDate, DateTime toDate) - { - return []; + _logger.LogInformation("GetAllWorkers"); + return _workerStorageContract.GetList() ?? throw new NullListException(); } public List GetAllWorkersByPost(string postId) { - return []; + _logger.LogInformation("GetAllWorkers params: {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 _workerStorageContract.GetList(postId) ?? throw new NullListException(); + } + + public List GetAllWorkersByBirthDate(DateTime fromDate, DateTime toDate) + { + _logger.LogInformation("GetAllWorkers params: {fromDate}, {toDate}", fromDate, toDate); + if (fromDate.IsDateNotOlder(toDate)) + { + throw new IncorrectDatesException(fromDate, toDate); + } + return _workerStorageContract.GetList(fromBirthDate: fromDate, toBirthDate: toDate) ?? throw new NullListException(); + } + + public List GetAllWorkersByEmploymentDate(DateTime fromDate, DateTime toDate) + { + _logger.LogInformation("GetAllWorkers params: {fromDate}, {toDate}", fromDate, toDate); + if (fromDate.IsDateNotOlder(toDate)) + { + throw new IncorrectDatesException(fromDate, toDate); + } + return _workerStorageContract.GetList(fromEmploymentDate: fromDate, toEmploymentDate: toDate) ?? throw new NullListException(); } public WorkerDataModel GetWorkerByData(string data) { - return new("", "", "", DateTime.Now, DateTime.Now); + _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); + } + return _workerStorageContract.GetElementByFIO(data) ?? throw new ElementNotFoundException(data); } public void InsertWorker(WorkerDataModel workerDataModel) { + _logger.LogInformation("New data: {json}", JsonSerializer.Serialize(workerDataModel)); + ArgumentNullException.ThrowIfNull(workerDataModel); + workerDataModel.Validate(); + _workerStorageContract.AddElement(workerDataModel); } public void UpdateWorker(WorkerDataModel workerDataModel) { + _logger.LogInformation("Update data: {json}", JsonSerializer.Serialize(workerDataModel)); + ArgumentNullException.ThrowIfNull(workerDataModel); + workerDataModel.Validate(); + _workerStorageContract.UpdElement(workerDataModel); } + public void DeleteWorker(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"); + } + _workerStorageContract.DelElement(id); } -} +} \ No newline at end of file