diff --git a/ComputerStoreBusinessLogic/BusinessLogic/OrderLogic.cs b/ComputerStoreBusinessLogic/BusinessLogic/OrderLogic.cs index 6f0d6b3..87d6240 100644 --- a/ComputerStoreBusinessLogic/BusinessLogic/OrderLogic.cs +++ b/ComputerStoreBusinessLogic/BusinessLogic/OrderLogic.cs @@ -58,12 +58,13 @@ namespace ComputerStoreBusinessLogic.BusinessLogic 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 + Price = viewModel.Price, + SellerID = viewModel.SellerID, + OrderConsignments = viewModel.OrderConsignments, + OrderRequests = viewModel.OrderRequests }; CheckModel(model, false); @@ -121,19 +122,6 @@ namespace ComputerStoreBusinessLogic.BusinessLogic { 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)); @@ -142,7 +130,7 @@ namespace ComputerStoreBusinessLogic.BusinessLogic { 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); + _logger.LogInformation("Order. OrderID:{ ID}. SellerID: { SellerID}", model.ID, model.SellerID); } } } diff --git a/ComputerStoreBusinessLogic/BusinessLogic/RequestLogic.cs b/ComputerStoreBusinessLogic/BusinessLogic/RequestLogic.cs index 05ba36d..2921fba 100644 --- a/ComputerStoreBusinessLogic/BusinessLogic/RequestLogic.cs +++ b/ComputerStoreBusinessLogic/BusinessLogic/RequestLogic.cs @@ -98,8 +98,9 @@ namespace ComputerStoreBusinessLogic.BusinessLogic 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)); } + if (string.IsNullOrEmpty(model.PCID.ToString())) { throw new ArgumentNullException("Invalid Request's PC ID", nameof(model)); } - _logger.LogInformation("Request. Request ID:{ ID}. Order ID: { OrderID}", model.ID, model.OrderID); + _logger.LogInformation("Request. Request ID:{ ID}. Order ID: { OrderID}. PC ID: { PCID}", model.ID, model.OrderID, model.PCID); } } } diff --git a/ComputerStoreContracts/BindingModels/ConsignmentBindingModel.cs b/ComputerStoreContracts/BindingModels/ConsignmentBindingModel.cs index b9ce1e4..08d0a50 100644 --- a/ComputerStoreContracts/BindingModels/ConsignmentBindingModel.cs +++ b/ComputerStoreContracts/BindingModels/ConsignmentBindingModel.cs @@ -12,5 +12,6 @@ namespace ComputerStoreContracts.BindingModels public int ID { get; set; } public int OrderID { get; set; } public double Price { get; set; } + public Dictionary ConsignmentProducts { get; set; } = new(); } } diff --git a/ComputerStoreContracts/BindingModels/OrderBindingModel.cs b/ComputerStoreContracts/BindingModels/OrderBindingModel.cs index 134e262..c70da7e 100644 --- a/ComputerStoreContracts/BindingModels/OrderBindingModel.cs +++ b/ComputerStoreContracts/BindingModels/OrderBindingModel.cs @@ -12,8 +12,8 @@ namespace ComputerStoreContracts.BindingModels { public int ID { get; set; } public double Price { get; set; } - public int? ConsignmentID { get; set; } - public int? RequestID { get; set; } + public List OrderConsignments { get; set; } = new(); + public List OrderRequests { get; set; } = new(); 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 584f10a..aab609c 100644 --- a/ComputerStoreContracts/BindingModels/RequestBindingModel.cs +++ b/ComputerStoreContracts/BindingModels/RequestBindingModel.cs @@ -12,5 +12,6 @@ namespace ComputerStoreContracts.BindingModels public int ID { get; set; } public int OrderID { get; set; } public double Price { get; set; } + public int PCID { get; set; } } } diff --git a/ComputerStoreContracts/BusinessLogicContracts/IRequestLogic.cs b/ComputerStoreContracts/BusinessLogicContracts/IRequestLogic.cs index 5e02f16..b69b689 100644 --- a/ComputerStoreContracts/BusinessLogicContracts/IRequestLogic.cs +++ b/ComputerStoreContracts/BusinessLogicContracts/IRequestLogic.cs @@ -12,9 +12,7 @@ namespace ComputerStoreContracts.BusinessLogicContracts public interface IRequestLogic { List? ReadList(RequestSearchModel? model); - RequestViewModel? ReadElement(RequestSearchModel model); - bool Create(RequestBindingModel model); bool Update(RequestBindingModel model); bool Delete(RequestBindingModel model); diff --git a/ComputerStoreContracts/SearchModels/ConsignmentSearchModel.cs b/ComputerStoreContracts/SearchModels/ConsignmentSearchModel.cs index 833023d..20d9c4a 100644 --- a/ComputerStoreContracts/SearchModels/ConsignmentSearchModel.cs +++ b/ComputerStoreContracts/SearchModels/ConsignmentSearchModel.cs @@ -1,4 +1,5 @@ -using System; +using ComputerStoreDataModels.Models; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -10,5 +11,6 @@ namespace ComputerStoreContracts.SearchModels { public int? ID { get; set; } public int? OrderID { get; set; } + public int? ProductID { get; set; } } } diff --git a/ComputerStoreContracts/SearchModels/RequestSearchModel.cs b/ComputerStoreContracts/SearchModels/RequestSearchModel.cs index 0ecc43a..7471425 100644 --- a/ComputerStoreContracts/SearchModels/RequestSearchModel.cs +++ b/ComputerStoreContracts/SearchModels/RequestSearchModel.cs @@ -10,5 +10,6 @@ namespace ComputerStoreContracts.SearchModels { public int? ID { get; set; } public int? OrderID { get; set; } + public int? PCID { get; set; } } } diff --git a/ComputerStoreContracts/ViewModels/ConsignmentViewModel.cs b/ComputerStoreContracts/ViewModels/ConsignmentViewModel.cs index 074f9e3..9e64aaf 100644 --- a/ComputerStoreContracts/ViewModels/ConsignmentViewModel.cs +++ b/ComputerStoreContracts/ViewModels/ConsignmentViewModel.cs @@ -16,5 +16,7 @@ namespace ComputerStoreContracts.ViewModels public int OrderID { get; set; } [DisplayName("Price")] public double Price { get; set; } + + public Dictionary ConsignmentProducts { get; set; } = new(); } } diff --git a/ComputerStoreContracts/ViewModels/OrderViewModel.cs b/ComputerStoreContracts/ViewModels/OrderViewModel.cs index 7a58025..fbc0610 100644 --- a/ComputerStoreContracts/ViewModels/OrderViewModel.cs +++ b/ComputerStoreContracts/ViewModels/OrderViewModel.cs @@ -15,17 +15,18 @@ namespace ComputerStoreContracts.ViewModels public int ID { get; set; } [DisplayName("Price")] public double Price { 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")] + [DisplayName("Creation date")] public DateTime DateCreate { get; set; } [DisplayName("Implementation date")] public DateTime? DateImplement { get; set; } - [DisplayName("Seller ID")] + public int SellerID { get; set; } + [DisplayName("SellerUsername")] + public string? SellerUsername { get; set; } = string.Empty; + + public List OrderConsignments { get; set; } = new(); + public List OrderRequests { get; set; } = new(); } } diff --git a/ComputerStoreContracts/ViewModels/RequestViewModel.cs b/ComputerStoreContracts/ViewModels/RequestViewModel.cs index d238197..a2e18b2 100644 --- a/ComputerStoreContracts/ViewModels/RequestViewModel.cs +++ b/ComputerStoreContracts/ViewModels/RequestViewModel.cs @@ -16,5 +16,8 @@ namespace ComputerStoreContracts.ViewModels public int OrderID { get; set; } [DisplayName("Price")] public double Price { get; set; } + + [DisplayName("PC ID")] + public int PCID { get; set; } } } diff --git a/ComputerStoreDataModels/Models/IConsignmentModel.cs b/ComputerStoreDataModels/Models/IConsignmentModel.cs index 360a278..a114658 100644 --- a/ComputerStoreDataModels/Models/IConsignmentModel.cs +++ b/ComputerStoreDataModels/Models/IConsignmentModel.cs @@ -10,5 +10,6 @@ namespace ComputerStoreDataModels.Models { int OrderID { get; } double Price { get; } + Dictionary ConsignmentProducts { get; } } } diff --git a/ComputerStoreDataModels/Models/IOrderModel.cs b/ComputerStoreDataModels/Models/IOrderModel.cs index d103421..8406ab5 100644 --- a/ComputerStoreDataModels/Models/IOrderModel.cs +++ b/ComputerStoreDataModels/Models/IOrderModel.cs @@ -10,8 +10,8 @@ namespace ComputerStoreDataModels.Models public interface IOrderModel : IID { double Price { get; } - int? ConsignmentID { get; } - int? RequestID { get; } + List OrderConsignments { get; } + List OrderRequests { get; } OrderStatus Status { get; } DateTime DateCreate { get; } DateTime? DateImplement { get; } diff --git a/ComputerStoreDataModels/Models/IRequestModel.cs b/ComputerStoreDataModels/Models/IRequestModel.cs index 4af0448..0fb3992 100644 --- a/ComputerStoreDataModels/Models/IRequestModel.cs +++ b/ComputerStoreDataModels/Models/IRequestModel.cs @@ -9,6 +9,7 @@ namespace ComputerStoreDataModels.Models public interface IRequestModel : IID { int OrderID { get; } + int PCID { get; } double Price { get; } } } diff --git a/ComputerStoreDatabaseImplement/Implements/ConsignmentStorage.cs b/ComputerStoreDatabaseImplement/Implements/ConsignmentStorage.cs new file mode 100644 index 0000000..d31608d --- /dev/null +++ b/ComputerStoreDatabaseImplement/Implements/ConsignmentStorage.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputerStoreDatabaseImplement.Implements +{ + public class ConsignmentStorage //: IConsignmentStorage + { + + } +} diff --git a/ComputerStoreDatabaseImplement/Implements/OrderStorage.cs b/ComputerStoreDatabaseImplement/Implements/OrderStorage.cs new file mode 100644 index 0000000..9c327dc --- /dev/null +++ b/ComputerStoreDatabaseImplement/Implements/OrderStorage.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputerStoreDatabaseImplement.Implements +{ + public class OrderStorage //: IOrderStorage + { + } +} diff --git a/ComputerStoreDatabaseImplement/Implements/RequestStorage.cs b/ComputerStoreDatabaseImplement/Implements/RequestStorage.cs new file mode 100644 index 0000000..ce9bf4c --- /dev/null +++ b/ComputerStoreDatabaseImplement/Implements/RequestStorage.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputerStoreDatabaseImplement.Implements +{ + public class RequestStorage //: IRequestStorage + { + } +} diff --git a/ComputerStoreDatabaseImplement/Implements/SellerStorage.cs b/ComputerStoreDatabaseImplement/Implements/SellerStorage.cs new file mode 100644 index 0000000..00748ba --- /dev/null +++ b/ComputerStoreDatabaseImplement/Implements/SellerStorage.cs @@ -0,0 +1,78 @@ +using ComputerStoreContracts.BindingModels; +using ComputerStoreContracts.SearchModels; +using ComputerStoreContracts.StorageContracts; +using ComputerStoreContracts.ViewModels; +using ComputerStoreDatabaseImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputerStoreDatabaseImplement.Implements +{ + public class SellerStorage : ISellerStorage + { + public SellerViewModel? GetElement(SellerSearchModel model) + { + if (string.IsNullOrEmpty(model.Username) && !model.ID.HasValue) + { + return null; + } + using var context = new ComputerStoreDatabase(); + return context.Sellers.FirstOrDefault(x => (!string.IsNullOrEmpty(model.Username) && model.Username == x.Username) || (model.ID.HasValue && model.ID == x.ID))?.GetViewModel; + } + + public List GetFilteredList(SellerSearchModel model) + { + if (string.IsNullOrEmpty(model.Username)) + { + return new(); + } + using var context = new ComputerStoreDatabase(); + return context.Sellers.Where(x => x.Username.Equals(model.Username)).Select(x => x.GetViewModel).ToList(); + } + + public List GetFullList() + { + using var context = new ComputerStoreDatabase(); + return context.Sellers.Select(x => x.GetViewModel).ToList(); + } + + public SellerViewModel? Insert(SellerBindingModel model) + { + using var context = new ComputerStoreDatabase(); + var newSeller = Seller.Create(model); + + if (newSeller == null) { return null; } + + context.Sellers.Add(newSeller); + context.SaveChanges(); + return newSeller.GetViewModel; + } + + public SellerViewModel? Update(SellerBindingModel model) + { + using var context = new ComputerStoreDatabase(); + var specSeller = context.Sellers.FirstOrDefault(x => x.ID == model.ID); + + if (specSeller == null) { return null; } + + specSeller.Update(model); + context.SaveChanges(); + return specSeller.GetViewModel; + } + + public SellerViewModel? Delete(SellerBindingModel model) + { + using var context = new ComputerStoreDatabase(); + var specSeller = context.Sellers.FirstOrDefault(x => x.ID == model.ID); + + if (specSeller == null) { return null; } + + context.Sellers.Remove(specSeller); + context.SaveChanges(); + return specSeller.GetViewModel; + } + } +} diff --git a/ComputerStoreDatabaseImplement/Models/Consignment.cs b/ComputerStoreDatabaseImplement/Models/Consignment.cs new file mode 100644 index 0000000..fc0e3b9 --- /dev/null +++ b/ComputerStoreDatabaseImplement/Models/Consignment.cs @@ -0,0 +1,95 @@ +using ComputerStoreContracts.BindingModels; +using ComputerStoreContracts.ViewModels; +using ComputerStoreDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations.Schema; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputerStoreDatabaseImplement.Models +{ + public class Consignment : IConsignmentModel + { + public int ID { get; private set; } + + [Required] + public int OrderID { get; private set; } + + [Required] + public double Price { get; private set; } + + private Dictionary? _consignmentProducts = null; + + [NotMapped] + public Dictionary ConsignmentProducts + { + get + { + if (_consignmentProducts == null) + { + _consignmentProducts = Products.ToDictionary(recPC => recPC.ProductID, recPC => (recPC.Product as IProductModel, recPC.Count)); + } + return _consignmentProducts; + } + } + + [ForeignKey("ConsignmentId")] + public virtual List Components { get; set; } = new(); + + + public virtual Product Product { get; private set; } + private Dictionary? Products = null; + + + + + [ForeignKey("ConsignmentID")] + public virtual List ConsignmentComponent { get; private set; } = new(); + + public static Consignment Create(ComputerStoreDatabase context, ConsignmentBindingModel model) + { + if (model == null) + { + return null; + } + return new Consignment() + { + ID = model.ID, + OrderID = model.OrderID, + Price = model.Price, + _orderProducts = model.ConsignmentProducts + + }; + + //return new Consignment() + //{ + // ID = model.ID, + // OrderID = model.OrderID, + // Price = model.Price, + // Products = model.ConsignmentProducts.Select(x => new ConsignmentComponent + // { + // Component = context.Components.First(y => y.ID == x.Key), + // Count = x.Value.Item2 + // }).ToList() + //}; + } + public void Update(ConsignmentBindingModel? model) + { + if (model == null) + { + return; + } + OrderID = model.OrderID; + Price = model.Price; + } + public ConsignmentViewModel GetViewModel => new() + { + ID = ID, + OrderID = OrderID, + Price = Price + }; + } +} diff --git a/ComputerStoreDatabaseImplement/Models/Order.cs b/ComputerStoreDatabaseImplement/Models/Order.cs new file mode 100644 index 0000000..167b6e9 --- /dev/null +++ b/ComputerStoreDatabaseImplement/Models/Order.cs @@ -0,0 +1,92 @@ +using ComputerStoreContracts.BindingModels; +using ComputerStoreContracts.ViewModels; +using ComputerStoreDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations.Schema; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ComputerStoreDataModels.Enums; +using System.Runtime.ConstrainedExecution; + +namespace ComputerStoreDatabaseImplement.Models +{ + public class Order : IOrderModel + { + public int ID { get; private set; } + + [Required] + public double Price { get; private set; } + + public int? ConsignmentID { get; private set; } + + public int? RequestID { get; private set; } + + [Required] + public OrderStatus Status { get; private set; } = OrderStatus.Unknown; + + [Required] + public DateTime DateCreate { get; private set; } = DateTime.Now; + + public DateTime? DateImplement { get; private set; } + + [Required] + public int SellerID { get; private set; } + + public virtual Seller Seller { get; set; } + public virtual Consignment Consignment { get; private set; } + public virtual Request Request { get; private set; } + + public static Order? Create(OrderBindingModel? model) + { + if (model == null) + { + return null; + } + return new Order + { + Price = model.Price, + ConsignmentID = model.ConsignmentID, + RequestID = model.RequestID, + Status = model.Status, + DateCreate = model.DateCreate, + DateImplement = model.DateImplement, + SellerID = model.SellerID, + ID = model.ID, + }; + } + + public void Update(OrderBindingModel? model) + { + if (model == null) + { + return; + } + Status = model.Status; + DateImplement = model.DateImplement; + } + + public OrderViewModel GetViewModel + { + get + { + var context = new ComputerStoreDatabase(); + return new() + { + + ID = ID, + ConsignmentID = ConsignmentID, + RequestID = RequestID, + DateCreate = DateCreate, + DateImplement = DateImplement, + Status = Status, + Price = Price, + SellerID = SellerID, + SellerUsername = context.Sellers.FirstOrDefault(x => x.ID == SellerID)?.Username ?? string.Empty + }; + } + } + } +} diff --git a/ComputerStoreDatabaseImplement/Models/Request.cs b/ComputerStoreDatabaseImplement/Models/Request.cs new file mode 100644 index 0000000..5a1d63a --- /dev/null +++ b/ComputerStoreDatabaseImplement/Models/Request.cs @@ -0,0 +1,58 @@ +using ComputerStoreContracts.BindingModels; +using ComputerStoreContracts.ViewModels; +using ComputerStoreDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations.Schema; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputerStoreDatabaseImplement.Models +{ + public class Request : IRequestModel + { + public int ID { get; private set; } + + [Required] + public int OrderID { get; private set; } + + [Required] + public double Price { get; private set; } + + [ForeignKey("RequestID")] + public virtual List RequestComponents { get; private set; } = new(); + + public static Request? Create(RequestBindingModel model) + { + if (model == null) + { + return null; + } + return new Request() + { + ID = model.ID, + OrderID = model.OrderID, + Price = model.Price + }; + } + + public void Update(RequestBindingModel? model) + { + if (model == null) + { + return; + } + OrderID = model.OrderID; + Price = model.Price; + } + + public RequestViewModel GetViewModel => new() + { + ID = ID, + OrderID = OrderID, + Price = Price + }; + } +} diff --git a/ComputerStoreDatabaseImplement/Models/Seller.cs b/ComputerStoreDatabaseImplement/Models/Seller.cs new file mode 100644 index 0000000..a48e66a --- /dev/null +++ b/ComputerStoreDatabaseImplement/Models/Seller.cs @@ -0,0 +1,66 @@ +using ComputerStoreContracts.BindingModels; +using ComputerStoreContracts.ViewModels; +using ComputerStoreDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations.Schema; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputerStoreDatabaseImplement.Models +{ + public class Seller : ISellerModel + { + public int ID { get; private set; } + [Required] + public string Username { get; private set; } = string.Empty; + [Required] + public string Password { get; private set; } = string.Empty; + + public string? FirstName { get; private set; } = string.Empty; + public string? LastName { get; private set; } = string.Empty; + public string? MiddleName { get; private set; } = string.Empty; + + [ForeignKey("OrderID")] + public virtual List Orders { get; set; } = new(); + + public static Seller? Create(SellerBindingModel? model) + { + if (model == null) + { + return null; + } + return new Seller() + { + ID = model.ID, + Username = model.Username, + Password = model.Password, + FirstName = model.FirstName, + LastName = model.LastName, + MiddleName = model.MiddleName + }; + } + + public void Update(SellerBindingModel? model) + { + if (model == null) { return; } + Username = model.Username; + Password = model.Password; + FirstName = model.FirstName; + LastName = model.LastName; + MiddleName = model.MiddleName; + } + + public SellerViewModel GetViewModel => new() + { + ID = ID, + Username = Username, + Password = Password, + FirstName = FirstName, + LastName = LastName, + MiddleName = MiddleName + }; + } +}