diff --git a/BusinessLogic/BusinessLogic/PurchaseLogic.cs b/BusinessLogic/BusinessLogic/PurchaseLogic.cs index 6dab37d..8e3724d 100644 --- a/BusinessLogic/BusinessLogic/PurchaseLogic.cs +++ b/BusinessLogic/BusinessLogic/PurchaseLogic.cs @@ -8,6 +8,7 @@ using Contracts.ViewModels; using DatabaseImplement.Implements; using DatabaseImplement.Models; using DataModels.Enums; +using DataModels.Models; using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; @@ -37,8 +38,8 @@ namespace BusinessLogic.BusinessLogic { throw new Exception("Insert operation failed."); } - throw new NotImplementedException(); - //return PurchaseConverter.ToView(purchase); + + return purchase; } public PurchaseViewModel Delete(PurchaseSearchModel model) @@ -51,8 +52,9 @@ namespace BusinessLogic.BusinessLogic { throw new Exception("Delete operation failed."); } - throw new NotImplementedException(); - //return PurchaseConverter.ToView(purchase); + + return purchase; + } public PurchaseViewModel ReadElement(PurchaseSearchModel model) @@ -66,11 +68,12 @@ namespace BusinessLogic.BusinessLogic throw new ElementNotFoundException(); } _logger.LogInformation("ReadElement find. Id: {0}", purchase.Id); - throw new NotImplementedException(); - //return PurchaseConverter.ToView(purchase); + + return purchase; + } - public IEnumerable ReadElements(PurchaseSearchModel? model) + public List ReadElements(PurchaseSearchModel? model) { _logger.LogInformation("ReadList. Id: {Id}", model?.Id); var purchase_list = _purchaseStorage.GetFullList(model); @@ -80,8 +83,9 @@ namespace BusinessLogic.BusinessLogic return []; } _logger.LogInformation("ReadList. Count: {Count}", purchase_list.Count()); - throw new NotImplementedException(); - //return purchase_list.Select(PurchaseConverter.ToView); + + return purchase_list; + } public PurchaseViewModel Update(PurchaseBindingModel model) @@ -93,8 +97,7 @@ namespace BusinessLogic.BusinessLogic { throw new Exception("Update operation failed."); } - throw new NotImplementedException(); - //return PurchaseConverter.ToView(purchase); - } + return purchase; + } } } diff --git a/BusinessLogic/BusinessLogic/SellLogic.cs b/BusinessLogic/BusinessLogic/SellLogic.cs index ababedf..cc7f722 100644 --- a/BusinessLogic/BusinessLogic/SellLogic.cs +++ b/BusinessLogic/BusinessLogic/SellLogic.cs @@ -31,8 +31,7 @@ namespace BusinessLogic.BusinessLogic { throw new Exception("Insert operation failed."); } - //return sell; - return new(); + return sell; } public SellViewModel GetElement(SellSearchModel model) { @@ -42,19 +41,18 @@ namespace BusinessLogic.BusinessLogic { throw new Exception("Get element operation failed."); } - return new(); - //return sell; + return sell; } + public IEnumerable GetElements(SellSearchModel? model) { - //var sell_list = _sellStorage.GetList(model); - //if (sell_list is null || sell_list.Count() == 0) - //{ - // _logger.LogWarning("ReadList return null list"); - // return []; - //} - return []; - //return sell_list; + var sell_list = _sellStorage.GetFullList(model); + if (sell_list is null || sell_list.Count() == 0) + { + _logger.LogWarning("ReadList return null list"); + return []; + } + return sell_list; } public SellViewModel Update(SellSearchModel model) { @@ -64,8 +62,7 @@ namespace BusinessLogic.BusinessLogic { throw new Exception("Update operation failed."); } - return new(); - //return sell; + return sell; } public SellViewModel Delete(SellSearchModel model) { @@ -75,8 +72,7 @@ namespace BusinessLogic.BusinessLogic { throw new Exception("Update operation failed."); } - return new(); - //return sell; + return sell; } } } diff --git a/Contracts/BindingModels/SellBindingModel.cs b/Contracts/BindingModels/SellBindingModel.cs index 0d93bcb..7956db5 100644 --- a/Contracts/BindingModels/SellBindingModel.cs +++ b/Contracts/BindingModels/SellBindingModel.cs @@ -1,4 +1,5 @@ -using System; +using DataModels.Models; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -10,6 +11,7 @@ namespace Contracts.BindingModels { public Guid Id { get; set; } public DateTime DateSell { get; set; } - public Guid? SupplyDocId { get; set; } + public Guid? UserId { get; set; } + public Dictionary SellProducts { get; set; } = new(); } } diff --git a/Contracts/BusinessLogicContracts/IPurchaseLogic.cs b/Contracts/BusinessLogicContracts/IPurchaseLogic.cs index 65892dd..7f3a623 100644 --- a/Contracts/BusinessLogicContracts/IPurchaseLogic.cs +++ b/Contracts/BusinessLogicContracts/IPurchaseLogic.cs @@ -17,7 +17,7 @@ namespace Contracts.BusinessLogicContracts PurchaseViewModel ReadElement(PurchaseSearchModel model); - IEnumerable ReadElements(PurchaseSearchModel? model); + List ReadElements(PurchaseSearchModel? model); PurchaseViewModel Delete(PurchaseSearchModel model); } diff --git a/Contracts/Converters/SellConverter.cs b/Contracts/Converters/SellConverter.cs index 0acd7fe..e24a426 100644 --- a/Contracts/Converters/SellConverter.cs +++ b/Contracts/Converters/SellConverter.cs @@ -15,14 +15,14 @@ namespace Contracts.Converters { Id = model.Id, DateSell = model.DateSell, - SupplyDocId = model.SupplyDocId + UserId = model.UserId }; public static SellBindingModel ToBinding(SellViewModel model) => new() { Id = model.Id, DateSell = model.DateSell, - SupplyDocId = model.SupplyDocId + UserId = model.UserId }; } } diff --git a/Contracts/ViewModels/SellViewModel.cs b/Contracts/ViewModels/SellViewModel.cs index 3d09e5a..8696eda 100644 --- a/Contracts/ViewModels/SellViewModel.cs +++ b/Contracts/ViewModels/SellViewModel.cs @@ -11,6 +11,7 @@ namespace Contracts.ViewModels { public Guid Id { get; set; } public DateTime DateSell { get; set; } - public Guid? SupplyDocId { get; set; } - } + public Guid? UserId { get; set; } + public Dictionary SellProducts { get; set; } = new(); + } } diff --git a/DatabaseImplement/Database.cs b/DatabaseImplement/Database.cs index c3284c4..8ca895d 100644 --- a/DatabaseImplement/Database.cs +++ b/DatabaseImplement/Database.cs @@ -30,6 +30,7 @@ namespace DatabaseImplement public virtual DbSet SupplierProducts { get; set; } = null!; public virtual DbSet MediaFiles { get; set; } = null!; public virtual DbSet PurchaseProducts { get; set; } = null!; + public virtual DbSet SellProducts { get; set; } = null!; public virtual DbSet SupplyDocs { get; set; } = null!; } } \ No newline at end of file diff --git a/DatabaseImplement/Implements/SellStorage.cs b/DatabaseImplement/Implements/SellStorage.cs index d82d531..acd69f6 100644 --- a/DatabaseImplement/Implements/SellStorage.cs +++ b/DatabaseImplement/Implements/SellStorage.cs @@ -32,7 +32,8 @@ namespace DatabaseImplement.Implements { using var context = new Database(); return context.Sells - .Include(x => x.SupplyDoc) + .Include(x => x.Products) + .ThenInclude(x => x.Product) .FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; } @@ -45,7 +46,8 @@ namespace DatabaseImplement.Implements { using var context = new Database(); return context.Sells - .Include(x => x.SupplyDoc) + .Include(x => x.Products) + .ThenInclude(x => x.Product) .ToList() .Select((Sell sell, int index) => sell.GetViewModel) .ToList(); diff --git a/DatabaseImplement/Migrations/20240624105540_sellproducts.Designer.cs b/DatabaseImplement/Migrations/20240624105540_sellproducts.Designer.cs new file mode 100644 index 0000000..2d66689 --- /dev/null +++ b/DatabaseImplement/Migrations/20240624105540_sellproducts.Designer.cs @@ -0,0 +1,477 @@ +// +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("20240624105540_sellproducts")] + partial class sellproducts + { + /// + 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.MediaFile", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("Location") + .IsRequired() + .HasColumnType("text"); + + b.Property("Name") + .IsRequired() + .HasColumnType("text"); + + b.Property("ProductId") + .HasColumnType("uuid"); + + b.HasKey("Id"); + + b.ToTable("MediaFiles"); + }); + + modelBuilder.Entity("DatabaseImplement.Models.Product", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("Amount") + .HasColumnType("integer"); + + b.Property("IsBeingSold") + .HasColumnType("boolean"); + + b.Property("Name") + .IsRequired() + .HasColumnType("text"); + + b.Property("Price") + .HasColumnType("double precision"); + + b.Property("Rate") + .HasColumnType("double precision"); + + b.HasKey("Id"); + + b.ToTable("Products"); + }); + + modelBuilder.Entity("DatabaseImplement.Models.Purchase", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("DatePurchase") + .HasColumnType("timestamp with time zone"); + + b.Property("Status") + .HasColumnType("integer"); + + b.Property("UserId") + .HasColumnType("uuid"); + + b.HasKey("Id"); + + b.HasIndex("UserId"); + + b.ToTable("Purchases"); + }); + + modelBuilder.Entity("DatabaseImplement.Models.PurchaseProducts", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("Count") + .HasColumnType("integer"); + + b.Property("ProductId") + .HasColumnType("uuid"); + + b.Property("PurchaseId") + .HasColumnType("uuid"); + + b.HasKey("Id"); + + b.HasIndex("ProductId"); + + b.HasIndex("PurchaseId"); + + b.ToTable("PurchaseProducts"); + }); + + 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.Sell", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("DateSell") + .HasColumnType("timestamp with 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 with 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 with time zone"); + + 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.Purchase", b => + { + b.HasOne("DatabaseImplement.Models.User", "User") + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("User"); + }); + + modelBuilder.Entity("DatabaseImplement.Models.PurchaseProducts", b => + { + b.HasOne("DatabaseImplement.Models.Product", "Product") + .WithMany("PurchaseProducts") + .HasForeignKey("ProductId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("DatabaseImplement.Models.Purchase", "Purchase") + .WithMany("Products") + .HasForeignKey("PurchaseId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Product"); + + b.Navigation("Purchase"); + }); + + 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("PurchaseProducts"); + + b.Navigation("SellProducts"); + }); + + modelBuilder.Entity("DatabaseImplement.Models.Purchase", 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/20240624105540_sellproducts.cs b/DatabaseImplement/Migrations/20240624105540_sellproducts.cs new file mode 100644 index 0000000..bff0898 --- /dev/null +++ b/DatabaseImplement/Migrations/20240624105540_sellproducts.cs @@ -0,0 +1,104 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace DatabaseImplement.Migrations +{ + /// + public partial class sellproducts : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.AddColumn( + name: "UserId", + table: "Sells", + type: "uuid", + nullable: true); + + migrationBuilder.CreateTable( + name: "SellProducts", + columns: table => new + { + Id = table.Column(type: "uuid", nullable: false), + SellId = table.Column(type: "uuid", nullable: false), + ProductId = table.Column(type: "uuid", nullable: false), + Count = table.Column(type: "integer", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_SellProducts", x => x.Id); + table.ForeignKey( + name: "FK_SellProducts_Products_ProductId", + column: x => x.ProductId, + principalTable: "Products", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_SellProducts_Sells_SellId", + column: x => x.SellId, + principalTable: "Sells", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "SupplyDocs", + columns: table => new + { + Id = table.Column(type: "uuid", nullable: false), + Name = table.Column(type: "text", nullable: false), + SupplyId = table.Column(type: "uuid", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_SupplyDocs", x => x.Id); + }); + + migrationBuilder.CreateIndex( + name: "IX_Sells_UserId", + table: "Sells", + column: "UserId"); + + migrationBuilder.CreateIndex( + name: "IX_SellProducts_ProductId", + table: "SellProducts", + column: "ProductId"); + + migrationBuilder.CreateIndex( + name: "IX_SellProducts_SellId", + table: "SellProducts", + column: "SellId"); + + migrationBuilder.AddForeignKey( + name: "FK_Sells_Users_UserId", + table: "Sells", + column: "UserId", + principalTable: "Users", + principalColumn: "Id"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropForeignKey( + name: "FK_Sells_Users_UserId", + table: "Sells"); + + migrationBuilder.DropTable( + name: "SellProducts"); + + migrationBuilder.DropTable( + name: "SupplyDocs"); + + migrationBuilder.DropIndex( + name: "IX_Sells_UserId", + table: "Sells"); + + migrationBuilder.DropColumn( + name: "UserId", + table: "Sells"); + } + } +} diff --git a/DatabaseImplement/Migrations/DatabaseModelSnapshot.cs b/DatabaseImplement/Migrations/DatabaseModelSnapshot.cs index b430445..1d1117b 100644 --- a/DatabaseImplement/Migrations/DatabaseModelSnapshot.cs +++ b/DatabaseImplement/Migrations/DatabaseModelSnapshot.cs @@ -41,7 +41,7 @@ namespace DatabaseImplement.Migrations b.HasKey("Id"); - b.ToTable("MediaFiles", (string)null); + b.ToTable("MediaFiles"); }); modelBuilder.Entity("DatabaseImplement.Models.Product", b => @@ -68,7 +68,7 @@ namespace DatabaseImplement.Migrations b.HasKey("Id"); - b.ToTable("Products", (string)null); + b.ToTable("Products"); }); modelBuilder.Entity("DatabaseImplement.Models.Purchase", b => @@ -90,7 +90,7 @@ namespace DatabaseImplement.Migrations b.HasIndex("UserId"); - b.ToTable("Purchases", (string)null); + b.ToTable("Purchases"); }); modelBuilder.Entity("DatabaseImplement.Models.PurchaseProducts", b => @@ -114,7 +114,7 @@ namespace DatabaseImplement.Migrations b.HasIndex("PurchaseId"); - b.ToTable("PurchaseProducts", (string)null); + b.ToTable("PurchaseProducts"); }); modelBuilder.Entity("DatabaseImplement.Models.Role", b => @@ -129,7 +129,7 @@ namespace DatabaseImplement.Migrations b.HasKey("Id"); - b.ToTable("Roles", (string)null); + b.ToTable("Roles"); }); modelBuilder.Entity("DatabaseImplement.Models.Sell", b => @@ -141,9 +141,38 @@ namespace DatabaseImplement.Migrations b.Property("DateSell") .HasColumnType("timestamp with time zone"); + b.Property("UserId") + .HasColumnType("uuid"); + b.HasKey("Id"); - b.ToTable("Sells", (string)null); + 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 => @@ -161,7 +190,7 @@ namespace DatabaseImplement.Migrations b.HasKey("Id"); - b.ToTable("Suppliers", (string)null); + b.ToTable("Suppliers"); }); modelBuilder.Entity("DatabaseImplement.Models.SupplierProduct", b => @@ -185,7 +214,7 @@ namespace DatabaseImplement.Migrations b.HasIndex("SupplierId"); - b.ToTable("SupplierProducts", (string)null); + b.ToTable("SupplierProducts"); }); modelBuilder.Entity("DatabaseImplement.Models.Supply", b => @@ -214,7 +243,25 @@ namespace DatabaseImplement.Migrations b.HasIndex("SupplierId"); - b.ToTable("Supplies", (string)null); + 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 => @@ -238,7 +285,7 @@ namespace DatabaseImplement.Migrations b.HasIndex("SupplyId"); - b.ToTable("SupplyProducts", (string)null); + b.ToTable("SupplyProducts"); }); modelBuilder.Entity("DatabaseImplement.Models.User", b => @@ -276,7 +323,7 @@ namespace DatabaseImplement.Migrations b.HasIndex("RoleId"); - b.ToTable("Users", (string)null); + b.ToTable("Users"); }); modelBuilder.Entity("DatabaseImplement.Models.Purchase", b => @@ -309,6 +356,34 @@ namespace DatabaseImplement.Migrations b.Navigation("Purchase"); }); + 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") @@ -370,6 +445,8 @@ namespace DatabaseImplement.Migrations modelBuilder.Entity("DatabaseImplement.Models.Product", b => { b.Navigation("PurchaseProducts"); + + b.Navigation("SellProducts"); }); modelBuilder.Entity("DatabaseImplement.Models.Purchase", b => @@ -377,6 +454,11 @@ namespace DatabaseImplement.Migrations b.Navigation("Products"); }); + modelBuilder.Entity("DatabaseImplement.Models.Sell", b => + { + b.Navigation("Products"); + }); + modelBuilder.Entity("DatabaseImplement.Models.Supplier", b => { b.Navigation("Products"); diff --git a/DatabaseImplement/Models/Product.cs b/DatabaseImplement/Models/Product.cs index 70da3ba..552b6cd 100644 --- a/DatabaseImplement/Models/Product.cs +++ b/DatabaseImplement/Models/Product.cs @@ -28,7 +28,9 @@ namespace DatabaseImplement.Models public int Amount { get; set; } [ForeignKey("ProductId")] public virtual List PurchaseProducts { get; set; } = new(); - public ProductBindingModel GetBindingModel() => new() + [ForeignKey("ProductId")] + public virtual List SellProducts { get; set; } = new(); + public ProductBindingModel GetBindingModel() => new() { Id = Id, Name = Name, diff --git a/DatabaseImplement/Models/Sell.cs b/DatabaseImplement/Models/Sell.cs index e986b30..9bd3754 100644 --- a/DatabaseImplement/Models/Sell.cs +++ b/DatabaseImplement/Models/Sell.cs @@ -4,7 +4,9 @@ using Contracts.ViewModels; using DataModels.Models; using System; using System.Collections.Generic; +using System.ComponentModel.DataAnnotations.Schema; using System.Linq; +using System.Runtime.Serialization; using System.Text; using System.Threading.Tasks; @@ -14,16 +16,32 @@ namespace DatabaseImplement.Models { public Guid Id { get; set; } public DateTime DateSell { get; set; } - public Guid? SupplyDocId { get; set; } - - public virtual SupplyDoc? SupplyDoc { get; set; } + public Guid? UserId { get; set; } + private Dictionary? _sellProducts = null; + public virtual User? User { get; set; } + [DataMember] + [NotMapped] + public Dictionary PurchaseProducts + { + get + { + if (_sellProducts == null) + { + _sellProducts = Products.ToDictionary(e => e.ProductId, e => (e.Product as IProduct, e.Count)); + } + return _sellProducts; + } + set { } + } + [ForeignKey("SellId")] + public virtual List Products { get; set; } = new(); public static Sell Create(Database context, SellBindingModel model) { return new Sell() { Id = model.Id, DateSell = model.DateSell, - SupplyDocId = model.SupplyDocId, + UserId = model.UserId, }; } @@ -31,13 +49,13 @@ namespace DatabaseImplement.Models { Id = Id, DateSell = DateSell, - SupplyDocId = SupplyDocId, + UserId = UserId, }; public SellViewModel GetViewModel => new() { Id = Id, DateSell = DateSell, - SupplyDocId = SupplyDocId, + UserId = UserId, }; public static Sell ToSellFromView(SellViewModel model, Sell sell) => new() @@ -60,7 +78,7 @@ namespace DatabaseImplement.Models } DateSell = model.DateSell; - SupplyDocId = model.SupplyDocId; + UserId = model.UserId; } } } diff --git a/DatabaseImplement/Models/SellProducts.cs b/DatabaseImplement/Models/SellProducts.cs new file mode 100644 index 0000000..4be3ea1 --- /dev/null +++ b/DatabaseImplement/Models/SellProducts.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DatabaseImplement.Models +{ + public class SellProducts + { + public Guid Id { get; set; } + [Required] + public Guid SellId { get; set; } + [Required] + public Guid ProductId { get; set; } + [Required] + public int Count { get; set; } + public virtual Sell Sell { get; set; } = new(); + public virtual Product Product { get; set; } = new(); + } +}