From 8d10707d698f2cb06bcf20d57154242dfe06ce83 Mon Sep 17 00:00:00 2001 From: antoc0der <1@DESKTOP-K1L8ND3> Date: Tue, 12 Mar 2024 09:31:03 +0400 Subject: [PATCH 1/3] =?UTF-8?q?=D0=B8=D0=BF=D1=81=D1=80=D0=B0=D0=B2=D0=B8?= =?UTF-8?q?=D0=BB=20=D0=BE=D1=88=D0=B8=D0=B1=D0=BA=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ProjectFlowerShop/FormFlower.Designer.cs | 3 --- ProjectFlowerShop/FormFlowerComponent.Designer.cs | 7 +++---- ProjectFlowerShop/FormFlowerComponent.cs | 5 ----- ProjectFlowerShop/FormFlowers.Designer.cs | 2 +- ProjectFlowerShop/MainForm.Designer.cs | 6 +++--- 5 files changed, 7 insertions(+), 16 deletions(-) diff --git a/ProjectFlowerShop/FormFlower.Designer.cs b/ProjectFlowerShop/FormFlower.Designer.cs index aa3790a..f1957bb 100644 --- a/ProjectFlowerShop/FormFlower.Designer.cs +++ b/ProjectFlowerShop/FormFlower.Designer.cs @@ -208,7 +208,6 @@ Controls.Add(textBoxName); Controls.Add(labelPrice); Controls.Add(labelName); - // Name = "FormProduct"; Text = "Изделие"; Load += FormProduct_Load; groupBoxComponent.ResumeLayout(false); @@ -231,10 +230,8 @@ private DataGridView dataGridView; private Button Save; private Button buttonCancel; - // private DataGridViewTextBoxColumn Id; private DataGridViewTextBoxColumn Name; private DataGridViewTextBoxColumn Number; - //private DataGridViewTextBoxColumn Id; private DataGridViewTextBoxColumn Component; private DataGridViewTextBoxColumn idd; private DataGridViewTextBoxColumn Numbers; diff --git a/ProjectFlowerShop/FormFlowerComponent.Designer.cs b/ProjectFlowerShop/FormFlowerComponent.Designer.cs index 8e3378b..7f6f9d3 100644 --- a/ProjectFlowerShop/FormFlowerComponent.Designer.cs +++ b/ProjectFlowerShop/FormFlowerComponent.Designer.cs @@ -89,7 +89,7 @@ buttonCancel.UseVisualStyleBackColor = true; buttonCancel.Click += buttonCancel_Click; // - // FormProductComponent + // FormFlowerComponent // AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleMode = AutoScaleMode.Font; @@ -100,9 +100,8 @@ Controls.Add(comboBoxComponent); Controls.Add(labelNumber); Controls.Add(labelComponent); - Name = "FormProductComponent"; - Text = "FormProductComponent"; - Load += FormProductComponent_Load; + Name = "FormFlowerComponent"; + Text = "Цветок-компонент"; ResumeLayout(false); PerformLayout(); } diff --git a/ProjectFlowerShop/FormFlowerComponent.cs b/ProjectFlowerShop/FormFlowerComponent.cs index 697ece2..cd55022 100644 --- a/ProjectFlowerShop/FormFlowerComponent.cs +++ b/ProjectFlowerShop/FormFlowerComponent.cs @@ -65,11 +65,6 @@ namespace ProjectFlowerShop } } - private void FormProductComponent_Load(object sender, EventArgs e) - { - - } - private void buttonSave_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(textBoxNumber.Text)) diff --git a/ProjectFlowerShop/FormFlowers.Designer.cs b/ProjectFlowerShop/FormFlowers.Designer.cs index 5d3d3e7..b9872db 100644 --- a/ProjectFlowerShop/FormFlowers.Designer.cs +++ b/ProjectFlowerShop/FormFlowers.Designer.cs @@ -96,7 +96,7 @@ Controls.Add(AddButton); Controls.Add(DataGridView); Name = "FormFlowers"; - Text = "FlowersForm"; + Text = "Форма цветов"; Load += IceCreamsForm_Load; ((System.ComponentModel.ISupportInitialize)DataGridView).EndInit(); ResumeLayout(false); diff --git a/ProjectFlowerShop/MainForm.Designer.cs b/ProjectFlowerShop/MainForm.Designer.cs index 56eb0e9..2b21f90 100644 --- a/ProjectFlowerShop/MainForm.Designer.cs +++ b/ProjectFlowerShop/MainForm.Designer.cs @@ -62,14 +62,14 @@ // КомпонентыStripMenuItem // КомпонентыStripMenuItem.Name = "КомпонентыStripMenuItem"; - КомпонентыStripMenuItem.Size = new Size(224, 26); + КомпонентыStripMenuItem.Size = new Size(182, 26); КомпонентыStripMenuItem.Text = "Компоненты"; КомпонентыStripMenuItem.Click += КомпонентыStripMenuItem_Click; // // ЦветыStripMenuItem // ЦветыStripMenuItem.Name = "ЦветыStripMenuItem"; - ЦветыStripMenuItem.Size = new Size(224, 26); + ЦветыStripMenuItem.Size = new Size(182, 26); ЦветыStripMenuItem.Text = "Цветы"; ЦветыStripMenuItem.Click += ЦветыStripMenuItem_Click; // @@ -146,7 +146,7 @@ Controls.Add(menuStrip1); MainMenuStrip = menuStrip1; Name = "MainForm"; - Text = "MainForm"; + Text = "Главная форма"; Load += MainForm_Load; menuStrip1.ResumeLayout(false); menuStrip1.PerformLayout(); -- 2.25.1 From 3bdf92c236493d28486e36db5680eb3eeff890ca Mon Sep 17 00:00:00 2001 From: antoc0der <1@DESKTOP-K1L8ND3> Date: Tue, 12 Mar 2024 21:47:28 +0400 Subject: [PATCH 2/3] =?UTF-8?q?=D0=B2=D1=80=D0=BE=D0=B4=D0=B5=20=D1=81?= =?UTF-8?q?=D0=B4=D0=B5=D0=BB=D0=B0=D0=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FlowerShopDatabaseImplement/Component.cs | 64 +++++++ .../ComponentStorage.cs | 89 ++++++++++ FlowerShopDatabaseImplement/Flower.cs | 97 ++++++++++ .../FlowerComponent.cs | 22 +++ .../FlowerShopDataBase.cs | 28 +++ .../FlowerShopDatabaseImplement.csproj | 23 +++ FlowerShopDatabaseImplement/FlowerStorage.cs | 110 ++++++++++++ .../20240312171524_InitialCreate.Designer.cs | 168 ++++++++++++++++++ .../20240312171524_InitialCreate.cs | 122 +++++++++++++ .../FlowerShopDataBaseModelSnapshot.cs | 166 +++++++++++++++++ FlowerShopDatabaseImplement/Order.cs | 71 ++++++++ FlowerShopDatabaseImplement/OrderStorage.cs | 101 +++++++++++ ProjectFlowerShop.sln | 8 +- ProjectFlowerShop/Program.cs | 2 +- ProjectFlowerShop/ProjectFlowerShop.csproj | 5 + 15 files changed, 1074 insertions(+), 2 deletions(-) create mode 100644 FlowerShopDatabaseImplement/Component.cs create mode 100644 FlowerShopDatabaseImplement/ComponentStorage.cs create mode 100644 FlowerShopDatabaseImplement/Flower.cs create mode 100644 FlowerShopDatabaseImplement/FlowerComponent.cs create mode 100644 FlowerShopDatabaseImplement/FlowerShopDataBase.cs create mode 100644 FlowerShopDatabaseImplement/FlowerShopDatabaseImplement.csproj create mode 100644 FlowerShopDatabaseImplement/FlowerStorage.cs create mode 100644 FlowerShopDatabaseImplement/Migrations/20240312171524_InitialCreate.Designer.cs create mode 100644 FlowerShopDatabaseImplement/Migrations/20240312171524_InitialCreate.cs create mode 100644 FlowerShopDatabaseImplement/Migrations/FlowerShopDataBaseModelSnapshot.cs create mode 100644 FlowerShopDatabaseImplement/Order.cs create mode 100644 FlowerShopDatabaseImplement/OrderStorage.cs diff --git a/FlowerShopDatabaseImplement/Component.cs b/FlowerShopDatabaseImplement/Component.cs new file mode 100644 index 0000000..b1bf724 --- /dev/null +++ b/FlowerShopDatabaseImplement/Component.cs @@ -0,0 +1,64 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +using FlowerShopDataModels.Models; +using FlowerShopContracts.BindingModels; +using FlowerShopContracts.ViewModels; +using System.ComponentModel.DataAnnotations.Schema; +using System.ComponentModel.DataAnnotations; + +namespace FlowerShopDatabaseImplement.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 FlowerComponents { 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/FlowerShopDatabaseImplement/ComponentStorage.cs b/FlowerShopDatabaseImplement/ComponentStorage.cs new file mode 100644 index 0000000..4d71a3a --- /dev/null +++ b/FlowerShopDatabaseImplement/ComponentStorage.cs @@ -0,0 +1,89 @@ +using FlowerShopContracts.StoragesContracts; +using FlowerShopContracts.BindingModels; +using FlowerShopContracts.SearchModels; +using FlowerShopContracts.ViewModels; +using FlowerShopDatabaseImplement.Models; + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FlowerShopDatabaseImplement.Implements +{ + public class ComponentStorage : IComponentStorage + { + public List GetFullList() + { + using var context = new FlowerShopDataBase(); + return context.Components + .Select(x => x.GetViewModel) + .ToList(); + } + public List GetFilteredList(ComponentSearchModel model) + { + if (string.IsNullOrEmpty(model.ComponentName)) + { + return new(); + } + using var context = new FlowerShopDataBase(); + 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 FlowerShopDataBase(); + 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 FlowerShopDataBase(); + context.Components.Add(newComponent); + context.SaveChanges(); + return newComponent.GetViewModel; + } + public ComponentViewModel? Update(ComponentBindingModel model) + { + using var context = new FlowerShopDataBase(); + 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 FlowerShopDataBase(); + 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/FlowerShopDatabaseImplement/Flower.cs b/FlowerShopDatabaseImplement/Flower.cs new file mode 100644 index 0000000..1c50d43 --- /dev/null +++ b/FlowerShopDatabaseImplement/Flower.cs @@ -0,0 +1,97 @@ +using FlowerShopDataModels.Models; +using FlowerShopContracts.BindingModels; +using FlowerShopContracts.ViewModels; +using System.ComponentModel.DataAnnotations.Schema; +using System.ComponentModel.DataAnnotations; +using FlowerShopDatabaseImplement; + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FlowerShopDatabaseImplement.Models +{ + public class Flower : IFlowerModel + { + public int Id { get; set; } + [Required] + public string FlowerName { get; set; } = string.Empty; + [Required] + public double Price { get; set; } + private Dictionary? _flowerComponents = null; + [NotMapped] + public Dictionary FlowerComponents + { + get + { + if (_flowerComponents == null) + { + _flowerComponents = Components + .ToDictionary(recPC => recPC.ComponentId, recPC => + (recPC.Component as IComponentModel, recPC.Count)); + } + return _flowerComponents; + } + } + [ForeignKey("FlowerId")] + public virtual List Components { get; set; } = new(); + [ForeignKey("FlowerId")] + public virtual List Orders { get; set; } = new(); + public static Flower Create(FlowerShopDataBase context, FlowerBindingModel model) + { + return new Flower() + { + Id = model.Id, + FlowerName = model.FlowerName, + Price = model.Price, + Components = model.FlowerComponents.Select(x => new FlowerComponent + { + Component = context.Components.First(y => y.Id == x.Key), + Count = x.Value.Item2 + }).ToList() + }; + } + public void Update(FlowerBindingModel model) + { + FlowerName = model.FlowerName; + Price = model.Price; + } + public FlowerViewModel GetViewModel => new() + { + Id = Id, + FlowerName = FlowerName, + Price = Price, + FlowerComponents = FlowerComponents + }; + public void UpdateComponents(FlowerShopDataBase context, FlowerBindingModel model) + { + var flowerComponents = context.FlowerComponents.Where(rec =>rec.FlowerId == model.Id).ToList(); + if (flowerComponents != null && FlowerComponents.Count > 0) + { + context.FlowerComponents.RemoveRange(flowerComponents.Where(rec => !model.FlowerComponents.ContainsKey(rec.ComponentId))); + context.SaveChanges(); + foreach (var updateComponent in flowerComponents) + { + updateComponent.Count = + model.FlowerComponents[updateComponent.ComponentId].Item2; + model.FlowerComponents.Remove(updateComponent.ComponentId); + } + context.SaveChanges(); + } + var flower = context.Flowers.First(x => x.Id == Id); + foreach (var pc in model.FlowerComponents) + { + context.FlowerComponents.Add(new FlowerComponent + { + Flower = flower, + Component = context.Components.First(x => x.Id == pc.Key), + Count = pc.Value.Item2 + }); + context.SaveChanges(); + } + _flowerComponents = null; + } + } +} diff --git a/FlowerShopDatabaseImplement/FlowerComponent.cs b/FlowerShopDatabaseImplement/FlowerComponent.cs new file mode 100644 index 0000000..58aad1f --- /dev/null +++ b/FlowerShopDatabaseImplement/FlowerComponent.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 FlowerShopDatabaseImplement.Models +{ + public class FlowerComponent + { + public int Id { get; set; } + [Required] + public int FlowerId { get; set; } + [Required] + public int ComponentId { get; set; } + [Required] + public int Count { get; set; } + public virtual Component Component { get; set; } = new(); + public virtual Flower Flower { get; set; } = new(); + } +} diff --git a/FlowerShopDatabaseImplement/FlowerShopDataBase.cs b/FlowerShopDatabaseImplement/FlowerShopDataBase.cs new file mode 100644 index 0000000..3f29bcd --- /dev/null +++ b/FlowerShopDatabaseImplement/FlowerShopDataBase.cs @@ -0,0 +1,28 @@ +using Microsoft.EntityFrameworkCore; +using FlowerShopDatabaseImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FlowerShopDatabaseImplement +{ + public class FlowerShopDataBase : DbContext + { + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + if (optionsBuilder.IsConfigured == false) + { + optionsBuilder.UseSqlServer(@"Data +Source=localhost\SQLEXPRESS;Initial Catalog=FlowerShopDatabaseFull;Integrated +Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); + } + base.OnConfiguring(optionsBuilder); + } + public virtual DbSet Components { set; get; } + public virtual DbSet Flowers { set; get; } + public virtual DbSet FlowerComponents { set; get; } + public virtual DbSet Orders { set; get; } + } +} diff --git a/FlowerShopDatabaseImplement/FlowerShopDatabaseImplement.csproj b/FlowerShopDatabaseImplement/FlowerShopDatabaseImplement.csproj new file mode 100644 index 0000000..43c2b67 --- /dev/null +++ b/FlowerShopDatabaseImplement/FlowerShopDatabaseImplement.csproj @@ -0,0 +1,23 @@ + + + + net6.0 + enable + enable + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + diff --git a/FlowerShopDatabaseImplement/FlowerStorage.cs b/FlowerShopDatabaseImplement/FlowerStorage.cs new file mode 100644 index 0000000..b690ace --- /dev/null +++ b/FlowerShopDatabaseImplement/FlowerStorage.cs @@ -0,0 +1,110 @@ +using FlowerShopContracts.StoragesContracts; +using FlowerShopContracts.BindingModels; +using FlowerShopContracts.SearchModels; +using FlowerShopContracts.ViewModels; +using FlowerShopDatabaseImplement.Models; + +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FlowerShopDatabaseImplement.Implements +{ + public class FlowerStorage : IFlowerStorage + { + public List GetFullList() + { + using var context = new FlowerShopDataBase(); + return context.Flowers + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + public List GetFilteredList(FlowerSearchModel model) + { + if (string.IsNullOrEmpty(model.FlowerName)) + { + return new(); + } + using var context = new FlowerShopDataBase(); + return context.Flowers.Include(x => x.Components) + .ThenInclude(x => x.Component) + .Where(x => x.FlowerName.Contains(model.FlowerName)) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + public FlowerViewModel? GetElement(FlowerSearchModel model) + { + if (string.IsNullOrEmpty(model.FlowerName) && + !model.Id.HasValue) + { + return null; + } + using var context = new FlowerShopDataBase(); + return context.Flowers + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.FlowerName) && + x.FlowerName == model.FlowerName) || + (model.Id.HasValue && x.Id == + model.Id)) + ?.GetViewModel; + } + public FlowerViewModel? Insert(FlowerBindingModel model) + { + using var context = new FlowerShopDataBase(); + var newFlower = Flower.Create(context, model); + if (newFlower == null) + { + return null; + } + context.Flowers.Add(newFlower); + context.SaveChanges(); + return newFlower.GetViewModel; + } + public FlowerViewModel? Update(FlowerBindingModel model) + { + using var context = new FlowerShopDataBase(); + using var transaction = context.Database.BeginTransaction(); + try + { + var flower = context.Flowers.FirstOrDefault(rec => + rec.Id == model.Id); + if (flower == null) + { + return null; + } + flower.Update(model); + context.SaveChanges(); + flower.UpdateComponents(context, model); + transaction.Commit(); + return flower.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + public FlowerViewModel? Delete(FlowerBindingModel model) + { + using var context = new FlowerShopDataBase(); + var element = context.Flowers + .Include(x => x.Components) + .FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Flowers.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/FlowerShopDatabaseImplement/Migrations/20240312171524_InitialCreate.Designer.cs b/FlowerShopDatabaseImplement/Migrations/20240312171524_InitialCreate.Designer.cs new file mode 100644 index 0000000..4be73f1 --- /dev/null +++ b/FlowerShopDatabaseImplement/Migrations/20240312171524_InitialCreate.Designer.cs @@ -0,0 +1,168 @@ +// +using System; +using FlowerShopDatabaseImplement; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace FlowerShopDatabaseImplement.Migrations +{ + [DbContext(typeof(FlowerShopDataBase))] + [Migration("20240312171524_InitialCreate")] + partial class InitialCreate + { + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "6.0.0") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder, 1L, 1); + + modelBuilder.Entity("FlowerShopDatabaseImplement.Models.Component", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id"), 1L, 1); + + b.Property("ComponentName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Cost") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Components"); + }); + + modelBuilder.Entity("FlowerShopDatabaseImplement.Models.Flower", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id"), 1L, 1); + + b.Property("FlowerName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Price") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Flowers"); + }); + + modelBuilder.Entity("FlowerShopDatabaseImplement.Models.FlowerComponent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id"), 1L, 1); + + b.Property("ComponentId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("FlowerId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ComponentId"); + + b.HasIndex("FlowerId"); + + b.ToTable("FlowerComponents"); + }); + + modelBuilder.Entity("FlowerShopDatabaseImplement.Order", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id"), 1L, 1); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("DateCreate") + .HasColumnType("datetime2"); + + b.Property("DateImplement") + .HasColumnType("datetime2"); + + b.Property("FlowerId") + .HasColumnType("int"); + + b.Property("Status") + .HasColumnType("int"); + + b.Property("Sum") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.HasIndex("FlowerId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("FlowerShopDatabaseImplement.Models.FlowerComponent", b => + { + b.HasOne("FlowerShopDatabaseImplement.Models.Component", "Component") + .WithMany("FlowerComponents") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("FlowerShopDatabaseImplement.Models.Flower", "Flower") + .WithMany("Components") + .HasForeignKey("FlowerId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Component"); + + b.Navigation("Flower"); + }); + + modelBuilder.Entity("FlowerShopDatabaseImplement.Order", b => + { + b.HasOne("FlowerShopDatabaseImplement.Models.Flower", null) + .WithMany("Orders") + .HasForeignKey("FlowerId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("FlowerShopDatabaseImplement.Models.Component", b => + { + b.Navigation("FlowerComponents"); + }); + + modelBuilder.Entity("FlowerShopDatabaseImplement.Models.Flower", b => + { + b.Navigation("Components"); + + b.Navigation("Orders"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/FlowerShopDatabaseImplement/Migrations/20240312171524_InitialCreate.cs b/FlowerShopDatabaseImplement/Migrations/20240312171524_InitialCreate.cs new file mode 100644 index 0000000..f2647dd --- /dev/null +++ b/FlowerShopDatabaseImplement/Migrations/20240312171524_InitialCreate.cs @@ -0,0 +1,122 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace FlowerShopDatabaseImplement.Migrations +{ + public partial class InitialCreate : Migration + { + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Components", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + ComponentName = table.Column(type: "nvarchar(max)", nullable: false), + Cost = table.Column(type: "float", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Components", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Flowers", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + FlowerName = table.Column(type: "nvarchar(max)", nullable: false), + Price = table.Column(type: "float", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Flowers", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "FlowerComponents", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + FlowerId = table.Column(type: "int", nullable: false), + ComponentId = table.Column(type: "int", nullable: false), + Count = table.Column(type: "int", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_FlowerComponents", x => x.Id); + table.ForeignKey( + name: "FK_FlowerComponents_Components_ComponentId", + column: x => x.ComponentId, + principalTable: "Components", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_FlowerComponents_Flowers_FlowerId", + column: x => x.FlowerId, + principalTable: "Flowers", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "Orders", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + Count = table.Column(type: "int", nullable: false), + Sum = table.Column(type: "float", nullable: false), + Status = table.Column(type: "int", nullable: false), + DateCreate = table.Column(type: "datetime2", nullable: false), + DateImplement = table.Column(type: "datetime2", nullable: true), + FlowerId = table.Column(type: "int", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Orders", x => x.Id); + table.ForeignKey( + name: "FK_Orders_Flowers_FlowerId", + column: x => x.FlowerId, + principalTable: "Flowers", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateIndex( + name: "IX_FlowerComponents_ComponentId", + table: "FlowerComponents", + column: "ComponentId"); + + migrationBuilder.CreateIndex( + name: "IX_FlowerComponents_FlowerId", + table: "FlowerComponents", + column: "FlowerId"); + + migrationBuilder.CreateIndex( + name: "IX_Orders_FlowerId", + table: "Orders", + column: "FlowerId"); + } + + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "FlowerComponents"); + + migrationBuilder.DropTable( + name: "Orders"); + + migrationBuilder.DropTable( + name: "Components"); + + migrationBuilder.DropTable( + name: "Flowers"); + } + } +} diff --git a/FlowerShopDatabaseImplement/Migrations/FlowerShopDataBaseModelSnapshot.cs b/FlowerShopDatabaseImplement/Migrations/FlowerShopDataBaseModelSnapshot.cs new file mode 100644 index 0000000..75b6ac8 --- /dev/null +++ b/FlowerShopDatabaseImplement/Migrations/FlowerShopDataBaseModelSnapshot.cs @@ -0,0 +1,166 @@ +// +using System; +using FlowerShopDatabaseImplement; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace FlowerShopDatabaseImplement.Migrations +{ + [DbContext(typeof(FlowerShopDataBase))] + partial class FlowerShopDataBaseModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "6.0.0") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder, 1L, 1); + + modelBuilder.Entity("FlowerShopDatabaseImplement.Models.Component", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id"), 1L, 1); + + b.Property("ComponentName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Cost") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Components"); + }); + + modelBuilder.Entity("FlowerShopDatabaseImplement.Models.Flower", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id"), 1L, 1); + + b.Property("FlowerName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Price") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Flowers"); + }); + + modelBuilder.Entity("FlowerShopDatabaseImplement.Models.FlowerComponent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id"), 1L, 1); + + b.Property("ComponentId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("FlowerId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ComponentId"); + + b.HasIndex("FlowerId"); + + b.ToTable("FlowerComponents"); + }); + + modelBuilder.Entity("FlowerShopDatabaseImplement.Order", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id"), 1L, 1); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("DateCreate") + .HasColumnType("datetime2"); + + b.Property("DateImplement") + .HasColumnType("datetime2"); + + b.Property("FlowerId") + .HasColumnType("int"); + + b.Property("Status") + .HasColumnType("int"); + + b.Property("Sum") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.HasIndex("FlowerId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("FlowerShopDatabaseImplement.Models.FlowerComponent", b => + { + b.HasOne("FlowerShopDatabaseImplement.Models.Component", "Component") + .WithMany("FlowerComponents") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("FlowerShopDatabaseImplement.Models.Flower", "Flower") + .WithMany("Components") + .HasForeignKey("FlowerId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Component"); + + b.Navigation("Flower"); + }); + + modelBuilder.Entity("FlowerShopDatabaseImplement.Order", b => + { + b.HasOne("FlowerShopDatabaseImplement.Models.Flower", null) + .WithMany("Orders") + .HasForeignKey("FlowerId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("FlowerShopDatabaseImplement.Models.Component", b => + { + b.Navigation("FlowerComponents"); + }); + + modelBuilder.Entity("FlowerShopDatabaseImplement.Models.Flower", b => + { + b.Navigation("Components"); + + b.Navigation("Orders"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/FlowerShopDatabaseImplement/Order.cs b/FlowerShopDatabaseImplement/Order.cs new file mode 100644 index 0000000..66b3063 --- /dev/null +++ b/FlowerShopDatabaseImplement/Order.cs @@ -0,0 +1,71 @@ +using FlowerShopDataModels; +using FlowerShopContracts.BindingModels; +using FlowerShopContracts.ViewModels; +using System.ComponentModel.DataAnnotations.Schema; +using System.ComponentModel.DataAnnotations; + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using FlowerShopDataModels.Enums; + +namespace FlowerShopDatabaseImplement +{ + public class Order : IOrderModel + { + public int Id { get; private set; } + [Required] + public int Count { get; private set; } + [Required] + public double Sum { get; private set; } + [Required] + public OrderStatus Status { get; private set; } + [Required] + public DateTime DateCreate { get; private set; } + public DateTime? DateImplement { get; private set; } + [Required] + public int FlowerId { get; private set; } + + public static Order? Create(OrderBindingModel model) + { + if (model == null) + { + return null; + } + return new Order() + { + Id = model.Id, + Count = model.Count, + Sum = model.Sum, + Status = model.Status, + DateCreate = model.DateCreate, + DateImplement = model.DateImplement, + FlowerId = model.FlowerId, + }; + } + + public void Update(OrderBindingModel? model) + { + if (model == null) + { + return; + } + Status = model.Status; + DateImplement = model.DateImplement; + } + + public OrderViewModel GetViewModel => new() + { + FlowerId = FlowerId, + Count = Count, + Sum = Sum, + Status = Status, + DateCreate = DateCreate, + DateImplement = DateImplement, + Id = Id, + }; + + } +} diff --git a/FlowerShopDatabaseImplement/OrderStorage.cs b/FlowerShopDatabaseImplement/OrderStorage.cs new file mode 100644 index 0000000..6e7996b --- /dev/null +++ b/FlowerShopDatabaseImplement/OrderStorage.cs @@ -0,0 +1,101 @@ +using FlowerShopContracts.StoragesContracts; +using FlowerShopContracts.BindingModels; +using FlowerShopContracts.SearchModels; +using FlowerShopContracts.ViewModels; +using FlowerShopDatabaseImplement.Models; + +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FlowerShopDatabaseImplement.Implements +{ + public class OrderStorage : IOrderStorage + { + public List GetFullList() + { + using var context = new FlowerShopDataBase(); + return context.Orders + .Select(x => AccessFlowerStorage(x.GetViewModel)) + .ToList(); + } + public List GetFilteredList(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return new(); + } + using var context = new FlowerShopDataBase(); + return context.Orders + .Where(x => x.Id == model.Id) + .Select(x => AccessFlowerStorage(x.GetViewModel)) + .ToList(); + } + public OrderViewModel? GetElement(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + using var context = new FlowerShopDataBase(); + return AccessFlowerStorage(context.Orders.FirstOrDefault(x => x.Id == model.Id)?.GetViewModel); + } + public OrderViewModel? Insert(OrderBindingModel model) + { + var newOrder = Order.Create(model); + if (newOrder == null) + { + return null; + } + using var context = new FlowerShopDataBase(); + context.Orders.Add(newOrder); + context.SaveChanges(); + return AccessFlowerStorage(newOrder.GetViewModel); + } + public OrderViewModel? Update(OrderBindingModel model) + { + using var context = new FlowerShopDataBase(); + var order = context.Orders.FirstOrDefault(x => x.Id == + model.Id); + if (order == null) + { + return null; + } + order.Update(model); + context.SaveChanges(); + return AccessFlowerStorage(order.GetViewModel); + } + public OrderViewModel? Delete(OrderBindingModel model) + { + using var context = new FlowerShopDataBase(); + var element = context.Orders.FirstOrDefault(rec => rec.Id == + model.Id); + if (element != null) + { + context.Orders.Remove(element); + context.SaveChanges(); + return AccessFlowerStorage(element.GetViewModel); + } + return null; + } + + public static OrderViewModel AccessFlowerStorage(OrderViewModel model) + { + if (model == null) + return null; + using var context = new FlowerShopDataBase(); + foreach (var flower in context.Flowers) + { + if (flower.Id == model.FlowerId) + { + model.FlowerName = flower.FlowerName; + break; + } + } + return model; + } + } +} diff --git a/ProjectFlowerShop.sln b/ProjectFlowerShop.sln index 0bcd78c..eecfa72 100644 --- a/ProjectFlowerShop.sln +++ b/ProjectFlowerShop.sln @@ -13,7 +13,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FlowerShopBusinessLogic", " EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FlowerShopListImplement", "FlowerShopListImplement\FlowerShopListImplement.csproj", "{62DAA9A0-9A71-4117-8D06-8825E1678D9D}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FlowerShopFileImplement", "FlowerShopFileImplement\FlowerShopFileImplement.csproj", "{0CFC7A18-2E56-4D0B-80AD-F52893222027}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FlowerShopFileImplement", "FlowerShopFileImplement\FlowerShopFileImplement.csproj", "{0CFC7A18-2E56-4D0B-80AD-F52893222027}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FlowerShopDatabaseImplement", "FlowerShopDatabaseImplement\FlowerShopDatabaseImplement.csproj", "{BD9FDCBB-1EE0-4726-85B7-4B9C6D40F761}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -45,6 +47,10 @@ Global {0CFC7A18-2E56-4D0B-80AD-F52893222027}.Debug|Any CPU.Build.0 = Debug|Any CPU {0CFC7A18-2E56-4D0B-80AD-F52893222027}.Release|Any CPU.ActiveCfg = Release|Any CPU {0CFC7A18-2E56-4D0B-80AD-F52893222027}.Release|Any CPU.Build.0 = Release|Any CPU + {BD9FDCBB-1EE0-4726-85B7-4B9C6D40F761}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {BD9FDCBB-1EE0-4726-85B7-4B9C6D40F761}.Debug|Any CPU.Build.0 = Debug|Any CPU + {BD9FDCBB-1EE0-4726-85B7-4B9C6D40F761}.Release|Any CPU.ActiveCfg = Release|Any CPU + {BD9FDCBB-1EE0-4726-85B7-4B9C6D40F761}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/ProjectFlowerShop/Program.cs b/ProjectFlowerShop/Program.cs index 67a1281..8ed9b73 100644 --- a/ProjectFlowerShop/Program.cs +++ b/ProjectFlowerShop/Program.cs @@ -1,7 +1,7 @@ using FlowerShopBusinessLogic.BusinessLogic; using FlowerShopContracts.BusinessLogicsContracts; using FlowerShopContracts.StoragesContracts; -using FlowerShopFileImplement.Implements; +using FlowerShopDatabaseImplement.Implements; using ProjectFlowerShop; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; diff --git a/ProjectFlowerShop/ProjectFlowerShop.csproj b/ProjectFlowerShop/ProjectFlowerShop.csproj index b135f64..5116ff2 100644 --- a/ProjectFlowerShop/ProjectFlowerShop.csproj +++ b/ProjectFlowerShop/ProjectFlowerShop.csproj @@ -9,6 +9,10 @@ + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + @@ -16,6 +20,7 @@ + -- 2.25.1 From a5d27523b9c0bf1de60849ebd00dacc1a7cae35c Mon Sep 17 00:00:00 2001 From: antoc0der <1@DESKTOP-K1L8ND3> Date: Wed, 13 Mar 2024 10:22:41 +0400 Subject: [PATCH 3/3] =?UTF-8?q?=D0=B8=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=B8?= =?UTF-8?q?=D0=BB=20=D0=BE=D1=88=D0=B8=D0=B1=D0=BA=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FlowerShopDatabaseImplement/FlowerShopDataBase.cs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/FlowerShopDatabaseImplement/FlowerShopDataBase.cs b/FlowerShopDatabaseImplement/FlowerShopDataBase.cs index 3f29bcd..2dcffdc 100644 --- a/FlowerShopDatabaseImplement/FlowerShopDataBase.cs +++ b/FlowerShopDatabaseImplement/FlowerShopDataBase.cs @@ -14,9 +14,7 @@ namespace FlowerShopDatabaseImplement { if (optionsBuilder.IsConfigured == false) { - optionsBuilder.UseSqlServer(@"Data -Source=localhost\SQLEXPRESS;Initial Catalog=FlowerShopDatabaseFull;Integrated -Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); + optionsBuilder.UseSqlServer(@"Data Source=localhost\SQLEXPRESS;Initial Catalog=FlowerShopDatabaseFull;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); } base.OnConfiguring(optionsBuilder); } -- 2.25.1