ПИбд-21 Лобашов Иван 3 лаб простая #3

Closed
goldfest228 wants to merge 2 commits from Lab3 into Lab2
15 changed files with 1082 additions and 3 deletions
Showing only changes of commit 9d2def59f3 - Show all commits

View File

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

View File

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

View File

@ -2,13 +2,17 @@
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net6.0-windows</TargetFramework>
<TargetFramework>net8.0-windows</TargetFramework>
<Nullable>enable</Nullable>
<UseWindowsForms>true</UseWindowsForms>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
@ -22,6 +26,7 @@
<ItemGroup>
<ProjectReference Include="..\TravelCompanyBusinessLogic\TravelCompanyBusinessLogic.csproj" />
<ProjectReference Include="..\TravelCompanyContracts\TravelCompanyContracts.csproj" />
<ProjectReference Include="..\TravelCompanyDatabaseImplement\TravelCompanyDatabaseImplement.csproj" />
<ProjectReference Include="..\TravelCompanyFileImplement\TravelCompanyFileImplement.csproj" />
<ProjectReference Include="..\TravelCompanyListImplement\TravelCompanyListImplement.csproj" />
</ItemGroup>

View File

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

View File

@ -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<OrderViewModel> GetFullList()
{
using var context = new TravelCompanyDataBase();
return context.Orders
.Select(x => AccessTravelStorage(x.GetViewModel))
.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))
.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)
Review

Cущности связаны, так что отдельный запрос для получения названия не требуется

Cущности связаны, так что отдельный запрос для получения названия не требуется
{
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;
}
}
}

View File

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

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("20240308202100_InitialCreate")]
partial class InitialCreate
{
/// <inheritdoc />
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<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<string>("ComponentName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<double>("Cost")
.HasColumnType("float");
b.HasKey("Id");
b.ToTable("Components");
});
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.TravelComponent", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<int>("ComponentId")
.HasColumnType("int");
b.Property<int>("Count")
.HasColumnType("int");
b.Property<int>("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
}
}
}

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: "Components",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
ComponentName = table.Column<string>(type: "nvarchar(max)", nullable: false),
Cost = table.Column<double>(type: "float", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Components", 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"),
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: true),
TravelId = table.Column<int>(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<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
TravelId = table.Column<int>(type: "int", nullable: false),
ComponentId = table.Column<int>(type: "int", nullable: false),
Count = table.Column<int>(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");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Orders");
migrationBuilder.DropTable(
name: "TravelComponents");
migrationBuilder.DropTable(
name: "Components");
migrationBuilder.DropTable(
name: "Travels");
}
}
}

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", "8.0.2")
.HasAnnotation("Relational:MaxIdentifierLength", 128);
SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);
modelBuilder.Entity("TravelCompanyDatabaseImplement.Models.Component", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<string>("ComponentName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<double>("Cost")
.HasColumnType("float");
b.HasKey("Id");
b.ToTable("Components");
});
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.TravelComponent", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<int>("ComponentId")
.HasColumnType("int");
b.Property<int>("Count")
.HasColumnType("int");
b.Property<int>("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)
Review

Связь настроена не до конца

Связь настроена не до конца
.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
}
}
}

View File

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

View File

@ -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<int, (IComponentModel, int)>? _TravelComponents =
null;
[NotMapped]
public Dictionary<int, (IComponentModel, int)> 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<TravelComponent> Components { get; set; } = new();
[ForeignKey("TravelId")]
public virtual List<Order> 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;
}
}
}

View File

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

View File

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

View File

@ -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<Models.Component> Components { set; get; }
public virtual DbSet<Travel> Travels { set; get; }
public virtual DbSet<TravelComponent> TravelComponents { set; get; }
public virtual DbSet<Order> Orders { set; get; }
}
}

View File

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