From 30b72fdf0631564a6eb02d11e4e050a9cc98b10b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D1=80=D1=82=D1=91=D0=BC=20=D0=90=D0=BB=D0=B5=D0=B9?= =?UTF-8?q?=D0=BA=D0=B8=D0=BD?= Date: Mon, 13 Mar 2023 22:55:48 +0400 Subject: [PATCH 1/2] =?UTF-8?q?=D0=9E=D1=81=D1=82=D0=B0=D0=BD=D0=BE=D0=B2?= =?UTF-8?q?=D0=B8=D0=BB=D1=81=D1=8F=20=D0=BD=D0=B0=20=D0=B7=D0=B0=D1=85?= =?UTF-8?q?=D0=BE=D0=B4=D0=B5=20=D0=B2=20=D0=B1=D0=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Confectionery/Confectionery.sln | 6 + .../ConfectioneryDatabase.cs | 32 ++++ .../ConfectioneryDatabaseImplement.csproj | 24 +++ .../20230313183606_InitMigration.Designer.cs | 156 ++++++++++++++++++ .../20230313183606_InitMigration.cs | 115 +++++++++++++ .../ConfectioneryDatabaseModelSnapshot.cs | 153 +++++++++++++++++ .../Models/Component.cs | 61 +++++++ .../Models/Order.cs | 70 ++++++++ .../Models/Pastry.cs | 107 ++++++++++++ .../Models/PastryComponent.cs | 27 +++ .../ConfectioneryView.csproj | 5 + 11 files changed, 756 insertions(+) create mode 100644 Confectionery/ConfectioneryDatabaseImplement/ConfectioneryDatabase.cs create mode 100644 Confectionery/ConfectioneryDatabaseImplement/ConfectioneryDatabaseImplement.csproj create mode 100644 Confectionery/ConfectioneryDatabaseImplement/Migrations/20230313183606_InitMigration.Designer.cs create mode 100644 Confectionery/ConfectioneryDatabaseImplement/Migrations/20230313183606_InitMigration.cs create mode 100644 Confectionery/ConfectioneryDatabaseImplement/Migrations/ConfectioneryDatabaseModelSnapshot.cs create mode 100644 Confectionery/ConfectioneryDatabaseImplement/Models/Component.cs create mode 100644 Confectionery/ConfectioneryDatabaseImplement/Models/Order.cs create mode 100644 Confectionery/ConfectioneryDatabaseImplement/Models/Pastry.cs create mode 100644 Confectionery/ConfectioneryDatabaseImplement/Models/PastryComponent.cs diff --git a/Confectionery/Confectionery.sln b/Confectionery/Confectionery.sln index 312640a..c49a1d3 100644 --- a/Confectionery/Confectionery.sln +++ b/Confectionery/Confectionery.sln @@ -15,6 +15,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConfectioneryListImplement" EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConfectioneryFileImplement", "ConfectioneryFileImplement\ConfectioneryFileImplement.csproj", "{2DA79E40-423F-4DEA-9FF1-F99F3A2A4DC9}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConfectioneryDatabaseImplement", "ConfectioneryDatabaseImplement\ConfectioneryDatabaseImplement.csproj", "{ECED35DF-7E6A-48F5-B0EB-3D01D772C7FD}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -45,6 +47,10 @@ Global {2DA79E40-423F-4DEA-9FF1-F99F3A2A4DC9}.Debug|Any CPU.Build.0 = Debug|Any CPU {2DA79E40-423F-4DEA-9FF1-F99F3A2A4DC9}.Release|Any CPU.ActiveCfg = Release|Any CPU {2DA79E40-423F-4DEA-9FF1-F99F3A2A4DC9}.Release|Any CPU.Build.0 = Release|Any CPU + {ECED35DF-7E6A-48F5-B0EB-3D01D772C7FD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {ECED35DF-7E6A-48F5-B0EB-3D01D772C7FD}.Debug|Any CPU.Build.0 = Debug|Any CPU + {ECED35DF-7E6A-48F5-B0EB-3D01D772C7FD}.Release|Any CPU.ActiveCfg = Release|Any CPU + {ECED35DF-7E6A-48F5-B0EB-3D01D772C7FD}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/Confectionery/ConfectioneryDatabaseImplement/ConfectioneryDatabase.cs b/Confectionery/ConfectioneryDatabaseImplement/ConfectioneryDatabase.cs new file mode 100644 index 0000000..478c0ee --- /dev/null +++ b/Confectionery/ConfectioneryDatabaseImplement/ConfectioneryDatabase.cs @@ -0,0 +1,32 @@ +using ConfectioneryDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConfectioneryDatabaseImplement +{ + public class ConfectioneryDatabase : DbContext + { + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + if (optionsBuilder.IsConfigured == false) + { + optionsBuilder.UseNpgsql(@" + Host=localhost; + Port=5432; + Database=RPP; + Username=postgres; + Password=12345678"); + } + base.OnConfiguring(optionsBuilder); + } + public virtual DbSet Components { set; get; } + public virtual DbSet Pastries { set; get; } + public virtual DbSet PastryComponents { set; get; } + public virtual DbSet Orders { set; get; } + } +} diff --git a/Confectionery/ConfectioneryDatabaseImplement/ConfectioneryDatabaseImplement.csproj b/Confectionery/ConfectioneryDatabaseImplement/ConfectioneryDatabaseImplement.csproj new file mode 100644 index 0000000..78fdc98 --- /dev/null +++ b/Confectionery/ConfectioneryDatabaseImplement/ConfectioneryDatabaseImplement.csproj @@ -0,0 +1,24 @@ + + + + net6.0 + enable + enable + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + + diff --git a/Confectionery/ConfectioneryDatabaseImplement/Migrations/20230313183606_InitMigration.Designer.cs b/Confectionery/ConfectioneryDatabaseImplement/Migrations/20230313183606_InitMigration.Designer.cs new file mode 100644 index 0000000..05c4d0b --- /dev/null +++ b/Confectionery/ConfectioneryDatabaseImplement/Migrations/20230313183606_InitMigration.Designer.cs @@ -0,0 +1,156 @@ +// +using System; +using ConfectioneryDatabaseImplement; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +#nullable disable + +namespace ConfectioneryDatabaseImplement.Migrations +{ + [DbContext(typeof(ConfectioneryDatabase))] + [Migration("20230313183606_InitMigration")] + partial class InitMigration + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.3") + .HasAnnotation("Relational:MaxIdentifierLength", 63); + + NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Component", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("ComponentName") + .IsRequired() + .HasColumnType("text"); + + b.Property("Cost") + .HasColumnType("double precision"); + + b.HasKey("Id"); + + b.ToTable("Components"); + }); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Order", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("integer"); + + b.Property("DateCreate") + .HasColumnType("timestamp with time zone"); + + b.Property("DateImplement") + .HasColumnType("timestamp with time zone"); + + b.Property("PastryId") + .HasColumnType("integer"); + + b.Property("Status") + .HasColumnType("integer"); + + b.Property("Sum") + .HasColumnType("double precision"); + + b.HasKey("Id"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Pastry", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("PastryName") + .IsRequired() + .HasColumnType("text"); + + b.Property("Price") + .HasColumnType("double precision"); + + b.HasKey("Id"); + + b.ToTable("Pastries"); + }); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.PastryComponent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("ComponentId") + .HasColumnType("integer"); + + b.Property("Count") + .HasColumnType("integer"); + + b.Property("PastryId") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("ComponentId"); + + b.HasIndex("PastryId"); + + b.ToTable("PastryComponents"); + }); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.PastryComponent", b => + { + b.HasOne("ConfectioneryDatabaseImplement.Models.Component", "Component") + .WithMany("PastryComponents") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("ConfectioneryDatabaseImplement.Models.Pastry", "Pastry") + .WithMany("Components") + .HasForeignKey("PastryId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Component"); + + b.Navigation("Pastry"); + }); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Component", b => + { + b.Navigation("PastryComponents"); + }); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Pastry", b => + { + b.Navigation("Components"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Confectionery/ConfectioneryDatabaseImplement/Migrations/20230313183606_InitMigration.cs b/Confectionery/ConfectioneryDatabaseImplement/Migrations/20230313183606_InitMigration.cs new file mode 100644 index 0000000..8b670d7 --- /dev/null +++ b/Confectionery/ConfectioneryDatabaseImplement/Migrations/20230313183606_InitMigration.cs @@ -0,0 +1,115 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +#nullable disable + +namespace ConfectioneryDatabaseImplement.Migrations +{ + /// + public partial class InitMigration : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Components", + columns: table => new + { + Id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + ComponentName = table.Column(type: "text", nullable: false), + Cost = table.Column(type: "double precision", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Components", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Orders", + columns: table => new + { + Id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + PastryId = table.Column(type: "integer", nullable: false), + Count = table.Column(type: "integer", nullable: false), + Sum = table.Column(type: "double precision", nullable: false), + Status = table.Column(type: "integer", nullable: false), + DateCreate = table.Column(type: "timestamp with time zone", nullable: false), + DateImplement = table.Column(type: "timestamp with time zone", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_Orders", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Pastries", + columns: table => new + { + Id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + PastryName = table.Column(type: "text", nullable: false), + Price = table.Column(type: "double precision", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Pastries", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "PastryComponents", + columns: table => new + { + Id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + PastryId = table.Column(type: "integer", nullable: false), + ComponentId = table.Column(type: "integer", nullable: false), + Count = table.Column(type: "integer", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_PastryComponents", x => x.Id); + table.ForeignKey( + name: "FK_PastryComponents_Components_ComponentId", + column: x => x.ComponentId, + principalTable: "Components", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_PastryComponents_Pastries_PastryId", + column: x => x.PastryId, + principalTable: "Pastries", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateIndex( + name: "IX_PastryComponents_ComponentId", + table: "PastryComponents", + column: "ComponentId"); + + migrationBuilder.CreateIndex( + name: "IX_PastryComponents_PastryId", + table: "PastryComponents", + column: "PastryId"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "Orders"); + + migrationBuilder.DropTable( + name: "PastryComponents"); + + migrationBuilder.DropTable( + name: "Components"); + + migrationBuilder.DropTable( + name: "Pastries"); + } + } +} diff --git a/Confectionery/ConfectioneryDatabaseImplement/Migrations/ConfectioneryDatabaseModelSnapshot.cs b/Confectionery/ConfectioneryDatabaseImplement/Migrations/ConfectioneryDatabaseModelSnapshot.cs new file mode 100644 index 0000000..a5b2bf6 --- /dev/null +++ b/Confectionery/ConfectioneryDatabaseImplement/Migrations/ConfectioneryDatabaseModelSnapshot.cs @@ -0,0 +1,153 @@ +// +using System; +using ConfectioneryDatabaseImplement; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +#nullable disable + +namespace ConfectioneryDatabaseImplement.Migrations +{ + [DbContext(typeof(ConfectioneryDatabase))] + partial class ConfectioneryDatabaseModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.3") + .HasAnnotation("Relational:MaxIdentifierLength", 63); + + NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Component", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("ComponentName") + .IsRequired() + .HasColumnType("text"); + + b.Property("Cost") + .HasColumnType("double precision"); + + b.HasKey("Id"); + + b.ToTable("Components"); + }); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Order", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("integer"); + + b.Property("DateCreate") + .HasColumnType("timestamp with time zone"); + + b.Property("DateImplement") + .HasColumnType("timestamp with time zone"); + + b.Property("PastryId") + .HasColumnType("integer"); + + b.Property("Status") + .HasColumnType("integer"); + + b.Property("Sum") + .HasColumnType("double precision"); + + b.HasKey("Id"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Pastry", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("PastryName") + .IsRequired() + .HasColumnType("text"); + + b.Property("Price") + .HasColumnType("double precision"); + + b.HasKey("Id"); + + b.ToTable("Pastries"); + }); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.PastryComponent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("ComponentId") + .HasColumnType("integer"); + + b.Property("Count") + .HasColumnType("integer"); + + b.Property("PastryId") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("ComponentId"); + + b.HasIndex("PastryId"); + + b.ToTable("PastryComponents"); + }); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.PastryComponent", b => + { + b.HasOne("ConfectioneryDatabaseImplement.Models.Component", "Component") + .WithMany("PastryComponents") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("ConfectioneryDatabaseImplement.Models.Pastry", "Pastry") + .WithMany("Components") + .HasForeignKey("PastryId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Component"); + + b.Navigation("Pastry"); + }); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Component", b => + { + b.Navigation("PastryComponents"); + }); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Pastry", b => + { + b.Navigation("Components"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Confectionery/ConfectioneryDatabaseImplement/Models/Component.cs b/Confectionery/ConfectioneryDatabaseImplement/Models/Component.cs new file mode 100644 index 0000000..2d560d8 --- /dev/null +++ b/Confectionery/ConfectioneryDatabaseImplement/Models/Component.cs @@ -0,0 +1,61 @@ +using ConfectioneryDataModels.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 ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.ViewModels; + +namespace ConfectioneryDatabaseImplement.Models +{ + public class Component : IComponentModel + { + public int Id { get; private set; } + [Required] + public string ComponentName { get; private set; } = string.Empty; + [Required] + public double Cost { get; set; } + [ForeignKey("ComponentId")] + public virtual List PastryComponents { get; set; } = new(); + public static Component? Create(ComponentBindingModel model) + { + if (model == null) + { + return null; + } + return new Component() + { + Id = model.Id, + ComponentName = model.ComponentName, + Cost = model.Cost + }; + } + public static Component Create(ComponentViewModel model) + { + return new Component + { + Id = model.Id, + ComponentName = model.ComponentName, + Cost = model.Cost + }; + } + public void Update(ComponentBindingModel model) + { + if (model == null) + { + return; + } + ComponentName = model.ComponentName; + Cost = model.Cost; + } + public ComponentViewModel GetViewModel => new() + { + Id = Id, + ComponentName = ComponentName, + Cost = Cost + }; + } +} diff --git a/Confectionery/ConfectioneryDatabaseImplement/Models/Order.cs b/Confectionery/ConfectioneryDatabaseImplement/Models/Order.cs new file mode 100644 index 0000000..933b560 --- /dev/null +++ b/Confectionery/ConfectioneryDatabaseImplement/Models/Order.cs @@ -0,0 +1,70 @@ +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.ViewModels; +using ConfectioneryDataModels.Enums; +using ConfectioneryDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConfectioneryDatabaseImplement.Models +{ + public class Order : IOrderModel + { + [Required] + public int PastryId { get; set; } + [Required] + public int Count { get; set; } + [Required] + public double Sum { get; set; } + [Required] + public OrderStatus Status { get; set; } + [Required] + public DateTime DateCreate { get; set; } + + public DateTime? DateImplement { get; set; } + + public int Id { get; set; } + + public static Order? Create(OrderBindingModel? model) + { + if (model == null) + { + return null; + } + return new Order() + { + Id = model.Id, + PastryId = model.PastryId, + Count = model.Count, + Sum = model.Sum, + Status = model.Status, + DateCreate = model.DateCreate, + DateImplement = model.DateImplement + }; + } + + public void Update(OrderBindingModel? model) + { + if (model == null) + { + return; + } + Status = model.Status; + DateImplement = model.DateImplement; + } + + public OrderViewModel GetViewModel => new() + { + Id = Id, + PastryId = PastryId, + Count = Count, + Sum = Sum, + Status = Status, + DateCreate = DateCreate, + DateImplement = DateImplement + }; + } +} diff --git a/Confectionery/ConfectioneryDatabaseImplement/Models/Pastry.cs b/Confectionery/ConfectioneryDatabaseImplement/Models/Pastry.cs new file mode 100644 index 0000000..f710367 --- /dev/null +++ b/Confectionery/ConfectioneryDatabaseImplement/Models/Pastry.cs @@ -0,0 +1,107 @@ +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.ViewModels; +using ConfectioneryDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; +using System.Linq; +using System.Net; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; + +namespace ConfectioneryDatabaseImplement.Models +{ + public class Pastry : IPastryModel + { + public int Id { get; private set; } + + [Required] + public string PastryName { get; private set; } = string.Empty; + + [Required] + public double Price { get; private set; } + + private Dictionary? _pastryComponents = null; + + [NotMapped] + public Dictionary PastryComponents + { + get + { + if (_pastryComponents == null) + { + _pastryComponents = Components.ToDictionary(recPC => recPC.ComponentId, recPC => + (recPC.Component as IComponentModel, recPC.Count)); + } + return _pastryComponents; + } + } + + [ForeignKey("PastryId")] + public virtual List Components { get; set; } = new(); + + public static Pastry? Create(ConfectioneryDatabase context ,PastryBindingModel? model) + { + if (model == null) + { + return null; + } + return new Pastry() + { + Id = model.Id, + PastryName = model.PastryName, + Price = model.Price, + Components = model.PastryComponents.Select(x => new PastryComponent + { + Component = context.Components.First(y => y.Id == x.Key), + Count = x.Value.Item2 + }).ToList() + }; + } + + public void Update(PastryBindingModel? model) + { + PastryName = model.PastryName; + Price = model.Price; + } + + public PastryViewModel GetViewModel => new() + { + Id = Id, + PastryName = PastryName, + Price = Price, + PastryComponents = PastryComponents + }; + + public void UpdateComponents(ConfectioneryDatabase context, PastryBindingModel model) + { + var PastryComponents = context.PastryComponents.Where(rec => rec.PastryId == model.Id).ToList(); + if (PastryComponents != null && PastryComponents.Count > 0) + { // удалили те, которых нет в модели + context.PastryComponents.RemoveRange(PastryComponents.Where(rec => !model.PastryComponents.ContainsKey(rec.ComponentId))); + context.SaveChanges(); + // обновили количество у существующих записей + foreach (var updateComponent in PastryComponents) + { + updateComponent.Count = model.PastryComponents[updateComponent.ComponentId].Item2; + model.PastryComponents.Remove(updateComponent.ComponentId); + } + context.SaveChanges(); + } + var Pastry = context.Pastries.First(x => x.Id == Id); + foreach (var pc in model.PastryComponents) + { + context.PastryComponents.Add(new PastryComponent + { + Pastry = Pastry, + Component = context.Components.First(x => x.Id == pc.Key), + Count = pc.Value.Item2 + }); + context.SaveChanges(); + } + _pastryComponents = null; + } + } +} diff --git a/Confectionery/ConfectioneryDatabaseImplement/Models/PastryComponent.cs b/Confectionery/ConfectioneryDatabaseImplement/Models/PastryComponent.cs new file mode 100644 index 0000000..238b802 --- /dev/null +++ b/Confectionery/ConfectioneryDatabaseImplement/Models/PastryComponent.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConfectioneryDatabaseImplement.Models +{ + public class PastryComponent + { + public int Id { get; set; } + + [Required] + public int PastryId { get; set; } + + [Required] + public int ComponentId { get; set; } + + [Required] + public int Count { get; set; } + + public virtual Component Component { get; set; } = new(); + + public virtual Pastry Pastry { get; set; } = new(); + } +} diff --git a/Confectionery/ConfectioneryView/ConfectioneryView.csproj b/Confectionery/ConfectioneryView/ConfectioneryView.csproj index c0ca3be..d15eaf6 100644 --- a/Confectionery/ConfectioneryView/ConfectioneryView.csproj +++ b/Confectionery/ConfectioneryView/ConfectioneryView.csproj @@ -9,6 +9,10 @@ + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + @@ -17,6 +21,7 @@ + -- 2.25.1 From 6c30c072f87ec99cbc5baea1e0748451ab14d60e Mon Sep 17 00:00:00 2001 From: 1yuee Date: Tue, 14 Mar 2023 11:35:28 +0400 Subject: [PATCH 2/2] =?UTF-8?q?=D0=9E=D1=81=D1=82=D0=B0=D0=BB=D0=B0=D1=81?= =?UTF-8?q?=D1=8C=20=D1=82=D0=B5=D0=BE=D1=80=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BusinessLogics/OrderLogic.cs | 2 +- .../BindingModels/OrderBindingModel.cs | 2 +- .../ConfectioneryDatabase.cs | 4 +- .../Implements/ComponentStorage.cs | 86 ++++++++++++++ .../Implements/OrderStorage.cs | 105 +++++++++++++++++ .../Implements/PastryStorage.cs | 111 ++++++++++++++++++ ...=> 20230314061048_InitMigrate.Designer.cs} | 4 +- ...ation.cs => 20230314061048_InitMigrate.cs} | 2 +- Confectionery/ConfectioneryView/Program.cs | 2 +- 9 files changed, 310 insertions(+), 8 deletions(-) create mode 100644 Confectionery/ConfectioneryDatabaseImplement/Implements/ComponentStorage.cs create mode 100644 Confectionery/ConfectioneryDatabaseImplement/Implements/OrderStorage.cs create mode 100644 Confectionery/ConfectioneryDatabaseImplement/Implements/PastryStorage.cs rename Confectionery/ConfectioneryDatabaseImplement/Migrations/{20230313183606_InitMigration.Designer.cs => 20230314061048_InitMigrate.Designer.cs} (98%) rename Confectionery/ConfectioneryDatabaseImplement/Migrations/{20230313183606_InitMigration.cs => 20230314061048_InitMigrate.cs} (98%) diff --git a/Confectionery/ConfectioneryBusinessLogic/BusinessLogics/OrderLogic.cs b/Confectionery/ConfectioneryBusinessLogic/BusinessLogics/OrderLogic.cs index c39f8bb..c84e7bf 100644 --- a/Confectionery/ConfectioneryBusinessLogic/BusinessLogics/OrderLogic.cs +++ b/Confectionery/ConfectioneryBusinessLogic/BusinessLogics/OrderLogic.cs @@ -56,7 +56,7 @@ namespace ConfectioneryBusinessLogic.BusinessLogics return false; } model.Status = newStatus; - if (model.Status == OrderStatus.Готов) model.DateImplement = DateTime.Now; + if (model.Status == OrderStatus.Готов) model.DateImplement = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Utc); else { model.DateImplement = viewModel.DateImplement; diff --git a/Confectionery/ConfectioneryContracts/BindingModels/OrderBindingModel.cs b/Confectionery/ConfectioneryContracts/BindingModels/OrderBindingModel.cs index 7859c0f..895fb3f 100644 --- a/Confectionery/ConfectioneryContracts/BindingModels/OrderBindingModel.cs +++ b/Confectionery/ConfectioneryContracts/BindingModels/OrderBindingModel.cs @@ -20,7 +20,7 @@ namespace ConfectioneryContracts.BindingModels public OrderStatus Status { get; set; } = OrderStatus.Неизвестен; - public DateTime DateCreate { get; set; } = DateTime.Now; + public DateTime DateCreate { get; set; } = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Utc); public DateTime? DateImplement { get; set; } } diff --git a/Confectionery/ConfectioneryDatabaseImplement/ConfectioneryDatabase.cs b/Confectionery/ConfectioneryDatabaseImplement/ConfectioneryDatabase.cs index 478c0ee..aa6da7b 100644 --- a/Confectionery/ConfectioneryDatabaseImplement/ConfectioneryDatabase.cs +++ b/Confectionery/ConfectioneryDatabaseImplement/ConfectioneryDatabase.cs @@ -18,9 +18,9 @@ namespace ConfectioneryDatabaseImplement optionsBuilder.UseNpgsql(@" Host=localhost; Port=5432; - Database=RPP; + Database=ConfectioneryDatabase; Username=postgres; - Password=12345678"); + Password=postgres"); } base.OnConfiguring(optionsBuilder); } diff --git a/Confectionery/ConfectioneryDatabaseImplement/Implements/ComponentStorage.cs b/Confectionery/ConfectioneryDatabaseImplement/Implements/ComponentStorage.cs new file mode 100644 index 0000000..b8fd402 --- /dev/null +++ b/Confectionery/ConfectioneryDatabaseImplement/Implements/ComponentStorage.cs @@ -0,0 +1,86 @@ +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.SearchModels; +using ConfectioneryContracts.StoragesContracts; +using ConfectioneryContracts.ViewModels; +using ConfectioneryDatabaseImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConfectioneryDatabaseImplement.Implements +{ + public class ComponentStorage : IComponentStorage + { + public List GetFullList() + { + using var context = new ConfectioneryDatabase(); + return context.Components.Select(x => x.GetViewModel).ToList(); + } + + public List GetFilteredList(ComponentSearchModel model) + { + if (string.IsNullOrEmpty(model.ComponentName)) + { + return new(); + } + using var context = new ConfectioneryDatabase(); + return context.Components + .Where(x => x.ComponentName.Contains(model.ComponentName)) + .Select(x => x.GetViewModel) + .ToList(); + } + + public ComponentViewModel? GetElement(ComponentSearchModel model) + { + if (string.IsNullOrEmpty(model.ComponentName) && !model.Id.HasValue) + { + return null; + } + using var context = new ConfectioneryDatabase(); + return context.Components + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.ComponentName) && x.ComponentName == model.ComponentName) + || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + + public ComponentViewModel? Insert(ComponentBindingModel model) + { + var newComponent = Component.Create(model); + if (newComponent == null) + { + return null; + } + using var context = new ConfectioneryDatabase(); + context.Components.Add(newComponent); + context.SaveChanges(); + return newComponent.GetViewModel; + } + + public ComponentViewModel? Update(ComponentBindingModel model) + { + using var context = new ConfectioneryDatabase(); + var component = context.Components.FirstOrDefault(x => x.Id == model.Id); + if (component == null) + { + return null; + } + component.Update(model); + context.SaveChanges(); + return component.GetViewModel; + } + + public ComponentViewModel? Delete(ComponentBindingModel model) + { + using var context = new ConfectioneryDatabase(); + var element = context.Components.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Components.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/Confectionery/ConfectioneryDatabaseImplement/Implements/OrderStorage.cs b/Confectionery/ConfectioneryDatabaseImplement/Implements/OrderStorage.cs new file mode 100644 index 0000000..deea67e --- /dev/null +++ b/Confectionery/ConfectioneryDatabaseImplement/Implements/OrderStorage.cs @@ -0,0 +1,105 @@ +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.SearchModels; +using ConfectioneryContracts.StoragesContracts; +using ConfectioneryContracts.ViewModels; +using ConfectioneryDatabaseImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConfectioneryDatabaseImplement.Implements +{ + public class OrderStorage : IOrderStorage + { + public OrderViewModel? Delete(OrderBindingModel model) + { + using var context = new ConfectioneryDatabase(); + + var element = context.Orders.FirstOrDefault(rec => rec.Id == model.Id); + + if (element != null) + { + context.Orders.Remove(element); + context.SaveChanges(); + + return GetViewModel(element); + } + + return null; + } + + public OrderViewModel? GetElement(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + + using var context = new ConfectioneryDatabase(); + + return GetViewModel(context.Orders.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))); + } + + public List GetFilteredList(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return new(); + } + + using var context = new ConfectioneryDatabase(); + + return context.Orders.Where(x => x.Id == model.Id).Select(x => GetViewModel(x)).ToList(); + } + + public List GetFullList() + { + using var context = new ConfectioneryDatabase(); + + return context.Orders.Select(x => GetViewModel(x)).ToList(); + } + + public OrderViewModel? Insert(OrderBindingModel model) + { + var newOrder = Order.Create(model); + if (newOrder == null) + { + return null; + } + + using var context = new ConfectioneryDatabase(); + + context.Orders.Add(newOrder); + context.SaveChanges(); + return GetViewModel(newOrder); + } + + public OrderViewModel? Update(OrderBindingModel model) + { + using var context = new ConfectioneryDatabase(); + + var order = context.Orders.FirstOrDefault(x => x.Id == model.Id); + + if (order == null) + { + return null; + } + + order.Update(model); + context.SaveChanges(); + + return GetViewModel(order); + } + + private static OrderViewModel GetViewModel(Order order) + { + var viewModel = order.GetViewModel; + using var context = new ConfectioneryDatabase(); + var element = context.Pastries.FirstOrDefault(x => x.Id == order.PastryId); + viewModel.PastryName = element.PastryName; + return viewModel; + } + } +} diff --git a/Confectionery/ConfectioneryDatabaseImplement/Implements/PastryStorage.cs b/Confectionery/ConfectioneryDatabaseImplement/Implements/PastryStorage.cs new file mode 100644 index 0000000..8207d5d --- /dev/null +++ b/Confectionery/ConfectioneryDatabaseImplement/Implements/PastryStorage.cs @@ -0,0 +1,111 @@ +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.SearchModels; +using ConfectioneryContracts.StoragesContracts; +using ConfectioneryContracts.ViewModels; +using ConfectioneryDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConfectioneryDatabaseImplement.Implements +{ + public class PastryStorage : IPastryStorage + { + public List GetFullList() + { + using var context = new ConfectioneryDatabase(); + return context.Pastries + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + public List GetFilteredList(PastrySearchModel model) + { + if (string.IsNullOrEmpty(model.PastryName)) + { + return new(); + } + using var context = new ConfectioneryDatabase(); + return context.Pastries + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .Where(x => x.PastryName.Contains(model.PastryName)) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + public PastryViewModel? GetElement(PastrySearchModel model) + { + if (string.IsNullOrEmpty(model.PastryName) && + !model.Id.HasValue) + { + return null; + } + using var context = new ConfectioneryDatabase(); + return context.Pastries + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.PastryName) && + x.PastryName == model.PastryName) || + (model.Id.HasValue && x.Id == + model.Id)) + ?.GetViewModel; + } + public PastryViewModel? Insert(PastryBindingModel model) + { + using var context = new ConfectioneryDatabase(); + var newPastry = Pastry.Create(context, model); + if (newPastry == null) + { + return null; + } + context.Pastries.Add(newPastry); + context.SaveChanges(); + return newPastry.GetViewModel; + } + public PastryViewModel? Update(PastryBindingModel model) + { + using var context = new ConfectioneryDatabase(); + using var transaction = context.Database.BeginTransaction(); + try + { + var pastry = context.Pastries.FirstOrDefault(rec => + rec.Id == model.Id); + if (pastry == null) + { + return null; + } + pastry.Update(model); + context.SaveChanges(); + pastry.UpdateComponents(context, model); + transaction.Commit(); + return pastry.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + public PastryViewModel? Delete(PastryBindingModel model) + { + using var context = new ConfectioneryDatabase(); + var element = context.Pastries + .Include(x => x.Components) + .FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Pastries.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + + } +} diff --git a/Confectionery/ConfectioneryDatabaseImplement/Migrations/20230313183606_InitMigration.Designer.cs b/Confectionery/ConfectioneryDatabaseImplement/Migrations/20230314061048_InitMigrate.Designer.cs similarity index 98% rename from Confectionery/ConfectioneryDatabaseImplement/Migrations/20230313183606_InitMigration.Designer.cs rename to Confectionery/ConfectioneryDatabaseImplement/Migrations/20230314061048_InitMigrate.Designer.cs index 05c4d0b..dadc731 100644 --- a/Confectionery/ConfectioneryDatabaseImplement/Migrations/20230313183606_InitMigration.Designer.cs +++ b/Confectionery/ConfectioneryDatabaseImplement/Migrations/20230314061048_InitMigrate.Designer.cs @@ -12,8 +12,8 @@ using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; namespace ConfectioneryDatabaseImplement.Migrations { [DbContext(typeof(ConfectioneryDatabase))] - [Migration("20230313183606_InitMigration")] - partial class InitMigration + [Migration("20230314061048_InitMigrate")] + partial class InitMigrate { /// protected override void BuildTargetModel(ModelBuilder modelBuilder) diff --git a/Confectionery/ConfectioneryDatabaseImplement/Migrations/20230313183606_InitMigration.cs b/Confectionery/ConfectioneryDatabaseImplement/Migrations/20230314061048_InitMigrate.cs similarity index 98% rename from Confectionery/ConfectioneryDatabaseImplement/Migrations/20230313183606_InitMigration.cs rename to Confectionery/ConfectioneryDatabaseImplement/Migrations/20230314061048_InitMigrate.cs index 8b670d7..1771d1b 100644 --- a/Confectionery/ConfectioneryDatabaseImplement/Migrations/20230313183606_InitMigration.cs +++ b/Confectionery/ConfectioneryDatabaseImplement/Migrations/20230314061048_InitMigrate.cs @@ -7,7 +7,7 @@ using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; namespace ConfectioneryDatabaseImplement.Migrations { /// - public partial class InitMigration : Migration + public partial class InitMigrate : Migration { /// protected override void Up(MigrationBuilder migrationBuilder) diff --git a/Confectionery/ConfectioneryView/Program.cs b/Confectionery/ConfectioneryView/Program.cs index 2d29ab3..8b7ae81 100644 --- a/Confectionery/ConfectioneryView/Program.cs +++ b/Confectionery/ConfectioneryView/Program.cs @@ -1,7 +1,7 @@ using ConfectioneryBusinessLogic.BusinessLogics; using ConfectioneryContracts.BusinessLogicsContracts; using ConfectioneryContracts.StoragesContracts; -using ConfectioneryFileImplement.Implements; +using ConfectioneryDatabaseImplement.Implements; using ConfectioneryView; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; -- 2.25.1