diff --git a/BusinessLogic/BusinessLogic/BillLogic.cs b/BusinessLogic/BusinessLogic/BillLogic.cs index 674c943..16130e1 100644 --- a/BusinessLogic/BusinessLogic/BillLogic.cs +++ b/BusinessLogic/BusinessLogic/BillLogic.cs @@ -40,6 +40,7 @@ namespace BusinessLogic.BusinessLogic h1Paragraph.Format.Font.Bold = true; h1Paragraph.Format.Font.Size = 16; h1Paragraph.AddText(h1); // Текст заголовка + h1Paragraph.Format.Alignment = ParagraphAlignment.Center; Paragraph h2Paragraph = section.AddParagraph(); h2Paragraph.Format.Font.Bold = true; @@ -64,18 +65,19 @@ namespace BusinessLogic.BusinessLogic table.Borders.Width = 0.75; // Определение колонок - Column columnProduct = table.AddColumn(Unit.FromCentimeter(1)); - columnProduct.Format.Alignment = ParagraphAlignment.Left; + Column columnProduct = table.AddColumn(Unit.FromCentimeter(3)); - Column columnPrice = table.AddColumn(Unit.FromCentimeter(1)); - columnPrice.Format.Alignment = ParagraphAlignment.Right; + Column columnPrice = table.AddColumn(Unit.FromCentimeter(2)); // Добавление строк с товарами foreach (var product in model.Products) { - Row row = table.AddRow(); - row.Cells[0].AddParagraph(product.Name); - row.Cells[1].AddParagraph($"{product.ActualPrice} руб."); + var row = table.AddRow(); + var cell0 = row[0]; + cell0.AddParagraph(product.Name); + + var cell1 = row[1]; + cell1.AddParagraph($"{product.ActualPrice} руб."); } Paragraph totalParagraph = section.AddParagraph(); diff --git a/BusinessLogic/BusinessLogic/CartItemLogic.cs b/BusinessLogic/BusinessLogic/CartItemLogic.cs index 546e327..67fefcc 100644 --- a/BusinessLogic/BusinessLogic/CartItemLogic.cs +++ b/BusinessLogic/BusinessLogic/CartItemLogic.cs @@ -101,5 +101,13 @@ namespace BusinessLogic.BusinessLogic _logger.LogInformation("Cart Item. Id: { Id}", model.Id); } + public void CloseItem(CartItemViewModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + _cartItemStorage.CloseItem(model); + } } } diff --git a/BusinessLogic/BusinessLogic/PurchaseLogic.cs b/BusinessLogic/BusinessLogic/PurchaseLogic.cs index 4ea4fd8..374c56c 100644 --- a/BusinessLogic/BusinessLogic/PurchaseLogic.cs +++ b/BusinessLogic/BusinessLogic/PurchaseLogic.cs @@ -22,14 +22,12 @@ namespace BusinessLogic.BusinessLogic public class PurchaseLogic : IPurchaseLogic { private readonly IPurchaseStorage _purchaseStorage; - private readonly IProductStorage _productStorage; private readonly ILogger _logger; - public PurchaseLogic(IPurchaseStorage purchaseStorage, IProductStorage productStorage, ILogger logger) + public PurchaseLogic(IPurchaseStorage purchaseStorage, ILogger logger) { _purchaseStorage = purchaseStorage; _logger = logger; - _productStorage = productStorage; } diff --git a/Contracts/BindingModels/CartItemBindingModel.cs b/Contracts/BindingModels/CartItemBindingModel.cs index fc042d1..409c24c 100644 --- a/Contracts/BindingModels/CartItemBindingModel.cs +++ b/Contracts/BindingModels/CartItemBindingModel.cs @@ -16,5 +16,7 @@ namespace Contracts.BindingModels public int Count { get; set; } public Guid UserId { get; set; } public Guid? PurchaseId { get; set; } + public IPurchase? Purchase { get; set; } + public bool IsClosed { get; set; } } } diff --git a/Contracts/BusinessLogicContracts/ICartItemLogic.cs b/Contracts/BusinessLogicContracts/ICartItemLogic.cs index ad1a4aa..3599e21 100644 --- a/Contracts/BusinessLogicContracts/ICartItemLogic.cs +++ b/Contracts/BusinessLogicContracts/ICartItemLogic.cs @@ -20,5 +20,6 @@ namespace Contracts.BusinessLogicContracts List ReadElements(CartItemSearchModel? model); bool Delete(CartItemBindingModel model); + void CloseItem(CartItemViewModel model); } } diff --git a/Contracts/SearchModels/CartItemSearchModel.cs b/Contracts/SearchModels/CartItemSearchModel.cs index 27dd057..719a2df 100644 --- a/Contracts/SearchModels/CartItemSearchModel.cs +++ b/Contracts/SearchModels/CartItemSearchModel.cs @@ -12,5 +12,6 @@ namespace Contracts.SearchModels public Guid? ProductId { get; set; } public string? ProductName { get; set; } public Guid? UserId { get; set; } + public bool? isClosed { get; set; } } } diff --git a/Contracts/StorageContracts/ICartItemStorage.cs b/Contracts/StorageContracts/ICartItemStorage.cs index e7a35d4..21c88a7 100644 --- a/Contracts/StorageContracts/ICartItemStorage.cs +++ b/Contracts/StorageContracts/ICartItemStorage.cs @@ -17,5 +17,6 @@ namespace Contracts.StorageContracts CartItemViewModel? Insert(CartItemBindingModel model); CartItemViewModel? Update(CartItemBindingModel model); CartItemViewModel? Delete(CartItemBindingModel model); + void CloseItem(CartItemViewModel model); } } diff --git a/DataModels/Models/ICartItem.cs b/DataModels/Models/ICartItem.cs index 4dba12d..3b35642 100644 --- a/DataModels/Models/ICartItem.cs +++ b/DataModels/Models/ICartItem.cs @@ -13,5 +13,6 @@ namespace DataModels.Models DateTime DateCreated { get; set; } Guid ProductId { get; set; } string ProductName { get; set; } + bool IsClosed { get; set; } } } diff --git a/DatabaseImplement/Implements/CartItemStorage.cs b/DatabaseImplement/Implements/CartItemStorage.cs index 8dadcc7..61cb859 100644 --- a/DatabaseImplement/Implements/CartItemStorage.cs +++ b/DatabaseImplement/Implements/CartItemStorage.cs @@ -40,11 +40,23 @@ namespace DatabaseImplement.Implements public List GetFilteredList(CartItemSearchModel model) { - if (model.UserId == Guid.Empty && string.IsNullOrEmpty(model.ProductName)) + + if (model.UserId == Guid.Empty && string.IsNullOrEmpty(model.ProductName) && model.isClosed == null) { + Console.WriteLine(); return new(); } using var context = new Database(); + + if (model.UserId != Guid.Empty && model.isClosed != null) + { + return context.CartItems + .Where(x => x.UserId == model.UserId).Where(x => x.IsClosed == model.isClosed) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + if (model.UserId != Guid.Empty) { return context.CartItems @@ -53,6 +65,14 @@ namespace DatabaseImplement.Implements .Select(x => x.GetViewModel) .ToList(); } + if (model.isClosed != null) + { + return context.CartItems + .Where(x => x.IsClosed == model.isClosed) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } return context.CartItems .Where(x => x.ProductName.Contains(model.ProductName)) .ToList() @@ -108,5 +128,41 @@ namespace DatabaseImplement.Implements throw; } } + public void CloseItem(CartItemViewModel model) + { + using var context = new Database(); + using var transaction = context.Database.BeginTransaction(); + try + { + var item = context.CartItems.FirstOrDefault(rec => + rec.Id == model.Id); + + if (item == null) + { + return; + } + + var product = context.Products.FirstOrDefault(x => x.Id == model.ProductId); + + if (product == null) + { + return; + } + var binding = product.GetBindingModel(); + binding.Amount -= item.Count; + product.Update(binding); + + item.IsClosed = true; + + context.SaveChanges(); + transaction.Commit(); + return; + } + catch + { + transaction.Rollback(); + throw; + } + } } } diff --git a/DatabaseImplement/Implements/PurchaseStorage.cs b/DatabaseImplement/Implements/PurchaseStorage.cs index 5fc14cb..77bf46d 100644 --- a/DatabaseImplement/Implements/PurchaseStorage.cs +++ b/DatabaseImplement/Implements/PurchaseStorage.cs @@ -50,7 +50,7 @@ namespace DatabaseImplement.Implements public List GetFilteredList(PurchaseSearchModel? model) { using var context = new Database(); - if (model.UserId == Guid.Empty && !model.CostFrom.HasValue && !model.CostTo.HasValue && !model.DateTo.HasValue + if (model != null && model.UserId == Guid.Empty && !model.CostFrom.HasValue && !model.CostTo.HasValue && !model.DateTo.HasValue && !model.DateFrom.HasValue && !model.Status.HasValue) { return new(); @@ -116,36 +116,62 @@ namespace DatabaseImplement.Implements .ToList() .Select(x => x.GetViewModel) .ToList(); - } + } public PurchaseViewModel? Insert(PurchaseBindingModel model) { using var context = new Database(); - var purchase = Purchase.Create(context, model); - if (purchase == null) - return null; - - - var cartItems = context.CartItems - .Where(x => x.PurchaseId == purchase.Id).ToList() - .Select(x => x.GetViewModel).ToList(); - - - var products = new List(); - - foreach (var item in cartItems) + using var transaction = context.Database.BeginTransaction(); + try { - var product = context.Products - .Where(x => x.Id == item.ProductId) - .FirstOrDefault(); + var purchase = Purchase.Create(context, model); + if (purchase == null) + return null; - products.Add(product); + var cartItems = context.CartItems + .Where(x => !x.IsClosed).ToList(); + var updated = new List(); + + foreach (var item in cartItems) + { + var bitem = item.GetBindingModel(); + bitem.IsClosed = true; + bitem.PurchaseId = purchase.Id; + bitem.Purchase = purchase; + item.Update(bitem); + updated.Add(item); + Console.WriteLine(); + } + + var products = new List(); + + foreach (var item in updated) + { + var product = context.Products + .FirstOrDefault(x => x.Id == item.ProductId); + + products.Add(product); + } + + purchase.Products = products; + context.Purchases.Add(purchase); + + context.SaveChanges(); + transaction.Commit(); + + return purchase.GetViewModel; } + catch (DbUpdateException ex) + { + transaction.Rollback(); + throw new InvalidOperationException("Не удалось сохранить изменения.", ex); + } + finally + { + transaction.Dispose(); + } + return null; - purchase.Products = products; - context.Purchases.Add(purchase); - context.SaveChanges(); - return purchase.GetViewModel; } public PurchaseViewModel? Update(PurchaseBindingModel model) @@ -196,17 +222,18 @@ namespace DatabaseImplement.Implements { return null; } - - var purchase = context.Purchases - .FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id)); - var products = new List(); - - if (purchase.Products == null) - return null; - - foreach (var product in purchase.Products) - products.Add(product.GetViewModel); + var cartItems = GetCartItems(model); + if (cartItems != null) + { + foreach (var item in cartItems) + { + var product = context.Products + .FirstOrDefault(x => x.Id == item.ProductId)?.GetViewModel; + if (product != null) products.Add(product) ; + } + } + else return null; return products; } diff --git a/DatabaseImplement/Migrations/20240730191618_closeitem.Designer.cs b/DatabaseImplement/Migrations/20240730191618_closeitem.Designer.cs new file mode 100644 index 0000000..8723c21 --- /dev/null +++ b/DatabaseImplement/Migrations/20240730191618_closeitem.Designer.cs @@ -0,0 +1,571 @@ +// +using System; +using DatabaseImplement; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +#nullable disable + +namespace DatabaseImplement.Migrations +{ + [DbContext(typeof(Database))] + [Migration("20240730191618_closeitem")] + partial class closeitem + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "8.0.6") + .HasAnnotation("Relational:MaxIdentifierLength", 63); + + NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); + + modelBuilder.Entity("DatabaseImplement.Models.CartItem", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("Count") + .HasColumnType("integer"); + + b.Property("DateCreated") + .HasColumnType("timestamp without time zone"); + + b.Property("IsClosed") + .HasColumnType("boolean"); + + b.Property("ProductId") + .HasColumnType("uuid"); + + b.Property("ProductName") + .IsRequired() + .HasColumnType("text"); + + b.Property("PurchaseId") + .HasColumnType("uuid"); + + b.Property("UserId") + .HasColumnType("uuid"); + + b.HasKey("Id"); + + b.HasIndex("ProductId"); + + b.HasIndex("PurchaseId"); + + b.ToTable("CartItems"); + }); + + modelBuilder.Entity("DatabaseImplement.Models.MediaFile", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("Image") + .IsRequired() + .HasColumnType("bytea"); + + b.Property("ProductId") + .HasColumnType("uuid"); + + b.HasKey("Id"); + + b.HasIndex("ProductId"); + + b.ToTable("MediaFiles"); + }); + + modelBuilder.Entity("DatabaseImplement.Models.Product", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("Amount") + .HasColumnType("integer"); + + b.Property("Category") + .HasColumnType("text"); + + b.Property("Description") + .HasColumnType("text"); + + b.Property("IsBeingSold") + .HasColumnType("boolean"); + + b.Property("Name") + .IsRequired() + .HasColumnType("text"); + + b.Property("Price") + .HasColumnType("double precision"); + + b.Property("Rate") + .HasColumnType("double precision"); + + b.Property("SaleId") + .HasColumnType("uuid"); + + b.HasKey("Id"); + + b.HasIndex("SaleId"); + + b.ToTable("Products"); + }); + + modelBuilder.Entity("DatabaseImplement.Models.Purchase", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("Cost") + .HasColumnType("double precision"); + + b.Property("DateClosed") + .HasColumnType("timestamp without time zone"); + + b.Property("DateCreated") + .HasColumnType("timestamp without time zone"); + + b.Property("IsPaid") + .HasColumnType("boolean"); + + b.Property("ProductCount") + .HasColumnType("integer"); + + b.Property("Status") + .HasColumnType("integer"); + + b.Property("UserId") + .HasColumnType("uuid"); + + b.HasKey("Id"); + + b.HasIndex("UserId"); + + b.ToTable("Purchases"); + }); + + modelBuilder.Entity("DatabaseImplement.Models.Role", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("Name") + .IsRequired() + .HasColumnType("text"); + + b.HasKey("Id"); + + b.ToTable("Roles"); + }); + + modelBuilder.Entity("DatabaseImplement.Models.Sale", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("Category") + .HasColumnType("text"); + + b.Property("Description") + .IsRequired() + .HasColumnType("text"); + + b.Property("End") + .HasColumnType("timestamp without time zone"); + + b.Property("FullDescription") + .IsRequired() + .HasColumnType("text"); + + b.Property("Name") + .IsRequired() + .HasColumnType("text"); + + b.Property("Start") + .HasColumnType("timestamp without time zone"); + + b.Property("Value") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.ToTable("Sales"); + }); + + modelBuilder.Entity("DatabaseImplement.Models.Sell", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("DateSell") + .HasColumnType("timestamp without time zone"); + + b.Property("UserId") + .HasColumnType("uuid"); + + b.HasKey("Id"); + + b.HasIndex("UserId"); + + b.ToTable("Sells"); + }); + + modelBuilder.Entity("DatabaseImplement.Models.SellProducts", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("Count") + .HasColumnType("integer"); + + b.Property("ProductId") + .HasColumnType("uuid"); + + b.Property("SellId") + .HasColumnType("uuid"); + + b.HasKey("Id"); + + b.HasIndex("ProductId"); + + b.HasIndex("SellId"); + + b.ToTable("SellProducts"); + }); + + modelBuilder.Entity("DatabaseImplement.Models.Supplier", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("Deals") + .HasColumnType("integer"); + + b.Property("Name") + .IsRequired() + .HasColumnType("text"); + + b.HasKey("Id"); + + b.ToTable("Suppliers"); + }); + + modelBuilder.Entity("DatabaseImplement.Models.SupplierProduct", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("Count") + .HasColumnType("integer"); + + b.Property("ProductId") + .HasColumnType("uuid"); + + b.Property("SupplierId") + .HasColumnType("uuid"); + + b.HasKey("Id"); + + b.HasIndex("ProductId"); + + b.HasIndex("SupplierId"); + + b.ToTable("SupplierProducts"); + }); + + modelBuilder.Entity("DatabaseImplement.Models.Supply", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("Date") + .HasColumnType("timestamp without time zone"); + + b.Property("DateArriving") + .HasColumnType("timestamp without time zone"); + + b.Property("DateComplete") + .HasColumnType("timestamp without time zone"); + + b.Property("Name") + .IsRequired() + .HasColumnType("text"); + + b.Property("Price") + .HasColumnType("double precision"); + + b.Property("Status") + .HasColumnType("integer"); + + b.Property("SupplierId") + .HasColumnType("uuid"); + + b.HasKey("Id"); + + b.HasIndex("SupplierId"); + + b.ToTable("Supplies"); + }); + + modelBuilder.Entity("DatabaseImplement.Models.SupplyDoc", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("Name") + .IsRequired() + .HasColumnType("text"); + + b.Property("SupplyId") + .HasColumnType("uuid"); + + b.HasKey("Id"); + + b.ToTable("SupplyDocs"); + }); + + modelBuilder.Entity("DatabaseImplement.Models.SupplyProduct", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("Count") + .HasColumnType("integer"); + + b.Property("ProductId") + .HasColumnType("uuid"); + + b.Property("SupplyId") + .HasColumnType("uuid"); + + b.HasKey("Id"); + + b.HasIndex("ProductId"); + + b.HasIndex("SupplyId"); + + b.ToTable("SupplyProducts"); + }); + + modelBuilder.Entity("DatabaseImplement.Models.User", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("Birthday") + .HasColumnType("timestamp without time zone"); + + b.Property("Bonus") + .HasColumnType("integer"); + + b.Property("Email") + .IsRequired() + .HasColumnType("text"); + + b.Property("FirstName") + .IsRequired() + .HasColumnType("text"); + + b.Property("OnlyImportantMails") + .HasColumnType("boolean"); + + b.Property("PasswordHash") + .IsRequired() + .HasColumnType("text"); + + b.Property("RoleId") + .HasColumnType("uuid"); + + b.Property("SecondName") + .IsRequired() + .HasColumnType("text"); + + b.HasKey("Id"); + + b.HasIndex("RoleId"); + + b.ToTable("Users"); + }); + + modelBuilder.Entity("DatabaseImplement.Models.CartItem", b => + { + b.HasOne("DatabaseImplement.Models.Product", "Product") + .WithMany() + .HasForeignKey("ProductId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("DatabaseImplement.Models.Purchase", "Purchase") + .WithMany() + .HasForeignKey("PurchaseId"); + + b.Navigation("Product"); + + b.Navigation("Purchase"); + }); + + modelBuilder.Entity("DatabaseImplement.Models.MediaFile", b => + { + b.HasOne("DatabaseImplement.Models.Product", "Product") + .WithMany() + .HasForeignKey("ProductId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Product"); + }); + + modelBuilder.Entity("DatabaseImplement.Models.Product", b => + { + b.HasOne("DatabaseImplement.Models.Sale", "Sale") + .WithMany("Products") + .HasForeignKey("SaleId"); + + b.Navigation("Sale"); + }); + + modelBuilder.Entity("DatabaseImplement.Models.Purchase", b => + { + b.HasOne("DatabaseImplement.Models.User", "User") + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("User"); + }); + + modelBuilder.Entity("DatabaseImplement.Models.Sell", b => + { + b.HasOne("DatabaseImplement.Models.User", "User") + .WithMany() + .HasForeignKey("UserId"); + + b.Navigation("User"); + }); + + modelBuilder.Entity("DatabaseImplement.Models.SellProducts", b => + { + b.HasOne("DatabaseImplement.Models.Product", "Product") + .WithMany("SellProducts") + .HasForeignKey("ProductId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("DatabaseImplement.Models.Sell", "Sell") + .WithMany("Products") + .HasForeignKey("SellId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Product"); + + b.Navigation("Sell"); + }); + + modelBuilder.Entity("DatabaseImplement.Models.SupplierProduct", b => + { + b.HasOne("DatabaseImplement.Models.Product", "Product") + .WithMany() + .HasForeignKey("ProductId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("DatabaseImplement.Models.Supplier", "Supplier") + .WithMany("Products") + .HasForeignKey("SupplierId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Product"); + + b.Navigation("Supplier"); + }); + + modelBuilder.Entity("DatabaseImplement.Models.Supply", b => + { + b.HasOne("DatabaseImplement.Models.Supplier", "Supplier") + .WithMany() + .HasForeignKey("SupplierId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Supplier"); + }); + + modelBuilder.Entity("DatabaseImplement.Models.SupplyProduct", b => + { + b.HasOne("DatabaseImplement.Models.Product", "Product") + .WithMany() + .HasForeignKey("ProductId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("DatabaseImplement.Models.Supply", "Supply") + .WithMany("Products") + .HasForeignKey("SupplyId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Product"); + + b.Navigation("Supply"); + }); + + modelBuilder.Entity("DatabaseImplement.Models.User", b => + { + b.HasOne("DatabaseImplement.Models.Role", "Role") + .WithMany() + .HasForeignKey("RoleId"); + + b.Navigation("Role"); + }); + + modelBuilder.Entity("DatabaseImplement.Models.Product", b => + { + b.Navigation("SellProducts"); + }); + + modelBuilder.Entity("DatabaseImplement.Models.Sale", b => + { + b.Navigation("Products"); + }); + + modelBuilder.Entity("DatabaseImplement.Models.Sell", b => + { + b.Navigation("Products"); + }); + + modelBuilder.Entity("DatabaseImplement.Models.Supplier", b => + { + b.Navigation("Products"); + }); + + modelBuilder.Entity("DatabaseImplement.Models.Supply", b => + { + b.Navigation("Products"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/DatabaseImplement/Migrations/20240730191618_closeitem.cs b/DatabaseImplement/Migrations/20240730191618_closeitem.cs new file mode 100644 index 0000000..4faa892 --- /dev/null +++ b/DatabaseImplement/Migrations/20240730191618_closeitem.cs @@ -0,0 +1,29 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace DatabaseImplement.Migrations +{ + /// + public partial class closeitem : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.AddColumn( + name: "IsClosed", + table: "CartItems", + type: "boolean", + nullable: false, + defaultValue: false); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropColumn( + name: "IsClosed", + table: "CartItems"); + } + } +} diff --git a/DatabaseImplement/Migrations/DatabaseModelSnapshot.cs b/DatabaseImplement/Migrations/DatabaseModelSnapshot.cs index 953543e..e63351a 100644 --- a/DatabaseImplement/Migrations/DatabaseModelSnapshot.cs +++ b/DatabaseImplement/Migrations/DatabaseModelSnapshot.cs @@ -34,6 +34,9 @@ namespace DatabaseImplement.Migrations b.Property("DateCreated") .HasColumnType("timestamp without time zone"); + b.Property("IsClosed") + .HasColumnType("boolean"); + b.Property("ProductId") .HasColumnType("uuid"); @@ -53,7 +56,7 @@ namespace DatabaseImplement.Migrations b.HasIndex("PurchaseId"); - b.ToTable("CartItems"); + b.ToTable("CartItems", (string)null); }); modelBuilder.Entity("DatabaseImplement.Models.MediaFile", b => @@ -73,7 +76,7 @@ namespace DatabaseImplement.Migrations b.HasIndex("ProductId"); - b.ToTable("MediaFiles"); + b.ToTable("MediaFiles", (string)null); }); modelBuilder.Entity("DatabaseImplement.Models.Product", b => @@ -111,7 +114,7 @@ namespace DatabaseImplement.Migrations b.HasIndex("SaleId"); - b.ToTable("Products"); + b.ToTable("Products", (string)null); }); modelBuilder.Entity("DatabaseImplement.Models.Purchase", b => @@ -145,7 +148,7 @@ namespace DatabaseImplement.Migrations b.HasIndex("UserId"); - b.ToTable("Purchases"); + b.ToTable("Purchases", (string)null); }); modelBuilder.Entity("DatabaseImplement.Models.Role", b => @@ -160,7 +163,7 @@ namespace DatabaseImplement.Migrations b.HasKey("Id"); - b.ToTable("Roles"); + b.ToTable("Roles", (string)null); }); modelBuilder.Entity("DatabaseImplement.Models.Sale", b => @@ -195,7 +198,7 @@ namespace DatabaseImplement.Migrations b.HasKey("Id"); - b.ToTable("Sales"); + b.ToTable("Sales", (string)null); }); modelBuilder.Entity("DatabaseImplement.Models.Sell", b => @@ -214,7 +217,7 @@ namespace DatabaseImplement.Migrations b.HasIndex("UserId"); - b.ToTable("Sells"); + b.ToTable("Sells", (string)null); }); modelBuilder.Entity("DatabaseImplement.Models.SellProducts", b => @@ -238,7 +241,7 @@ namespace DatabaseImplement.Migrations b.HasIndex("SellId"); - b.ToTable("SellProducts"); + b.ToTable("SellProducts", (string)null); }); modelBuilder.Entity("DatabaseImplement.Models.Supplier", b => @@ -256,7 +259,7 @@ namespace DatabaseImplement.Migrations b.HasKey("Id"); - b.ToTable("Suppliers"); + b.ToTable("Suppliers", (string)null); }); modelBuilder.Entity("DatabaseImplement.Models.SupplierProduct", b => @@ -280,7 +283,7 @@ namespace DatabaseImplement.Migrations b.HasIndex("SupplierId"); - b.ToTable("SupplierProducts"); + b.ToTable("SupplierProducts", (string)null); }); modelBuilder.Entity("DatabaseImplement.Models.Supply", b => @@ -315,7 +318,7 @@ namespace DatabaseImplement.Migrations b.HasIndex("SupplierId"); - b.ToTable("Supplies"); + b.ToTable("Supplies", (string)null); }); modelBuilder.Entity("DatabaseImplement.Models.SupplyDoc", b => @@ -333,7 +336,7 @@ namespace DatabaseImplement.Migrations b.HasKey("Id"); - b.ToTable("SupplyDocs"); + b.ToTable("SupplyDocs", (string)null); }); modelBuilder.Entity("DatabaseImplement.Models.SupplyProduct", b => @@ -357,7 +360,7 @@ namespace DatabaseImplement.Migrations b.HasIndex("SupplyId"); - b.ToTable("SupplyProducts"); + b.ToTable("SupplyProducts", (string)null); }); modelBuilder.Entity("DatabaseImplement.Models.User", b => @@ -398,7 +401,7 @@ namespace DatabaseImplement.Migrations b.HasIndex("RoleId"); - b.ToTable("Users"); + b.ToTable("Users", (string)null); }); modelBuilder.Entity("DatabaseImplement.Models.CartItem", b => diff --git a/DatabaseImplement/Models/CartItem.cs b/DatabaseImplement/Models/CartItem.cs index 7db0737..b7ee5ff 100644 --- a/DatabaseImplement/Models/CartItem.cs +++ b/DatabaseImplement/Models/CartItem.cs @@ -27,6 +27,8 @@ namespace DatabaseImplement.Models [Required] public Guid ProductId { get; set; } [Required] + public bool IsClosed { get; set; } + [Required] public string ProductName { get; set; } = string.Empty; public virtual Product? Product { get; set; } public virtual Purchase? Purchase { get; set; } @@ -41,6 +43,7 @@ namespace DatabaseImplement.Models ProductId = model.ProductId, ProductName = model.ProductName, PurchaseId = model.PurchaseId, + IsClosed = false }; } public void Update(CartItemBindingModel model) @@ -51,6 +54,7 @@ namespace DatabaseImplement.Models } Count = model.Count; PurchaseId = model.PurchaseId; + IsClosed = model.IsClosed; } public CartItemViewModel GetViewModel { diff --git a/DatabaseImplement/Models/Purchase.cs b/DatabaseImplement/Models/Purchase.cs index 721d4ef..45160f2 100644 --- a/DatabaseImplement/Models/Purchase.cs +++ b/DatabaseImplement/Models/Purchase.cs @@ -32,11 +32,12 @@ namespace DatabaseImplement.Models public bool IsPaid { get; set; } public virtual User? User { get; set; } [NotMapped] - public List Products { get; set; } + public List? Products { get; set; } public static Purchase Create(Database context, PurchaseBindingModel model) { return new Purchase() { + Id = model.Id, DateCreated = model.DateCreated, UserId = model.UserId, Status = model.Status, diff --git a/RestAPI/Controllers/CartItemController.cs b/RestAPI/Controllers/CartItemController.cs index 313a926..5cc419c 100644 --- a/RestAPI/Controllers/CartItemController.cs +++ b/RestAPI/Controllers/CartItemController.cs @@ -4,6 +4,7 @@ using Contracts.BusinessLogicContracts; using Contracts.Exceptions; using Contracts.SearchModels; using Contracts.ViewModels; +using DatabaseImplement.Models; using Microsoft.AspNetCore.Mvc; namespace RestAPI.Controllers @@ -35,6 +36,19 @@ namespace RestAPI.Controllers } } [HttpGet] + public List GetFilteredList(Guid userId, bool? isclosed) + { + try + { + return _cartItemLogic.ReadElements(new CartItemSearchModel() { UserId = userId, isClosed = isclosed }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка получения списка"); + throw; + } + } + [HttpGet] public CartItemViewModel GetElement(Guid id) { try @@ -118,30 +132,6 @@ namespace RestAPI.Controllers return Results.Problem(ex.Message); } } - [HttpDelete] - public IResult DeleteAll() - { - try - { - var list = _cartItemLogic.ReadElements(null); - var res = new List(); - foreach (var element in list) - { - _cartItemLogic.Delete(new() { Id = element.Id }); - } - return Results.Ok(res); - } - catch (ElementNotFoundException ex) - { - _logger.LogInformation(ex, "Item not found"); - return Results.NoContent(); - } - catch (Exception ex) - { - _logger.LogError(ex, "Error delete item"); - return Results.Problem(ex.Message); - } - } } } diff --git a/WebApp/Pages/Cart.cshtml.cs b/WebApp/Pages/Cart.cshtml.cs index 89f8711..637df00 100644 --- a/WebApp/Pages/Cart.cshtml.cs +++ b/WebApp/Pages/Cart.cshtml.cs @@ -25,7 +25,7 @@ namespace WebApp.Pages return; } - cartItemsView = APIClient.GetRequest>($"cartitem/GetFullList?userId={id}"); + cartItemsView = APIClient.GetRequest>($"cartitem/GetFilteredList?userId={id}&isclosed={false}"); } public ProductViewModel GetProduct(Guid cartItemId) { diff --git a/WebApp/Pages/Index.cshtml b/WebApp/Pages/Index.cshtml index ac51bb5..a0cb068 100644 --- a/WebApp/Pages/Index.cshtml +++ b/WebApp/Pages/Index.cshtml @@ -15,7 +15,7 @@
- +
- В корзину + В корзину } -