diff --git a/Secure/SecureShop.sln b/Secure/SecureShop.sln index de77bbc..4fb98ff 100644 --- a/Secure/SecureShop.sln +++ b/Secure/SecureShop.sln @@ -15,6 +15,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SecureShopListImplement", " EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SecureShopFileImplement", "SecureShopFileImplement\SecureShopFileImplement.csproj", "{3E416EAB-041A-46EE-9017-2C3D7127D802}" EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SecureShopDatabaseImplement", "SecureShopDatabaseImplement\SecureShopDatabaseImplement.csproj", "{E6A53ECF-5E75-4124-B2A3-5BAECD2F2609}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -45,6 +47,10 @@ Global {3E416EAB-041A-46EE-9017-2C3D7127D802}.Debug|Any CPU.Build.0 = Debug|Any CPU {3E416EAB-041A-46EE-9017-2C3D7127D802}.Release|Any CPU.ActiveCfg = Release|Any CPU {3E416EAB-041A-46EE-9017-2C3D7127D802}.Release|Any CPU.Build.0 = Release|Any CPU + {E6A53ECF-5E75-4124-B2A3-5BAECD2F2609}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {E6A53ECF-5E75-4124-B2A3-5BAECD2F2609}.Debug|Any CPU.Build.0 = Debug|Any CPU + {E6A53ECF-5E75-4124-B2A3-5BAECD2F2609}.Release|Any CPU.ActiveCfg = Release|Any CPU + {E6A53ECF-5E75-4124-B2A3-5BAECD2F2609}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/Secure/SecureShop/Program.cs b/Secure/SecureShop/Program.cs index bee32ee..f420b4f 100644 --- a/Secure/SecureShop/Program.cs +++ b/Secure/SecureShop/Program.cs @@ -1,7 +1,7 @@ using SecureShopBusinessLogic.BusinessLogics; using SecureShopContracts.BusinessLogicsContracts; using SecureShopContracts.StoragesContracts; -using SecureShopFileImplement.Implements; +using SecureShopDataBaseImplement.Implements; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using NLog.Extensions.Logging; diff --git a/Secure/SecureShop/SecureShopView.csproj b/Secure/SecureShop/SecureShopView.csproj index 3a4dc50..ae82c11 100644 --- a/Secure/SecureShop/SecureShopView.csproj +++ b/Secure/SecureShop/SecureShopView.csproj @@ -9,6 +9,10 @@ + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + @@ -17,6 +21,7 @@ + diff --git a/Secure/SecureShopDatabaseImplement/Implements/FacilitiesStorage.cs b/Secure/SecureShopDatabaseImplement/Implements/FacilitiesStorage.cs new file mode 100644 index 0000000..c6335fe --- /dev/null +++ b/Secure/SecureShopDatabaseImplement/Implements/FacilitiesStorage.cs @@ -0,0 +1,84 @@ +using SecureShopContracts.BindingModels; +using SecureShopContracts.SearchModels; +using SecureShopContracts.StoragesContracts; +using SecureShopContracts.ViewModels; +using SecureShopDatabaseImplement.Models; + +namespace SecureShopDatabaseImplement.Implements +{ + public class FacilitiesStorage : IFacilitiesStorage + { + public List GetFullList() + { + using var context = new SecureShopDatabase(); + return context.Facilitiess + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFilteredList(FacilitiesSearchModel model) + { + if (string.IsNullOrEmpty(model.FacilitiesName)) + { + return new(); + } + using var context = new SecureShopDatabase(); + return context.Facilitiess + .Where(x => x.FacilitiesName.Contains(model.FacilitiesName)) + .Select(x => x.GetViewModel) + .ToList(); + } + + public FacilitiesViewModel? GetElement(FacilitiesSearchModel model) + { + if (string.IsNullOrEmpty(model.FacilitiesName) && !model.Id.HasValue) + { + return null; + } + using var context = new SecureShopDatabase(); + return context.Facilitiess + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.FacilitiesName) && x.FacilitiesName == model.FacilitiesName) || + (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + } + + public FacilitiesViewModel? Insert(FacilitiesBindingModel model) + { + var newFacilities = Facilities.Create(model); + if (newFacilities == null) + { + return null; + } + using var context = new SecureShopDatabase(); + context.Facilitiess.Add(newFacilities); + context.SaveChanges(); + return newFacilities.GetViewModel; + } + + public FacilitiesViewModel? Update(FacilitiesBindingModel model) + { + using var context = new SecureShopDatabase(); + var Facilities = context.Facilitiess.FirstOrDefault(x => x.Id == model.Id); + if (Facilities == null) + { + return null; + } + Facilities.Update(model); + context.SaveChanges(); + return Facilities.GetViewModel; + } + + public FacilitiesViewModel? Delete(FacilitiesBindingModel model) + { + using var context = new SecureShopDatabase(); + var element = context.Facilitiess.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Facilitiess.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/Secure/SecureShopDatabaseImplement/Implements/OrderStorage.cs b/Secure/SecureShopDatabaseImplement/Implements/OrderStorage.cs new file mode 100644 index 0000000..1246618 --- /dev/null +++ b/Secure/SecureShopDatabaseImplement/Implements/OrderStorage.cs @@ -0,0 +1,99 @@ +using SecureShopContracts.BindingModels; +using SecureShopContracts.SearchModels; +using SecureShopContracts.StoragesContracts; +using SecureShopContracts.ViewModels; +using SecureShopDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; + +namespace SecureShopDatabaseImplement.Implements +{ + public class OrderStorage : IOrderStorage + { + public OrderViewModel? Delete(OrderBindingModel model) + { + using var context = new SecureShopDatabase(); + var element = context.Orders + .FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + // для отображения КОРРЕКТНОЙ viewmodelи + var deletedElement = context.Orders + .Include(x => x.Secure) + .FirstOrDefault(x => x.Id == model.Id) + ?.GetViewModel; + context.Orders.Remove(element); + context.SaveChanges(); + return deletedElement; + } + return null; + } + + public OrderViewModel? GetElement(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + using var context = new SecureShopDatabase(); + return context.Orders + .Include(x => x.Secure) + .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 SecureShopDatabase(); + return context.Orders + .Include(x => x.Secure) + .Where(x => x.Id == model.Id) + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFullList() + { + using var context = new SecureShopDatabase(); + return context.Orders + .Include(x => x.Secure) + .Select(x => x.GetViewModel) + .ToList(); + } + + public OrderViewModel? Insert(OrderBindingModel model) + { + var newOrder = Order.Create(model); + if (newOrder == null) + { + return null; + } + using var context = new SecureShopDatabase(); + context.Orders.Add(newOrder); + context.SaveChanges(); + return context.Orders + .Include(x => x.Secure) + .FirstOrDefault(x => x.Id == newOrder.Id) + ?.GetViewModel; + } + + public OrderViewModel? Update(OrderBindingModel model) + { + using var context = new SecureShopDatabase(); + 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.Secure) + .FirstOrDefault(x => x.Id == model.Id) + ?.GetViewModel; + } + } +} diff --git a/Secure/SecureShopDatabaseImplement/Implements/SecureStorage.cs b/Secure/SecureShopDatabaseImplement/Implements/SecureStorage.cs new file mode 100644 index 0000000..c516060 --- /dev/null +++ b/Secure/SecureShopDatabaseImplement/Implements/SecureStorage.cs @@ -0,0 +1,107 @@ +using SecureShopContracts.BindingModels; +using SecureShopContracts.SearchModels; +using SecureShopContracts.StoragesContracts; +using SecureShopContracts.ViewModels; +using SecureShopDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; + +namespace SecureShopDatabaseImplement.Implements +{ + public class SecureStorage : ISecureStorage + { + public List GetFullList() + { + using var context = new SecureShopDatabase(); + return context.Secures + .Include(x => x.Facilitiess) + .ThenInclude(x => x.Facilities) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFilteredList(SecureSearchModel model) + { + if (string.IsNullOrEmpty(model.SecureName)) + { + return new(); + } + using var context = new SecureShopDatabase(); + return context.Secures + .Include(x => x.Facilitiess) + .ThenInclude(x => x.Facilities) + .Where(x => x.SecureName.Contains(model.SecureName)) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + + public SecureViewModel? GetElement(SecureSearchModel model) + { + if (string.IsNullOrEmpty(model.SecureName) && !model.Id.HasValue) + { + return null; + } + using var context = new SecureShopDatabase(); + return context.Secures + .Include(x => x.Facilitiess) + .ThenInclude(x => x.Facilities) + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.SecureName) && x.SecureName == model.SecureName) || + (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + } + + public SecureViewModel? Insert(SecureBindingModel model) + { + using var context = new SecureShopDatabase(); + var newSecure = Secure.Create(context, model); + if (newSecure == null) + { + return null; + } + context.Secures.Add(newSecure); + context.SaveChanges(); + return newSecure.GetViewModel; + } + + public SecureViewModel? Update(SecureBindingModel model) + { + using var context = new SecureShopDatabase(); + using var transaction = context.Database.BeginTransaction(); + try + { + var Secure = context.Secures.FirstOrDefault(rec => rec.Id == model.Id); + if (Secure == null) + { + return null; + } + Secure.Update(model); + context.SaveChanges(); + Secure.UpdateFacilitiess(context, model); + transaction.Commit(); + return Secure.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + + public SecureViewModel? Delete(SecureBindingModel model) + { + using var context = new SecureShopDatabase(); + var element = context.Secures + .Include(x => x.Facilitiess) + .Include(x => x.Orders) + .FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Secures.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/Secure/SecureShopDatabaseImplement/Migrations/20230406085714_init.Designer.cs b/Secure/SecureShopDatabaseImplement/Migrations/20230406085714_init.Designer.cs new file mode 100644 index 0000000..a4775d7 --- /dev/null +++ b/Secure/SecureShopDatabaseImplement/Migrations/20230406085714_init.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 SecureShopDatabaseImplement; + +#nullable disable + +namespace SecureShopDatabaseImplement.Migrations +{ + [DbContext(typeof(SecureShopDatabase))] + [Migration("20230406085714_init")] + partial class init + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.4") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("SecureShopDatabaseImplement.Models.Facilities", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Cost") + .HasColumnType("float"); + + b.Property("FacilitiesName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Facilitiess"); + }); + + modelBuilder.Entity("SecureShopDatabaseImplement.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("SecureId") + .HasColumnType("int"); + + b.Property("Status") + .HasColumnType("int"); + + b.Property("Sum") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.HasIndex("SecureId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("SecureShopDatabaseImplement.Models.Secure", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Price") + .HasColumnType("float"); + + b.Property("SecureName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Secures"); + }); + + modelBuilder.Entity("SecureShopDatabaseImplement.Models.SecureFacilities", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("FacilitiesId") + .HasColumnType("int"); + + b.Property("SecureId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("FacilitiesId"); + + b.HasIndex("SecureId"); + + b.ToTable("SecureFacilitiess"); + }); + + modelBuilder.Entity("SecureShopDatabaseImplement.Models.Order", b => + { + b.HasOne("SecureShopDatabaseImplement.Models.Secure", "Secure") + .WithMany("Orders") + .HasForeignKey("SecureId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Secure"); + }); + + modelBuilder.Entity("SecureShopDatabaseImplement.Models.SecureFacilities", b => + { + b.HasOne("SecureShopDatabaseImplement.Models.Facilities", "Facilities") + .WithMany("SecureFacilitiess") + .HasForeignKey("FacilitiesId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("SecureShopDatabaseImplement.Models.Secure", "Secure") + .WithMany("Facilitiess") + .HasForeignKey("SecureId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Facilities"); + + b.Navigation("Secure"); + }); + + modelBuilder.Entity("SecureShopDatabaseImplement.Models.Facilities", b => + { + b.Navigation("SecureFacilitiess"); + }); + + modelBuilder.Entity("SecureShopDatabaseImplement.Models.Secure", b => + { + b.Navigation("Facilitiess"); + + b.Navigation("Orders"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Secure/SecureShopDatabaseImplement/Migrations/20230406085714_init.cs b/Secure/SecureShopDatabaseImplement/Migrations/20230406085714_init.cs new file mode 100644 index 0000000..da82803 --- /dev/null +++ b/Secure/SecureShopDatabaseImplement/Migrations/20230406085714_init.cs @@ -0,0 +1,125 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace SecureShopDatabaseImplement.Migrations +{ + /// + public partial class init : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Facilitiess", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + FacilitiesName = table.Column(type: "nvarchar(max)", nullable: false), + Cost = table.Column(type: "float", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Facilitiess", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Secures", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + SecureName = table.Column(type: "nvarchar(max)", nullable: false), + Price = table.Column(type: "float", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Secures", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Orders", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + SecureId = 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_Secures_SecureId", + column: x => x.SecureId, + principalTable: "Secures", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "SecureFacilitiess", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + SecureId = table.Column(type: "int", nullable: false), + FacilitiesId = table.Column(type: "int", nullable: false), + Count = table.Column(type: "int", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_SecureFacilitiess", x => x.Id); + table.ForeignKey( + name: "FK_SecureFacilitiess_Facilitiess_FacilitiesId", + column: x => x.FacilitiesId, + principalTable: "Facilitiess", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_SecureFacilitiess_Secures_SecureId", + column: x => x.SecureId, + principalTable: "Secures", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateIndex( + name: "IX_Orders_SecureId", + table: "Orders", + column: "SecureId"); + + migrationBuilder.CreateIndex( + name: "IX_SecureFacilitiess_FacilitiesId", + table: "SecureFacilitiess", + column: "FacilitiesId"); + + migrationBuilder.CreateIndex( + name: "IX_SecureFacilitiess_SecureId", + table: "SecureFacilitiess", + column: "SecureId"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "Orders"); + + migrationBuilder.DropTable( + name: "SecureFacilitiess"); + + migrationBuilder.DropTable( + name: "Facilitiess"); + + migrationBuilder.DropTable( + name: "Secures"); + } + } +} diff --git a/Secure/SecureShopDatabaseImplement/Migrations/SecureShopDatabaseModelSnapshot.cs b/Secure/SecureShopDatabaseImplement/Migrations/SecureShopDatabaseModelSnapshot.cs new file mode 100644 index 0000000..fe6c5b9 --- /dev/null +++ b/Secure/SecureShopDatabaseImplement/Migrations/SecureShopDatabaseModelSnapshot.cs @@ -0,0 +1,168 @@ +// +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using SecureShopDatabaseImplement; + +#nullable disable + +namespace SecureShopDatabaseImplement.Migrations +{ + [DbContext(typeof(SecureShopDatabase))] + partial class SecureShopDatabaseModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.4") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("SecureShopDatabaseImplement.Models.Facilities", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Cost") + .HasColumnType("float"); + + b.Property("FacilitiesName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Facilitiess"); + }); + + modelBuilder.Entity("SecureShopDatabaseImplement.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("SecureId") + .HasColumnType("int"); + + b.Property("Status") + .HasColumnType("int"); + + b.Property("Sum") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.HasIndex("SecureId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("SecureShopDatabaseImplement.Models.Secure", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Price") + .HasColumnType("float"); + + b.Property("SecureName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Secures"); + }); + + modelBuilder.Entity("SecureShopDatabaseImplement.Models.SecureFacilities", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("FacilitiesId") + .HasColumnType("int"); + + b.Property("SecureId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("FacilitiesId"); + + b.HasIndex("SecureId"); + + b.ToTable("SecureFacilitiess"); + }); + + modelBuilder.Entity("SecureShopDatabaseImplement.Models.Order", b => + { + b.HasOne("SecureShopDatabaseImplement.Models.Secure", "Secure") + .WithMany("Orders") + .HasForeignKey("SecureId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Secure"); + }); + + modelBuilder.Entity("SecureShopDatabaseImplement.Models.SecureFacilities", b => + { + b.HasOne("SecureShopDatabaseImplement.Models.Facilities", "Facilities") + .WithMany("SecureFacilitiess") + .HasForeignKey("FacilitiesId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("SecureShopDatabaseImplement.Models.Secure", "Secure") + .WithMany("Facilitiess") + .HasForeignKey("SecureId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Facilities"); + + b.Navigation("Secure"); + }); + + modelBuilder.Entity("SecureShopDatabaseImplement.Models.Facilities", b => + { + b.Navigation("SecureFacilitiess"); + }); + + modelBuilder.Entity("SecureShopDatabaseImplement.Models.Secure", b => + { + b.Navigation("Facilitiess"); + + b.Navigation("Orders"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Secure/SecureShopDatabaseImplement/Models/Facilities.cs b/Secure/SecureShopDatabaseImplement/Models/Facilities.cs new file mode 100644 index 0000000..4bf81fc --- /dev/null +++ b/Secure/SecureShopDatabaseImplement/Models/Facilities.cs @@ -0,0 +1,63 @@ +using SecureShopContracts.BindingModels; +using SecureShopContracts.ViewModels; +using SecureShopDataModels.Models; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; + +namespace SecureShopDatabaseImplement.Models +{ + public class Facilities : IFacilitiesModel + { + public int Id { get; private set; } + + [Required] + public string FacilitiesName { get; private set; } = string.Empty; + + [Required] + public double Cost { get; set; } + + [ForeignKey("FacilitiesId")] + public virtual List SecureFacilitiess { get; set; } = new(); + + public static Facilities? Create(FacilitiesBindingModel model) + { + if (model == null) + { + return null; + } + return new Facilities() + { + Id = model.Id, + FacilitiesName = model.FacilitiesName, + Cost = model.Cost + }; + } + + public static Facilities Create(FacilitiesViewModel model) + { + return new Facilities + { + Id = model.Id, + FacilitiesName = model.FacilitiesName, + Cost = model.Cost + }; + } + + public void Update(FacilitiesBindingModel model) + { + if (model == null) + { + return; + } + FacilitiesName = model.FacilitiesName; + Cost = model.Cost; + } + + public FacilitiesViewModel GetViewModel => new() + { + Id = Id, + FacilitiesName = FacilitiesName, + Cost = Cost + }; + } +} diff --git a/Secure/SecureShopDatabaseImplement/Models/Order.cs b/Secure/SecureShopDatabaseImplement/Models/Order.cs new file mode 100644 index 0000000..7fcc888 --- /dev/null +++ b/Secure/SecureShopDatabaseImplement/Models/Order.cs @@ -0,0 +1,67 @@ +using SecureShopContracts.BindingModels; +using SecureShopContracts.ViewModels; +using SecureShopDataModels.Enums; +using SecureShopDataModels.Models; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; + +namespace SecureShopDatabaseImplement.Models +{ + public class Order : IOrderModel + { + [Required] + public int SecureId { get; set; } + [Required] + public int Count { get; set; } + [Required] + public double Sum { get; set; } + [Required] + public OrderStatus Status { get; set; } + [Required] + public DateTime DateCreate { get; set; } + + public DateTime? DateImplement { get; set; } + + public int Id { get; set; } + public virtual Secure Secure { get; set; } + public static Order? Create(OrderBindingModel? model) + { + if (model == null) + { + return null; + } + return new Order() + { + Id = model.Id, + SecureId = model.SecureId, + 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, + SecureId = SecureId, + Count = Count, + Sum = Sum, + Status = Status, + DateCreate = DateCreate, + DateImplement = DateImplement, + SecureName = Secure.SecureName + }; + } +} \ No newline at end of file diff --git a/Secure/SecureShopDatabaseImplement/Models/Secure.cs b/Secure/SecureShopDatabaseImplement/Models/Secure.cs new file mode 100644 index 0000000..b96140e --- /dev/null +++ b/Secure/SecureShopDatabaseImplement/Models/Secure.cs @@ -0,0 +1,99 @@ +using SecureShopContracts.BindingModels; +using SecureShopContracts.ViewModels; +using SecureShopDataModels.Models; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; + +namespace SecureShopDatabaseImplement.Models +{ + public class Secure : ISecureModel + { + public int Id { get; set; } + + [Required] + public string SecureName { get; set; } = string.Empty; + + [Required] + public double Price { get; set; } + + private Dictionary? _SecureFacilitiess = null; + + [NotMapped] + public Dictionary SecureFacilitiess + { + get + { + if (_SecureFacilitiess == null) + { + _SecureFacilitiess = Facilitiess + .ToDictionary(recPC => recPC.FacilitiesId, recPC => (recPC.Facilities as IFacilitiesModel, recPC.Count)); + } + return _SecureFacilitiess; + } + } + + [ForeignKey("SecureId")] + public virtual List Facilitiess { get; set; } = new(); + + [ForeignKey("SecureId")] + public virtual List Orders { get; set; } = new(); + + public static Secure Create(SecureShopDatabase context, SecureBindingModel model) + { + return new Secure() + { + Id = model.Id, + SecureName = model.SecureName, + Price = model.Price, + Facilitiess = model.SecureFacilitiess.Select(x => new SecureFacilities + { + Facilities = context.Facilitiess.First(y => y.Id == x.Key), + Count = x.Value.Item2 + }).ToList() + }; + } + + public void Update(SecureBindingModel model) + { + SecureName = model.SecureName; + Price = model.Price; + } + + public SecureViewModel GetViewModel => new() + { + Id = Id, + SecureName = SecureName, + Price = Price, + SecureFacilitiess = SecureFacilitiess + }; + + public void UpdateFacilitiess(SecureShopDatabase context, SecureBindingModel model) + { + var SecureFacilitiess = context.SecureFacilitiess.Where(rec => rec.SecureId == model.Id).ToList(); + if (SecureFacilitiess != null && SecureFacilitiess.Count > 0) + { // удалили те, которых нет в модели + context.SecureFacilitiess.RemoveRange(SecureFacilitiess.Where(rec => !model.SecureFacilitiess.ContainsKey(rec.FacilitiesId))); + context.SaveChanges(); + // обновили количество у существующих записей + foreach (var updateFacilities in SecureFacilitiess) + { + updateFacilities.Count = model.SecureFacilitiess[updateFacilities.FacilitiesId].Item2; + model.SecureFacilitiess.Remove(updateFacilities.FacilitiesId); + } + context.SaveChanges(); + } + var Secure = context.Secures.First(x => x.Id == Id); + foreach (var ia in model.SecureFacilitiess) + { + context.SecureFacilitiess.Add(new SecureFacilities + { + Secure = Secure, + Facilities = context.Facilitiess.First(x => x.Id == ia.Key), + Count = ia.Value.Item2 + }); + context.SaveChanges(); + } + _SecureFacilitiess = null; + } + } +} diff --git a/Secure/SecureShopDatabaseImplement/Models/SecureFacilities.cs b/Secure/SecureShopDatabaseImplement/Models/SecureFacilities.cs new file mode 100644 index 0000000..c1a8c88 --- /dev/null +++ b/Secure/SecureShopDatabaseImplement/Models/SecureFacilities.cs @@ -0,0 +1,22 @@ +using System.ComponentModel.DataAnnotations; + +namespace SecureShopDatabaseImplement.Models +{ + public class SecureFacilities + { + public int Id { get; set; } + + [Required] + public int SecureId { get; set; } + + [Required] + public int FacilitiesId { get; set; } + + [Required] + public int Count { get; set; } + + public virtual Facilities Facilities { get; set; } = new(); + + public virtual Secure Secure { get; set; } = new(); + } +} diff --git a/Secure/SecureShopDatabaseImplement/SecureShopDatabase.cs b/Secure/SecureShopDatabaseImplement/SecureShopDatabase.cs new file mode 100644 index 0000000..e35d7b1 --- /dev/null +++ b/Secure/SecureShopDatabaseImplement/SecureShopDatabase.cs @@ -0,0 +1,25 @@ +using SecureShopDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; + +namespace SecureShopDatabaseImplement +{ + public class SecureShopDatabase : DbContext + { + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + if (optionsBuilder.IsConfigured == false) + { + optionsBuilder.UseSqlServer(@"Data Source=DESKTOP-E9D4764\SQLEXPRESS;Initial Catalog=SecureShopDatabase;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); + } + base.OnConfiguring(optionsBuilder); + } + + public virtual DbSet Facilitiess { set; get; } + + public virtual DbSet Secures { set; get; } + + public virtual DbSet SecureFacilitiess { set; get; } + + public virtual DbSet Orders { set; get; } + } +} diff --git a/Secure/SecureShopDatabaseImplement/SecureShopDatabaseImplement.csproj b/Secure/SecureShopDatabaseImplement/SecureShopDatabaseImplement.csproj new file mode 100644 index 0000000..09a39b6 --- /dev/null +++ b/Secure/SecureShopDatabaseImplement/SecureShopDatabaseImplement.csproj @@ -0,0 +1,23 @@ + + + + net6.0 + enable + enable + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + diff --git a/Secure/SecureShopFileImplement/DataFileSingleton.cs b/Secure/SecureShopFileImplement/DataFileSingleton.cs index 77ef735..ae9b467 100644 --- a/Secure/SecureShopFileImplement/DataFileSingleton.cs +++ b/Secure/SecureShopFileImplement/DataFileSingleton.cs @@ -1,7 +1,7 @@ -using SecureShopFileImplement.Models; +using SecureShopDataBaseImplement.Models; using System.Xml.Linq; -namespace SecureShopFileImplement +namespace SecureShopDataBaseImplement { public class DataFileSingleton { diff --git a/Secure/SecureShopFileImplement/Implements/FacilitiesStorage.cs b/Secure/SecureShopFileImplement/Implements/FacilitiesStorage.cs index ebd94dc..3f987ae 100644 --- a/Secure/SecureShopFileImplement/Implements/FacilitiesStorage.cs +++ b/Secure/SecureShopFileImplement/Implements/FacilitiesStorage.cs @@ -2,9 +2,9 @@ using SecureShopContracts.SearchModels; using SecureShopContracts.StoragesContracts; using SecureShopContracts.ViewModels; -using SecureShopFileImplement.Models; +using SecureShopDataBaseImplement.Models; -namespace SecureShopFileImplement.Implements +namespace SecureShopDataBaseImplement.Implements { public class FacilitiesStorage : IFacilitiesStorage { diff --git a/Secure/SecureShopFileImplement/Implements/OrderStorage.cs b/Secure/SecureShopFileImplement/Implements/OrderStorage.cs index 43d834c..ae29453 100644 --- a/Secure/SecureShopFileImplement/Implements/OrderStorage.cs +++ b/Secure/SecureShopFileImplement/Implements/OrderStorage.cs @@ -2,9 +2,9 @@ using SecureShopContracts.SearchModels; using SecureShopContracts.StoragesContracts; using SecureShopContracts.ViewModels; -using SecureShopFileImplement.Models; +using SecureShopDataBaseImplement.Models; -namespace SecureShopFileImplement.Implements +namespace SecureShopDataBaseImplement.Implements { public class OrderStorage : IOrderStorage { diff --git a/Secure/SecureShopFileImplement/Implements/SecureStorage.cs b/Secure/SecureShopFileImplement/Implements/SecureStorage.cs index 442e4e7..7f5b767 100644 --- a/Secure/SecureShopFileImplement/Implements/SecureStorage.cs +++ b/Secure/SecureShopFileImplement/Implements/SecureStorage.cs @@ -2,9 +2,9 @@ using SecureShopContracts.SearchModels; using SecureShopContracts.StoragesContracts; using SecureShopContracts.ViewModels; -using SecureShopFileImplement.Models; +using SecureShopDataBaseImplement.Models; -namespace SecureShopFileImplement.Implements +namespace SecureShopDataBaseImplement.Implements { public class SecureStorage : ISecureStorage { diff --git a/Secure/SecureShopFileImplement/Models/Facilities.cs b/Secure/SecureShopFileImplement/Models/Facilities.cs index b740925..9a02927 100644 --- a/Secure/SecureShopFileImplement/Models/Facilities.cs +++ b/Secure/SecureShopFileImplement/Models/Facilities.cs @@ -3,7 +3,7 @@ using SecureShopContracts.ViewModels; using SecureShopDataModels.Models; using System.Xml.Linq; -namespace SecureShopFileImplement.Models +namespace SecureShopDataBaseImplement.Models { public class Facilities : IFacilitiesModel { diff --git a/Secure/SecureShopFileImplement/Models/Order.cs b/Secure/SecureShopFileImplement/Models/Order.cs index 56d9bf2..bc1b2fe 100644 --- a/Secure/SecureShopFileImplement/Models/Order.cs +++ b/Secure/SecureShopFileImplement/Models/Order.cs @@ -4,7 +4,7 @@ using SecureShopDataModels.Enums; using SecureShopDataModels.Models; using System.Xml.Linq; -namespace SecureShopFileImplement.Models +namespace SecureShopDataBaseImplement.Models { public class Order : IOrderModel { diff --git a/Secure/SecureShopFileImplement/Models/Secure.cs b/Secure/SecureShopFileImplement/Models/Secure.cs index 5706a63..2d58488 100644 --- a/Secure/SecureShopFileImplement/Models/Secure.cs +++ b/Secure/SecureShopFileImplement/Models/Secure.cs @@ -3,7 +3,7 @@ using SecureShopContracts.ViewModels; using SecureShopDataModels.Models; using System.Xml.Linq; -namespace SecureShopFileImplement.Models +namespace SecureShopDataBaseImplement.Models { public class Secure : ISecureModel {