diff --git a/Pizzeria/Pizzeria.sln b/Pizzeria/Pizzeria.sln index 2f7e985..e536891 100644 --- a/Pizzeria/Pizzeria.sln +++ b/Pizzeria/Pizzeria.sln @@ -15,6 +15,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PizzeriaBusinessLogic", "Pi EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PizzeriaFileImplement", "PizzeriaFileImplement\PizzeriaFileImplement.csproj", "{5FA9C75E-4A06-4B3E-AD87-48B0C96BB4A1}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PizzeriaDatabaseImplement", "PizzeriaDatabaseImplement\PizzeriaDatabaseImplement.csproj", "{31FC8CAC-BED8-4DF0-8B57-20917DC66A5C}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -45,6 +47,10 @@ Global {5FA9C75E-4A06-4B3E-AD87-48B0C96BB4A1}.Debug|Any CPU.Build.0 = Debug|Any CPU {5FA9C75E-4A06-4B3E-AD87-48B0C96BB4A1}.Release|Any CPU.ActiveCfg = Release|Any CPU {5FA9C75E-4A06-4B3E-AD87-48B0C96BB4A1}.Release|Any CPU.Build.0 = Release|Any CPU + {31FC8CAC-BED8-4DF0-8B57-20917DC66A5C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {31FC8CAC-BED8-4DF0-8B57-20917DC66A5C}.Debug|Any CPU.Build.0 = Debug|Any CPU + {31FC8CAC-BED8-4DF0-8B57-20917DC66A5C}.Release|Any CPU.ActiveCfg = Release|Any CPU + {31FC8CAC-BED8-4DF0-8B57-20917DC66A5C}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/Pizzeria/Pizzeria/Pizzeria.csproj b/Pizzeria/Pizzeria/Pizzeria.csproj index d5d1372..2428964 100644 --- a/Pizzeria/Pizzeria/Pizzeria.csproj +++ b/Pizzeria/Pizzeria/Pizzeria.csproj @@ -19,6 +19,14 @@ + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + @@ -28,6 +36,7 @@ + diff --git a/Pizzeria/Pizzeria/Program.cs b/Pizzeria/Pizzeria/Program.cs index 82ced79..c4418d1 100644 --- a/Pizzeria/Pizzeria/Program.cs +++ b/Pizzeria/Pizzeria/Program.cs @@ -4,7 +4,7 @@ using PizzeriaContracts.StorageContracts; using PizzeriaBusinessLogic.BusinessLogic; using Microsoft.Extensions.Logging; using NLog.Extensions.Logging; -using PizzeriaFileImplement.Implements; +using PizzeriaDatabaseImplement.Implements; namespace Pizzeria { diff --git a/Pizzeria/PizzeriaBusinessLogic/PizzeriaBusinessLogic.csproj b/Pizzeria/PizzeriaBusinessLogic/PizzeriaBusinessLogic.csproj index d899681..eb42739 100644 --- a/Pizzeria/PizzeriaBusinessLogic/PizzeriaBusinessLogic.csproj +++ b/Pizzeria/PizzeriaBusinessLogic/PizzeriaBusinessLogic.csproj @@ -7,6 +7,10 @@ + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/Pizzeria/PizzeriaContracts/PizzeriaContracts.csproj b/Pizzeria/PizzeriaContracts/PizzeriaContracts.csproj index f8743a9..581d85e 100644 --- a/Pizzeria/PizzeriaContracts/PizzeriaContracts.csproj +++ b/Pizzeria/PizzeriaContracts/PizzeriaContracts.csproj @@ -6,6 +6,13 @@ enable + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + diff --git a/Pizzeria/PizzeriaDataModels/PizzeriaDataModels.csproj b/Pizzeria/PizzeriaDataModels/PizzeriaDataModels.csproj index 132c02c..7fb12f1 100644 --- a/Pizzeria/PizzeriaDataModels/PizzeriaDataModels.csproj +++ b/Pizzeria/PizzeriaDataModels/PizzeriaDataModels.csproj @@ -6,4 +6,11 @@ enable + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + diff --git a/Pizzeria/PizzeriaDatabaseImplement/Implements/ComponentStorage.cs b/Pizzeria/PizzeriaDatabaseImplement/Implements/ComponentStorage.cs new file mode 100644 index 0000000..e1b74ce --- /dev/null +++ b/Pizzeria/PizzeriaDatabaseImplement/Implements/ComponentStorage.cs @@ -0,0 +1,72 @@ +using PizzeriaContracts.BindingModels; +using PizzeriaContracts.SearchModels; +using PizzeriaContracts.StorageContracts; +using PizzeriaContracts.ViewModels; +using PizzeriaDatabaseImplement.Models; + +namespace PizzeriaDatabaseImplement.Implements +{ + public class ComponentStorage : IComponentStorage + { + public List GetFullList() + { + using var context = new PizzeriaDatabase(); + return context.Components.Select(x => x.GetViewModel).ToList(); + } + public List GetFilteredList(ComponentSearchModel model) + { + if (string.IsNullOrEmpty(model.ComponentName)) + { + return new(); + } + using var context = new PizzeriaDatabase(); + 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 PizzeriaDatabase(); + 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 PizzeriaDatabase(); + context.Components.Add(newComponent); + context.SaveChanges(); + return newComponent.GetViewModel; + } + public ComponentViewModel? Update(ComponentBindingModel model) + { + using var context = new PizzeriaDatabase(); + 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 PizzeriaDatabase(); + 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/Pizzeria/PizzeriaDatabaseImplement/Implements/OrderStorage.cs b/Pizzeria/PizzeriaDatabaseImplement/Implements/OrderStorage.cs new file mode 100644 index 0000000..05ac0fb --- /dev/null +++ b/Pizzeria/PizzeriaDatabaseImplement/Implements/OrderStorage.cs @@ -0,0 +1,76 @@ +using Microsoft.EntityFrameworkCore; +using PizzeriaContracts.BindingModels; +using PizzeriaContracts.SearchModels; +using PizzeriaContracts.StorageContracts; +using PizzeriaContracts.ViewModels; +using PizzeriaDatabaseImplement.Models; +namespace PizzeriaDatabaseImplement.Implements +{ + public class OrderStorage : IOrderStorage + { + + public OrderViewModel? GetElement(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + using var context = new PizzeriaDatabase(); + return context.Orders.Include(x => x.Pizza).FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + + public List GetFilteredList(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return new(); + } + using var context = new PizzeriaDatabase(); + return context.Orders.Include(x => x.Pizza).Where(x => x.Id == model.Id).Select(x => x.GetViewModel).ToList(); + } + + public List GetFullList() + { + using var context = new PizzeriaDatabase(); + return context.Orders.Include(x => x.Pizza).Select(x => x.GetViewModel).ToList(); + } + + public OrderViewModel? Insert(OrderBindingModel model) + { + var newOrder = Order.Create(model); + if (newOrder == null) + { + return null; + } + using var context = new PizzeriaDatabase(); + context.Orders.Add(newOrder); + context.SaveChanges(); + return context.Orders.Include(x => x.Pizza).FirstOrDefault(x => x.Id == newOrder.Id)?.GetViewModel; + } + + public OrderViewModel? Update(OrderBindingModel model) + { + using var context = new PizzeriaDatabase(); + var component = context.Orders.FirstOrDefault(x => x.Id == model.Id); + if (component == null) + { + return null; + } + component.Update(model); + context.SaveChanges(); + return context.Orders.Include(x => x.Pizza).FirstOrDefault(x => x.Id == model.Id)?.GetViewModel; + } + public OrderViewModel? Delete(OrderBindingModel model) + { + using var context = new PizzeriaDatabase(); + var element = context.Orders.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Orders.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/Pizzeria/PizzeriaDatabaseImplement/Implements/PizzaStorage.cs b/Pizzeria/PizzeriaDatabaseImplement/Implements/PizzaStorage.cs new file mode 100644 index 0000000..5aa93f7 --- /dev/null +++ b/Pizzeria/PizzeriaDatabaseImplement/Implements/PizzaStorage.cs @@ -0,0 +1,84 @@ +using Microsoft.EntityFrameworkCore; +using PizzeriaContracts.BindingModels; +using PizzeriaContracts.SearchModels; +using PizzeriaContracts.StorageContracts; +using PizzeriaContracts.ViewModels; +using PizzeriaDatabaseImplement.Models; + +namespace PizzeriaDatabaseImplement.Implements +{ + public class PizzaStorage : IPizzaStorage + { + public List GetFullList() + { + using var context = new PizzeriaDatabase(); + return context.Pizzas.Include(x => x.Components).ThenInclude(x => x.Component).ToList().Select(x => x.GetViewModel).ToList(); + } + public List GetFilteredList(PizzaSearchModel model) + { + if (string.IsNullOrEmpty(model.PizzaName)) + { + return new(); + } + using var context = new PizzeriaDatabase(); + return context.Pizzas.Include(x => x.Components).ThenInclude(x => x.Component).Where(x => x.PizzaName.Contains(model.PizzaName)).ToList().Select(x => x.GetViewModel).ToList(); + } + public PizzaViewModel? GetElement(PizzaSearchModel model) + { + if (string.IsNullOrEmpty(model.PizzaName) && + !model.Id.HasValue) + { + return null; + } + using var context = new PizzeriaDatabase(); + return context.Pizzas.Include(x => x.Components).ThenInclude(x => x.Component).FirstOrDefault(x => (!string.IsNullOrEmpty(model.PizzaName) && x.PizzaName == model.PizzaName) ||(model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + public PizzaViewModel? Insert(PizzaBindingModel model) + { + using var context = new PizzeriaDatabase(); + var newPizza = Pizza.Create(context, model); + if (newPizza == null) + { + return null; + } + context.Pizzas.Add(newPizza); + context.SaveChanges(); + return newPizza.GetViewModel; + } + public PizzaViewModel? Update(PizzaBindingModel model) + { + using var context = new PizzeriaDatabase(); + using var transaction = context.Database.BeginTransaction(); + try + { + var Pizza = context.Pizzas.FirstOrDefault(rec => rec.Id == model.Id); + if (Pizza == null) + { + return null; + } + Pizza.Update(model); + context.SaveChanges(); + Pizza.UpdateComponents(context, model); + transaction.Commit(); + return Pizza.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + public PizzaViewModel? Delete(PizzaBindingModel model) + { + using var context = new PizzeriaDatabase(); + var element = context.Pizzas.Include(x => x.Components).FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Pizzas.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/Pizzeria/PizzeriaDatabaseImplement/Migrations/20240406203725_InitMigration.Designer.cs b/Pizzeria/PizzeriaDatabaseImplement/Migrations/20240406203725_InitMigration.Designer.cs new file mode 100644 index 0000000..64ea112 --- /dev/null +++ b/Pizzeria/PizzeriaDatabaseImplement/Migrations/20240406203725_InitMigration.Designer.cs @@ -0,0 +1,171 @@ +// +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using PizzeriaDatabaseImplement; + +#nullable disable + +namespace PizzeriaDatabaseImplement.Migrations +{ + [DbContext(typeof(PizzeriaDatabase))] + [Migration("20240406203725_InitMigration")] + partial class InitMigration + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.17") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("PizzeriaDatabaseImplement.Models.Component", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComponentName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Cost") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Components"); + }); + + modelBuilder.Entity("PizzeriaDatabaseImplement.Models.Order", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("DateCreate") + .HasColumnType("datetime2"); + + b.Property("DateImplement") + .HasColumnType("datetime2"); + + b.Property("PizzaId") + .HasColumnType("int"); + + b.Property("Status") + .HasColumnType("int"); + + b.Property("Sum") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.HasIndex("PizzaId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("PizzeriaDatabaseImplement.Models.Pizza", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("PizzaName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Price") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Pizzas"); + }); + + modelBuilder.Entity("PizzeriaDatabaseImplement.Models.PizzaComponent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComponentId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("PizzaId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ComponentId"); + + b.HasIndex("PizzaId"); + + b.ToTable("PizzaComponents"); + }); + + modelBuilder.Entity("PizzeriaDatabaseImplement.Models.Order", b => + { + b.HasOne("PizzeriaDatabaseImplement.Models.Pizza", "Pizza") + .WithMany("Orders") + .HasForeignKey("PizzaId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Pizza"); + }); + + modelBuilder.Entity("PizzeriaDatabaseImplement.Models.PizzaComponent", b => + { + b.HasOne("PizzeriaDatabaseImplement.Models.Component", "Component") + .WithMany("PizzaComponents") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("PizzeriaDatabaseImplement.Models.Pizza", "Pizza") + .WithMany("Components") + .HasForeignKey("PizzaId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Component"); + + b.Navigation("Pizza"); + }); + + modelBuilder.Entity("PizzeriaDatabaseImplement.Models.Component", b => + { + b.Navigation("PizzaComponents"); + }); + + modelBuilder.Entity("PizzeriaDatabaseImplement.Models.Pizza", b => + { + b.Navigation("Components"); + + b.Navigation("Orders"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Pizzeria/PizzeriaDatabaseImplement/Migrations/20240406203725_InitMigration.cs b/Pizzeria/PizzeriaDatabaseImplement/Migrations/20240406203725_InitMigration.cs new file mode 100644 index 0000000..02976ac --- /dev/null +++ b/Pizzeria/PizzeriaDatabaseImplement/Migrations/20240406203725_InitMigration.cs @@ -0,0 +1,125 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace PizzeriaDatabaseImplement.Migrations +{ + /// + public partial class InitMigration : 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: "Pizzas", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + PizzaName = table.Column(type: "nvarchar(max)", nullable: false), + Price = table.Column(type: "float", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Pizzas", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Orders", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + PizzaId = table.Column(type: "int", nullable: false), + 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) + }, + constraints: table => + { + table.PrimaryKey("PK_Orders", x => x.Id); + table.ForeignKey( + name: "FK_Orders_Pizzas_PizzaId", + column: x => x.PizzaId, + principalTable: "Pizzas", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "PizzaComponents", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + PizzaId = 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_PizzaComponents", x => x.Id); + table.ForeignKey( + name: "FK_PizzaComponents_Components_ComponentId", + column: x => x.ComponentId, + principalTable: "Components", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_PizzaComponents_Pizzas_PizzaId", + column: x => x.PizzaId, + principalTable: "Pizzas", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateIndex( + name: "IX_Orders_PizzaId", + table: "Orders", + column: "PizzaId"); + + migrationBuilder.CreateIndex( + name: "IX_PizzaComponents_ComponentId", + table: "PizzaComponents", + column: "ComponentId"); + + migrationBuilder.CreateIndex( + name: "IX_PizzaComponents_PizzaId", + table: "PizzaComponents", + column: "PizzaId"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "Orders"); + + migrationBuilder.DropTable( + name: "PizzaComponents"); + + migrationBuilder.DropTable( + name: "Components"); + + migrationBuilder.DropTable( + name: "Pizzas"); + } + } +} diff --git a/Pizzeria/PizzeriaDatabaseImplement/Migrations/PizzeriaDatabaseModelSnapshot.cs b/Pizzeria/PizzeriaDatabaseImplement/Migrations/PizzeriaDatabaseModelSnapshot.cs new file mode 100644 index 0000000..953507f --- /dev/null +++ b/Pizzeria/PizzeriaDatabaseImplement/Migrations/PizzeriaDatabaseModelSnapshot.cs @@ -0,0 +1,168 @@ +// +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using PizzeriaDatabaseImplement; + +#nullable disable + +namespace PizzeriaDatabaseImplement.Migrations +{ + [DbContext(typeof(PizzeriaDatabase))] + partial class PizzeriaDatabaseModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.17") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("PizzeriaDatabaseImplement.Models.Component", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComponentName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Cost") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Components"); + }); + + modelBuilder.Entity("PizzeriaDatabaseImplement.Models.Order", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("DateCreate") + .HasColumnType("datetime2"); + + b.Property("DateImplement") + .HasColumnType("datetime2"); + + b.Property("PizzaId") + .HasColumnType("int"); + + b.Property("Status") + .HasColumnType("int"); + + b.Property("Sum") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.HasIndex("PizzaId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("PizzeriaDatabaseImplement.Models.Pizza", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("PizzaName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Price") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Pizzas"); + }); + + modelBuilder.Entity("PizzeriaDatabaseImplement.Models.PizzaComponent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComponentId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("PizzaId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ComponentId"); + + b.HasIndex("PizzaId"); + + b.ToTable("PizzaComponents"); + }); + + modelBuilder.Entity("PizzeriaDatabaseImplement.Models.Order", b => + { + b.HasOne("PizzeriaDatabaseImplement.Models.Pizza", "Pizza") + .WithMany("Orders") + .HasForeignKey("PizzaId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Pizza"); + }); + + modelBuilder.Entity("PizzeriaDatabaseImplement.Models.PizzaComponent", b => + { + b.HasOne("PizzeriaDatabaseImplement.Models.Component", "Component") + .WithMany("PizzaComponents") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("PizzeriaDatabaseImplement.Models.Pizza", "Pizza") + .WithMany("Components") + .HasForeignKey("PizzaId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Component"); + + b.Navigation("Pizza"); + }); + + modelBuilder.Entity("PizzeriaDatabaseImplement.Models.Component", b => + { + b.Navigation("PizzaComponents"); + }); + + modelBuilder.Entity("PizzeriaDatabaseImplement.Models.Pizza", b => + { + b.Navigation("Components"); + + b.Navigation("Orders"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Pizzeria/PizzeriaDatabaseImplement/Models/Component.cs b/Pizzeria/PizzeriaDatabaseImplement/Models/Component.cs new file mode 100644 index 0000000..e64a658 --- /dev/null +++ b/Pizzeria/PizzeriaDatabaseImplement/Models/Component.cs @@ -0,0 +1,56 @@ +using PizzeriaDataModels.Models; +using System.ComponentModel.DataAnnotations.Schema; +using System.ComponentModel.DataAnnotations; +using PizzeriaContracts.BindingModels; +using PizzeriaContracts.ViewModels; + +namespace PizzeriaDatabaseImplement.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 PizzaComponents { 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/Pizzeria/PizzeriaDatabaseImplement/Models/Order.cs b/Pizzeria/PizzeriaDatabaseImplement/Models/Order.cs new file mode 100644 index 0000000..22a0856 --- /dev/null +++ b/Pizzeria/PizzeriaDatabaseImplement/Models/Order.cs @@ -0,0 +1,63 @@ +using PizzeriaContracts.BindingModels; +using PizzeriaContracts.ViewModels; +using PizzeriaDataModels.Enums; +using PizzeriaDataModels.Models; +using System.ComponentModel.DataAnnotations; + +namespace PizzeriaDatabaseImplement.Models +{ + public class Order : IOrderModel + { + public int Id { get; private set; } + [Required] + public int PizzaId { 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; } + public virtual Pizza Pizza { get; set; } + public static Order? Create(OrderBindingModel? model) + { + if (model == null) + { + return null; + } + return new Order() + { + Id = model.Id, + PizzaId = model.PizzaId, + 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, + PizzaId = PizzaId, + Count = Count, + Sum = Sum, + Status = Status, + DateCreate = DateCreate, + DateImplement = DateImplement, + PizzaName = Pizza.PizzaName + }; + } +} diff --git a/Pizzeria/PizzeriaDatabaseImplement/Models/Pizza.cs b/Pizzeria/PizzeriaDatabaseImplement/Models/Pizza.cs new file mode 100644 index 0000000..9b14d96 --- /dev/null +++ b/Pizzeria/PizzeriaDatabaseImplement/Models/Pizza.cs @@ -0,0 +1,90 @@ +using PizzeriaDataModels.Models; +using System.ComponentModel.DataAnnotations.Schema; +using System.ComponentModel.DataAnnotations; +using PizzeriaContracts.BindingModels; +using PizzeriaContracts.ViewModels; + +namespace PizzeriaDatabaseImplement.Models +{ + public class Pizza : IPizzaModel + { + public int Id { get; set; } + [Required] + public string PizzaName { get; set; } = string.Empty; + [Required] + public double Price { get; set; } + private Dictionary? _PizzaComponents = null; + [NotMapped] + public Dictionary PizzaComponents + { + get + { + if (_PizzaComponents == null) + { + _PizzaComponents = Components.ToDictionary(recPC => recPC.ComponentId, recPC => (recPC.Component as IComponentModel, recPC.Count)); + } + return _PizzaComponents; + } + } + [ForeignKey("PizzaId")] + public virtual List Components { get; set; } = new(); + [ForeignKey("PizzaId")] + + public virtual List Orders { get; set; } = new(); + public static Pizza Create(PizzeriaDatabase context, PizzaBindingModel model) + { + return new Pizza() + { + Id = model.Id, + PizzaName = model.PizzaName, + Price = model.Price, + Components = model.PizzaComponents.Select(x => new PizzaComponent + { + Component = context.Components.First(y => y.Id == x.Key), + Count = x.Value.Item2 + }).ToList() + }; + } + public void Update(PizzaBindingModel model) + { + PizzaName = model.PizzaName; + Price = model.Price; + } + public PizzaViewModel GetViewModel => new() + { + Id = Id, + PizzaName = PizzaName, + Price = Price, + PizzaComponents = PizzaComponents + }; + public void UpdateComponents(PizzeriaDatabase context, PizzaBindingModel model) + { + var PizzaComponents = context.PizzaComponents.Where(rec => rec.PizzaId == model.Id).ToList(); + if (PizzaComponents != null && PizzaComponents.Count > 0) + { // удалили те, которых нет в модели + context.PizzaComponents.RemoveRange(PizzaComponents.Where(rec => !model.PizzaComponents.ContainsKey(rec.ComponentId))); + context.SaveChanges(); + // обновили количество у существующих записей + foreach (var updateComponent in PizzaComponents) + { + updateComponent.Count = model.PizzaComponents[updateComponent.ComponentId].Item2; + model.PizzaComponents.Remove(updateComponent.ComponentId); + } + context.SaveChanges(); + } + var Pizza = context.Pizzas.First(x => x.Id == Id); + foreach (var pc in model.PizzaComponents) + { + context.PizzaComponents.Add(new PizzaComponent + { + Pizza = Pizza, + Component = context.Components.First(x => x.Id == pc.Key), + Count = pc.Value.Item2 + }); + context.SaveChanges(); + } + _PizzaComponents = null; + } + } +} + diff --git a/Pizzeria/PizzeriaDatabaseImplement/Models/PizzaComponent.cs b/Pizzeria/PizzeriaDatabaseImplement/Models/PizzaComponent.cs new file mode 100644 index 0000000..5843173 --- /dev/null +++ b/Pizzeria/PizzeriaDatabaseImplement/Models/PizzaComponent.cs @@ -0,0 +1,18 @@ +using System.ComponentModel.DataAnnotations; + +namespace PizzeriaDatabaseImplement.Models +{ + public class PizzaComponent + { + public int Id { get; set; } + [Required] + public int PizzaId { get; set; } + [Required] + public int ComponentId { get; set; } + [Required] + public int Count { get; set; } + public virtual Component Component { get; set; } = new(); + public virtual Pizza Pizza { get; set; } = new(); + } + +} diff --git a/Pizzeria/PizzeriaDatabaseImplement/PizzeriaDatabase.cs b/Pizzeria/PizzeriaDatabaseImplement/PizzeriaDatabase.cs new file mode 100644 index 0000000..0a7cefe --- /dev/null +++ b/Pizzeria/PizzeriaDatabaseImplement/PizzeriaDatabase.cs @@ -0,0 +1,22 @@ +using Microsoft.EntityFrameworkCore; +using PizzeriaDatabaseImplement.Models; + +namespace PizzeriaDatabaseImplement +{ + public class PizzeriaDatabase : DbContext + { + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + if (optionsBuilder.IsConfigured == false) + { + optionsBuilder.UseSqlServer(@"Data Source=KOMPUTER\SQLEXPRESS;Initial Catalog=PizzeriaDatabaseFull;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); + } + base.OnConfiguring(optionsBuilder); + } + public virtual DbSet Components { set; get; } + public virtual DbSet Pizzas { set; get; } + public virtual DbSet PizzaComponents { set; get; } + public virtual DbSet Orders { set; get; } + + } +} diff --git a/Pizzeria/PizzeriaDatabaseImplement/PizzeriaDatabaseImplement.csproj b/Pizzeria/PizzeriaDatabaseImplement/PizzeriaDatabaseImplement.csproj new file mode 100644 index 0000000..97d5f76 --- /dev/null +++ b/Pizzeria/PizzeriaDatabaseImplement/PizzeriaDatabaseImplement.csproj @@ -0,0 +1,23 @@ + + + + net6.0 + enable + enable + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + diff --git a/Pizzeria/PizzeriaFileImplement/PizzeriaFileImplement.csproj b/Pizzeria/PizzeriaFileImplement/PizzeriaFileImplement.csproj index 8b7a50e..975c892 100644 --- a/Pizzeria/PizzeriaFileImplement/PizzeriaFileImplement.csproj +++ b/Pizzeria/PizzeriaFileImplement/PizzeriaFileImplement.csproj @@ -6,6 +6,13 @@ enable + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + diff --git a/Pizzeria/PizzeriaListImplement/PizzeriaListImplement.csproj b/Pizzeria/PizzeriaListImplement/PizzeriaListImplement.csproj index b612a23..02ecfad 100644 --- a/Pizzeria/PizzeriaListImplement/PizzeriaListImplement.csproj +++ b/Pizzeria/PizzeriaListImplement/PizzeriaListImplement.csproj @@ -6,6 +6,13 @@ enable + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + +