diff --git a/ComputerStoreBusinessLogic/BusinessLogic/ConsignmentLogic.cs b/ComputerStoreBusinessLogic/BusinessLogic/ConsignmentLogic.cs new file mode 100644 index 0000000..1faeded --- /dev/null +++ b/ComputerStoreBusinessLogic/BusinessLogic/ConsignmentLogic.cs @@ -0,0 +1,105 @@ +using ComputerStoreContracts.BindingModels; +using ComputerStoreContracts.BusinessLogicContracts; +using ComputerStoreContracts.SearchModels; +using ComputerStoreContracts.StorageContracts; +using ComputerStoreContracts.ViewModels; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputerStoreBusinessLogic.BusinessLogic +{ + public class ConsignmentLogic : IConsignmentLogic + { + private readonly ILogger _logger; + private readonly IConsignmentStorage _consignmentStorage; + public ConsignmentLogic(ILogger logger, IConsignmentStorage consignmentStorage) + { + _logger = logger; + _consignmentStorage = consignmentStorage; + } + + public bool Create(ConsignmentBindingModel model) + { + CheckModel(model); + if (_consignmentStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + + public bool Update(ConsignmentBindingModel model) + { + CheckModel(model); + if (_consignmentStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } + + public bool Delete(ConsignmentBindingModel model) + { + CheckModel(model, false); + _logger.LogInformation("Delete. ID:{ID}", model.ID); + if (_consignmentStorage.Delete(model) == null) + { + _logger.LogWarning("Delete operation failed"); + return false; + } + return true; + } + + public ConsignmentViewModel? ReadElement(ConsignmentSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + + _logger.LogInformation("ReadElement. Consignment ID:{ ID}", model.ID); + + var element = _consignmentStorage.GetElement(model); + + if (element == null) + { + _logger.LogWarning("ReadElement element not found"); + return null; + } + + _logger.LogInformation("ReadElement find. Consignment ID:{ID}", element.ID); + return element; + } + + public List? ReadList(ConsignmentSearchModel? model) + { + _logger.LogInformation("ReadList. Consignment ID:{ ID}", model?.ID); + + var list = model == null ? _consignmentStorage.GetFullList() : _consignmentStorage.GetFilteredList(model); + + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + return null; + } + _logger.LogInformation("ReadList. Count:{Count}", list.Count); + return list; + } + + private void CheckModel(ConsignmentBindingModel model, bool withParams = true) + { + if (model == null) { return; } + if (!withParams) { return; } + if (model.Price <= 0) { throw new ArgumentNullException("Invalid consignment's price", nameof(model)); } + if (string.IsNullOrEmpty(model.OrderID.ToString())) { throw new ArgumentNullException("Invalid Consignment's order ID", nameof(model)); } + + _logger.LogInformation("Consignment. Consignment ID:{ ID}. Order ID:{ OrderID}.", model.ID, model.OrderID); + } + } +} diff --git a/ComputerStoreBusinessLogic/BusinessLogic/OrderLogic.cs b/ComputerStoreBusinessLogic/BusinessLogic/OrderLogic.cs new file mode 100644 index 0000000..6f0d6b3 --- /dev/null +++ b/ComputerStoreBusinessLogic/BusinessLogic/OrderLogic.cs @@ -0,0 +1,148 @@ +using ComputerStoreContracts.BindingModels; +using ComputerStoreContracts.BusinessLogicContracts; +using ComputerStoreContracts.SearchModels; +using ComputerStoreContracts.StorageContracts; +using ComputerStoreContracts.ViewModels; +using ComputerStoreDataModels.Enums; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputerStoreBusinessLogic.BusinessLogic +{ + public class OrderLogic : IOrderLogic + { + private readonly ILogger _logger; + private readonly IOrderStorage _orderStorage; + + public OrderLogic(ILogger logger, IOrderStorage orderStorage) + { + _logger = logger; + _orderStorage = orderStorage; + } + + public bool CreateOrder(OrderBindingModel model) + { + CheckModel(model); + if (model.Status != OrderStatus.Unknown) + { + _logger.LogWarning("Insert operation failed. Order status incorrect."); + return false; + } + model.Status = OrderStatus.Accepted; + if (_orderStorage.Insert(model) == null) + { + model.Status = OrderStatus.Unknown; + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + + public bool StatusUpdate(OrderBindingModel rawModel, OrderStatus newStatus) + { + var viewModel = _orderStorage.GetElement(new OrderSearchModel + { + ID = rawModel.ID + }); + + if (viewModel == null) + { + _logger.LogWarning("Order model not found"); + return false; + } + + OrderBindingModel model = new OrderBindingModel + { + ID = viewModel.ID, + ConsignmentID = viewModel.ConsignmentID, + RequestID = viewModel.RequestID, + Status = viewModel.Status, + DateCreate = viewModel.DateCreate, + DateImplement = viewModel.DateImplement, + Price = viewModel.Price + }; + + CheckModel(model, false); + if (model.Status + 1 != newStatus) + { + _logger.LogWarning("Status update to " + newStatus.ToString() + " operation failed. Order status incorrect."); + return false; + } + model.Status = newStatus; + if (model.Status == OrderStatus.Given) model.DateImplement = DateTime.Now; + if (_orderStorage.Update(model) == null) + { + model.Status--; + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } + + public bool TakeOrderInProcess(OrderBindingModel model) + { + return StatusUpdate(model, OrderStatus.InProcess); + } + + public bool TakeOrderReady(OrderBindingModel model) + { + return StatusUpdate(model, OrderStatus.Ready); + } + + public bool TakeOrderGiven(OrderBindingModel model) + { + return StatusUpdate(model, OrderStatus.Given); + } + + public List? ReadList(OrderSearchModel? model) + { + _logger.LogInformation("Order. OrderID:{Id}", model?.ID); + var list = model == null ? _orderStorage.GetFullList() : _orderStorage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + return null; + } + _logger.LogInformation("ReadList. Count:{Count}", list.Count); + return list; + } + + private void CheckModel(OrderBindingModel model, bool withParams = true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (!withParams) + { + return; + } + if (string.IsNullOrEmpty(model.RequestID.ToString()) && string.IsNullOrEmpty(model.ConsignmentID.ToString())) + { + throw new ArgumentNullException("Incorrect consignment or request ID", nameof(model.ConsignmentID)); + } + + if (string.IsNullOrEmpty(model.RequestID.ToString()) && model.ConsignmentID < 0) + { + throw new ArgumentNullException("Incorrect consignment ID", nameof(model.ConsignmentID)); + } + if (string.IsNullOrEmpty(model.ConsignmentID.ToString()) && model.RequestID < 0) + { + throw new ArgumentNullException("Incorrect request ID", nameof(model.RequestID)); + } + if (model.Price <= 0) + { + throw new ArgumentNullException("Incorrect Price", nameof(model.Price)); + } + if (string.IsNullOrEmpty(model.SellerID.ToString()) && model.SellerID < 0) + { + throw new ArgumentNullException("Incorrect seller ID", nameof(model.SellerID)); + } + _logger.LogInformation("Order. OrderID:{ ID}.RequestID:{ RequestID}. ConsignmentID: { ConsignmentID}. SellerID: { SellerID}", model.ID, model.RequestID, model.ConsignmentID, model.SellerID); + } + } +} diff --git a/ComputerStoreBusinessLogic/BusinessLogic/RequestLogic.cs b/ComputerStoreBusinessLogic/BusinessLogic/RequestLogic.cs new file mode 100644 index 0000000..05ba36d --- /dev/null +++ b/ComputerStoreBusinessLogic/BusinessLogic/RequestLogic.cs @@ -0,0 +1,105 @@ +using ComputerStoreContracts.BindingModels; +using ComputerStoreContracts.BusinessLogicContracts; +using ComputerStoreContracts.SearchModels; +using ComputerStoreContracts.StorageContracts; +using ComputerStoreContracts.ViewModels; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputerStoreBusinessLogic.BusinessLogic +{ + public class RequestLogic : IRequestLogic + { + private readonly ILogger _logger; + private readonly IRequestStorage _requestStorage; + public RequestLogic(ILogger logger, IRequestStorage requestStorage) + { + _logger = logger; + _requestStorage = requestStorage; + } + + public bool Create(RequestBindingModel model) + { + CheckModel(model); + if (_requestStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + + public bool Update(RequestBindingModel model) + { + CheckModel(model); + if (_requestStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } + + public bool Delete(RequestBindingModel model) + { + CheckModel(model, false); + _logger.LogInformation("Delete. ID:{ID}", model.ID); + if (_requestStorage.Delete(model) == null) + { + _logger.LogWarning("Delete operation failed"); + return false; + } + return true; + } + + public RequestViewModel? ReadElement(RequestSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + + _logger.LogInformation("ReadElement. Request ID:{ ID}. Order ID:{ OrderID}", model.ID, model.OrderID); + + var element = _requestStorage.GetElement(model); + + if (element == null) + { + _logger.LogWarning("ReadElement element not found"); + return null; + } + + _logger.LogInformation("ReadElement find. ID:{ID}", element.ID); + return element; + } + + public List? ReadList(RequestSearchModel? model) + { + _logger.LogInformation("ReadList. Request ID:{ ID}. Order ID: { OrderID}", model?.ID, model?.OrderID); + + var list = model == null ? _requestStorage.GetFullList() : _requestStorage.GetFilteredList(model); + + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + return null; + } + _logger.LogInformation("ReadList. Count:{Count}", list.Count); + return list; + } + + private void CheckModel(RequestBindingModel model, bool withParams = true) + { + if (model == null) { return; } + if (!withParams) { return; } + if (model.Price <= 0) { throw new ArgumentNullException("Invalid request's price", nameof(model)); } + if (string.IsNullOrEmpty(model.OrderID.ToString())) { throw new ArgumentNullException("Invalid Request's order ID", nameof(model)); } + + _logger.LogInformation("Request. Request ID:{ ID}. Order ID: { OrderID}", model.ID, model.OrderID); + } + } +} diff --git a/ComputerStoreBusinessLogic/BusinessLogic/SellerLogic.cs b/ComputerStoreBusinessLogic/BusinessLogic/SellerLogic.cs new file mode 100644 index 0000000..14910e4 --- /dev/null +++ b/ComputerStoreBusinessLogic/BusinessLogic/SellerLogic.cs @@ -0,0 +1,122 @@ +using ComputerStoreContracts.BindingModels; +using ComputerStoreContracts.BusinessLogicContracts; +using ComputerStoreContracts.SearchModels; +using ComputerStoreContracts.StorageContracts; +using ComputerStoreContracts.ViewModels; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputerStoreBusinessLogic.BusinessLogic +{ + public class SellerLogic : ISellerLogic + { + private readonly ILogger _logger; + private readonly ISellerStorage _sellerStorage; + public SellerLogic(ILogger logger, ISellerStorage sellerStorage) + { + _logger = logger; + _sellerStorage = sellerStorage; + } + public bool Create(SellerBindingModel model) + { + CheckSeller(model); + if (_sellerStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + + public bool Update(SellerBindingModel model) + { + CheckSeller(model); + if (_sellerStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } + + public bool Delete(SellerBindingModel model) + { + CheckSeller(model, false); + _logger.LogInformation("Delete. ID:{ID}", model.ID); + if (_sellerStorage.Delete(model) == null) + { + _logger.LogWarning("Delete operation failed"); + return false; + } + return true; + } + + public SellerViewModel? ReadElement(SellerSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + _logger.LogInformation("ReadElement. Username:{Username}.ID:{ ID}", model.Username, model.ID); + var element = _sellerStorage.GetElement(model); + if (element == null) + { + _logger.LogWarning("ReadElement element not found"); + return null; + } + _logger.LogInformation("ReadElement find. ID:{ID}", element.ID); + return element; + } + + public List? ReadList(SellerSearchModel? model) + { + _logger.LogInformation("ReadList. Username:{Username}. ID:{ ID}", model?.Username, model?.ID); + + var list = model == null ? _sellerStorage.GetFullList() : _sellerStorage.GetFilteredList(model); + + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + return null; + } + _logger.LogInformation("ReadList. Count:{Count}", list.Count); + return list; + } + + public void CheckSeller(SellerBindingModel model, bool withParams = true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (!withParams) + { + return; + } + if (string.IsNullOrEmpty(model.Username)) + { + throw new ArgumentNullException("Invalid username of user", nameof(model.Username)); + } + if (string.IsNullOrEmpty(model.Password)) + { + throw new ArgumentNullException("Invalid password of user", nameof(model.Password)); + } + + _logger.LogInformation("Client. Username:{ Username}. ID: { ID} ", model.Username, model.ID); + + var element = _sellerStorage.GetElement(new SellerSearchModel + { + Username = model.Username + }); + + if (element != null && element.ID != model.ID) + { + throw new InvalidOperationException("Seller with such username already exists."); + } + } + } +} diff --git a/ComputerStoreContracts/BindingModels/ConsignmentBindingModel.cs b/ComputerStoreContracts/BindingModels/ConsignmentBindingModel.cs index ddd858f..b9ce1e4 100644 --- a/ComputerStoreContracts/BindingModels/ConsignmentBindingModel.cs +++ b/ComputerStoreContracts/BindingModels/ConsignmentBindingModel.cs @@ -9,9 +9,8 @@ namespace ComputerStoreContracts.BindingModels { public class ConsignmentBindingModel : IConsignmentModel { + public int ID { get; set; } public int OrderID { get; set; } - public int ProductID { get; set; } public double Price { get; set; } - public int Count { get; set; } } } diff --git a/ComputerStoreContracts/BindingModels/OrderBindingModel.cs b/ComputerStoreContracts/BindingModels/OrderBindingModel.cs index 6053ad4..134e262 100644 --- a/ComputerStoreContracts/BindingModels/OrderBindingModel.cs +++ b/ComputerStoreContracts/BindingModels/OrderBindingModel.cs @@ -12,7 +12,8 @@ namespace ComputerStoreContracts.BindingModels { public int ID { get; set; } public double Price { get; set; } - public OrderType Type { get; set; } = OrderType.Unknown; + public int? ConsignmentID { get; set; } + public int? RequestID { get; set; } public OrderStatus Status { get; set; } = OrderStatus.Unknown; public DateTime DateCreate { get; set; } = DateTime.Now; public DateTime? DateImplement { get; set; } diff --git a/ComputerStoreContracts/BindingModels/RequestBindingModel.cs b/ComputerStoreContracts/BindingModels/RequestBindingModel.cs index 5b700e4..584f10a 100644 --- a/ComputerStoreContracts/BindingModels/RequestBindingModel.cs +++ b/ComputerStoreContracts/BindingModels/RequestBindingModel.cs @@ -12,6 +12,5 @@ namespace ComputerStoreContracts.BindingModels public int ID { get; set; } public int OrderID { get; set; } public double Price { get; set; } - public int Count { get; set; } } } diff --git a/ComputerStoreContracts/BusinessLogicContracts/IOrderLogic.cs b/ComputerStoreContracts/BusinessLogicContracts/IOrderLogic.cs index f5699c7..6334f53 100644 --- a/ComputerStoreContracts/BusinessLogicContracts/IOrderLogic.cs +++ b/ComputerStoreContracts/BusinessLogicContracts/IOrderLogic.cs @@ -12,9 +12,9 @@ namespace ComputerStoreContracts.BusinessLogicContracts public interface IOrderLogic { List? ReadList(OrderSearchModel? model); - OrderViewModel? ReadElement(OrderSearchModel model); - bool Create(OrderBindingModel model); - bool Update(OrderBindingModel model); - bool Delete(OrderBindingModel model); + bool CreateOrder(OrderBindingModel model); + bool TakeOrderInProcess(OrderBindingModel model); + bool TakeOrderReady(OrderBindingModel model); + bool TakeOrderGiven(OrderBindingModel model); } } diff --git a/ComputerStoreContracts/SearchModels/ConsignmentSearchModel.cs b/ComputerStoreContracts/SearchModels/ConsignmentSearchModel.cs index 35e06b3..833023d 100644 --- a/ComputerStoreContracts/SearchModels/ConsignmentSearchModel.cs +++ b/ComputerStoreContracts/SearchModels/ConsignmentSearchModel.cs @@ -8,6 +8,7 @@ namespace ComputerStoreContracts.SearchModels { public class ConsignmentSearchModel { + public int? ID { get; set; } public int? OrderID { get; set; } } } diff --git a/ComputerStoreContracts/SearchModels/OrderSearchModel.cs b/ComputerStoreContracts/SearchModels/OrderSearchModel.cs index 5a18d58..41e4091 100644 --- a/ComputerStoreContracts/SearchModels/OrderSearchModel.cs +++ b/ComputerStoreContracts/SearchModels/OrderSearchModel.cs @@ -1,4 +1,5 @@ -using System; +using ComputerStoreDataModels.Enums; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -9,6 +10,8 @@ namespace ComputerStoreContracts.SearchModels public class OrderSearchModel { public int? ID { get; set; } + public int? ConsignmentID { get; set; } + public int? RequestID { get; set; } public DateTime? DateFrom { get; set; } public DateTime? DateTo { get; set; } public int? SellerID { get; set; } diff --git a/ComputerStoreContracts/ViewModels/ConsignmentViewModel.cs b/ComputerStoreContracts/ViewModels/ConsignmentViewModel.cs index 6b14a4a..074f9e3 100644 --- a/ComputerStoreContracts/ViewModels/ConsignmentViewModel.cs +++ b/ComputerStoreContracts/ViewModels/ConsignmentViewModel.cs @@ -10,13 +10,11 @@ namespace ComputerStoreContracts.ViewModels { public class ConsignmentViewModel : IConsignmentModel { + [DisplayName("Consignment ID")] + public int ID { get; set; } [DisplayName("Order ID")] - public int OrderID { get; } - [DisplayName("Product ID")] - public int ProductID { get; } + public int OrderID { get; set; } [DisplayName("Price")] - public double Price { get; } - [DisplayName("Count")] - public int Count { get; } + public double Price { get; set; } } } diff --git a/ComputerStoreContracts/ViewModels/OrderViewModel.cs b/ComputerStoreContracts/ViewModels/OrderViewModel.cs index 43523e2..7a58025 100644 --- a/ComputerStoreContracts/ViewModels/OrderViewModel.cs +++ b/ComputerStoreContracts/ViewModels/OrderViewModel.cs @@ -15,8 +15,10 @@ namespace ComputerStoreContracts.ViewModels public int ID { get; set; } [DisplayName("Price")] public double Price { get; set; } - [DisplayName("Type of order")] - public OrderType Type { get; set; } + [DisplayName("Consignment ID")] + public int? ConsignmentID { get; set; } + [DisplayName("Request ID")] + public int? RequestID { get; set; } [DisplayName("Status")] public OrderStatus Status { get; set; } [DisplayName("Creation date")] diff --git a/ComputerStoreContracts/ViewModels/RequestViewModel.cs b/ComputerStoreContracts/ViewModels/RequestViewModel.cs index 1eb52e3..d238197 100644 --- a/ComputerStoreContracts/ViewModels/RequestViewModel.cs +++ b/ComputerStoreContracts/ViewModels/RequestViewModel.cs @@ -16,7 +16,5 @@ namespace ComputerStoreContracts.ViewModels public int OrderID { get; set; } [DisplayName("Price")] public double Price { get; set; } - [DisplayName("Count")] - public int Count { get; set; } } } diff --git a/ComputerStoreContracts/ViewModels/SellerViewModel.cs b/ComputerStoreContracts/ViewModels/SellerViewModel.cs index 956ad7a..8396f08 100644 --- a/ComputerStoreContracts/ViewModels/SellerViewModel.cs +++ b/ComputerStoreContracts/ViewModels/SellerViewModel.cs @@ -14,13 +14,13 @@ namespace ComputerStoreContracts.ViewModels public int ID { get; set; } [DisplayName("Username")] public string Username { get; set; } = string.Empty; - [DisplayName("Password")] + public string Password { get; set; } = string.Empty; [DisplayName("FirstName")] - public string FirstName { get; set; } = string.Empty; + public string? FirstName { get; set; } = string.Empty; [DisplayName("LastName")] - public string LastName { get; set; } = string.Empty; + public string? LastName { get; set; } = string.Empty; [DisplayName("MiddleName")] - public string MiddleName { get; set; } = string.Empty; + public string? MiddleName { get; set; } = string.Empty; } } diff --git a/ComputerStoreDataModels/Enums/OrderType.cs b/ComputerStoreDataModels/Enums/OrderType.cs deleted file mode 100644 index 39c549e..0000000 --- a/ComputerStoreDataModels/Enums/OrderType.cs +++ /dev/null @@ -1,15 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace ComputerStoreDataModels.Enums -{ - public enum OrderType - { - Unknown = -1, - Consigment = 0, - Request = 1 - } -} diff --git a/ComputerStoreDataModels/Models/IConsignmentModel.cs b/ComputerStoreDataModels/Models/IConsignmentModel.cs index 155f7cb..360a278 100644 --- a/ComputerStoreDataModels/Models/IConsignmentModel.cs +++ b/ComputerStoreDataModels/Models/IConsignmentModel.cs @@ -6,11 +6,9 @@ using System.Threading.Tasks; namespace ComputerStoreDataModels.Models { - public interface IConsignmentModel + public interface IConsignmentModel : IID { int OrderID { get; } - int ProductID { get; } double Price { get; } - int Count { get; } } } diff --git a/ComputerStoreDataModels/Models/IOrderModel.cs b/ComputerStoreDataModels/Models/IOrderModel.cs index a61fa42..d103421 100644 --- a/ComputerStoreDataModels/Models/IOrderModel.cs +++ b/ComputerStoreDataModels/Models/IOrderModel.cs @@ -10,7 +10,8 @@ namespace ComputerStoreDataModels.Models public interface IOrderModel : IID { double Price { get; } - OrderType Type { get; } + int? ConsignmentID { get; } + int? RequestID { get; } OrderStatus Status { get; } DateTime DateCreate { get; } DateTime? DateImplement { get; } diff --git a/ComputerStoreDataModels/Models/IRequestModel.cs b/ComputerStoreDataModels/Models/IRequestModel.cs index 2139b18..4af0448 100644 --- a/ComputerStoreDataModels/Models/IRequestModel.cs +++ b/ComputerStoreDataModels/Models/IRequestModel.cs @@ -10,6 +10,5 @@ namespace ComputerStoreDataModels.Models { int OrderID { get; } double Price { get; } - int Count { get; } } }