PIbd-23. Sergunov M.A. LabWork03_base #3

Closed
Maksim-S wants to merge 2 commits from LabWork03_base into LabWork02_base
16 changed files with 1066 additions and 9 deletions

View File

@ -13,7 +13,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GiftShopListImplement", "Gi
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GiftShopBusinessLogic", "GiftShopBusinessLogic\GiftShopBusinessLogic.csproj", "{07C14020-5905-4CCC-9DAC-53507C8F28F8}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GiftShopFileImplement", "GiftShopFileImplement\GiftShopFileImplement.csproj", "{05AE2E86-5464-4B1F-BE91-60FA43CE69F1}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GiftShopFileImplement", "GiftShopFileImplement\GiftShopFileImplement.csproj", "{05AE2E86-5464-4B1F-BE91-60FA43CE69F1}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GiftShopDatabaseImplement", "GiftShopDatabaseImplement\GiftShopDatabaseImplement.csproj", "{50B7E5D7-A58E-4E36-8A5C-F7E78BB526B0}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -45,6 +47,10 @@ Global
{05AE2E86-5464-4B1F-BE91-60FA43CE69F1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{05AE2E86-5464-4B1F-BE91-60FA43CE69F1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{05AE2E86-5464-4B1F-BE91-60FA43CE69F1}.Release|Any CPU.Build.0 = Release|Any CPU
{50B7E5D7-A58E-4E36-8A5C-F7E78BB526B0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{50B7E5D7-A58E-4E36-8A5C-F7E78BB526B0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{50B7E5D7-A58E-4E36-8A5C-F7E78BB526B0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{50B7E5D7-A58E-4E36-8A5C-F7E78BB526B0}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

View File

@ -0,0 +1,25 @@
using GiftShopDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
namespace GiftShopDatabaseImplement
{
public class GiftShopDatabase : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (optionsBuilder.IsConfigured == false)
{
optionsBuilder.UseSqlServer(@"Data Source=LAPTOP-43DJMKMK\SQLEXPRESS;Initial Catalog=GiftShopDatabase;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True");
}
base.OnConfiguring(optionsBuilder);
}
public virtual DbSet<Component> Components { set; get; }
public virtual DbSet<Gift> Gifts { set; get; }
public virtual DbSet<GiftComponent> GiftComponents { 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.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.4">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\GiftShopContracts\GiftShopContracts.csproj" />
<ProjectReference Include="..\GiftShopDataModels\GiftShopDataModels.csproj" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,85 @@
using GiftShopContracts.BindingModels;
using GiftShopContracts.SearchModels;
using GiftShopContracts.StoragesContracts;
using GiftShopContracts.ViewModels;
using GiftShopDatabaseImplement.Models;
namespace GiftShopDatabaseImplement.Implements
{
public class ComponentStorage : IComponentStorage
{
public List<ComponentViewModel> GetFullList()
{
using var context = new GiftShopDatabase();
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 GiftShopDatabase();
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 GiftShopDatabase();
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 GiftShopDatabase();
context.Components.Add(newComponent);
context.SaveChanges();
return newComponent.GetViewModel;
}
public ComponentViewModel? Update(ComponentBindingModel model)
{
using var context = new GiftShopDatabase();
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 GiftShopDatabase();
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,107 @@
using GiftShopContracts.BindingModels;
using GiftShopContracts.SearchModels;
using GiftShopContracts.StoragesContracts;
using GiftShopContracts.ViewModels;
using GiftShopDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
namespace GiftShopDatabaseImplement.Implements
{
public class GiftStorage : IGiftStorage
{
public List<GiftViewModel> GetFullList()
{
using var context = new GiftShopDatabase();
return context.Gifts
.Include(x => x.Components)
.ThenInclude(x => x.Component)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public List<GiftViewModel> GetFilteredList(GiftSearchModel model)
{
if (string.IsNullOrEmpty(model.GiftName))
{
return new();
}
using var context = new GiftShopDatabase();
return context.Gifts
.Include(x => x.Components)
.ThenInclude(x => x.Component)
.Where(x => x.GiftName.Contains(model.GiftName))
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public GiftViewModel? GetElement(GiftSearchModel model)
{
if (string.IsNullOrEmpty(model.GiftName) &&
!model.Id.HasValue)
{
return null;
}
using var context = new GiftShopDatabase();
return context.Gifts
.Include(x => x.Components)
.ThenInclude(x => x.Component)
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.GiftName) &&
x.GiftName == model.GiftName) || (model.Id.HasValue && x.Id == model.Id))
?.GetViewModel;
}
public GiftViewModel? Insert(GiftBindingModel model)
{
using var context = new GiftShopDatabase();
var newGift = Gift.Create(context, model);
if (newGift == null)
{
return null;
}
context.Gifts.Add(newGift);
context.SaveChanges();
return newGift.GetViewModel;
}
public GiftViewModel? Update(GiftBindingModel model)
{
using var context = new GiftShopDatabase();
using var transaction = context.Database.BeginTransaction();
try
{
var gift = context.Gifts.FirstOrDefault(rec => rec.Id == model.Id);
if (gift == null)
{
return null;
}
gift.Update(model);
context.SaveChanges();
gift.UpdateComponents(context, model);
transaction.Commit();
return gift.GetViewModel;
}
catch
{
transaction.Rollback();
throw;
}
}
public GiftViewModel? Delete(GiftBindingModel model)
{
using var context = new GiftShopDatabase();
var element = context.Gifts
.Include(x => x.Components)
.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Gifts.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}

View File

@ -0,0 +1,76 @@
using GiftShopContracts.BindingModels;
using GiftShopContracts.SearchModels;
using GiftShopContracts.StoragesContracts;
using GiftShopContracts.ViewModels;
using GiftShopDatabaseImplement.Models;
namespace GiftShopDatabaseImplement.Implements
{
public class OrderStorage : IOrderStorage
{
public OrderViewModel? Delete(OrderBindingModel model)
{
using var context = new GiftShopDatabase();
var element = context.Orders.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Orders.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
public OrderViewModel? GetElement(OrderSearchModel model)
{
if (!model.Id.HasValue)
{
return null;
}
using var context = new GiftShopDatabase();
return context.Orders.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
}
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
{
if (!model.Id.HasValue)
{
return new();
}
using var context = new GiftShopDatabase();
return context.Orders.Where(x => x.Id == model.Id).Select(x => x.GetViewModel).ToList();
}
public List<OrderViewModel> GetFullList()
{
using var context = new GiftShopDatabase();
return context.Orders.Select(x => x.GetViewModel).ToList();
}
public OrderViewModel? Insert(OrderBindingModel model)
{
var newOrder = Order.Create(model);
if (newOrder == null)
{
return null;
}
using var context = new GiftShopDatabase();
context.Orders.Add(newOrder);
context.SaveChanges();
return newOrder.GetViewModel;
}
public OrderViewModel? Update(OrderBindingModel model)
{
using var context = new GiftShopDatabase();
var order = context.Orders.FirstOrDefault(x => x.Id == model.Id);
if (order == null)
{
return null;
}
order.Update(model);
context.SaveChanges();
return order.GetViewModel;
}
}
}

View File

@ -0,0 +1,175 @@
// <auto-generated />
using System;
using GiftShopDatabaseImplement;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
#nullable disable
namespace GiftShopDatabaseImplement.Migrations
{
[DbContext(typeof(GiftShopDatabase))]
[Migration("20230402200549_InitMigration")]
partial class InitMigration
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "7.0.4")
.HasAnnotation("Relational:MaxIdentifierLength", 128);
SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);
modelBuilder.Entity("GiftShopDatabaseImplement.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("GiftShopDatabaseImplement.Models.Gift", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<string>("GiftName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<double>("Price")
.HasColumnType("float");
b.HasKey("Id");
b.ToTable("Gifts");
});
modelBuilder.Entity("GiftShopDatabaseImplement.Models.GiftComponent", 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>("GiftId")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("ComponentId");
b.HasIndex("GiftId");
b.ToTable("GiftComponents");
});
modelBuilder.Entity("GiftShopDatabaseImplement.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>("GiftId")
.HasColumnType("int");
b.Property<string>("GiftName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<int>("Status")
.HasColumnType("int");
b.Property<double>("Sum")
.HasColumnType("float");
b.HasKey("Id");
b.HasIndex("GiftId");
b.ToTable("Orders");
});
modelBuilder.Entity("GiftShopDatabaseImplement.Models.GiftComponent", b =>
{
b.HasOne("GiftShopDatabaseImplement.Models.Component", "Component")
.WithMany("GiftComponents")
.HasForeignKey("ComponentId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("GiftShopDatabaseImplement.Models.Gift", "Gift")
.WithMany("Components")
.HasForeignKey("GiftId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Component");
b.Navigation("Gift");
});
modelBuilder.Entity("GiftShopDatabaseImplement.Models.Order", b =>
{
b.HasOne("GiftShopDatabaseImplement.Models.Gift", "Gift")
.WithMany("Orders")
.HasForeignKey("GiftId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Gift");
});
modelBuilder.Entity("GiftShopDatabaseImplement.Models.Component", b =>
{
b.Navigation("GiftComponents");
});
modelBuilder.Entity("GiftShopDatabaseImplement.Models.Gift", b =>
{
b.Navigation("Components");
b.Navigation("Orders");
});
#pragma warning restore 612, 618
}
}
}

View File

@ -0,0 +1,125 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace GiftShopDatabaseImplement.Migrations
{
/// <inheritdoc />
public partial class InitMigration : 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: "Gifts",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
GiftName = table.Column<string>(type: "nvarchar(max)", nullable: false),
Price = table.Column<double>(type: "float", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Gifts", x => x.Id);
});
migrationBuilder.CreateTable(
name: "GiftComponents",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
GiftId = 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_GiftComponents", x => x.Id);
table.ForeignKey(
name: "FK_GiftComponents_Components_ComponentId",
column: x => x.ComponentId,
principalTable: "Components",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "FK_GiftComponents_Gifts_GiftId",
column: x => x.GiftId,
principalTable: "Gifts",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
name: "Orders",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
GiftId = table.Column<int>(type: "int", nullable: false),
GiftName = table.Column<string>(type: "nvarchar(max)", 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: true)
},
constraints: table =>
{
table.PrimaryKey("PK_Orders", x => x.Id);
table.ForeignKey(
name: "FK_Orders_Gifts_GiftId",
column: x => x.GiftId,
principalTable: "Gifts",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateIndex(
name: "IX_GiftComponents_ComponentId",
table: "GiftComponents",
column: "ComponentId");
migrationBuilder.CreateIndex(
name: "IX_GiftComponents_GiftId",
table: "GiftComponents",
column: "GiftId");
migrationBuilder.CreateIndex(
name: "IX_Orders_GiftId",
table: "Orders",
column: "GiftId");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "GiftComponents");
migrationBuilder.DropTable(
name: "Orders");
migrationBuilder.DropTable(
name: "Components");
migrationBuilder.DropTable(
name: "Gifts");
}
}
}

View File

@ -0,0 +1,172 @@
// <auto-generated />
using System;
using GiftShopDatabaseImplement;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
#nullable disable
namespace GiftShopDatabaseImplement.Migrations
{
[DbContext(typeof(GiftShopDatabase))]
partial class GiftShopDatabaseModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "7.0.4")
.HasAnnotation("Relational:MaxIdentifierLength", 128);
SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);
modelBuilder.Entity("GiftShopDatabaseImplement.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("GiftShopDatabaseImplement.Models.Gift", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<string>("GiftName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<double>("Price")
.HasColumnType("float");
b.HasKey("Id");
b.ToTable("Gifts");
});
modelBuilder.Entity("GiftShopDatabaseImplement.Models.GiftComponent", 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>("GiftId")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("ComponentId");
b.HasIndex("GiftId");
b.ToTable("GiftComponents");
});
modelBuilder.Entity("GiftShopDatabaseImplement.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>("GiftId")
.HasColumnType("int");
b.Property<string>("GiftName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<int>("Status")
.HasColumnType("int");
b.Property<double>("Sum")
.HasColumnType("float");
b.HasKey("Id");
b.HasIndex("GiftId");
b.ToTable("Orders");
});
modelBuilder.Entity("GiftShopDatabaseImplement.Models.GiftComponent", b =>
{
b.HasOne("GiftShopDatabaseImplement.Models.Component", "Component")
.WithMany("GiftComponents")
.HasForeignKey("ComponentId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("GiftShopDatabaseImplement.Models.Gift", "Gift")
.WithMany("Components")
.HasForeignKey("GiftId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Component");
b.Navigation("Gift");
});
modelBuilder.Entity("GiftShopDatabaseImplement.Models.Order", b =>
{
b.HasOne("GiftShopDatabaseImplement.Models.Gift", "Gift")
.WithMany("Orders")
.HasForeignKey("GiftId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Gift");
});
modelBuilder.Entity("GiftShopDatabaseImplement.Models.Component", b =>
{
b.Navigation("GiftComponents");
});
modelBuilder.Entity("GiftShopDatabaseImplement.Models.Gift", b =>
{
b.Navigation("Components");
b.Navigation("Orders");
});
#pragma warning restore 612, 618
}
}
}

View File

@ -0,0 +1,63 @@
using GiftShopDataModels.Models;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using GiftShopContracts.ViewModels;
using GiftShopContracts.BindingModels;
namespace GiftShopDatabaseImplement.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<GiftComponent> GiftComponents { 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,99 @@
using GiftShopDataModels.Models;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using GiftShopContracts.BindingModels;
using GiftShopContracts.ViewModels;
namespace GiftShopDatabaseImplement.Models
{
public class Gift : IGiftModel
{
public int Id { get; set; }
[Required]
public string GiftName { get; set; } = string.Empty;
[Required]
public double Price { get; set; }
private Dictionary<int, (IComponentModel, int)>? _giftComponents = null;
[NotMapped]
public Dictionary<int, (IComponentModel, int)> GiftComponents
{
get
{
if (_giftComponents == null)
{
_giftComponents = Components.ToDictionary(recPC => recPC.ComponentId, recPC =>
(recPC.Component as IComponentModel, recPC.Count));
}
return _giftComponents;
}
}
[ForeignKey("GiftId")]
public virtual List<GiftComponent> Components { get; set; } = new();
[ForeignKey("GiftId")]
public virtual List<Order> Orders { get; set; } = new();
public static Gift Create(GiftShopDatabase context, GiftBindingModel model)
{
return new Gift()
{
Id = model.Id,
GiftName = model.GiftName,
Price = model.Price,
Components = model.GiftComponents.Select(x => new GiftComponent
{
Component = context.Components.First(y => y.Id == x.Key), Count = x.Value.Item2
}).ToList()
};
}
public void Update(GiftBindingModel model)
{
GiftName = model.GiftName;
Price = model.Price;
}
public GiftViewModel GetViewModel => new()
{
Id = Id,
GiftName = GiftName,
Price = Price,
GiftComponents = GiftComponents
};
public void UpdateComponents(GiftShopDatabase context, GiftBindingModel model)
{
var giftComponents = context.GiftComponents.Where(rec => rec.GiftId == model.Id).ToList();
if (giftComponents != null && giftComponents.Count > 0)
{ // удалили те, которых нет в модели
context.GiftComponents.RemoveRange(giftComponents.Where
(rec => !model.GiftComponents.ContainsKey(rec.ComponentId)));
context.SaveChanges();
// обновили количество у существующих записей
foreach (var updateComponent in giftComponents)
{
updateComponent.Count = model.GiftComponents[updateComponent.ComponentId].Item2;
model.GiftComponents.Remove(updateComponent.ComponentId);
}
context.SaveChanges();
}
var gift = context.Gifts.First(x => x.Id == Id);
foreach (var pc in model.GiftComponents)
{
context.GiftComponents.Add(new GiftComponent
{
Gift = gift,
Component = context.Components.First(x => x.Id == pc.Key),
Count = pc.Value.Item2
});
context.SaveChanges();
}
_giftComponents = null;
}
}
}

View File

@ -0,0 +1,22 @@
using System.ComponentModel.DataAnnotations;
namespace GiftShopDatabaseImplement.Models
{
public class GiftComponent
{
public int Id { get; set; }
[Required]
public int GiftId { get; set; }
[Required]
public int ComponentId { get; set; }
[Required]
public int Count { get; set; }
public virtual Component Component { get; set; } = new();
public virtual Gift Gift { get; set; } = new();
}
}

View File

@ -0,0 +1,81 @@
using GiftShopContracts.BindingModels;
using GiftShopContracts.ViewModels;
using GiftShopDataModels.Enums;
using GiftShopDataModels.Models;
using System.ComponentModel.DataAnnotations;
namespace GiftShopDatabaseImplement.Models
{
public class Order : IOrderModel
{
public int Id { get; private set; }
public int GiftId { get; private set; }
public string GiftName { get; private set; } = string.Empty;
Review

Вычисляемое поле, не хранится в памяти

Вычисляемое поле, не хранится в памяти
[Required]
public int Count { get; private set; }
[Required]
public double Sum { get; private set; }
[Required]
public OrderStatus Status { get; private set; } = OrderStatus.Неизвестен;
[Required]
public DateTime DateCreate { get; private set; } = DateTime.Now;
public DateTime? DateImplement { get; private set; }
public virtual Gift Gift { get; set; }
public static Order? Create(OrderBindingModel? model)
{
if (model == null)
{
return null;
}
return new Order()
{
Id = model.Id,
GiftId = model.GiftId,
GiftName = model.GiftName,
Count = model.Count,
Sum = model.Sum,
Status = model.Status,
DateCreate = model.DateCreate,
DateImplement = model.DateImplement
};
}
public void Update(OrderBindingModel? model)
{
if (model == null)
{
return;
}
GiftId = model.GiftId;
Review

Не требуется обновлять все данные, только статус и дату выполнения

Не требуется обновлять все данные, только статус и дату выполнения
GiftName = model.GiftName;
Count = model.Count;
Sum = model.Sum;
Status = model.Status;
DateCreate = model.DateCreate;
DateImplement = model.DateImplement;
}
public OrderViewModel GetViewModel => new()
{
Id = Id,
GiftId = GiftId,
GiftName = GiftName,
Count = Count,
Sum = Sum,
Status = Status,
DateCreate = DateCreate,
DateImplement = DateImplement
};
}
}

View File

@ -10,8 +10,6 @@ namespace GiftShopFileImplement.Models
{
public int GiftId { get; private set; }
public string GiftName { get; private set; }
public int Count { get; private set; }
public double Sum { get; private set; }
@ -34,7 +32,6 @@ namespace GiftShopFileImplement.Models
{
Id = model.Id,
GiftId = model.GiftId,
GiftName = model.GiftName,
Count = model.Count,
Sum = model.Sum,
Status = model.Status,
@ -53,7 +50,6 @@ namespace GiftShopFileImplement.Models
{
Id = Convert.ToInt32(element.Attribute("Id")!.Value),
GiftId = Convert.ToInt32(element.Element("GiftId")!.Value),
GiftName = element.Element("GiftName")!.Value,
Count = Convert.ToInt32(element.Element("Count")!.Value),
Sum = Convert.ToDouble(element.Element("Sum")!.Value),
Status = (OrderStatus)Enum.Parse(typeof(OrderStatus), element.Element("Status")!.Value),
@ -73,7 +69,6 @@ namespace GiftShopFileImplement.Models
return;
}
GiftId = model.GiftId;
GiftName = model.GiftName;
Count = model.Count;
Sum = model.Sum;
Status = model.Status;
@ -85,7 +80,6 @@ namespace GiftShopFileImplement.Models
{
Id = Id,
GiftId = GiftId,
GiftName = GiftName,
Count = Count,
Sum = Sum,
Status = Status,
@ -95,7 +89,6 @@ namespace GiftShopFileImplement.Models
public XElement GetXElement => new("Order",
new XAttribute("Id", Id),
new XElement("GiftName", GiftName),
new XElement("GiftId", GiftId.ToString()),
new XElement("Count", Count.ToString()),
new XElement("Sum", Sum.ToString()),

View File

@ -19,6 +19,10 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.4">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Configuration" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="7.0.0" />
@ -28,6 +32,7 @@
<ItemGroup>
<ProjectReference Include="..\GiftShopBusinessLogic\GiftShopBusinessLogic.csproj" />
<ProjectReference Include="..\GiftShopContracts\GiftShopContracts.csproj" />
<ProjectReference Include="..\GiftShopDatabaseImplement\GiftShopDatabaseImplement.csproj" />
<ProjectReference Include="..\GiftShopFileImplement\GiftShopFileImplement.csproj" />
<ProjectReference Include="..\GiftShopListImplement\GiftShopListImplement.csproj" />
</ItemGroup>

View File

@ -1,7 +1,7 @@
using GiftShopBusinessLogic.BusinessLogics;
using GiftShopContracts.BusinessLogicsContracts;
using GiftShopContracts.StoragesContracts;
using GiftShopFileImplement.Implements;
using GiftShopDatabaseImplement.Implements;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using NLog.Extensions.Logging;