From 9d2def59f3046cf7f86a8ffbf2af05581067c195 Mon Sep 17 00:00:00 2001 From: goldfest Date: Sat, 9 Mar 2024 00:39:32 +0400 Subject: [PATCH 1/2] =?UTF-8?q?=D1=81=D0=B4=D0=B5=D0=BB=D0=B0=D0=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TravelCompany/TravelCompany.sln | 8 +- TravelCompany/TravelCompany/Program.cs | 2 +- .../TravelCompany/TravelCompanyView.csproj | 7 +- .../Implements/ComponentStorage.cs | 91 ++++++++++ .../Implements/OrderStorage.cs | 99 ++++++++++ .../Implements/TravelStorage.cs | 110 ++++++++++++ .../20240308202100_InitialCreate.Designer.cs | 169 ++++++++++++++++++ .../20240308202100_InitialCreate.cs | 125 +++++++++++++ .../TravelCompanyDataBaseModelSnapshot.cs | 166 +++++++++++++++++ .../Models/Component.cs | 63 +++++++ .../Models/IceCream.cs | 103 +++++++++++ .../Models/IceCreamComponent.cs | 22 +++ .../Models/Order.cs | 69 +++++++ .../TravelCompanyDataBase.cs | 29 +++ .../TravelCompanyDatabaseImplement.csproj | 22 +++ 15 files changed, 1082 insertions(+), 3 deletions(-) create mode 100644 TravelCompany/TravelCompanyDatabaseImplement/Implements/ComponentStorage.cs create mode 100644 TravelCompany/TravelCompanyDatabaseImplement/Implements/OrderStorage.cs create mode 100644 TravelCompany/TravelCompanyDatabaseImplement/Implements/TravelStorage.cs create mode 100644 TravelCompany/TravelCompanyDatabaseImplement/Migrations/20240308202100_InitialCreate.Designer.cs create mode 100644 TravelCompany/TravelCompanyDatabaseImplement/Migrations/20240308202100_InitialCreate.cs create mode 100644 TravelCompany/TravelCompanyDatabaseImplement/Migrations/TravelCompanyDataBaseModelSnapshot.cs create mode 100644 TravelCompany/TravelCompanyDatabaseImplement/Models/Component.cs create mode 100644 TravelCompany/TravelCompanyDatabaseImplement/Models/IceCream.cs create mode 100644 TravelCompany/TravelCompanyDatabaseImplement/Models/IceCreamComponent.cs create mode 100644 TravelCompany/TravelCompanyDatabaseImplement/Models/Order.cs create mode 100644 TravelCompany/TravelCompanyDatabaseImplement/TravelCompanyDataBase.cs create mode 100644 TravelCompany/TravelCompanyDatabaseImplement/TravelCompanyDatabaseImplement.csproj diff --git a/TravelCompany/TravelCompany.sln b/TravelCompany/TravelCompany.sln index c013173..c230dfd 100644 --- a/TravelCompany/TravelCompany.sln +++ b/TravelCompany/TravelCompany.sln @@ -13,7 +13,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TravelCompanyBusinessLogic" EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TravelCompanyListImplement", "TravelCompanyListImplement\TravelCompanyListImplement.csproj", "{46AEB3CA-9FB8-444A-9145-B274C5FBEF6B}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TravelCompanyFileImplement", "TravelCompanyFileImplement\TravelCompanyFileImplement.csproj", "{3684BE6E-B9D1-450E-B450-FCE08421C87B}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TravelCompanyFileImplement", "TravelCompanyFileImplement\TravelCompanyFileImplement.csproj", "{3684BE6E-B9D1-450E-B450-FCE08421C87B}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TravelCompanyDatabaseImplement", "TravelCompanyDatabaseImplement\TravelCompanyDatabaseImplement.csproj", "{1232CA3D-F5FE-4BD6-88E9-719F822E605B}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -45,6 +47,10 @@ Global {3684BE6E-B9D1-450E-B450-FCE08421C87B}.Debug|Any CPU.Build.0 = Debug|Any CPU {3684BE6E-B9D1-450E-B450-FCE08421C87B}.Release|Any CPU.ActiveCfg = Release|Any CPU {3684BE6E-B9D1-450E-B450-FCE08421C87B}.Release|Any CPU.Build.0 = Release|Any CPU + {1232CA3D-F5FE-4BD6-88E9-719F822E605B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {1232CA3D-F5FE-4BD6-88E9-719F822E605B}.Debug|Any CPU.Build.0 = Debug|Any CPU + {1232CA3D-F5FE-4BD6-88E9-719F822E605B}.Release|Any CPU.ActiveCfg = Release|Any CPU + {1232CA3D-F5FE-4BD6-88E9-719F822E605B}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/TravelCompany/TravelCompany/Program.cs b/TravelCompany/TravelCompany/Program.cs index e4bdf5f..3419d03 100644 --- a/TravelCompany/TravelCompany/Program.cs +++ b/TravelCompany/TravelCompany/Program.cs @@ -1,7 +1,7 @@ using TravelCompanyContracts.BusinessLogicsContracts; using TravelCompanyBusinessLogic.BusinessLogic; using TravelCompanyContracts.StoragesContracts; -using TravelCompanyFileImplement.Implements; +using TravelCompanyDatabaseImplement.Implements; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using NLog.Extensions.Logging; diff --git a/TravelCompany/TravelCompany/TravelCompanyView.csproj b/TravelCompany/TravelCompany/TravelCompanyView.csproj index fc8bff8..68b65d0 100644 --- a/TravelCompany/TravelCompany/TravelCompanyView.csproj +++ b/TravelCompany/TravelCompany/TravelCompanyView.csproj @@ -2,13 +2,17 @@ WinExe - net6.0-windows + net8.0-windows enable true enable + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + @@ -22,6 +26,7 @@ + diff --git a/TravelCompany/TravelCompanyDatabaseImplement/Implements/ComponentStorage.cs b/TravelCompany/TravelCompanyDatabaseImplement/Implements/ComponentStorage.cs new file mode 100644 index 0000000..91e3725 --- /dev/null +++ b/TravelCompany/TravelCompanyDatabaseImplement/Implements/ComponentStorage.cs @@ -0,0 +1,91 @@ +using TravelCompanyContracts.BindingModels; +using TravelCompanyContracts.SearchModels; +using TravelCompanyContracts.StoragesContracts; +using TravelCompanyContracts.ViewModels; +using TravelCompanyDatabaseImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace TravelCompanyDatabaseImplement.Implements +{ + public class ComponentStorage : IComponentStorage + { + public List GetFullList() + { + using var context = new TravelCompanyDataBase(); + return context.Components + .Select(x => x.GetViewModel) + .ToList(); + } + public List GetFilteredList(ComponentSearchModel + model) + { + if (string.IsNullOrEmpty(model.ComponentName)) + { + return new(); + } + using var context = new TravelCompanyDataBase(); + 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 TravelCompanyDataBase(); + 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 TravelCompanyDataBase(); + context.Components.Add(newComponent); + context.SaveChanges(); + return newComponent.GetViewModel; + } + public ComponentViewModel? Update(ComponentBindingModel model) + { + using var context = new TravelCompanyDataBase(); + 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 TravelCompanyDataBase(); + 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/TravelCompany/TravelCompanyDatabaseImplement/Implements/OrderStorage.cs b/TravelCompany/TravelCompanyDatabaseImplement/Implements/OrderStorage.cs new file mode 100644 index 0000000..60e0dd4 --- /dev/null +++ b/TravelCompany/TravelCompanyDatabaseImplement/Implements/OrderStorage.cs @@ -0,0 +1,99 @@ +using TravelCompanyContracts.BindingModels; +using TravelCompanyContracts.SearchModels; +using TravelCompanyContracts.StoragesContracts; +using TravelCompanyContracts.ViewModels; +using TravelCompanyDatabaseImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace TravelCompanyDatabaseImplement.Implements +{ + public class OrderStorage : IOrderStorage + { + public List GetFullList() + { + using var context = new TravelCompanyDataBase(); + return context.Orders + .Select(x => AccessTravelStorage(x.GetViewModel)) + .ToList(); + } + public List GetFilteredList(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return new(); + } + using var context = new TravelCompanyDataBase(); + return context.Orders + .Where(x => x.Id == model.Id) + .Select(x => AccessTravelStorage(x.GetViewModel)) + .ToList(); + } + public OrderViewModel? GetElement(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + using var context = new TravelCompanyDataBase(); + return AccessTravelStorage(context.Orders.FirstOrDefault(x => x.Id == model.Id)?.GetViewModel); + } + public OrderViewModel? Insert(OrderBindingModel model) + { + var newOrder = Order.Create(model); + if (newOrder == null) + { + return null; + } + using var context = new TravelCompanyDataBase(); + context.Orders.Add(newOrder); + context.SaveChanges(); + return AccessTravelStorage(newOrder.GetViewModel); + } + public OrderViewModel? Update(OrderBindingModel model) + { + using var context = new TravelCompanyDataBase(); + var order = context.Orders.FirstOrDefault(x => x.Id == + model.Id); + if (order == null) + { + return null; + } + order.Update(model); + context.SaveChanges(); + return AccessTravelStorage(order.GetViewModel); + } + public OrderViewModel? Delete(OrderBindingModel model) + { + using var context = new TravelCompanyDataBase(); + var element = context.Orders.FirstOrDefault(rec => rec.Id == + model.Id); + if (element != null) + { + context.Orders.Remove(element); + context.SaveChanges(); + return AccessTravelStorage(element.GetViewModel); + } + return null; + } + + public static OrderViewModel AccessTravelStorage(OrderViewModel model) + { + if (model == null) + return null; + using var context = new TravelCompanyDataBase(); + foreach (var Travel in context.Travels) + { + if (Travel.Id == model.TravelId) + { + model.TravelName = Travel.TravelName; + break; + } + } + return model; + } + } +} diff --git a/TravelCompany/TravelCompanyDatabaseImplement/Implements/TravelStorage.cs b/TravelCompany/TravelCompanyDatabaseImplement/Implements/TravelStorage.cs new file mode 100644 index 0000000..20dd6d5 --- /dev/null +++ b/TravelCompany/TravelCompanyDatabaseImplement/Implements/TravelStorage.cs @@ -0,0 +1,110 @@ +using TravelCompanyContracts.BindingModels; +using TravelCompanyContracts.SearchModels; +using TravelCompanyContracts.StoragesContracts; +using TravelCompanyContracts.ViewModels; +using TravelCompanyDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace TravelCompanyDatabaseImplement.Implements +{ + public class TravelStorage : ITravelStorage + { + public List GetFullList() + { + using var context = new TravelCompanyDataBase(); + return context.Travels + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + public List GetFilteredList(TravelSearchModel model) + { + if (string.IsNullOrEmpty(model.TravelName)) + { + return new(); + } + using var context = new TravelCompanyDataBase(); + return context.Travels.Include(x => x.Components) + .ThenInclude(x => x.Component) + .Where(x => x.TravelName.Contains(model.TravelName)) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + public TravelViewModel? GetElement(TravelSearchModel model) + { + if (string.IsNullOrEmpty(model.TravelName) && + !model.Id.HasValue) + { + return null; + } + using var context = new TravelCompanyDataBase(); + return context.Travels + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.TravelName) && + x.TravelName == model.TravelName) || + (model.Id.HasValue && x.Id == + model.Id)) + ?.GetViewModel; + } + public TravelViewModel? Insert(TravelBindingModel model) + { + using var context = new TravelCompanyDataBase(); + var newTravel = Travel.Create(context, model); + if (newTravel == null) + { + return null; + } + context.Travels.Add(newTravel); + context.SaveChanges(); + return newTravel.GetViewModel; + } + public TravelViewModel? Update(TravelBindingModel model) + { + using var context = new TravelCompanyDataBase(); + using var transaction = context.Database.BeginTransaction(); + try + { + var Travel = context.Travels.FirstOrDefault(rec => + rec.Id == model.Id); + if (Travel == null) + { + return null; + } + Travel.Update(model); + context.SaveChanges(); + Travel.UpdateComponents(context, model); + transaction.Commit(); + return Travel.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + public TravelViewModel? Delete(TravelBindingModel model) + { + using var context = new TravelCompanyDataBase(); + var element = context.Travels + .Include(x => x.Components) + .FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Travels.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} + diff --git a/TravelCompany/TravelCompanyDatabaseImplement/Migrations/20240308202100_InitialCreate.Designer.cs b/TravelCompany/TravelCompanyDatabaseImplement/Migrations/20240308202100_InitialCreate.Designer.cs new file mode 100644 index 0000000..5e4c3df --- /dev/null +++ b/TravelCompany/TravelCompanyDatabaseImplement/Migrations/20240308202100_InitialCreate.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 TravelCompanyDatabaseImplement; + +#nullable disable + +namespace TravelCompanyDatabaseImplement.Migrations +{ + [DbContext(typeof(TravelCompanyDataBase))] + [Migration("20240308202100_InitialCreate")] + partial class InitialCreate + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "8.0.2") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("TravelCompanyDatabaseImplement.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("TravelCompanyDatabaseImplement.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("Status") + .HasColumnType("int"); + + b.Property("Sum") + .HasColumnType("float"); + + b.Property("TravelId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("TravelId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("TravelCompanyDatabaseImplement.Models.Travel", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Price") + .HasColumnType("float"); + + b.Property("TravelName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Travels"); + }); + + modelBuilder.Entity("TravelCompanyDatabaseImplement.Models.TravelComponent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComponentId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("TravelId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ComponentId"); + + b.HasIndex("TravelId"); + + b.ToTable("TravelComponents"); + }); + + modelBuilder.Entity("TravelCompanyDatabaseImplement.Models.Order", b => + { + b.HasOne("TravelCompanyDatabaseImplement.Models.Travel", null) + .WithMany("Orders") + .HasForeignKey("TravelId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("TravelCompanyDatabaseImplement.Models.TravelComponent", b => + { + b.HasOne("TravelCompanyDatabaseImplement.Models.Component", "Component") + .WithMany("TravelComponents") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("TravelCompanyDatabaseImplement.Models.Travel", "Travel") + .WithMany("Components") + .HasForeignKey("TravelId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Component"); + + b.Navigation("Travel"); + }); + + modelBuilder.Entity("TravelCompanyDatabaseImplement.Models.Component", b => + { + b.Navigation("TravelComponents"); + }); + + modelBuilder.Entity("TravelCompanyDatabaseImplement.Models.Travel", b => + { + b.Navigation("Components"); + + b.Navigation("Orders"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/TravelCompany/TravelCompanyDatabaseImplement/Migrations/20240308202100_InitialCreate.cs b/TravelCompany/TravelCompanyDatabaseImplement/Migrations/20240308202100_InitialCreate.cs new file mode 100644 index 0000000..9b1d5c0 --- /dev/null +++ b/TravelCompany/TravelCompanyDatabaseImplement/Migrations/20240308202100_InitialCreate.cs @@ -0,0 +1,125 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace TravelCompanyDatabaseImplement.Migrations +{ + /// + public partial class InitialCreate : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Components", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + ComponentName = table.Column(type: "nvarchar(max)", nullable: false), + Cost = table.Column(type: "float", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Components", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Travels", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + TravelName = table.Column(type: "nvarchar(max)", nullable: false), + Price = table.Column(type: "float", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Travels", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Orders", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + Count = table.Column(type: "int", nullable: false), + Sum = table.Column(type: "float", nullable: false), + Status = table.Column(type: "int", nullable: false), + DateCreate = table.Column(type: "datetime2", nullable: false), + DateImplement = table.Column(type: "datetime2", nullable: true), + TravelId = table.Column(type: "int", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Orders", x => x.Id); + table.ForeignKey( + name: "FK_Orders_Travels_TravelId", + column: x => x.TravelId, + principalTable: "Travels", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "TravelComponents", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + TravelId = 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_TravelComponents", x => x.Id); + table.ForeignKey( + name: "FK_TravelComponents_Components_ComponentId", + column: x => x.ComponentId, + principalTable: "Components", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_TravelComponents_Travels_TravelId", + column: x => x.TravelId, + principalTable: "Travels", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateIndex( + name: "IX_Orders_TravelId", + table: "Orders", + column: "TravelId"); + + migrationBuilder.CreateIndex( + name: "IX_TravelComponents_ComponentId", + table: "TravelComponents", + column: "ComponentId"); + + migrationBuilder.CreateIndex( + name: "IX_TravelComponents_TravelId", + table: "TravelComponents", + column: "TravelId"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "Orders"); + + migrationBuilder.DropTable( + name: "TravelComponents"); + + migrationBuilder.DropTable( + name: "Components"); + + migrationBuilder.DropTable( + name: "Travels"); + } + } +} diff --git a/TravelCompany/TravelCompanyDatabaseImplement/Migrations/TravelCompanyDataBaseModelSnapshot.cs b/TravelCompany/TravelCompanyDatabaseImplement/Migrations/TravelCompanyDataBaseModelSnapshot.cs new file mode 100644 index 0000000..3cacb43 --- /dev/null +++ b/TravelCompany/TravelCompanyDatabaseImplement/Migrations/TravelCompanyDataBaseModelSnapshot.cs @@ -0,0 +1,166 @@ +// +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using TravelCompanyDatabaseImplement; + +#nullable disable + +namespace TravelCompanyDatabaseImplement.Migrations +{ + [DbContext(typeof(TravelCompanyDataBase))] + partial class TravelCompanyDataBaseModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "8.0.2") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("TravelCompanyDatabaseImplement.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("TravelCompanyDatabaseImplement.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("Status") + .HasColumnType("int"); + + b.Property("Sum") + .HasColumnType("float"); + + b.Property("TravelId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("TravelId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("TravelCompanyDatabaseImplement.Models.Travel", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Price") + .HasColumnType("float"); + + b.Property("TravelName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Travels"); + }); + + modelBuilder.Entity("TravelCompanyDatabaseImplement.Models.TravelComponent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComponentId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("TravelId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ComponentId"); + + b.HasIndex("TravelId"); + + b.ToTable("TravelComponents"); + }); + + modelBuilder.Entity("TravelCompanyDatabaseImplement.Models.Order", b => + { + b.HasOne("TravelCompanyDatabaseImplement.Models.Travel", null) + .WithMany("Orders") + .HasForeignKey("TravelId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("TravelCompanyDatabaseImplement.Models.TravelComponent", b => + { + b.HasOne("TravelCompanyDatabaseImplement.Models.Component", "Component") + .WithMany("TravelComponents") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("TravelCompanyDatabaseImplement.Models.Travel", "Travel") + .WithMany("Components") + .HasForeignKey("TravelId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Component"); + + b.Navigation("Travel"); + }); + + modelBuilder.Entity("TravelCompanyDatabaseImplement.Models.Component", b => + { + b.Navigation("TravelComponents"); + }); + + modelBuilder.Entity("TravelCompanyDatabaseImplement.Models.Travel", b => + { + b.Navigation("Components"); + + b.Navigation("Orders"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/TravelCompany/TravelCompanyDatabaseImplement/Models/Component.cs b/TravelCompany/TravelCompanyDatabaseImplement/Models/Component.cs new file mode 100644 index 0000000..81fc7a6 --- /dev/null +++ b/TravelCompany/TravelCompanyDatabaseImplement/Models/Component.cs @@ -0,0 +1,63 @@ +using TravelCompanyContracts.BindingModels; +using TravelCompanyContracts.ViewModels; +using TravelCompanyDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations.Schema; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace TravelCompanyDatabaseImplement.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 TravelComponents { 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/TravelCompany/TravelCompanyDatabaseImplement/Models/IceCream.cs b/TravelCompany/TravelCompanyDatabaseImplement/Models/IceCream.cs new file mode 100644 index 0000000..88ffaa3 --- /dev/null +++ b/TravelCompany/TravelCompanyDatabaseImplement/Models/IceCream.cs @@ -0,0 +1,103 @@ +using TravelCompanyContracts.BindingModels; +using TravelCompanyContracts.ViewModels; +using TravelCompanyDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace TravelCompanyDatabaseImplement.Models +{ + public class Travel : ITravelModel + { + public int Id { get; set; } + [Required] + public string TravelName { get; set; } = string.Empty; + [Required] + public double Price { get; set; } + private Dictionary? _TravelComponents = + null; + [NotMapped] + public Dictionary TravelComponents + { + get + { + if (_TravelComponents == null) + { + _TravelComponents = Components + .ToDictionary(recPC => recPC.ComponentId, recPC => + (recPC.Component as IComponentModel, recPC.Count)); + } + return _TravelComponents; + } + } + [ForeignKey("TravelId")] + public virtual List Components { get; set; } = new(); + [ForeignKey("TravelId")] + public virtual List Orders { get; set; } = new(); + public static Travel Create(TravelCompanyDataBase context, + TravelBindingModel model) + { + return new Travel() + { + Id = model.Id, + TravelName = model.TravelName, + Price = model.Price, + Components = model.TravelComponents.Select(x => new + TravelComponent + { + Component = context.Components.First(y => y.Id == x.Key), + Count = x.Value.Item2 + }).ToList() + }; + } + public void Update(TravelBindingModel model) + { + TravelName = model.TravelName; + Price = model.Price; + } + public TravelViewModel GetViewModel => new() + { + Id = Id, + TravelName = TravelName, + Price = Price, + TravelComponents = TravelComponents + }; + public void UpdateComponents(TravelCompanyDataBase context, + TravelBindingModel model) + { + var TravelComponents = context.TravelComponents.Where(rec => + rec.TravelId == model.Id).ToList(); + if (TravelComponents != null && TravelComponents.Count > 0) + { + context.TravelComponents.RemoveRange(TravelComponents.Where(rec +=> !model.TravelComponents.ContainsKey(rec.ComponentId))); + + context.SaveChanges(); + foreach (var updateComponent in TravelComponents) + { + updateComponent.Count = + model.TravelComponents[updateComponent.ComponentId].Item2; + model.TravelComponents.Remove(updateComponent.ComponentId); + } + context.SaveChanges(); + } + var Travel = context.Travels.First(x => x.Id == Id); + foreach (var pc in model.TravelComponents) + { + context.TravelComponents.Add(new TravelComponent + { + Travel = Travel, + Component = context.Components.First(x => x.Id == pc.Key), + Count = pc.Value.Item2 + }); + context.SaveChanges(); + } + _TravelComponents = null; + } + } +} + diff --git a/TravelCompany/TravelCompanyDatabaseImplement/Models/IceCreamComponent.cs b/TravelCompany/TravelCompanyDatabaseImplement/Models/IceCreamComponent.cs new file mode 100644 index 0000000..c84efb4 --- /dev/null +++ b/TravelCompany/TravelCompanyDatabaseImplement/Models/IceCreamComponent.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace TravelCompanyDatabaseImplement.Models +{ + public class TravelComponent + { + public int Id { get; set; } + [Required] + public int TravelId { get; set; } + [Required] + public int ComponentId { get; set; } + [Required] + public int Count { get; set; } + public virtual Component Component { get; set; } = new(); + public virtual Travel Travel { get; set; } = new(); + } +} diff --git a/TravelCompany/TravelCompanyDatabaseImplement/Models/Order.cs b/TravelCompany/TravelCompanyDatabaseImplement/Models/Order.cs new file mode 100644 index 0000000..d139b08 --- /dev/null +++ b/TravelCompany/TravelCompanyDatabaseImplement/Models/Order.cs @@ -0,0 +1,69 @@ +using TravelCompanyContracts.BindingModels; +using TravelCompanyContracts.ViewModels; +using TravelCompanyDataModels.Enums; +using TravelCompanyDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace TravelCompanyDatabaseImplement.Models +{ + public class Order : IOrderModel + { + public int Id { get; private set; } + [Required] + public int Count { get; private set; } + [Required] + public double Sum { get; private set; } + [Required] + public OrderStatus Status { get; private set; } + [Required] + public DateTime DateCreate { get; private set; } + public DateTime? DateImplement { get; private set; } + [Required] + public int TravelId { get; private set; } + + public static Order? Create(OrderBindingModel model) + { + if (model == null) + { + return null; + } + return new Order() + { + Id = model.Id, + Count = model.Count, + Sum = model.Sum, + Status = model.Status, + DateCreate = model.DateCreate, + DateImplement = model.DateImplement, + TravelId = model.TravelId, + }; + } + + public void Update(OrderBindingModel? model) + { + if (model == null) + { + return; + } + Status = model.Status; + DateImplement = model.DateImplement; + } + + public OrderViewModel GetViewModel => new() + { + TravelId = TravelId, + Count = Count, + Sum = Sum, + Status = Status, + DateCreate = DateCreate, + DateImplement = DateImplement, + Id = Id, + }; + + } +} diff --git a/TravelCompany/TravelCompanyDatabaseImplement/TravelCompanyDataBase.cs b/TravelCompany/TravelCompanyDatabaseImplement/TravelCompanyDataBase.cs new file mode 100644 index 0000000..f533f89 --- /dev/null +++ b/TravelCompany/TravelCompanyDatabaseImplement/TravelCompanyDataBase.cs @@ -0,0 +1,29 @@ +using TravelCompanyDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace TravelCompanyDatabaseImplement +{ + public class TravelCompanyDataBase : DbContext + { + protected override void OnConfiguring(DbContextOptionsBuilder +optionsBuilder) + { + if (optionsBuilder.IsConfigured == false) + { + optionsBuilder.UseSqlServer(@"Data Source=DESKTOP-D5A5OOG\GOLDFEST;Initial Catalog=TravelCompanyDatabase;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); + } + base.OnConfiguring(optionsBuilder); + } + public virtual DbSet Components { set; get; } + public virtual DbSet Travels { set; get; } + public virtual DbSet TravelComponents { set; get; } + public virtual DbSet Orders { set; get; } + + } +} diff --git a/TravelCompany/TravelCompanyDatabaseImplement/TravelCompanyDatabaseImplement.csproj b/TravelCompany/TravelCompanyDatabaseImplement/TravelCompanyDatabaseImplement.csproj new file mode 100644 index 0000000..530cfdb --- /dev/null +++ b/TravelCompany/TravelCompanyDatabaseImplement/TravelCompanyDatabaseImplement.csproj @@ -0,0 +1,22 @@ + + + + net8.0 + enable + enable + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + -- 2.25.1 From 10e7cbdb1a3461c4b0517bef9964bb915ec961fa Mon Sep 17 00:00:00 2001 From: goldfest Date: Thu, 14 Mar 2024 21:10:11 +0400 Subject: [PATCH 2/2] ^ --- .../Models/{IceCream.cs => Travel.cs} | 0 .../Models/{IceCreamComponent.cs => TravelComponent.cs} | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename TravelCompany/TravelCompanyDatabaseImplement/Models/{IceCream.cs => Travel.cs} (100%) rename TravelCompany/TravelCompanyDatabaseImplement/Models/{IceCreamComponent.cs => TravelComponent.cs} (100%) diff --git a/TravelCompany/TravelCompanyDatabaseImplement/Models/IceCream.cs b/TravelCompany/TravelCompanyDatabaseImplement/Models/Travel.cs similarity index 100% rename from TravelCompany/TravelCompanyDatabaseImplement/Models/IceCream.cs rename to TravelCompany/TravelCompanyDatabaseImplement/Models/Travel.cs diff --git a/TravelCompany/TravelCompanyDatabaseImplement/Models/IceCreamComponent.cs b/TravelCompany/TravelCompanyDatabaseImplement/Models/TravelComponent.cs similarity index 100% rename from TravelCompany/TravelCompanyDatabaseImplement/Models/IceCreamComponent.cs rename to TravelCompany/TravelCompanyDatabaseImplement/Models/TravelComponent.cs -- 2.25.1