diff --git a/ElectronicsShop/ElectronicsShopBusinessLogic/BusinessLogic/OrderLogic.cs b/ElectronicsShop/ElectronicsShopBusinessLogic/BusinessLogic/OrderLogic.cs index 70c9c48..f961bbc 100644 --- a/ElectronicsShop/ElectronicsShopBusinessLogic/BusinessLogic/OrderLogic.cs +++ b/ElectronicsShop/ElectronicsShopBusinessLogic/BusinessLogic/OrderLogic.cs @@ -30,6 +30,15 @@ namespace ElectronicsShopBusinessLogic.BusinessLogic return true; } + public bool Update(OrderBindingModel model) { + CheckModel(model); + if (_storage.Update(model) == null) { + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } + public List? ReadList(OrderSearchModel? model) { _logger.LogInformation($"ReadList:ID:{model?.ID}"); @@ -68,29 +77,5 @@ namespace ElectronicsShopBusinessLogic.BusinessLogic _logger.LogInformation($"ReadElement find. ID:{element.ID}"); return element; } - - public bool AddProduct(IProductModel product, int count, int orderID) { - var _order = ReadElement(new OrderSearchModel { ID = orderID }); - try { - if (_order.ProductList.ContainsKey(product.ID)) { - _order.ProductList[product.ID] = (product, count); - } - else { - _order.ProductList.Add(product.ID, (product, count)); - _storage.Update(new OrderBindingModel { - ClientID = _order.ClientID, - ProductList = _order.ProductList, - Sum = 5000, - DateCreate = DateTime.Now, - ID = _order.ID, - }); - } - } - catch { - _logger.LogWarning("AddProduct. operation is failed"); - return false; - } - return true; - } } } diff --git a/ElectronicsShop/ElectronicsShopContracts/BusinessLogicContracts/IOrderLogic.cs b/ElectronicsShop/ElectronicsShopContracts/BusinessLogicContracts/IOrderLogic.cs index ffc04a6..fad4955 100644 --- a/ElectronicsShop/ElectronicsShopContracts/BusinessLogicContracts/IOrderLogic.cs +++ b/ElectronicsShop/ElectronicsShopContracts/BusinessLogicContracts/IOrderLogic.cs @@ -17,6 +17,6 @@ namespace ElectronicsShopContracts.BusinessLogicContracts OrderViewModel? ReadElement(OrderSearchModel model); bool CreateOrder(OrderBindingModel model); - bool AddProduct(IProductModel product, int count, int orderID); + bool Update(OrderBindingModel model); } } diff --git a/ElectronicsShop/ElectronicsShopDataBaseImplement/Implements/OrderStorage.cs b/ElectronicsShop/ElectronicsShopDataBaseImplement/Implements/OrderStorage.cs index d13eb78..8d93598 100644 --- a/ElectronicsShop/ElectronicsShopDataBaseImplement/Implements/OrderStorage.cs +++ b/ElectronicsShop/ElectronicsShopDataBaseImplement/Implements/OrderStorage.cs @@ -27,13 +27,13 @@ namespace ElectronicsShopDataBaseImplement.Implements using var context = new Database(); using var transcation = context.Database.BeginTransaction(); try { - var product = context.Orders.FirstOrDefault(rec => rec.ID == model.ID); - if (product == null) { + var order = context.Orders.FirstOrDefault(rec => rec.ID == model.ID); + if (order == null) { return null; } - product.UpdateProducts(context, model); + order.UpdateProducts(context, model); transcation.Commit(); - return product.GetViewModel; + return order.GetViewModel; } catch { transcation.Rollback(); @@ -61,7 +61,8 @@ namespace ElectronicsShopDataBaseImplement.Implements .Include(x => x.Payments) .Include(x => x.Products) .ThenInclude(x => x._product) - .FirstOrDefault(x => (model.ClientID.HasValue && x.ClientID == model.ClientID))?.GetViewModel; + .OrderBy(x => x.ID) + .LastOrDefault(x => (model.ClientID.HasValue && x.ClientID == model.ClientID))?.GetViewModel; } if (model.ID.HasValue) { @@ -77,7 +78,7 @@ namespace ElectronicsShopDataBaseImplement.Implements public List GetFilteredList(OrderSearchModel model) { using var context = new Database(); - if (!model.ID.HasValue && (model.DateFrom == null || model.DateTo == null)) + if (!model.ID.HasValue && model.DateFrom.HasValue && model.DateTo.HasValue) { return new(); } @@ -99,7 +100,7 @@ namespace ElectronicsShopDataBaseImplement.Implements public List GetFullList() { using var context = new Database(); - return context.Orders + return context.Orders .Include(x => x.Products) .ThenInclude(x => x._product).ToList() .Select(x => x.GetViewModel).ToList(); diff --git a/ElectronicsShop/ElectronicsShopDataBaseImplement/Migrations/20240529092847_Init.Designer.cs b/ElectronicsShop/ElectronicsShopDataBaseImplement/Migrations/20240529092847_Init.Designer.cs deleted file mode 100644 index 57b08ac..0000000 --- a/ElectronicsShop/ElectronicsShopDataBaseImplement/Migrations/20240529092847_Init.Designer.cs +++ /dev/null @@ -1,283 +0,0 @@ -// -using System; -using ElectronicsShopDataBaseImplement; -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Infrastructure; -using Microsoft.EntityFrameworkCore.Metadata; -using Microsoft.EntityFrameworkCore.Migrations; -using Microsoft.EntityFrameworkCore.Storage.ValueConversion; - -#nullable disable - -namespace ElectronicsShopDataBaseImplement.Migrations -{ - [DbContext(typeof(Database))] - [Migration("20240529092847_Init")] - partial class Init - { - /// - protected override void BuildTargetModel(ModelBuilder modelBuilder) - { -#pragma warning disable 612, 618 - modelBuilder - .HasAnnotation("ProductVersion", "8.0.4") - .HasAnnotation("Relational:MaxIdentifierLength", 128); - - SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); - - modelBuilder.Entity("ElectronicsShopDataBaseImplement.Models.Client", b => - { - b.Property("ID") - .ValueGeneratedOnAdd() - .HasColumnType("int"); - - SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("ID")); - - b.Property("ClientFIO") - .IsRequired() - .HasColumnType("nvarchar(max)"); - - b.Property("Email") - .IsRequired() - .HasColumnType("nvarchar(max)"); - - b.Property("Password") - .IsRequired() - .HasColumnType("nvarchar(max)"); - - b.HasKey("ID"); - - b.ToTable("Clients"); - }); - - modelBuilder.Entity("ElectronicsShopDataBaseImplement.Models.CostItem", b => - { - b.Property("ID") - .ValueGeneratedOnAdd() - .HasColumnType("int"); - - SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("ID")); - - b.Property("CostNum") - .HasColumnType("int"); - - b.Property("EmployeeID") - .HasColumnType("int"); - - b.Property("Name") - .IsRequired() - .HasColumnType("nvarchar(max)"); - - b.Property("Price") - .HasColumnType("float"); - - b.HasKey("ID"); - - b.HasIndex("EmployeeID"); - - b.ToTable("CostItems"); - }); - - modelBuilder.Entity("ElectronicsShopDataBaseImplement.Models.Employee", b => - { - b.Property("ID") - .ValueGeneratedOnAdd() - .HasColumnType("int"); - - SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("ID")); - - b.Property("EmployeeFIO") - .IsRequired() - .HasColumnType("nvarchar(max)"); - - b.Property("Login") - .IsRequired() - .HasColumnType("nvarchar(max)"); - - b.Property("Password") - .IsRequired() - .HasColumnType("nvarchar(max)"); - - b.HasKey("ID"); - - b.ToTable("Employees"); - }); - - modelBuilder.Entity("ElectronicsShopDataBaseImplement.Models.Order", b => - { - b.Property("ID") - .ValueGeneratedOnAdd() - .HasColumnType("int"); - - SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("ID")); - - b.Property("ClientID") - .HasColumnType("int"); - - b.Property("DateCreate") - .HasColumnType("datetime2"); - - b.Property("Sum") - .HasColumnType("float"); - - b.HasKey("ID"); - - b.HasIndex("ClientID"); - - b.ToTable("Orders"); - }); - - modelBuilder.Entity("ElectronicsShopDataBaseImplement.Models.OrderProduct", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("int"); - - SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); - - b.Property("Count") - .HasColumnType("int"); - - b.Property("OrderID") - .HasColumnType("int"); - - b.Property("ProductID") - .HasColumnType("int"); - - b.Property("_productID") - .HasColumnType("int"); - - b.HasKey("Id"); - - b.HasIndex("ProductID"); - - b.HasIndex("_productID"); - - b.ToTable("OrderProducts"); - }); - - modelBuilder.Entity("ElectronicsShopDataBaseImplement.Models.Paymeant", b => - { - b.Property("ID") - .ValueGeneratedOnAdd() - .HasColumnType("int"); - - SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("ID")); - - b.Property("OrderID") - .HasColumnType("int"); - - b.Property("PayOption") - .HasColumnType("int"); - - b.Property("PaymentID") - .HasColumnType("int"); - - b.Property("ProductID") - .HasColumnType("int"); - - b.Property("SumPayment") - .HasColumnType("float"); - - b.HasKey("ID"); - - b.HasIndex("PaymentID"); - - b.ToTable("Paymeants"); - }); - - modelBuilder.Entity("ElectronicsShopDataBaseImplement.Models.Product", b => - { - b.Property("ID") - .ValueGeneratedOnAdd() - .HasColumnType("int"); - - SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("ID")); - - b.Property("CostItemID") - .HasColumnType("int"); - - b.Property("Price") - .HasColumnType("float"); - - b.Property("ProductName") - .IsRequired() - .HasColumnType("nvarchar(max)"); - - b.HasKey("ID"); - - b.HasIndex("CostItemID"); - - b.ToTable("Products"); - }); - - modelBuilder.Entity("ElectronicsShopDataBaseImplement.Models.CostItem", b => - { - b.HasOne("ElectronicsShopDataBaseImplement.Models.Employee", "Employee") - .WithMany() - .HasForeignKey("EmployeeID") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.Navigation("Employee"); - }); - - modelBuilder.Entity("ElectronicsShopDataBaseImplement.Models.Order", b => - { - b.HasOne("ElectronicsShopDataBaseImplement.Models.Client", null) - .WithMany("Orders") - .HasForeignKey("ClientID") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - }); - - modelBuilder.Entity("ElectronicsShopDataBaseImplement.Models.OrderProduct", b => - { - b.HasOne("ElectronicsShopDataBaseImplement.Models.Order", "_order") - .WithMany("Products") - .HasForeignKey("ProductID") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.HasOne("ElectronicsShopDataBaseImplement.Models.Product", "_product") - .WithMany() - .HasForeignKey("_productID"); - - b.Navigation("_order"); - - b.Navigation("_product"); - }); - - modelBuilder.Entity("ElectronicsShopDataBaseImplement.Models.Paymeant", b => - { - b.HasOne("ElectronicsShopDataBaseImplement.Models.Order", null) - .WithMany("Payments") - .HasForeignKey("PaymentID"); - }); - - modelBuilder.Entity("ElectronicsShopDataBaseImplement.Models.Product", b => - { - b.HasOne("ElectronicsShopDataBaseImplement.Models.CostItem", "CostItem") - .WithMany() - .HasForeignKey("CostItemID") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.Navigation("CostItem"); - }); - - modelBuilder.Entity("ElectronicsShopDataBaseImplement.Models.Client", b => - { - b.Navigation("Orders"); - }); - - modelBuilder.Entity("ElectronicsShopDataBaseImplement.Models.Order", b => - { - b.Navigation("Payments"); - - b.Navigation("Products"); - }); -#pragma warning restore 612, 618 - } - } -} diff --git a/ElectronicsShop/ElectronicsShopDataBaseImplement/Migrations/20240530150159_AddMessages.cs b/ElectronicsShop/ElectronicsShopDataBaseImplement/Migrations/20240530150159_AddMessages.cs deleted file mode 100644 index 8284989..0000000 --- a/ElectronicsShop/ElectronicsShopDataBaseImplement/Migrations/20240530150159_AddMessages.cs +++ /dev/null @@ -1,48 +0,0 @@ -using System; -using Microsoft.EntityFrameworkCore.Migrations; - -#nullable disable - -namespace ElectronicsShopDataBaseImplement.Migrations -{ - /// - public partial class AddMessages : Migration - { - /// - protected override void Up(MigrationBuilder migrationBuilder) - { - migrationBuilder.CreateTable( - name: "Messages", - columns: table => new - { - MessageID = table.Column(type: "nvarchar(450)", nullable: false), - ClientID = table.Column(type: "int", nullable: true), - SenderName = table.Column(type: "nvarchar(max)", nullable: false), - DateDelivery = table.Column(type: "datetime2", nullable: false), - Subject = table.Column(type: "nvarchar(max)", nullable: false), - Body = table.Column(type: "nvarchar(max)", nullable: false) - }, - constraints: table => - { - table.PrimaryKey("PK_Messages", x => x.MessageID); - table.ForeignKey( - name: "FK_Messages_Clients_ClientID", - column: x => x.ClientID, - principalTable: "Clients", - principalColumn: "ID"); - }); - - migrationBuilder.CreateIndex( - name: "IX_Messages_ClientID", - table: "Messages", - column: "ClientID"); - } - - /// - protected override void Down(MigrationBuilder migrationBuilder) - { - migrationBuilder.DropTable( - name: "Messages"); - } - } -} diff --git a/ElectronicsShop/ElectronicsShopDataBaseImplement/Migrations/20240530150159_AddMessages.Designer.cs b/ElectronicsShop/ElectronicsShopDataBaseImplement/Migrations/20240531061328_InitMigration.Designer.cs similarity index 97% rename from ElectronicsShop/ElectronicsShopDataBaseImplement/Migrations/20240530150159_AddMessages.Designer.cs rename to ElectronicsShop/ElectronicsShopDataBaseImplement/Migrations/20240531061328_InitMigration.Designer.cs index daf6f22..2b6c0ab 100644 --- a/ElectronicsShop/ElectronicsShopDataBaseImplement/Migrations/20240530150159_AddMessages.Designer.cs +++ b/ElectronicsShop/ElectronicsShopDataBaseImplement/Migrations/20240531061328_InitMigration.Designer.cs @@ -12,8 +12,8 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion; namespace ElectronicsShopDataBaseImplement.Migrations { [DbContext(typeof(Database))] - [Migration("20240530150159_AddMessages")] - partial class AddMessages + [Migration("20240531061328_InitMigration")] + partial class InitMigration { /// protected override void BuildTargetModel(ModelBuilder modelBuilder) @@ -174,14 +174,11 @@ namespace ElectronicsShopDataBaseImplement.Migrations b.Property("ProductID") .HasColumnType("int"); - b.Property("_productID") - .HasColumnType("int"); - b.HasKey("Id"); - b.HasIndex("ProductID"); + b.HasIndex("OrderID"); - b.HasIndex("_productID"); + b.HasIndex("ProductID"); b.ToTable("OrderProducts"); }); @@ -274,13 +271,15 @@ namespace ElectronicsShopDataBaseImplement.Migrations { b.HasOne("ElectronicsShopDataBaseImplement.Models.Order", "_order") .WithMany("Products") - .HasForeignKey("ProductID") + .HasForeignKey("OrderID") .OnDelete(DeleteBehavior.Cascade) .IsRequired(); b.HasOne("ElectronicsShopDataBaseImplement.Models.Product", "_product") .WithMany() - .HasForeignKey("_productID"); + .HasForeignKey("ProductID") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); b.Navigation("_order"); diff --git a/ElectronicsShop/ElectronicsShopDataBaseImplement/Migrations/20240529092847_Init.cs b/ElectronicsShop/ElectronicsShopDataBaseImplement/Migrations/20240531061328_InitMigration.cs similarity index 83% rename from ElectronicsShop/ElectronicsShopDataBaseImplement/Migrations/20240529092847_Init.cs rename to ElectronicsShop/ElectronicsShopDataBaseImplement/Migrations/20240531061328_InitMigration.cs index e164d84..6c6ae62 100644 --- a/ElectronicsShop/ElectronicsShopDataBaseImplement/Migrations/20240529092847_Init.cs +++ b/ElectronicsShop/ElectronicsShopDataBaseImplement/Migrations/20240531061328_InitMigration.cs @@ -6,7 +6,7 @@ using Microsoft.EntityFrameworkCore.Migrations; namespace ElectronicsShopDataBaseImplement.Migrations { /// - public partial class Init : Migration + public partial class InitMigration : Migration { /// protected override void Up(MigrationBuilder migrationBuilder) @@ -41,6 +41,27 @@ namespace ElectronicsShopDataBaseImplement.Migrations table.PrimaryKey("PK_Employees", x => x.ID); }); + migrationBuilder.CreateTable( + name: "Messages", + columns: table => new + { + MessageID = table.Column(type: "nvarchar(450)", nullable: false), + ClientID = table.Column(type: "int", nullable: true), + SenderName = table.Column(type: "nvarchar(max)", nullable: false), + DateDelivery = table.Column(type: "datetime2", nullable: false), + Subject = table.Column(type: "nvarchar(max)", nullable: false), + Body = table.Column(type: "nvarchar(max)", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Messages", x => x.MessageID); + table.ForeignKey( + name: "FK_Messages_Clients_ClientID", + column: x => x.ClientID, + principalTable: "Clients", + principalColumn: "ID"); + }); + migrationBuilder.CreateTable( name: "Orders", columns: table => new @@ -135,23 +156,23 @@ namespace ElectronicsShopDataBaseImplement.Migrations .Annotation("SqlServer:Identity", "1, 1"), OrderID = table.Column(type: "int", nullable: false), ProductID = table.Column(type: "int", nullable: false), - Count = table.Column(type: "int", nullable: false), - _productID = table.Column(type: "int", nullable: true) + Count = table.Column(type: "int", nullable: false) }, constraints: table => { table.PrimaryKey("PK_OrderProducts", x => x.Id); table.ForeignKey( - name: "FK_OrderProducts_Orders_ProductID", - column: x => x.ProductID, + name: "FK_OrderProducts_Orders_OrderID", + column: x => x.OrderID, principalTable: "Orders", principalColumn: "ID", onDelete: ReferentialAction.Cascade); table.ForeignKey( - name: "FK_OrderProducts_Products__productID", - column: x => x._productID, + name: "FK_OrderProducts_Products_ProductID", + column: x => x.ProductID, principalTable: "Products", - principalColumn: "ID"); + principalColumn: "ID", + onDelete: ReferentialAction.Cascade); }); migrationBuilder.CreateIndex( @@ -160,9 +181,14 @@ namespace ElectronicsShopDataBaseImplement.Migrations column: "EmployeeID"); migrationBuilder.CreateIndex( - name: "IX_OrderProducts__productID", + name: "IX_Messages_ClientID", + table: "Messages", + column: "ClientID"); + + migrationBuilder.CreateIndex( + name: "IX_OrderProducts_OrderID", table: "OrderProducts", - column: "_productID"); + column: "OrderID"); migrationBuilder.CreateIndex( name: "IX_OrderProducts_ProductID", @@ -188,6 +214,9 @@ namespace ElectronicsShopDataBaseImplement.Migrations /// protected override void Down(MigrationBuilder migrationBuilder) { + migrationBuilder.DropTable( + name: "Messages"); + migrationBuilder.DropTable( name: "OrderProducts"); diff --git a/ElectronicsShop/ElectronicsShopDataBaseImplement/Migrations/DatabaseModelSnapshot.cs b/ElectronicsShop/ElectronicsShopDataBaseImplement/Migrations/DatabaseModelSnapshot.cs index a578fb3..ed01306 100644 --- a/ElectronicsShop/ElectronicsShopDataBaseImplement/Migrations/DatabaseModelSnapshot.cs +++ b/ElectronicsShop/ElectronicsShopDataBaseImplement/Migrations/DatabaseModelSnapshot.cs @@ -171,14 +171,11 @@ namespace ElectronicsShopDataBaseImplement.Migrations b.Property("ProductID") .HasColumnType("int"); - b.Property("_productID") - .HasColumnType("int"); - b.HasKey("Id"); - b.HasIndex("ProductID"); + b.HasIndex("OrderID"); - b.HasIndex("_productID"); + b.HasIndex("ProductID"); b.ToTable("OrderProducts"); }); @@ -271,13 +268,15 @@ namespace ElectronicsShopDataBaseImplement.Migrations { b.HasOne("ElectronicsShopDataBaseImplement.Models.Order", "_order") .WithMany("Products") - .HasForeignKey("ProductID") + .HasForeignKey("OrderID") .OnDelete(DeleteBehavior.Cascade) .IsRequired(); b.HasOne("ElectronicsShopDataBaseImplement.Models.Product", "_product") .WithMany() - .HasForeignKey("_productID"); + .HasForeignKey("ProductID") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); b.Navigation("_order"); diff --git a/ElectronicsShop/ElectronicsShopDataBaseImplement/Models/Order.cs b/ElectronicsShop/ElectronicsShopDataBaseImplement/Models/Order.cs index 4a17865..bda47a0 100644 --- a/ElectronicsShop/ElectronicsShopDataBaseImplement/Models/Order.cs +++ b/ElectronicsShop/ElectronicsShopDataBaseImplement/Models/Order.cs @@ -40,7 +40,7 @@ namespace ElectronicsShopDataBaseImplement.Models } } - [ForeignKey("ProductID")] + [ForeignKey("OrderID")] public virtual List Products { get; set; } = new(); [ForeignKey("PaymentID")] @@ -72,10 +72,11 @@ namespace ElectronicsShopDataBaseImplement.Models ClientID = ClientID, Sum = Sum, DateCreate = DateCreate, + ProductList = ProductList, }; public void UpdateProducts(Database context,OrderBindingModel model) { - var orderProducts = context.OrderProducts.Where(rec => rec.ProductID == model.ID).ToList(); + var orderProducts = context.OrderProducts.Where(rec => rec.OrderID == model.ID).ToList(); if (orderProducts != null && orderProducts.Count > 0) { context.OrderProducts.RemoveRange(orderProducts.Where(rec => !model.ProductList.ContainsKey(rec.ProductID))); context.SaveChanges(); @@ -85,7 +86,7 @@ namespace ElectronicsShopDataBaseImplement.Models } context.SaveChanges(); } - var order = context.Orders.First(x => x.ID == ID); + var order = context.Orders.First(x => x.ID == model.ID); foreach (var op in model.ProductList) { context.OrderProducts.Add(new OrderProduct { _order = order, diff --git a/ElectronicsShop/ElectronicsShopDataBaseImplement/Models/OrderProduct.cs b/ElectronicsShop/ElectronicsShopDataBaseImplement/Models/OrderProduct.cs index c592e73..359727c 100644 --- a/ElectronicsShop/ElectronicsShopDataBaseImplement/Models/OrderProduct.cs +++ b/ElectronicsShop/ElectronicsShopDataBaseImplement/Models/OrderProduct.cs @@ -12,8 +12,10 @@ namespace ElectronicsShopDataBaseImplement.Models { public int Id { get; set; } + [Required] public int OrderID { get; set; } + [Required] public int ProductID { get; set; } [Required] diff --git a/ElectronicsShop/ElectronicsShopDataBaseImplement/Models/Product.cs b/ElectronicsShop/ElectronicsShopDataBaseImplement/Models/Product.cs index 0ca9adf..2f940f5 100644 --- a/ElectronicsShop/ElectronicsShopDataBaseImplement/Models/Product.cs +++ b/ElectronicsShop/ElectronicsShopDataBaseImplement/Models/Product.cs @@ -56,7 +56,7 @@ namespace ElectronicsShopDataBaseImplement.Models ProductName = ProductName, Price = Price, CostItemID = CostItemID, - CostItemName = CostItem.Name + CostItemName = CostItem?.Name }; } } diff --git a/ElectronicsShop/ElectronicsShopRestAPI/Controllers/MainController.cs b/ElectronicsShop/ElectronicsShopRestAPI/Controllers/MainController.cs index 38b9be9..88a33cf 100644 --- a/ElectronicsShop/ElectronicsShopRestAPI/Controllers/MainController.cs +++ b/ElectronicsShop/ElectronicsShopRestAPI/Controllers/MainController.cs @@ -2,9 +2,11 @@ using ElectronicsShopContracts.BusinessLogicContracts; using ElectronicsShopContracts.SearchModels; using ElectronicsShopContracts.ViewModels; +using ElectronicsShopDataBaseImplement; using ElectronicsShopDataBaseImplement.Models; using ElectronicsShopDataModels.Models; using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; using Newtonsoft.Json; namespace ElectronicsShopRestAPI.Controllers { @@ -18,11 +20,14 @@ namespace ElectronicsShopRestAPI.Controllers { private readonly IOrderLogic _order; private readonly IMessageInfoLogic _message; + private Dictionary _productlist; + public MainController(ILogger logger, IProductLogic product, IOrderLogic orderLogic) { _logger = logger; _product = product; _order = orderLogic; + _productlist = new Dictionary(); } [HttpGet] @@ -89,6 +94,28 @@ namespace ElectronicsShopRestAPI.Controllers { } } + [HttpGet] + public List> GetOrderProducts(int _orderid) { + var list = new List>(); + try { + var products = _order.ReadElement(new OrderSearchModel { ID = _orderid}); + + foreach (var pr in products.ProductList) { + var sentence = new List { + JsonConvert.SerializeObject(pr.Value.Item1), + JsonConvert.SerializeObject(pr.Value.Item2.ToString()) + }; + + list.Add(sentence); + } + return list; + } + catch (Exception ex) { + _logger.LogError(ex, "Ошибка получения списка товаров"); + throw; + } + } + [HttpPost] public void AddProduct(List jslist) { @@ -96,11 +123,39 @@ namespace ElectronicsShopRestAPI.Controllers { int count = JsonConvert.DeserializeObject(jslist[1]); int orderid = JsonConvert.DeserializeObject(jslist[2]); + var view = _order.ReadElement(new OrderSearchModel { ID = orderid }); + if (view != null) { + _productlist = view.ProductList; + } + + + _logger.LogInformation($"Добавление нового товара: {product.ProductName} - {count}"); + if (_productlist.ContainsKey(product.ID)) { + _productlist[product.ID] = (product, count); + } + else { + _productlist.Add(product.ID, (product, count)); + } + + if (_productlist == null || _productlist.Count == 0) { + _logger.LogInformation("Корзина пуста, ошибка"); + } + _logger.LogInformation("Сохранение Заказа"); + try { - _order.AddProduct(product, count, orderid); + var model = new OrderBindingModel { + ID = orderid, + ClientID = view.ClientID, + DateCreate = view.DateCreate, + ProductList = _productlist + }; + var operationResult = _order.Update(model); + if (!operationResult) { + throw new Exception("Ошибка при сохранении, дополнительная информация в логах"); + } } catch (Exception ex) { - _logger.LogError(ex, "Ошибка добавления заказа"); + _logger.LogError(ex, "Ошибка добавления товара"); throw; } } diff --git a/ElectronicsShop/ElectronicsShopShopClientApp/Controllers/HomeController.cs b/ElectronicsShop/ElectronicsShopShopClientApp/Controllers/HomeController.cs index f88098f..3ef23d6 100644 --- a/ElectronicsShop/ElectronicsShopShopClientApp/Controllers/HomeController.cs +++ b/ElectronicsShop/ElectronicsShopShopClientApp/Controllers/HomeController.cs @@ -5,9 +5,11 @@ using ElectronicsShopContracts.ViewModels; using ElectronicsShopDataModels.Models; using ElectronicsShopUserApp.Models; using Microsoft.AspNetCore.Mvc; +using Newtonsoft.Json; using System.Diagnostics; using System.Reflection; using System.Runtime.Serialization; +using System.Xml.Serialization; namespace ElectronicsShopUserApp.Controllers { public class HomeController : Controller { @@ -18,6 +20,7 @@ namespace ElectronicsShopUserApp.Controllers { public HomeController(ILogger logger/*, IOrderLogic orderLogic*/) { _logger = logger; //_order = orderLogic; + _productList = new Dictionary(); } public IActionResult Index() { @@ -101,8 +104,9 @@ namespace ElectronicsShopUserApp.Controllers { return Redirect("~/Home/Enter"); } + var view = APIClient.GetRequset>($"api/main/getorders?_clientid={APIClient.Client.ID}"); - return View(APIClient.GetRequset>($"api/main/getorders?_clientid={APIClient.Client.ID}")); + return View(view); } [HttpGet] @@ -123,8 +127,20 @@ namespace ElectronicsShopUserApp.Controllers { return Redirect("~/Home/Enter"); } var view = APIClient.GetRequset($"api/main/getorder?_clientid={APIClient.Client?.ID}"); + var products = APIClient.GetRequset>>($"api/main/getorderproducts?_orderid={view?.ID}"); - return View(view); + foreach (var pr in products) { + var product = JsonConvert.DeserializeObject(pr[0]); + int count = JsonConvert.DeserializeObject(pr[1]); + _productList.Add(product.ID, (product, count)); + } + + return View(_productList); + } + + [HttpPost] + public void OrderView(int sum) { + Response.Redirect("Orders"); } [HttpGet] @@ -142,16 +158,6 @@ namespace ElectronicsShopUserApp.Controllers { Response.Redirect("OrderView"); } - [HttpPost] - private double CalcAll(Dictionary ProductList) - { - Double Sum = 0; - foreach (var ProductItem in ProductList) - { - Sum += (ProductItem.Value.Item1.Price * ProductItem.Value.Item2); - } - return Sum; - } [HttpPost] public double Calc(int count, int product) { diff --git a/ElectronicsShop/ElectronicsShopShopClientApp/Views/Home/OrderView.cshtml b/ElectronicsShop/ElectronicsShopShopClientApp/Views/Home/OrderView.cshtml index 58a0f13..33dae27 100644 --- a/ElectronicsShop/ElectronicsShopShopClientApp/Views/Home/OrderView.cshtml +++ b/ElectronicsShop/ElectronicsShopShopClientApp/Views/Home/OrderView.cshtml @@ -1,6 +1,7 @@ @using ElectronicsShopContracts.ViewModels +@using ElectronicsShopDataModels.Models -@model OrderViewModel +@model Dictionary @{ ViewData["Title"] = "OrderView"; @@ -10,11 +11,12 @@

Создание корзины

-
+
+
- +
Сумма:
- +

@@ -35,22 +37,53 @@ - @foreach (var item in Model.ProductList) { - + @foreach (var item in Model) { + - @Html.DisplayFor(modelItem => item.Value.Item1.ProductName); - - - @Html.DisplayFor(modelItem => item.Value.Item2); - - - @Html.DisplayFor(modelItem => (item.Value.Item1.Price * item.Value.Item2)); + @Html.DisplayFor(modelItem => item.Value.Item1.ProductName) + + + @Html.DisplayFor(modelItem => item.Value.Item2) + + + + @Html.DisplayFor(modelItem => item.Value.Item1.Price) + + - + Удалить } -

\ No newline at end of file +
+
+
+ +
+
+
+ + + + \ No newline at end of file