Лабораторная работа 3.

This commit is contained in:
ksenianeva 2023-03-20 08:43:09 +04:00
parent 298256bcbd
commit 433c75e083
17 changed files with 1275 additions and 2 deletions

View File

@ -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

View File

@ -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

View File

@ -19,6 +19,10 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.3">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="7.0.0" />
@ -28,6 +32,7 @@
<ItemGroup>
<ProjectReference Include="..\TravelCompanyBusinessLogic\TravelCompanyBusinessLogic.csproj" />
<ProjectReference Include="..\TravelCompanyContracts\TravelCompanyContracts.csproj" />
<ProjectReference Include="..\TravelCompanyDatabaseImplement\TravelCompanyDatabaseImplement.csproj" />
<ProjectReference Include="..\TravelCompanyDataModels\TravelCompanyDataModels.csproj" />
<ProjectReference Include="..\TravelCompanyFileImplement\TravelCompanyFileImplement.csproj" />
<ProjectReference Include="..\TravelCompanyListImplement\TravelCompanyListImplement.csproj" />

View File

@ -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<ConditionViewModel> GetFullList()
{
using var context = new TravelCompanyDatabase();
return context.Conditions
.Select(x => x.GetViewModel)
.ToList();
}
public List<ConditionViewModel> 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;
}
}
}

View File

@ -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<OrderViewModel> GetFullList()
{
using var context = new TravelCompanyDatabase();
return context.Orders
.Select(x => AccessTravelStorage(x.GetViewModel, context))
.ToList();
}
public List<OrderViewModel> 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;
}
}
}

View File

@ -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<TravelViewModel> 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<TravelViewModel> 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;
}
}
}

View File

@ -0,0 +1,170 @@
// <auto-generated />
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
{
/// <inheritdoc />
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<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<string>("ConditionName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<double>("Cost")
.HasColumnType("float");
b.HasKey("Id");
b.ToTable("Conditions");
});
modelBuilder.Entity("TravelCompanyDatabaseImplement.Models.Order", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<int>("Count")
.HasColumnType("int");
b.Property<DateTime>("DateCreate")
.HasColumnType("datetime2");
b.Property<DateTime?>("DateImplement")
.IsRequired()
.HasColumnType("datetime2");
b.Property<int>("Status")
.HasColumnType("int");
b.Property<double>("Sum")
.HasColumnType("float");
b.Property<int>("TravelId")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("TravelId");
b.ToTable("Orders");
});
modelBuilder.Entity("TravelCompanyDatabaseImplement.Models.Travel", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<double>("Price")
.HasColumnType("float");
b.Property<string>("TravelName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.HasKey("Id");
b.ToTable("Travels");
});
modelBuilder.Entity("TravelCompanyDatabaseImplement.Models.TravelCondition", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<int>("ConditionId")
.HasColumnType("int");
b.Property<int>("Count")
.HasColumnType("int");
b.Property<int>("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
}
}
}

View File

@ -0,0 +1,125 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace TravelCompanyDatabaseImplement.Migrations
{
/// <inheritdoc />
public partial class InitialCreate : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Conditions",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
ConditionName = table.Column<string>(type: "nvarchar(max)", nullable: false),
Cost = table.Column<double>(type: "float", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Conditions", x => x.Id);
});
migrationBuilder.CreateTable(
name: "Travels",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
TravelName = table.Column<string>(type: "nvarchar(max)", nullable: false),
Price = table.Column<double>(type: "float", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Travels", x => x.Id);
});
migrationBuilder.CreateTable(
name: "Orders",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
TravelId = table.Column<int>(type: "int", nullable: false),
Count = table.Column<int>(type: "int", nullable: false),
Sum = table.Column<double>(type: "float", nullable: false),
Status = table.Column<int>(type: "int", nullable: false),
DateCreate = table.Column<DateTime>(type: "datetime2", nullable: false),
DateImplement = table.Column<DateTime>(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<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
TravelId = table.Column<int>(type: "int", nullable: false),
ConditionId = table.Column<int>(type: "int", nullable: false),
Count = table.Column<int>(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");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Orders");
migrationBuilder.DropTable(
name: "TravelConditions");
migrationBuilder.DropTable(
name: "Conditions");
migrationBuilder.DropTable(
name: "Travels");
}
}
}

View File

@ -0,0 +1,169 @@
// <auto-generated />
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
{
/// <inheritdoc />
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<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<string>("ConditionName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<double>("Cost")
.HasColumnType("float");
b.HasKey("Id");
b.ToTable("Conditions");
});
modelBuilder.Entity("TravelCompanyDatabaseImplement.Models.Order", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<int>("Count")
.HasColumnType("int");
b.Property<DateTime>("DateCreate")
.HasColumnType("datetime2");
b.Property<DateTime?>("DateImplement")
.HasColumnType("datetime2");
b.Property<int>("Status")
.HasColumnType("int");
b.Property<double>("Sum")
.HasColumnType("float");
b.Property<int>("TravelId")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("TravelId");
b.ToTable("Orders");
});
modelBuilder.Entity("TravelCompanyDatabaseImplement.Models.Travel", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<double>("Price")
.HasColumnType("float");
b.Property<string>("TravelName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.HasKey("Id");
b.ToTable("Travels");
});
modelBuilder.Entity("TravelCompanyDatabaseImplement.Models.TravelCondition", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<int>("ConditionId")
.HasColumnType("int");
b.Property<int>("Count")
.HasColumnType("int");
b.Property<int>("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
}
}
}

View File

@ -0,0 +1,37 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace TravelCompanyDatabaseImplement.Migrations
{
/// <inheritdoc />
public partial class fixDateImplementNN : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AlterColumn<DateTime>(
name: "DateImplement",
table: "Orders",
type: "datetime2",
nullable: true,
oldClrType: typeof(DateTime),
oldType: "datetime2");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.AlterColumn<DateTime>(
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);
}
}
}

View File

@ -0,0 +1,166 @@
// <auto-generated />
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<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<string>("ConditionName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<double>("Cost")
.HasColumnType("float");
b.HasKey("Id");
b.ToTable("Conditions");
});
modelBuilder.Entity("TravelCompanyDatabaseImplement.Models.Order", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<int>("Count")
.HasColumnType("int");
b.Property<DateTime>("DateCreate")
.HasColumnType("datetime2");
b.Property<DateTime?>("DateImplement")
.HasColumnType("datetime2");
b.Property<int>("Status")
.HasColumnType("int");
b.Property<double>("Sum")
.HasColumnType("float");
b.Property<int>("TravelId")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("TravelId");
b.ToTable("Orders");
});
modelBuilder.Entity("TravelCompanyDatabaseImplement.Models.Travel", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<double>("Price")
.HasColumnType("float");
b.Property<string>("TravelName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.HasKey("Id");
b.ToTable("Travels");
});
modelBuilder.Entity("TravelCompanyDatabaseImplement.Models.TravelCondition", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<int>("ConditionId")
.HasColumnType("int");
b.Property<int>("Count")
.HasColumnType("int");
b.Property<int>("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
}
}
}

View File

@ -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<TravelCondition> 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
};
}
}

View File

@ -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
};
}
}

View File

@ -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<int, (IConditionModel, int)>? _travelConditions = null;
[NotMapped]
public virtual List<Order> Orders { get; set; } = new();
public Dictionary<int, (IConditionModel, int)> 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<TravelCondition> 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;
}
}
}

View File

@ -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();
}
}

View File

@ -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<Condition> Conditions { set; get; }
public virtual DbSet<Travel> Travels { set; get; }
public virtual DbSet<TravelCondition> TravelConditions { set; get; }
public virtual DbSet<Order> Orders { set; get; }
}
}

View File

@ -0,0 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.3">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\TravelCompanyContracts\TravelCompanyContracts.csproj" />
<ProjectReference Include="..\TravelCompanyDataModels\TravelCompanyDataModels.csproj" />
</ItemGroup>
</Project>