diff --git a/Bar/Bar.sln b/Bar/Bar.sln index c7521ef..9b9b0bb 100644 --- a/Bar/Bar.sln +++ b/Bar/Bar.sln @@ -15,6 +15,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BarListImplement", "BarList EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BarFileImplement", "BarFileImplement\BarFileImplement\BarFileImplement.csproj", "{7B4E6858-4E4D-4D64-B9D4-536F8EC52412}" EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BarDatabaseImplement", "BarDatabaseImplement\BarDatabaseImplement\BarDatabaseImplement.csproj", "{DBBB5E6B-D6D6-4910-B97F-7568D24D66B1}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -45,6 +47,10 @@ Global {7B4E6858-4E4D-4D64-B9D4-536F8EC52412}.Debug|Any CPU.Build.0 = Debug|Any CPU {7B4E6858-4E4D-4D64-B9D4-536F8EC52412}.Release|Any CPU.ActiveCfg = Release|Any CPU {7B4E6858-4E4D-4D64-B9D4-536F8EC52412}.Release|Any CPU.Build.0 = Release|Any CPU + {DBBB5E6B-D6D6-4910-B97F-7568D24D66B1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {DBBB5E6B-D6D6-4910-B97F-7568D24D66B1}.Debug|Any CPU.Build.0 = Debug|Any CPU + {DBBB5E6B-D6D6-4910-B97F-7568D24D66B1}.Release|Any CPU.ActiveCfg = Release|Any CPU + {DBBB5E6B-D6D6-4910-B97F-7568D24D66B1}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/Bar/Bar/BarView.csproj b/Bar/Bar/BarView.csproj index 2d2734a..8ba9294 100644 --- a/Bar/Bar/BarView.csproj +++ b/Bar/Bar/BarView.csproj @@ -16,14 +16,9 @@ + - - - Always - - - \ No newline at end of file diff --git a/Bar/Bar/FormMain.Designer.cs b/Bar/Bar/FormMain.Designer.cs index c41b570..59d2b32 100644 --- a/Bar/Bar/FormMain.Designer.cs +++ b/Bar/Bar/FormMain.Designer.cs @@ -44,7 +44,7 @@ // // CreateOrderButton // - this.CreateOrderButton.Location = new System.Drawing.Point(1075, 90); + this.CreateOrderButton.Location = new System.Drawing.Point(1073, 93); this.CreateOrderButton.Name = "CreateOrderButton"; this.CreateOrderButton.Size = new System.Drawing.Size(194, 40); this.CreateOrderButton.TabIndex = 9; @@ -54,7 +54,7 @@ // // TakeInWorkButton // - this.TakeInWorkButton.Location = new System.Drawing.Point(1075, 145); + this.TakeInWorkButton.Location = new System.Drawing.Point(1073, 148); this.TakeInWorkButton.Name = "TakeInWorkButton"; this.TakeInWorkButton.Size = new System.Drawing.Size(194, 40); this.TakeInWorkButton.TabIndex = 10; @@ -64,7 +64,7 @@ // // ReadyButton // - this.ReadyButton.Location = new System.Drawing.Point(1075, 204); + this.ReadyButton.Location = new System.Drawing.Point(1073, 207); this.ReadyButton.Name = "ReadyButton"; this.ReadyButton.Size = new System.Drawing.Size(194, 40); this.ReadyButton.TabIndex = 11; @@ -74,7 +74,7 @@ // // IssuedButton // - this.IssuedButton.Location = new System.Drawing.Point(1075, 261); + this.IssuedButton.Location = new System.Drawing.Point(1073, 264); this.IssuedButton.Name = "IssuedButton"; this.IssuedButton.Size = new System.Drawing.Size(194, 40); this.IssuedButton.TabIndex = 12; @@ -84,7 +84,7 @@ // // RefreshButton // - this.RefreshButton.Location = new System.Drawing.Point(1075, 322); + this.RefreshButton.Location = new System.Drawing.Point(1073, 325); this.RefreshButton.Name = "RefreshButton"; this.RefreshButton.Size = new System.Drawing.Size(194, 40); this.RefreshButton.TabIndex = 13; @@ -99,7 +99,7 @@ this.DataGridView.Name = "DataGridView"; this.DataGridView.RowHeadersWidth = 51; this.DataGridView.RowTemplate.Height = 29; - this.DataGridView.Size = new System.Drawing.Size(1042, 362); + this.DataGridView.Size = new System.Drawing.Size(1029, 362); this.DataGridView.TabIndex = 14; // // menuStrip1 @@ -109,7 +109,7 @@ this.ToolStripMenu}); this.menuStrip1.Location = new System.Drawing.Point(0, 0); this.menuStrip1.Name = "menuStrip1"; - this.menuStrip1.Size = new System.Drawing.Size(1283, 28); + this.menuStrip1.Size = new System.Drawing.Size(1279, 28); this.menuStrip1.TabIndex = 15; this.menuStrip1.Text = "menuStrip1"; // @@ -140,7 +140,7 @@ // this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(1283, 450); + this.ClientSize = new System.Drawing.Size(1279, 450); this.Controls.Add(this.menuStrip1); this.Controls.Add(this.DataGridView); this.Controls.Add(this.RefreshButton); diff --git a/Bar/Bar/Program.cs b/Bar/Bar/Program.cs index e0c18a0..f2d9b0b 100644 --- a/Bar/Bar/Program.cs +++ b/Bar/Bar/Program.cs @@ -1,7 +1,7 @@ using BarBusinessLogic.BusinessLogics; using BarContracts.BusinessLogicContracts; using BarContracts.StoragesContracts; -using BarFileImplement.Implements; +using BarDatabaseImplement.Implements; using BarView.Forms; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; diff --git a/Bar/BarDatabaseImplement/BarDatabaseImplement.sln b/Bar/BarDatabaseImplement/BarDatabaseImplement.sln new file mode 100644 index 0000000..8721518 --- /dev/null +++ b/Bar/BarDatabaseImplement/BarDatabaseImplement.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.1.32210.238 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BarDatabaseImplement", "BarDatabaseImplement\BarDatabaseImplement.csproj", "{281C0312-6550-4A44-8AD4-087DF7F562F1}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {281C0312-6550-4A44-8AD4-087DF7F562F1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {281C0312-6550-4A44-8AD4-087DF7F562F1}.Debug|Any CPU.Build.0 = Debug|Any CPU + {281C0312-6550-4A44-8AD4-087DF7F562F1}.Release|Any CPU.ActiveCfg = Release|Any CPU + {281C0312-6550-4A44-8AD4-087DF7F562F1}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {B1D89B1D-502D-4E12-98AF-F34B3DE21208} + EndGlobalSection +EndGlobal diff --git a/Bar/BarDatabaseImplement/BarDatabaseImplement/BarDatabase.cs b/Bar/BarDatabaseImplement/BarDatabaseImplement/BarDatabase.cs new file mode 100644 index 0000000..b7a7508 --- /dev/null +++ b/Bar/BarDatabaseImplement/BarDatabaseImplement/BarDatabase.cs @@ -0,0 +1,29 @@ +using BarDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; + +namespace BarDatabaseImplement +{ + public class BarDatabase : DbContext + { + protected override void OnConfiguring(DbContextOptionsBuilder OptionsBuilder) + { + if (OptionsBuilder.IsConfigured == false) + { + OptionsBuilder.UseNpgsql(@"Host=localhost;Database=Bar;Username=postgres;Password=postgres"); + } + + base.OnConfiguring(OptionsBuilder); + + AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true); + AppContext.SetSwitch("Npgsql.DisableDateTimeInfinityConversions", true); + } + + public virtual DbSet Components { set; get; } + + public virtual DbSet Cocktails { set; get; } + + public virtual DbSet CocktailComponents { set; get; } + + public virtual DbSet Orders { set; get; } + } +} diff --git a/Bar/BarDatabaseImplement/BarDatabaseImplement/BarDatabaseImplement.csproj b/Bar/BarDatabaseImplement/BarDatabaseImplement/BarDatabaseImplement.csproj new file mode 100644 index 0000000..1112139 --- /dev/null +++ b/Bar/BarDatabaseImplement/BarDatabaseImplement/BarDatabaseImplement.csproj @@ -0,0 +1,24 @@ + + + + net6.0 + enable + enable + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + + diff --git a/Bar/BarDatabaseImplement/BarDatabaseImplement/Implements/CocktailStorage.cs b/Bar/BarDatabaseImplement/BarDatabaseImplement/Implements/CocktailStorage.cs new file mode 100644 index 0000000..8f9283a --- /dev/null +++ b/Bar/BarDatabaseImplement/BarDatabaseImplement/Implements/CocktailStorage.cs @@ -0,0 +1,109 @@ +using BarContracts.BindingModels; +using BarContracts.SearchModels; +using BarContracts.StoragesContracts; +using BarContracts.ViewModels; +using BarDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; + +namespace BarDatabaseImplement.Implements +{ + public class CocktailStorage : ICocktailStorage + { + public List GetFullList() + { + using var Context = new BarDatabase(); + return Context.Cocktails + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFilteredList(CocktailSearchModel Model) + { + if (string.IsNullOrEmpty(Model.CocktailName)) + return new(); + + using var Context = new BarDatabase(); + + return Context.Cocktails + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .Where(x => x.CocktailName.Contains(Model.CocktailName)) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + + public CocktailViewModel? GetElement(CocktailSearchModel Model) + { + if (string.IsNullOrEmpty(Model.CocktailName) && !Model.Id.HasValue) + return null; + + using var Context = new BarDatabase(); + + return Context.Cocktails + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .FirstOrDefault(x => (!string.IsNullOrEmpty(Model.CocktailName) && x.CocktailName == Model.CocktailName) || + (Model.Id.HasValue && x.Id == Model.Id))? + .GetViewModel; + } + + public CocktailViewModel? Insert(CocktailBindingModel Model) + { + using var Context = new BarDatabase(); + + var NewCocktail = Cocktail.Create(Context, Model); + if (NewCocktail == null) + return null; + + Context.Cocktails.Add(NewCocktail); + Context.SaveChanges(); + + return NewCocktail.GetViewModel; + } + + public CocktailViewModel? Update(CocktailBindingModel Model) + { + using var Context = new BarDatabase(); + + using var Transaction = Context.Database.BeginTransaction(); + try + { + var Cocktail = Context.Cocktails.FirstOrDefault(rec => rec.Id == Model.Id); + + if (Cocktail == null) + return null; + + Cocktail.Update(Model); + Context.SaveChanges(); + Cocktail.UpdateComponents(Context, Model); + + Transaction.Commit(); + return Cocktail.GetViewModel; + } + catch + { + Transaction.Rollback(); + throw; + } + } + public CocktailViewModel? Delete(CocktailBindingModel Model) + { + using var Context = new BarDatabase(); + + var Cocktail = Context.Cocktails + .Include(x => x.Components) + .FirstOrDefault(rec => rec.Id == Model.Id); + + if (Cocktail == null) + return null; + + Context.Cocktails.Remove(Cocktail); + Context.SaveChanges(); + return Cocktail.GetViewModel; + } + } +} diff --git a/Bar/BarDatabaseImplement/BarDatabaseImplement/Implements/ComponentStorage.cs b/Bar/BarDatabaseImplement/BarDatabaseImplement/Implements/ComponentStorage.cs new file mode 100644 index 0000000..dea6fe0 --- /dev/null +++ b/Bar/BarDatabaseImplement/BarDatabaseImplement/Implements/ComponentStorage.cs @@ -0,0 +1,82 @@ +using BarContracts.BindingModels; +using BarContracts.SearchModels; +using BarContracts.StoragesContracts; +using BarContracts.ViewModels; +using BarDatabaseImplement.Models; + +namespace BarDatabaseImplement.Implements +{ + public class ComponentStorage : IComponentStorage + { + public List GetFullList() + { + using var Context = new BarDatabase(); + return Context.Components.Select(x => x.GetViewModel).ToList(); + } + + public List GetFilteredList(ComponentSearchModel Model) + { + if (string.IsNullOrEmpty(Model.ComponentName)) + return new(); + + using var Context = new BarDatabase(); + 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 BarDatabase(); + 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 BarDatabase(); + Context.Components.Add(NewComponent); + Context.SaveChanges(); + + return NewComponent.GetViewModel; + } + + public ComponentViewModel? Update(ComponentBindingModel Model) + { + using var Context = new BarDatabase(); + + 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 BarDatabase(); + var Component = Context.Components.FirstOrDefault(rec => rec.Id == Model.Id); + + if (Component == null) + return null; + + Context.Components.Remove(Component); + Context.SaveChanges(); + return Component.GetViewModel; + } + } +} diff --git a/Bar/BarDatabaseImplement/BarDatabaseImplement/Implements/OrderStorage.cs b/Bar/BarDatabaseImplement/BarDatabaseImplement/Implements/OrderStorage.cs new file mode 100644 index 0000000..86521e7 --- /dev/null +++ b/Bar/BarDatabaseImplement/BarDatabaseImplement/Implements/OrderStorage.cs @@ -0,0 +1,94 @@ +using BarContracts.BindingModels; +using BarContracts.SearchModels; +using BarContracts.StoragesContracts; +using BarContracts.ViewModels; +using BarDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; + +namespace BarDatabaseImplement.Implements +{ + public class OrderStorage : IOrderStorage + { + public List GetFullList() + { + using var Context = new BarDatabase(); + + return Context.Orders + .Include(x => x.Cocktail) + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFilteredList(OrderSearchModel Model) + { + if (!Model.Id.HasValue) + return new(); + + using var Context = new BarDatabase(); + + return Context.Orders + .Include(x => x.Cocktail) + .Where(x => x.Id == Model.Id) + .Select(x => x.GetViewModel) + .ToList(); + } + + public OrderViewModel? GetElement(OrderSearchModel Model) + { + if (!Model.Id.HasValue) + return null; + + using var Context = new BarDatabase(); + + return Context.Orders + .Include(x => x.Cocktail) + .FirstOrDefault(x => Model.Id.HasValue && x.Id == Model.Id)? + .GetViewModel; + } + + public OrderViewModel? Insert(OrderBindingModel Model) + { + if (Model == null) + return null; + + var NewOrder = Order.Create(Model); + if (NewOrder == null) + return null; + + using var Context = new BarDatabase(); + + Context.Orders.Add(NewOrder); + Context.SaveChanges(); + + return Context.Orders.Include(x => x.Cocktail).FirstOrDefault(x => x.Id == NewOrder.Id)?.GetViewModel; + } + + public OrderViewModel? Update(OrderBindingModel Model) + { + using var Context = new BarDatabase(); + + var Order = Context.Orders.FirstOrDefault(x => x.Id == Model.Id); + if (Order == null) + return null; + + Order.Update(Model); + Context.SaveChanges(); + + return Context.Orders.Include(x => x.Cocktail).FirstOrDefault(x => x.Id == Model.Id)?.GetViewModel; + } + + public OrderViewModel? Delete(OrderBindingModel Model) + { + using var Context = new BarDatabase(); + var Order = Context.Orders.FirstOrDefault(rec => rec.Id == Model.Id); + + if (Order == null) + return null; + + Context.Orders.Remove(Order); + Context.SaveChanges(); + + return Order.GetViewModel; + } + } +} diff --git a/Bar/BarDatabaseImplement/BarDatabaseImplement/Migrations/20240502084440_InitialCreate.Designer.cs b/Bar/BarDatabaseImplement/BarDatabaseImplement/Migrations/20240502084440_InitialCreate.Designer.cs new file mode 100644 index 0000000..b0046fc --- /dev/null +++ b/Bar/BarDatabaseImplement/BarDatabaseImplement/Migrations/20240502084440_InitialCreate.Designer.cs @@ -0,0 +1,171 @@ +// +using System; +using BarDatabaseImplement; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +#nullable disable + +namespace BarDatabaseImplement.Migrations +{ + [DbContext(typeof(BarDatabase))] + [Migration("20240502084440_InitialCreate")] + partial class InitialCreate + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.0") + .HasAnnotation("Relational:MaxIdentifierLength", 63); + + NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); + + modelBuilder.Entity("BarDatabaseImplement.Models.Cocktail", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("CocktailName") + .IsRequired() + .HasColumnType("text"); + + b.Property("Price") + .HasColumnType("double precision"); + + b.HasKey("Id"); + + b.ToTable("Cocktails"); + }); + + modelBuilder.Entity("BarDatabaseImplement.Models.CocktailComponent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("CocktailId") + .HasColumnType("integer"); + + b.Property("ComponentId") + .HasColumnType("integer"); + + b.Property("Count") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("CocktailId"); + + b.HasIndex("ComponentId"); + + b.ToTable("CocktailComponents"); + }); + + modelBuilder.Entity("BarDatabaseImplement.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("BarDatabaseImplement.Models.Order", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("CocktailId") + .HasColumnType("integer"); + + b.Property("Count") + .HasColumnType("integer"); + + b.Property("DateCreate") + .HasColumnType("timestamp without time zone"); + + b.Property("DateImplement") + .HasColumnType("timestamp without time zone"); + + b.Property("Status") + .HasColumnType("integer"); + + b.Property("Sum") + .HasColumnType("double precision"); + + b.HasKey("Id"); + + b.HasIndex("CocktailId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("BarDatabaseImplement.Models.CocktailComponent", b => + { + b.HasOne("BarDatabaseImplement.Models.Cocktail", "Cocktail") + .WithMany("Components") + .HasForeignKey("CocktailId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("BarDatabaseImplement.Models.Component", "Component") + .WithMany("CocktailComponents") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Cocktail"); + + b.Navigation("Component"); + }); + + modelBuilder.Entity("BarDatabaseImplement.Models.Order", b => + { + b.HasOne("BarDatabaseImplement.Models.Cocktail", "Cocktail") + .WithMany("Orders") + .HasForeignKey("CocktailId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Cocktail"); + }); + + modelBuilder.Entity("BarDatabaseImplement.Models.Cocktail", b => + { + b.Navigation("Components"); + + b.Navigation("Orders"); + }); + + modelBuilder.Entity("BarDatabaseImplement.Models.Component", b => + { + b.Navigation("CocktailComponents"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Bar/BarDatabaseImplement/BarDatabaseImplement/Migrations/20240502084440_InitialCreate.cs b/Bar/BarDatabaseImplement/BarDatabaseImplement/Migrations/20240502084440_InitialCreate.cs new file mode 100644 index 0000000..4bf6091 --- /dev/null +++ b/Bar/BarDatabaseImplement/BarDatabaseImplement/Migrations/20240502084440_InitialCreate.cs @@ -0,0 +1,126 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +#nullable disable + +namespace BarDatabaseImplement.Migrations +{ + /// + public partial class InitialCreate : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Cocktails", + columns: table => new + { + Id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + CocktailName = table.Column(type: "text", nullable: false), + Price = table.Column(type: "double precision", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Cocktails", x => x.Id); + }); + + 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), + CocktailId = 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 without time zone", nullable: false), + DateImplement = table.Column(type: "timestamp without time zone", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_Orders", x => x.Id); + table.ForeignKey( + name: "FK_Orders_Cocktails_CocktailId", + column: x => x.CocktailId, + principalTable: "Cocktails", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "CocktailComponents", + columns: table => new + { + Id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + CocktailId = 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_CocktailComponents", x => x.Id); + table.ForeignKey( + name: "FK_CocktailComponents_Cocktails_CocktailId", + column: x => x.CocktailId, + principalTable: "Cocktails", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_CocktailComponents_Components_ComponentId", + column: x => x.ComponentId, + principalTable: "Components", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateIndex( + name: "IX_CocktailComponents_CocktailId", + table: "CocktailComponents", + column: "CocktailId"); + + migrationBuilder.CreateIndex( + name: "IX_CocktailComponents_ComponentId", + table: "CocktailComponents", + column: "ComponentId"); + + migrationBuilder.CreateIndex( + name: "IX_Orders_CocktailId", + table: "Orders", + column: "CocktailId"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "CocktailComponents"); + + migrationBuilder.DropTable( + name: "Orders"); + + migrationBuilder.DropTable( + name: "Components"); + + migrationBuilder.DropTable( + name: "Cocktails"); + } + } +} diff --git a/Bar/BarDatabaseImplement/BarDatabaseImplement/Migrations/BarDatabaseModelSnapshot.cs b/Bar/BarDatabaseImplement/BarDatabaseImplement/Migrations/BarDatabaseModelSnapshot.cs new file mode 100644 index 0000000..25b8ad3 --- /dev/null +++ b/Bar/BarDatabaseImplement/BarDatabaseImplement/Migrations/BarDatabaseModelSnapshot.cs @@ -0,0 +1,168 @@ +// +using System; +using BarDatabaseImplement; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +#nullable disable + +namespace BarDatabaseImplement.Migrations +{ + [DbContext(typeof(BarDatabase))] + partial class BarDatabaseModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.0") + .HasAnnotation("Relational:MaxIdentifierLength", 63); + + NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); + + modelBuilder.Entity("BarDatabaseImplement.Models.Cocktail", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("CocktailName") + .IsRequired() + .HasColumnType("text"); + + b.Property("Price") + .HasColumnType("double precision"); + + b.HasKey("Id"); + + b.ToTable("Cocktails"); + }); + + modelBuilder.Entity("BarDatabaseImplement.Models.CocktailComponent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("CocktailId") + .HasColumnType("integer"); + + b.Property("ComponentId") + .HasColumnType("integer"); + + b.Property("Count") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("CocktailId"); + + b.HasIndex("ComponentId"); + + b.ToTable("CocktailComponents"); + }); + + modelBuilder.Entity("BarDatabaseImplement.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("BarDatabaseImplement.Models.Order", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("CocktailId") + .HasColumnType("integer"); + + b.Property("Count") + .HasColumnType("integer"); + + b.Property("DateCreate") + .HasColumnType("timestamp without time zone"); + + b.Property("DateImplement") + .HasColumnType("timestamp without time zone"); + + b.Property("Status") + .HasColumnType("integer"); + + b.Property("Sum") + .HasColumnType("double precision"); + + b.HasKey("Id"); + + b.HasIndex("CocktailId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("BarDatabaseImplement.Models.CocktailComponent", b => + { + b.HasOne("BarDatabaseImplement.Models.Cocktail", "Cocktail") + .WithMany("Components") + .HasForeignKey("CocktailId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("BarDatabaseImplement.Models.Component", "Component") + .WithMany("CocktailComponents") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Cocktail"); + + b.Navigation("Component"); + }); + + modelBuilder.Entity("BarDatabaseImplement.Models.Order", b => + { + b.HasOne("BarDatabaseImplement.Models.Cocktail", "Cocktail") + .WithMany("Orders") + .HasForeignKey("CocktailId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Cocktail"); + }); + + modelBuilder.Entity("BarDatabaseImplement.Models.Cocktail", b => + { + b.Navigation("Components"); + + b.Navigation("Orders"); + }); + + modelBuilder.Entity("BarDatabaseImplement.Models.Component", b => + { + b.Navigation("CocktailComponents"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Bar/BarDatabaseImplement/BarDatabaseImplement/Models/Cocktail.cs b/Bar/BarDatabaseImplement/BarDatabaseImplement/Models/Cocktail.cs new file mode 100644 index 0000000..9acdf03 --- /dev/null +++ b/Bar/BarDatabaseImplement/BarDatabaseImplement/Models/Cocktail.cs @@ -0,0 +1,112 @@ +using BarContracts.BindingModels; +using BarContracts.ViewModels; +using BarDataModels.Models; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; + +namespace BarDatabaseImplement.Models +{ + public class Cocktail : ICocktailModel + { + public int Id { get; set; } + + [Required] + public string CocktailName { get; set; } = string.Empty; + + [Required] + public double Price { get; set; } + + private Dictionary? _cocktailComponents = null; + + [NotMapped] + public Dictionary CocktailComponents + { + get + { + if (_cocktailComponents == null) + { + _cocktailComponents = Components.ToDictionary(RepComp => RepComp.ComponentId, RepComp => + (RepComp.Component as IComponentModel, RepComp.Count)); + } + + return _cocktailComponents; + } + } + + [ForeignKey("CocktailId")] + public virtual List Components { get; set; } = new(); + + [ForeignKey("CocktailId")] + public virtual List Orders { get; set; } = new(); + + public static Cocktail Create(BarDatabase Context, CocktailBindingModel Model) + { + return new Cocktail() + { + Id = Model.Id, + CocktailName = Model.CocktailName, + Price = Model.Price, + Components = Model.CocktailComponents.Select(x => new CocktailComponent + { + Component = Context.Components.First(y => y.Id == x.Key), + Count = x.Value.Item2 + }).ToList() + }; + } + + public void Update(CocktailBindingModel Model) + { + CocktailName = Model.CocktailName; + Price = Model.Price; + } + + public CocktailViewModel GetViewModel => new() + { + Id = Id, + CocktailName = CocktailName, + Price = Price, + CocktailComponents = CocktailComponents + }; + + public void UpdateComponents(BarDatabase Context, CocktailBindingModel Model) + { + var CocktailComponents = Context.CocktailComponents.Where(rec => rec.CocktailId == Model.Id).ToList(); + + if (CocktailComponents != null && CocktailComponents.Count > 0) + { + Context.CocktailComponents.RemoveRange(CocktailComponents.Where(rec => !Model.CocktailComponents.ContainsKey(rec.ComponentId))); + Context.SaveChanges(); + + try + { + foreach (var ComponentToUpdate in CocktailComponents) + { + ComponentToUpdate.Count = Model.CocktailComponents[ComponentToUpdate.ComponentId].Item2; + Model.CocktailComponents.Remove(ComponentToUpdate.ComponentId); + } + }catch (Exception ex) + { + + } + + Context.SaveChanges(); + } + + var Cocktail = Context.Cocktails.First(x => x.Id == Id); + + foreach (var CocktailComponent in Model.CocktailComponents) + { + Context.CocktailComponents.Add(new CocktailComponent + { + Cocktail = Cocktail, + Component = Context.Components.First(x => x.Id == CocktailComponent.Key), + Count = CocktailComponent.Value.Item2 + }); + + Context.SaveChanges(); + } + + _cocktailComponents = null; + } + } +} diff --git a/Bar/BarDatabaseImplement/BarDatabaseImplement/Models/CocktailComponent.cs b/Bar/BarDatabaseImplement/BarDatabaseImplement/Models/CocktailComponent.cs new file mode 100644 index 0000000..0ee8129 --- /dev/null +++ b/Bar/BarDatabaseImplement/BarDatabaseImplement/Models/CocktailComponent.cs @@ -0,0 +1,22 @@ +using System.ComponentModel.DataAnnotations; + +namespace BarDatabaseImplement.Models +{ + public class CocktailComponent + { + public int Id { get; set; } + + [Required] + public int CocktailId { get; set; } + + [Required] + public int ComponentId { get; set; } + + [Required] + public int Count { get; set; } + + public virtual Component Component { get; set; } = new(); + + public virtual Cocktail Cocktail { get; set; } = new(); + } +} diff --git a/Bar/BarDatabaseImplement/BarDatabaseImplement/Models/Component.cs b/Bar/BarDatabaseImplement/BarDatabaseImplement/Models/Component.cs new file mode 100644 index 0000000..7c2e115 --- /dev/null +++ b/Bar/BarDatabaseImplement/BarDatabaseImplement/Models/Component.cs @@ -0,0 +1,61 @@ +using BarContracts.BindingModels; +using BarContracts.ViewModels; +using BarDataModels.Models; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; + +namespace BarDatabaseImplement.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 CocktailComponents { 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/Bar/BarDatabaseImplement/BarDatabaseImplement/Models/Order.cs b/Bar/BarDatabaseImplement/BarDatabaseImplement/Models/Order.cs new file mode 100644 index 0000000..eebf082 --- /dev/null +++ b/Bar/BarDatabaseImplement/BarDatabaseImplement/Models/Order.cs @@ -0,0 +1,69 @@ +using BarContracts.BindingModels; +using BarContracts.ViewModels; +using BarDataModels.Enums; +using BarDataModels.Models; +using System.ComponentModel.DataAnnotations; +namespace BarDatabaseImplement.Models +{ + public class Order : IOrderModel + { + public int Id { get; private set; } + + [Required] + public int CocktailId { get; private set; } + + public virtual Cocktail Cocktail { get; set; } + + [Required] + public int Count { get; private set; } + + [Required] + public double Sum { get; private set; } + + [Required] + public OrderStatus Status { get; private set; } = OrderStatus.Undefined; + + [Required] + public DateTime DateCreate { get; private set; } = DateTime.Now; + + public DateTime? DateImplement { get; private set; } + + public static Order? Create(OrderBindingModel Model) + { + if (Model is null) + return null; + + return new Order() + { + Id = Model.Id, + CocktailId = Model.CocktailId, + Count = Model.Count, + Sum = Model.Sum, + Status = Model.Status, + DateCreate = Model.DateCreate, + DateImplement = Model.DateImplement, + }; + } + + public void Update(OrderBindingModel Model) + { + if (Model is null) + return; + + Status = Model.Status; + DateImplement = Model.DateImplement; + } + + public OrderViewModel GetViewModel => new() + { + Id = Id, + CocktailId = CocktailId, + CocktailName = Cocktail.CocktailName, + Count = Count, + Sum = Sum, + Status = Status, + DateCreate = DateCreate, + DateImplement = DateImplement, + }; + } +}