diff --git a/TravelCompany/TravelCompany.sln b/TravelCompany/TravelCompany.sln index 817e619..84ba4b3 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", "{0FF615FA-A053-4DF6-99E6-CFC50A1D88C9}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TravelCompanyFileImplement", "TravelCompanyFileImplement\TravelCompanyFileImplement.csproj", "{388A7F25-BA80-436B-85C2-479FFD9CD6F2}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TravelCompanyFileImplement", "TravelCompanyFileImplement\TravelCompanyFileImplement.csproj", "{388A7F25-BA80-436B-85C2-479FFD9CD6F2}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TravelCompanyDatabaseImplement", "TravelCompanyDatabaseImplement\TravelCompanyDatabaseImplement.csproj", "{FD10A23B-E974-4B1B-A41D-B7F9343CE1A9}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -45,6 +47,10 @@ Global {388A7F25-BA80-436B-85C2-479FFD9CD6F2}.Debug|Any CPU.Build.0 = Debug|Any CPU {388A7F25-BA80-436B-85C2-479FFD9CD6F2}.Release|Any CPU.ActiveCfg = Release|Any CPU {388A7F25-BA80-436B-85C2-479FFD9CD6F2}.Release|Any CPU.Build.0 = Release|Any CPU + {FD10A23B-E974-4B1B-A41D-B7F9343CE1A9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {FD10A23B-E974-4B1B-A41D-B7F9343CE1A9}.Debug|Any CPU.Build.0 = Debug|Any CPU + {FD10A23B-E974-4B1B-A41D-B7F9343CE1A9}.Release|Any CPU.ActiveCfg = Release|Any CPU + {FD10A23B-E974-4B1B-A41D-B7F9343CE1A9}.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 a355230..aaa5fbd 100644 --- a/TravelCompany/TravelCompany/Program.cs +++ b/TravelCompany/TravelCompany/Program.cs @@ -3,7 +3,7 @@ using Microsoft.Extensions.Logging; using NLog.Extensions.Logging; using TravelCompanyContracts.BusinessLogicsContracts; using TravelCompanyContracts.StoragesContracts; -using TravelCompanyFileImplement.Implements; +using TravelCompanyDatabaseImplement.Implements; using TravelCompanyBusinessLogic.BusinessLogic; namespace TravelCompany diff --git a/TravelCompany/TravelCompany/TravelCompany.csproj b/TravelCompany/TravelCompany/TravelCompany.csproj index 1674708..8daa154 100644 --- a/TravelCompany/TravelCompany/TravelCompany.csproj +++ b/TravelCompany/TravelCompany/TravelCompany.csproj @@ -19,6 +19,10 @@ + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + @@ -28,6 +32,7 @@ + diff --git a/TravelCompany/TravelCompanyDatabaseImplement/Implements/ConditionStorage.cs b/TravelCompany/TravelCompanyDatabaseImplement/Implements/ConditionStorage.cs new file mode 100644 index 0000000..e07127f --- /dev/null +++ b/TravelCompany/TravelCompanyDatabaseImplement/Implements/ConditionStorage.cs @@ -0,0 +1,89 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using TravelCompanyContracts.BindingModels; +using TravelCompanyContracts.SearchModels; +using TravelCompanyContracts.StoragesContracts; +using TravelCompanyContracts.ViewModels; +using TravelCompanyDatabaseImplement.Models; + +namespace TravelCompanyDatabaseImplement.Implements +{ + public class ConditionStorage : IConditionStorage + { + public List GetFullList() + { + using var context = new TravelCompanyDatabase(); + return context.Conditions + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFilteredList(ConditionSearchModel model) + { + if (string.IsNullOrEmpty(model.ConditionName)) + { + return new(); + } + using var context = new TravelCompanyDatabase(); + return context.Conditions + .Where(x => x.ConditionName.Contains(model.ConditionName)) + .Select(x => x.GetViewModel) + .ToList(); + } + + public ConditionViewModel? GetElement(ConditionSearchModel model) + { + if (string.IsNullOrEmpty(model.ConditionName) && !model.Id.HasValue) + { + return null; + } + using var context = new TravelCompanyDatabase(); + return context.Conditions + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.ConditionName) && x.ConditionName == model.ConditionName) || + (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + } + + public ConditionViewModel? Insert(ConditionBindingModel model) + { + var newCondition = Condition.Create(model); + if (newCondition == null) + { + return null; + } + using var context = new TravelCompanyDatabase(); + context.Conditions.Add(newCondition); + context.SaveChanges(); + return newCondition.GetViewModel; + } + + public ConditionViewModel? Update(ConditionBindingModel model) + { + using var context = new TravelCompanyDatabase(); + var condition = context.Conditions.FirstOrDefault(x => x.Id == model.Id); + if (condition == null) + { + return null; + } + condition.Update(model); + context.SaveChanges(); + return condition.GetViewModel; + } + + public ConditionViewModel? Delete(ConditionBindingModel model) + { + using var context = new TravelCompanyDatabase(); + var element = context.Conditions.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Conditions.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..07e0902 --- /dev/null +++ b/TravelCompany/TravelCompanyDatabaseImplement/Implements/OrderStorage.cs @@ -0,0 +1,90 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using TravelCompanyContracts.BindingModels; +using TravelCompanyContracts.SearchModels; +using TravelCompanyContracts.StoragesContracts; +using TravelCompanyContracts.ViewModels; +using TravelCompanyDatabaseImplement.Models; + +namespace TravelCompanyDatabaseImplement.Implements +{ + public class OrderStorage : IOrderStorage + { + public List GetFullList() + { + using var context = new TravelCompanyDatabase(); + return context.Orders + .Select(x => AccessTravelStorage(x.GetViewModel, context)) + .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, context)) + .ToList(); + } + public OrderViewModel? GetElement(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return new(); + } + using var context = new TravelCompanyDatabase(); + return AccessTravelStorage(context.Orders + .FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id) + ?.GetViewModel, context); + } + 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, context); + } + 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, context); + } + 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, context); + } + return null; + } + static OrderViewModel AccessTravelStorage(OrderViewModel model, TravelCompanyDatabase context) + { + if (model == null) return model; + string? travelName = context.Travels.FirstOrDefault(x => x.Id == model.TravelId)?.TravelName; + if (travelName != null) model.TravelName = travelName; + return model; + } + } +} diff --git a/TravelCompany/TravelCompanyDatabaseImplement/Implements/TravelStorage.cs b/TravelCompany/TravelCompanyDatabaseImplement/Implements/TravelStorage.cs new file mode 100644 index 0000000..471a9e1 --- /dev/null +++ b/TravelCompany/TravelCompanyDatabaseImplement/Implements/TravelStorage.cs @@ -0,0 +1,111 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using TravelCompanyContracts.StoragesContracts; +using TravelCompanyContracts.BindingModels; +using TravelCompanyContracts.SearchModels; +using TravelCompanyContracts.ViewModels; +using TravelCompanyDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; + +namespace TravelCompanyDatabaseImplement.Implements +{ + public class TravelStorage : ITravelStorage + { + public List GetFullList() + { + using var context = new TravelCompanyDatabase(); + return context.Travels + .Include(x => x.Conditions) + .ThenInclude(x => x.Condition) + .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.Conditions) + .ThenInclude(x => x.Condition) + .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.Conditions) + .ThenInclude(x => x.Condition) + .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.UpdateConditions(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.Conditions) + .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/20230319202545_InitialCreate.Designer.cs b/TravelCompany/TravelCompanyDatabaseImplement/Migrations/20230319202545_InitialCreate.Designer.cs new file mode 100644 index 0000000..1705827 --- /dev/null +++ b/TravelCompany/TravelCompanyDatabaseImplement/Migrations/20230319202545_InitialCreate.Designer.cs @@ -0,0 +1,170 @@ +// +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("20230319202545_InitialCreate")] + partial class InitialCreate + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.3") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("TravelCompanyDatabaseImplement.Models.Condition", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ConditionName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Cost") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Conditions"); + }); + + 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") + .IsRequired() + .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.TravelCondition", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ConditionId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("TravelId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ConditionId"); + + b.HasIndex("TravelId"); + + b.ToTable("TravelConditions"); + }); + + modelBuilder.Entity("TravelCompanyDatabaseImplement.Models.Order", b => + { + b.HasOne("TravelCompanyDatabaseImplement.Models.Travel", "Travel") + .WithMany() + .HasForeignKey("TravelId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Travel"); + }); + + modelBuilder.Entity("TravelCompanyDatabaseImplement.Models.TravelCondition", b => + { + b.HasOne("TravelCompanyDatabaseImplement.Models.Condition", "Condition") + .WithMany("TravelConditions") + .HasForeignKey("ConditionId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("TravelCompanyDatabaseImplement.Models.Travel", "Travel") + .WithMany("Conditions") + .HasForeignKey("TravelId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Condition"); + + b.Navigation("Travel"); + }); + + modelBuilder.Entity("TravelCompanyDatabaseImplement.Models.Condition", b => + { + b.Navigation("TravelConditions"); + }); + + modelBuilder.Entity("TravelCompanyDatabaseImplement.Models.Travel", b => + { + b.Navigation("Conditions"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/TravelCompany/TravelCompanyDatabaseImplement/Migrations/20230319202545_InitialCreate.cs b/TravelCompany/TravelCompanyDatabaseImplement/Migrations/20230319202545_InitialCreate.cs new file mode 100644 index 0000000..03e4a85 --- /dev/null +++ b/TravelCompany/TravelCompanyDatabaseImplement/Migrations/20230319202545_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: "Conditions", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + ConditionName = table.Column(type: "nvarchar(max)", nullable: false), + Cost = table.Column(type: "float", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Conditions", 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"), + TravelId = 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: 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: "TravelConditions", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + TravelId = table.Column(type: "int", nullable: false), + ConditionId = table.Column(type: "int", nullable: false), + Count = table.Column(type: "int", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_TravelConditions", x => x.Id); + table.ForeignKey( + name: "FK_TravelConditions_Conditions_ConditionId", + column: x => x.ConditionId, + principalTable: "Conditions", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_TravelConditions_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_TravelConditions_ConditionId", + table: "TravelConditions", + column: "ConditionId"); + + migrationBuilder.CreateIndex( + name: "IX_TravelConditions_TravelId", + table: "TravelConditions", + column: "TravelId"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "Orders"); + + migrationBuilder.DropTable( + name: "TravelConditions"); + + migrationBuilder.DropTable( + name: "Conditions"); + + migrationBuilder.DropTable( + name: "Travels"); + } + } +} diff --git a/TravelCompany/TravelCompanyDatabaseImplement/Migrations/20230319203248_fixDateImplementNN.Designer.cs b/TravelCompany/TravelCompanyDatabaseImplement/Migrations/20230319203248_fixDateImplementNN.Designer.cs new file mode 100644 index 0000000..c8f5f22 --- /dev/null +++ b/TravelCompany/TravelCompanyDatabaseImplement/Migrations/20230319203248_fixDateImplementNN.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("20230319203248_fixDateImplementNN")] + partial class fixDateImplementNN + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.3") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("TravelCompanyDatabaseImplement.Models.Condition", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ConditionName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Cost") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Conditions"); + }); + + 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.TravelCondition", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ConditionId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("TravelId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ConditionId"); + + b.HasIndex("TravelId"); + + b.ToTable("TravelConditions"); + }); + + modelBuilder.Entity("TravelCompanyDatabaseImplement.Models.Order", b => + { + b.HasOne("TravelCompanyDatabaseImplement.Models.Travel", "Travel") + .WithMany() + .HasForeignKey("TravelId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Travel"); + }); + + modelBuilder.Entity("TravelCompanyDatabaseImplement.Models.TravelCondition", b => + { + b.HasOne("TravelCompanyDatabaseImplement.Models.Condition", "Condition") + .WithMany("TravelConditions") + .HasForeignKey("ConditionId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("TravelCompanyDatabaseImplement.Models.Travel", "Travel") + .WithMany("Conditions") + .HasForeignKey("TravelId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Condition"); + + b.Navigation("Travel"); + }); + + modelBuilder.Entity("TravelCompanyDatabaseImplement.Models.Condition", b => + { + b.Navigation("TravelConditions"); + }); + + modelBuilder.Entity("TravelCompanyDatabaseImplement.Models.Travel", b => + { + b.Navigation("Conditions"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/TravelCompany/TravelCompanyDatabaseImplement/Migrations/20230319203248_fixDateImplementNN.cs b/TravelCompany/TravelCompanyDatabaseImplement/Migrations/20230319203248_fixDateImplementNN.cs new file mode 100644 index 0000000..b5400e5 --- /dev/null +++ b/TravelCompany/TravelCompanyDatabaseImplement/Migrations/20230319203248_fixDateImplementNN.cs @@ -0,0 +1,37 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace TravelCompanyDatabaseImplement.Migrations +{ + /// + public partial class fixDateImplementNN : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.AlterColumn( + name: "DateImplement", + table: "Orders", + type: "datetime2", + nullable: true, + oldClrType: typeof(DateTime), + oldType: "datetime2"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.AlterColumn( + name: "DateImplement", + table: "Orders", + type: "datetime2", + nullable: false, + defaultValue: new DateTime(1, 1, 1, 0, 0, 0, 0, DateTimeKind.Unspecified), + oldClrType: typeof(DateTime), + oldType: "datetime2", + oldNullable: true); + } + } +} diff --git a/TravelCompany/TravelCompanyDatabaseImplement/Migrations/TravelCompanyDatabaseModelSnapshot.cs b/TravelCompany/TravelCompanyDatabaseImplement/Migrations/TravelCompanyDatabaseModelSnapshot.cs new file mode 100644 index 0000000..306ab69 --- /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", "7.0.3") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("TravelCompanyDatabaseImplement.Models.Condition", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ConditionName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Cost") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Conditions"); + }); + + 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.TravelCondition", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ConditionId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("TravelId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ConditionId"); + + b.HasIndex("TravelId"); + + b.ToTable("TravelConditions"); + }); + + modelBuilder.Entity("TravelCompanyDatabaseImplement.Models.Order", b => + { + b.HasOne("TravelCompanyDatabaseImplement.Models.Travel", "Travel") + .WithMany() + .HasForeignKey("TravelId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Travel"); + }); + + modelBuilder.Entity("TravelCompanyDatabaseImplement.Models.TravelCondition", b => + { + b.HasOne("TravelCompanyDatabaseImplement.Models.Condition", "Condition") + .WithMany("TravelConditions") + .HasForeignKey("ConditionId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("TravelCompanyDatabaseImplement.Models.Travel", "Travel") + .WithMany("Conditions") + .HasForeignKey("TravelId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Condition"); + + b.Navigation("Travel"); + }); + + modelBuilder.Entity("TravelCompanyDatabaseImplement.Models.Condition", b => + { + b.Navigation("TravelConditions"); + }); + + modelBuilder.Entity("TravelCompanyDatabaseImplement.Models.Travel", b => + { + b.Navigation("Conditions"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/TravelCompany/TravelCompanyDatabaseImplement/Models/Condition.cs b/TravelCompany/TravelCompanyDatabaseImplement/Models/Condition.cs new file mode 100644 index 0000000..7c4e891 --- /dev/null +++ b/TravelCompany/TravelCompanyDatabaseImplement/Models/Condition.cs @@ -0,0 +1,65 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using TravelCompanyContracts.BindingModels; +using TravelCompanyContracts.ViewModels; +using TravelCompanyDataModels.Models; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; + + +namespace TravelCompanyDatabaseImplement.Models +{ + public class Condition : IConditionModel + { + public int Id { get; private set; } + [Required] + public string ConditionName { get; private set; } = string.Empty; + [Required] + public double Cost { get; set; } + [ForeignKey("ConditionId")] + public virtual List TravelConditions { get; set; } = new(); + public static Condition? Create(ConditionBindingModel model) + { + if (model == null) + { + return null; + } + return new Condition() + { + Id = model.Id, + ConditionName = model.ConditionName, + Cost = model.Cost + }; + } + public static Condition Create(ConditionViewModel model) + { + return new Condition + { + Id = model.Id, + ConditionName = model.ConditionName, + Cost = model.Cost + }; + } + public void Update(ConditionBindingModel model) + { + if (model == null) + { + return; + } + ConditionName = model.ConditionName; + Cost = model.Cost; + } + public ConditionViewModel GetViewModel => new() + { + Id = Id, + ConditionName = ConditionName, + Cost = Cost + }; + + } +} + diff --git a/TravelCompany/TravelCompanyDatabaseImplement/Models/Order.cs b/TravelCompany/TravelCompanyDatabaseImplement/Models/Order.cs new file mode 100644 index 0000000..53b973b --- /dev/null +++ b/TravelCompany/TravelCompanyDatabaseImplement/Models/Order.cs @@ -0,0 +1,68 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using TravelCompanyContracts.BindingModels; +using TravelCompanyContracts.ViewModels; +using TravelCompanyDataModels.Enums; +using TravelCompanyDataModels.Models; + +namespace TravelCompanyDatabaseImplement.Models +{ + public class Order : IOrderModel + { + public int Id { get; private set; } + [Required] + public int TravelId { 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 Travel Travel { get; set; } + + public static Order? Create(OrderBindingModel? model) + { + if (model == null) + { + return null; + } + return new Order() + { + Id = model.Id, + TravelId = model.TravelId, + 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; + if (model.DateImplement.HasValue) DateImplement = model.DateImplement; + } + public OrderViewModel GetViewModel => new() + { + Id = Id, + TravelId = TravelId, + Count = Count, + Sum = Sum, + Status = Status, + DateCreate = DateCreate, + DateImplement = DateImplement + }; + + } +} diff --git a/TravelCompany/TravelCompanyDatabaseImplement/Models/Travel.cs b/TravelCompany/TravelCompanyDatabaseImplement/Models/Travel.cs new file mode 100644 index 0000000..05d9520 --- /dev/null +++ b/TravelCompany/TravelCompanyDatabaseImplement/Models/Travel.cs @@ -0,0 +1,99 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using TravelCompanyContracts.BindingModels; +using TravelCompanyContracts.ViewModels; +using TravelCompanyDataModels.Models; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; + +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? _travelConditions = null; + [NotMapped] + public virtual List Orders { get; set; } = new(); + public Dictionary TravelConditions + { + get + { + if (_travelConditions == null) + { + _travelConditions = Conditions.ToDictionary(recPC => recPC.ConditionId, recPC => + (recPC.Condition as IConditionModel, recPC.Count)); + } + return _travelConditions; + } + } + [ForeignKey("TravelId")] + public virtual List Conditions { get; set; } = new(); + public static Travel Create(TravelCompanyDatabase context, + TravelBindingModel model) + { + return new Travel() + { + Id = model.Id, + TravelName = model.TravelName, + Price = model.Price, + Conditions = model.TravelConditions.Select(x => new + TravelCondition + { + Condition = context.Conditions.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, + TravelConditions = TravelConditions + }; + public void UpdateConditions(TravelCompanyDatabase context, + TravelBindingModel model) + { + var travelConditions = context.TravelConditions.Where(rec => rec.TravelId == model.Id).ToList(); + if (travelConditions != null && travelConditions.Count > 0) + { // удалили те, которых нет в модели + context.TravelConditions.RemoveRange(travelConditions.Where(rec + => !model.TravelConditions.ContainsKey(rec.ConditionId))); + context.SaveChanges(); + // обновили количество у существующих записей + foreach (var updateCondition in travelConditions) + { + updateCondition.Count = + model.TravelConditions[updateCondition.ConditionId].Item2; + model.TravelConditions.Remove(updateCondition.ConditionId); + } + context.SaveChanges(); + } + var travel = context.Travels.First(x => x.Id == Id); + foreach (var pc in model.TravelConditions) + { + context.TravelConditions.Add(new TravelCondition + { + Travel = travel, + Condition = context.Conditions.First(x => x.Id == pc.Key), + Count = pc.Value.Item2 + }); + context.SaveChanges(); + } + _travelConditions = null; + } + } +} diff --git a/TravelCompany/TravelCompanyDatabaseImplement/Models/TravelCondition.cs b/TravelCompany/TravelCompanyDatabaseImplement/Models/TravelCondition.cs new file mode 100644 index 0000000..dbb1872 --- /dev/null +++ b/TravelCompany/TravelCompanyDatabaseImplement/Models/TravelCondition.cs @@ -0,0 +1,23 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel; + +namespace TravelCompanyDatabaseImplement.Models +{ + public class TravelCondition + { + public int Id { get; set; } + [Required] + public int TravelId { get; set; } + [Required] + public int ConditionId { get; set; } + [Required] + public int Count { get; set; } + public virtual Condition Condition { get; set; } = new(); + public virtual Travel Travel { get; set; } = new(); + } +} diff --git a/TravelCompany/TravelCompanyDatabaseImplement/TravelCompanyDatabase.cs b/TravelCompany/TravelCompanyDatabaseImplement/TravelCompanyDatabase.cs new file mode 100644 index 0000000..d803413 --- /dev/null +++ b/TravelCompany/TravelCompanyDatabaseImplement/TravelCompanyDatabase.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using TravelCompanyDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; + + +namespace TravelCompanyDatabaseImplement +{ + public class TravelCompanyDatabase : DbContext + { + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + if (optionsBuilder.IsConfigured == false) + { + optionsBuilder.UseSqlServer(@"Data Source=localhost\SQLEXPRESS;Initial Catalog=TravelCompanyDatabaseFull;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); + } + base.OnConfiguring(optionsBuilder); + } + public virtual DbSet Conditions { set; get; } + public virtual DbSet Travels { set; get; } + public virtual DbSet TravelConditions { 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..6403995 --- /dev/null +++ b/TravelCompany/TravelCompanyDatabaseImplement/TravelCompanyDatabaseImplement.csproj @@ -0,0 +1,23 @@ + + + + net6.0 + enable + enable + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + +