diff --git a/SewingDresses/SewingDresses.sln b/SewingDresses/SewingDresses.sln index 9fc1da9..98b6471 100644 --- a/SewingDresses/SewingDresses.sln +++ b/SewingDresses/SewingDresses.sln @@ -13,7 +13,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SewingDressesBusinessLogic" EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SewingDressesListImplement", "SewingDressesListImplement\SewingDressesListImplement.csproj", "{0F2DC551-6221-4471-BBAA-C5C65E45E66C}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SewingDressesFileImplement", "SewingDressesFileImplement\SewingDressesFileImplement.csproj", "{340D02B6-5B03-47A2-8498-BB7DBBCB7F5B}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SewingDressesFileImplement", "SewingDressesFileImplement\SewingDressesFileImplement.csproj", "{340D02B6-5B03-47A2-8498-BB7DBBCB7F5B}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SewingDressesDatabaseImplement", "SewingDressesDatabaseImplement\SewingDressesDatabaseImplement.csproj", "{A3420FD5-1798-447D-957E-6455474228CC}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -45,6 +47,10 @@ Global {340D02B6-5B03-47A2-8498-BB7DBBCB7F5B}.Debug|Any CPU.Build.0 = Debug|Any CPU {340D02B6-5B03-47A2-8498-BB7DBBCB7F5B}.Release|Any CPU.ActiveCfg = Release|Any CPU {340D02B6-5B03-47A2-8498-BB7DBBCB7F5B}.Release|Any CPU.Build.0 = Release|Any CPU + {A3420FD5-1798-447D-957E-6455474228CC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {A3420FD5-1798-447D-957E-6455474228CC}.Debug|Any CPU.Build.0 = Debug|Any CPU + {A3420FD5-1798-447D-957E-6455474228CC}.Release|Any CPU.ActiveCfg = Release|Any CPU + {A3420FD5-1798-447D-957E-6455474228CC}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/SewingDresses/SewingDressesDatabaseImplement/Implements/ComponentStorage.cs b/SewingDresses/SewingDressesDatabaseImplement/Implements/ComponentStorage.cs new file mode 100644 index 0000000..5867658 --- /dev/null +++ b/SewingDresses/SewingDressesDatabaseImplement/Implements/ComponentStorage.cs @@ -0,0 +1,70 @@ +using SewingDressesContracts.BindingModels; +using SewingDressesContracts.SearchModels; +using SewingDressesContracts.StoragesContracts; +using SewingDressesContracts.ViewModels; +using SewingDressesDatabaseImplement.Models; +namespace SewingDressesDatabaseImplement.Implements +{ + public class ComponentStorage : IComponentStorage + { + public List GetFullList() + { + using var context = new SewingDressesDatabase(); + return context.Components.Select(x => x.GetViewModel).ToList(); + } + public List GetFilteredList(ComponentSearchModel model) + { + if (string.IsNullOrEmpty(model.ComponentName)) + { + return new(); + } + using var context = new SewingDressesDatabase(); + 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 SewingDressesDatabase(); + 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 SewingDressesDatabase(); + context.Components.Add(newComponent); + context.SaveChanges(); + return newComponent.GetViewModel; + } + public ComponentViewModel? Update(ComponentBindingModel model) + { + using var context = new SewingDressesDatabase(); + 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 SewingDressesDatabase(); + var element = context.Components.FirstOrDefault(rec => rec.Id == model.Id); + if (element == null ) + { + return null; + } + context.Components.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + } +} diff --git a/SewingDresses/SewingDressesDatabaseImplement/Implements/DressStorage.cs b/SewingDresses/SewingDressesDatabaseImplement/Implements/DressStorage.cs new file mode 100644 index 0000000..2d39bd6 --- /dev/null +++ b/SewingDresses/SewingDressesDatabaseImplement/Implements/DressStorage.cs @@ -0,0 +1,90 @@ +using Microsoft.EntityFrameworkCore; +using SewingDressesContracts.BindingModels; +using SewingDressesContracts.SearchModels; +using SewingDressesContracts.StoragesContracts; +using SewingDressesContracts.ViewModels; +using SewingDressesDatabaseImplement.Models; + +namespace SewingDressesDatabaseImplement.Implements +{ + public class DressStorage : IDressStorage + { + public List GetFullList() + { + using var context = new SewingDressesDatabase(); + return context.Dresses.Include(x => x.Components).ThenInclude(x => x.Component).ToList().Select(x => x.GetViewModel).ToList(); + } + public List GetFilteredList(DressSearchModel model) + { + if (string.IsNullOrEmpty(model.DressName)) + { + return new(); + } + using var context = new SewingDressesDatabase(); + return context.Dresses.Include(x => x.Components).ThenInclude(x => x.Component).Where(x => x.DressName.Contains(model.DressName)).ToList().Select(x => x.GetViewModel).ToList(); + + } + public DressViewModel? GetElement(DressSearchModel model) + { + if (string.IsNullOrEmpty(model.DressName) && + !model.Id.HasValue) + { + return null; + } + using var context = new SewingDressesDatabase(); + return context.Dresses.Include(x => x.Components).ThenInclude(x => x.Component) + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.DressName) && x.DressName == model.DressName) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + public DressViewModel? Insert(DressBindingModel model) + { + using var context = new SewingDressesDatabase(); + var newDress = Dress.Create(context, model); + if (newDress == null) + { + return null; + } + context.Dresses.Add(newDress); + context.SaveChanges(); + return newDress.GetViewModel; + } + public DressViewModel? Update(DressBindingModel model) + { + using var context = new SewingDressesDatabase(); + using var transaction = context.Database.BeginTransaction(); + try + { + var dress = context.Dresses.FirstOrDefault(rec => + rec.Id == model.Id); + if (dress == null) + { + return null; + } + dress.Update(model); + context.SaveChanges(); + dress.UpdateComponents(context, model); + transaction.Commit(); + return dress.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + public DressViewModel? Delete(DressBindingModel model) + { + using var context = new SewingDressesDatabase(); + var element = context.Dresses + .Include(x => x.Components) + .FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Dresses.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + + } +} diff --git a/SewingDresses/SewingDressesDatabaseImplement/Implements/OrderStorage.cs b/SewingDresses/SewingDressesDatabaseImplement/Implements/OrderStorage.cs new file mode 100644 index 0000000..b482b0e --- /dev/null +++ b/SewingDresses/SewingDressesDatabaseImplement/Implements/OrderStorage.cs @@ -0,0 +1,89 @@ +using SewingDressesContracts.BindingModels; +using SewingDressesContracts.SearchModels; +using SewingDressesContracts.StoragesContracts; +using SewingDressesContracts.ViewModels; +using SewingDressesDatabaseImplement.Models; + +namespace SewingDressesDatabaseImplement.Implements +{ + public class OrderStorage : IOrderStorage + { + public List GetFullList() + { + using var context = new SewingDressesDatabase(); + return context.Orders.Select(x => AcessDressesStorage(x.GetViewModel, context)).ToList(); + } + public List GetFilteredList(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return new(); + } + using var context = new SewingDressesDatabase(); + return context.Orders.Where(x => x.Id == model.Id).Select(x => AcessDressesStorage(x.GetViewModel, context)).ToList(); + } + public OrderViewModel? GetElement(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + using var context = new SewingDressesDatabase(); + return AcessDressesStorage(context.Orders.FirstOrDefault(x => x.Id == model.Id)?.GetViewModel, context); + } + public OrderViewModel? Insert(OrderBindingModel model) + { + var order = Order.Create(model); + if (order == null) + { + return null; + } + using var context = new SewingDressesDatabase(); + context.Orders.Add(order); + context.SaveChanges(); + return AcessDressesStorage(order.GetViewModel, context); + + } + public OrderViewModel? Update(OrderBindingModel model) + { + + using var context = new SewingDressesDatabase(); + var order = context.Orders.FirstOrDefault(x => x.Id == model.Id); + if (order == null) + { + return null; + } + order.Update(model); + context.SaveChanges(); + return AcessDressesStorage(order.GetViewModel, context); + + } + public OrderViewModel? Delete(OrderBindingModel model) + { + using var context = new SewingDressesDatabase(); + var element = context.Orders.FirstOrDefault(x => x.Id == model.Id); + if (element == null) + { + return null; + } + context.Orders.Remove(element); + context.SaveChanges(); + return AcessDressesStorage(element.GetViewModel, context); + } + public static OrderViewModel? AcessDressesStorage(OrderViewModel? model, SewingDressesDatabase context) + { + if (model == null) + { + return null; + } + var dress = context.Dresses.FirstOrDefault(x => x.Id == model.DressId); + if (dress == null) + { + return null; + } + model.DressName = dress.DressName; + return model; + } + + } +} diff --git a/SewingDresses/SewingDressesDatabaseImplement/Migrations/20240411082028_tb.Designer.cs b/SewingDresses/SewingDressesDatabaseImplement/Migrations/20240411082028_tb.Designer.cs new file mode 100644 index 0000000..666ca01 --- /dev/null +++ b/SewingDresses/SewingDressesDatabaseImplement/Migrations/20240411082028_tb.Designer.cs @@ -0,0 +1,169 @@ +// +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using SewingDressesDatabaseImplement; + +#nullable disable + +namespace SewingDressesDatabaseImplement.Migrations +{ + [DbContext(typeof(SewingDressesDatabase))] + [Migration("20240411082028_tb")] + partial class tb + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.16") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("SewingDressesDatabaseImplement.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("SewingDressesDatabaseImplement.Models.Dress", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("DressName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Price") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Dresses"); + }); + + modelBuilder.Entity("SewingDressesDatabaseImplement.Models.DressComponent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComponentId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("DressId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ComponentId"); + + b.HasIndex("DressId"); + + b.ToTable("DressComponents"); + }); + + modelBuilder.Entity("SewingDressesDatabaseImplement.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("DressId") + .HasColumnType("int"); + + b.Property("Status") + .HasColumnType("int"); + + b.Property("Sum") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.HasIndex("DressId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("SewingDressesDatabaseImplement.Models.DressComponent", b => + { + b.HasOne("SewingDressesDatabaseImplement.Models.Component", "Component") + .WithMany("DressComponents") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("SewingDressesDatabaseImplement.Models.Dress", "Dress") + .WithMany("Components") + .HasForeignKey("DressId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Component"); + + b.Navigation("Dress"); + }); + + modelBuilder.Entity("SewingDressesDatabaseImplement.Models.Order", b => + { + b.HasOne("SewingDressesDatabaseImplement.Models.Dress", null) + .WithMany("Orders") + .HasForeignKey("DressId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("SewingDressesDatabaseImplement.Models.Component", b => + { + b.Navigation("DressComponents"); + }); + + modelBuilder.Entity("SewingDressesDatabaseImplement.Models.Dress", b => + { + b.Navigation("Components"); + + b.Navigation("Orders"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/SewingDresses/SewingDressesDatabaseImplement/Migrations/20240411082028_tb.cs b/SewingDresses/SewingDressesDatabaseImplement/Migrations/20240411082028_tb.cs new file mode 100644 index 0000000..b3adff1 --- /dev/null +++ b/SewingDresses/SewingDressesDatabaseImplement/Migrations/20240411082028_tb.cs @@ -0,0 +1,125 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace SewingDressesDatabaseImplement.Migrations +{ + /// + public partial class tb : 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: "Dresses", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + DressName = table.Column(type: "nvarchar(max)", nullable: false), + Price = table.Column(type: "float", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Dresses", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "DressComponents", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + DressId = 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_DressComponents", x => x.Id); + table.ForeignKey( + name: "FK_DressComponents_Components_ComponentId", + column: x => x.ComponentId, + principalTable: "Components", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_DressComponents_Dresses_DressId", + column: x => x.DressId, + principalTable: "Dresses", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "Orders", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + DressId = 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_Dresses_DressId", + column: x => x.DressId, + principalTable: "Dresses", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateIndex( + name: "IX_DressComponents_ComponentId", + table: "DressComponents", + column: "ComponentId"); + + migrationBuilder.CreateIndex( + name: "IX_DressComponents_DressId", + table: "DressComponents", + column: "DressId"); + + migrationBuilder.CreateIndex( + name: "IX_Orders_DressId", + table: "Orders", + column: "DressId"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "DressComponents"); + + migrationBuilder.DropTable( + name: "Orders"); + + migrationBuilder.DropTable( + name: "Components"); + + migrationBuilder.DropTable( + name: "Dresses"); + } + } +} diff --git a/SewingDresses/SewingDressesDatabaseImplement/Migrations/SewingDressesDatabaseModelSnapshot.cs b/SewingDresses/SewingDressesDatabaseImplement/Migrations/SewingDressesDatabaseModelSnapshot.cs new file mode 100644 index 0000000..42078c2 --- /dev/null +++ b/SewingDresses/SewingDressesDatabaseImplement/Migrations/SewingDressesDatabaseModelSnapshot.cs @@ -0,0 +1,166 @@ +// +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using SewingDressesDatabaseImplement; + +#nullable disable + +namespace SewingDressesDatabaseImplement.Migrations +{ + [DbContext(typeof(SewingDressesDatabase))] + partial class SewingDressesDatabaseModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.16") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("SewingDressesDatabaseImplement.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("SewingDressesDatabaseImplement.Models.Dress", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("DressName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Price") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Dresses"); + }); + + modelBuilder.Entity("SewingDressesDatabaseImplement.Models.DressComponent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComponentId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("DressId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ComponentId"); + + b.HasIndex("DressId"); + + b.ToTable("DressComponents"); + }); + + modelBuilder.Entity("SewingDressesDatabaseImplement.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("DressId") + .HasColumnType("int"); + + b.Property("Status") + .HasColumnType("int"); + + b.Property("Sum") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.HasIndex("DressId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("SewingDressesDatabaseImplement.Models.DressComponent", b => + { + b.HasOne("SewingDressesDatabaseImplement.Models.Component", "Component") + .WithMany("DressComponents") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("SewingDressesDatabaseImplement.Models.Dress", "Dress") + .WithMany("Components") + .HasForeignKey("DressId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Component"); + + b.Navigation("Dress"); + }); + + modelBuilder.Entity("SewingDressesDatabaseImplement.Models.Order", b => + { + b.HasOne("SewingDressesDatabaseImplement.Models.Dress", null) + .WithMany("Orders") + .HasForeignKey("DressId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("SewingDressesDatabaseImplement.Models.Component", b => + { + b.Navigation("DressComponents"); + }); + + modelBuilder.Entity("SewingDressesDatabaseImplement.Models.Dress", b => + { + b.Navigation("Components"); + + b.Navigation("Orders"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/SewingDresses/SewingDressesDatabaseImplement/Models/Component.cs b/SewingDresses/SewingDressesDatabaseImplement/Models/Component.cs new file mode 100644 index 0000000..8c51f4b --- /dev/null +++ b/SewingDresses/SewingDressesDatabaseImplement/Models/Component.cs @@ -0,0 +1,57 @@ +using SewingDressesContracts.BindingModels; +using SewingDressesContracts.ViewModels; +using SewingDressesDataModels.Models; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; + +namespace SewingDressesDatabaseImplement.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 DressComponents { 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/SewingDresses/SewingDressesDatabaseImplement/Models/Dress.cs b/SewingDresses/SewingDressesDatabaseImplement/Models/Dress.cs new file mode 100644 index 0000000..82dbf42 --- /dev/null +++ b/SewingDresses/SewingDressesDatabaseImplement/Models/Dress.cs @@ -0,0 +1,89 @@ +using SewingDressesContracts.BindingModels; +using SewingDressesContracts.ViewModels; +using SewingDressesDataModels.Models; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; + + +namespace SewingDressesDatabaseImplement.Models +{ + public class Dress : IDressModel + { + public int Id { get; set; } + [Required] + public string DressName { get; set; } = string.Empty; + [Required] + public double Price { get; set; } + private Dictionary? _dressComponents = null; + [NotMapped] + public Dictionary DressComponents + { + get + { + if (_dressComponents == null) + { + _dressComponents = Components.ToDictionary(recDC => recDC.ComponentId, recDC => (recDC.Component as IComponentModel, recDC.Count)); + } + return _dressComponents; + } + } + [ForeignKey("DressId")] + public virtual List Components { get; set; } = new(); + [ForeignKey("DressId")] + public virtual List Orders { get; set; } = new(); + public static Dress Create(SewingDressesDatabase context, DressBindingModel model) + { + return new Dress() + { + Id = model.Id, + DressName = model.DressName, + Price = model.Price, + Components = model.DressComponents.Select(x => new DressComponent + { + Component = context.Components.First(y => y.Id == x.Key), + Count = x.Value.Item2 + }).ToList() + }; + } + public void Update(DressBindingModel model) + { + DressName = model.DressName; + Price = model.Price; + } + public DressViewModel GetViewModel => new() + { + Id = Id, + DressName = DressName, + Price = Price, + DressComponents = DressComponents + }; + public void UpdateComponents(SewingDressesDatabase context, DressBindingModel model) + { + var dressComponents = context.DressComponents.Where(rec => rec.DressId == model.Id).ToList(); + if (dressComponents != null && dressComponents.Count > 0) + { + context.DressComponents.RemoveRange(dressComponents.Where(rec => !model.DressComponents.ContainsKey(rec.ComponentId))); + context.SaveChanges(); + foreach (var updateComponent in dressComponents) + { + updateComponent.Count = model.DressComponents[updateComponent.ComponentId].Item2; + model.DressComponents.Remove(updateComponent.ComponentId); + } + context.SaveChanges(); + } + var dress = context.Dresses.First(x => x.Id == Id); + foreach (var pc in model.DressComponents) + { + context.DressComponents.Add(new DressComponent + { + Dress = dress, + Component = context.Components.First(x => x.Id == pc.Key), + Count = pc.Value.Item2 + }); + context.SaveChanges(); + } + _dressComponents = null; + } + + } +} diff --git a/SewingDresses/SewingDressesDatabaseImplement/Models/DressComponent.cs b/SewingDresses/SewingDressesDatabaseImplement/Models/DressComponent.cs new file mode 100644 index 0000000..5a35a8e --- /dev/null +++ b/SewingDresses/SewingDressesDatabaseImplement/Models/DressComponent.cs @@ -0,0 +1,17 @@ +using System.ComponentModel.DataAnnotations; + +namespace SewingDressesDatabaseImplement.Models +{ + public class DressComponent + { + public int Id { get; set; } + [Required] + public int DressId { get; set; } + [Required] + public int ComponentId { get; set; } + [Required] + public int Count { get; set; } + public virtual Component Component { get; set; } = new(); + public virtual Dress Dress { get; set; } = new(); + } +} diff --git a/SewingDresses/SewingDressesDatabaseImplement/Models/Order.cs b/SewingDresses/SewingDressesDatabaseImplement/Models/Order.cs new file mode 100644 index 0000000..c840c87 --- /dev/null +++ b/SewingDresses/SewingDressesDatabaseImplement/Models/Order.cs @@ -0,0 +1,61 @@ +using SewingDressesContracts.BindingModels; +using SewingDressesContracts.ViewModels; +using SewingDressesDataModels.Enums; +using SewingDressesDataModels.Models; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; + +namespace SewingDressesDatabaseImplement.Models +{ + public class Order : IOrderModel + { + public int Id { get; private set; } + [Required] + public int DressId { 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; } = DateTime.Now; + public DateTime? DateImplement { get; private set; } + + public static Order? Create(OrderBindingModel model) + { + if (model == null) + { + return null; + } + return new Order() + { + Id = model.Id, + DressId = model.DressId, + Count = model.Count, + Sum = model.Sum, + Status = model.Status, + DateCreate = model.DateCreate + }; + } + public void Update(OrderBindingModel model) + { + if (model == null) + { + return; + } + Status = model.Status; + DateImplement = model.DateImplement; + } + public OrderViewModel GetViewModel => new() + { + Id = Id, + DressId = DressId, + Count = Count, + Sum = Sum, + Status = Status, + DateCreate = DateCreate, + DateImplement = DateImplement + }; + } +} diff --git a/SewingDresses/SewingDressesDatabaseImplement/SewingDressesDatabase.cs b/SewingDresses/SewingDressesDatabaseImplement/SewingDressesDatabase.cs new file mode 100644 index 0000000..d323297 --- /dev/null +++ b/SewingDresses/SewingDressesDatabaseImplement/SewingDressesDatabase.cs @@ -0,0 +1,20 @@ +using SewingDressesDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; +namespace SewingDressesDatabaseImplement +{ + public class SewingDressesDatabase : DbContext + { + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + if (optionsBuilder.IsConfigured == false) + { + optionsBuilder.UseSqlServer(@"Data Source=LAPTOP-J8AJ26MV\SQLEXPRESS;Initial Catalog=DressAtelierDatabaseFull01;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); + } + base.OnConfiguring(optionsBuilder); + } + public virtual DbSet Components { get; set; } + public virtual DbSet Dresses { get; set; } + public virtual DbSet DressComponents { get; set; } + public virtual DbSet Orders { get; set; } + } +} diff --git a/SewingDresses/SewingDressesDatabaseImplement/SewingDressesDatabaseImplement.csproj b/SewingDresses/SewingDressesDatabaseImplement/SewingDressesDatabaseImplement.csproj new file mode 100644 index 0000000..2574adc --- /dev/null +++ b/SewingDresses/SewingDressesDatabaseImplement/SewingDressesDatabaseImplement.csproj @@ -0,0 +1,23 @@ + + + + net6.0 + enable + enable + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + diff --git a/SewingDresses/SewingDressesView/Program.cs b/SewingDresses/SewingDressesView/Program.cs index c8ebbb3..d4223c2 100644 --- a/SewingDresses/SewingDressesView/Program.cs +++ b/SewingDresses/SewingDressesView/Program.cs @@ -1,7 +1,7 @@ using SewingDressesBusinessLogic.BusinessLogic; using SewingDressesContracts.BusinessLogicsContracts; using SewingDressesContracts.StoragesContracts; -using SewingDressesFileImplement.Implements; +using SewingDressesDatabaseImplement.Implements; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using NLog.Extensions.Logging; diff --git a/SewingDresses/SewingDressesView/SewingDressesView.csproj b/SewingDresses/SewingDressesView/SewingDressesView.csproj index 48c6708..6433c9e 100644 --- a/SewingDresses/SewingDressesView/SewingDressesView.csproj +++ b/SewingDresses/SewingDressesView/SewingDressesView.csproj @@ -8,9 +8,17 @@ enable + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + +