не рабоатает

This commit is contained in:
Казначеева Елизавета 2024-03-26 15:23:03 +04:00
parent d1861a1eb3
commit 9a7864ae4e
14 changed files with 1061 additions and 1 deletions

View File

@ -13,7 +13,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SoftwareInstallationBusines
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SoftwareInstallationListImplement", "SoftwareInstallationListImplement\SoftwareInstallationListImplement.csproj", "{A49F0730-79D5-41CC-8A5C-CCAE4CD75DCF}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SoftwareInstallationFileImplement", "SoftwareInstallationFileImplement\SoftwareInstallationFileImplement.csproj", "{E6BB4FD2-3153-47CB-8791-1FF51EA9C491}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SoftwareInstallationFileImplement", "SoftwareInstallationFileImplement\SoftwareInstallationFileImplement.csproj", "{E6BB4FD2-3153-47CB-8791-1FF51EA9C491}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SoftwareInstallationDatabaseImplement", "SoftwareInstallationDatabaseImplement\SoftwareInstallationDatabaseImplement.csproj", "{B6A9B215-62E7-4FC4-9D1D-E4FAE1AA828B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -45,6 +47,10 @@ Global
{E6BB4FD2-3153-47CB-8791-1FF51EA9C491}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E6BB4FD2-3153-47CB-8791-1FF51EA9C491}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E6BB4FD2-3153-47CB-8791-1FF51EA9C491}.Release|Any CPU.Build.0 = Release|Any CPU
{B6A9B215-62E7-4FC4-9D1D-E4FAE1AA828B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B6A9B215-62E7-4FC4-9D1D-E4FAE1AA828B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B6A9B215-62E7-4FC4-9D1D-E4FAE1AA828B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B6A9B215-62E7-4FC4-9D1D-E4FAE1AA828B}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

View File

@ -0,0 +1,56 @@
using SoftwareInstallationContracts.BindingModels;
using SoftwareInstallationContracts.ViewModels;
using SoftwareInstallationDataModels.Models;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
namespace SoftwareInstallationDatabaseImplement
{
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<PackageComponent> PackageComponents { 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,85 @@
using SoftwareInstallationContracts.BindingModels;
using SoftwareInstallationContracts.SearchModels;
using SoftwareInstallationContracts.StoragesContracts;
using SoftwareInstallationContracts.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SoftwareInstallationDatabaseImplement
{
public class ComponentStorage : IComponentStorage
{
public List<ComponentViewModel> GetFullList()
{
using var context = new SoftwareInstallationDatabase();
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 SoftwareInstallationDatabase();
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 SoftwareInstallationDatabase();
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 SoftwareInstallationDatabase();
context.Components.Add(newComponent);
context.SaveChanges();
return newComponent.GetViewModel;
}
public ComponentViewModel? Update(ComponentBindingModel model)
{
using var context = new SoftwareInstallationDatabase();
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 SoftwareInstallationDatabase();
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,171 @@
// <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 SoftwareInstallationDatabaseImplement;
#nullable disable
namespace SoftwareInstallationDatabaseImplement.Migrations
{
[DbContext(typeof(SoftwareInstallationDatabase))]
[Migration("20240326092327_InitialCreate")]
partial class InitialCreate
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "8.0.3")
.HasAnnotation("Relational:MaxIdentifierLength", 128);
SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);
modelBuilder.Entity("SoftwareInstallationDatabaseImplement.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("SoftwareInstallationDatabaseImplement.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>("PackageId")
.HasColumnType("int");
b.Property<int>("Status")
.HasColumnType("int");
b.Property<double>("Sum")
.HasColumnType("float");
b.HasKey("Id");
b.HasIndex("PackageId");
b.ToTable("Orders");
});
modelBuilder.Entity("SoftwareInstallationDatabaseImplement.Package", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<string>("PackageName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<double>("Price")
.HasColumnType("float");
b.HasKey("Id");
b.ToTable("Packages");
});
modelBuilder.Entity("SoftwareInstallationDatabaseImplement.PackageComponent", 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>("PackageId")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("ComponentId");
b.HasIndex("PackageId");
b.ToTable("PackageComponents");
});
modelBuilder.Entity("SoftwareInstallationDatabaseImplement.Order", b =>
{
b.HasOne("SoftwareInstallationDatabaseImplement.Package", "Package")
.WithMany("Orders")
.HasForeignKey("PackageId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Package");
});
modelBuilder.Entity("SoftwareInstallationDatabaseImplement.PackageComponent", b =>
{
b.HasOne("SoftwareInstallationDatabaseImplement.Component", "Component")
.WithMany("PackageComponents")
.HasForeignKey("ComponentId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("SoftwareInstallationDatabaseImplement.Package", "Package")
.WithMany("Components")
.HasForeignKey("PackageId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Component");
b.Navigation("Package");
});
modelBuilder.Entity("SoftwareInstallationDatabaseImplement.Component", b =>
{
b.Navigation("PackageComponents");
});
modelBuilder.Entity("SoftwareInstallationDatabaseImplement.Package", 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 SoftwareInstallationDatabaseImplement.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: "Packages",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
PackageName = table.Column<string>(type: "nvarchar(max)", nullable: false),
Price = table.Column<double>(type: "float", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Packages", x => x.Id);
});
migrationBuilder.CreateTable(
name: "Orders",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
PackageId = 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: true)
},
constraints: table =>
{
table.PrimaryKey("PK_Orders", x => x.Id);
table.ForeignKey(
name: "FK_Orders_Packages_PackageId",
column: x => x.PackageId,
principalTable: "Packages",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
name: "PackageComponents",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
PackageId = 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_PackageComponents", x => x.Id);
table.ForeignKey(
name: "FK_PackageComponents_Components_ComponentId",
column: x => x.ComponentId,
principalTable: "Components",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "FK_PackageComponents_Packages_PackageId",
column: x => x.PackageId,
principalTable: "Packages",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateIndex(
name: "IX_Orders_PackageId",
table: "Orders",
column: "PackageId");
migrationBuilder.CreateIndex(
name: "IX_PackageComponents_ComponentId",
table: "PackageComponents",
column: "ComponentId");
migrationBuilder.CreateIndex(
name: "IX_PackageComponents_PackageId",
table: "PackageComponents",
column: "PackageId");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Orders");
migrationBuilder.DropTable(
name: "PackageComponents");
migrationBuilder.DropTable(
name: "Components");
migrationBuilder.DropTable(
name: "Packages");
}
}
}

View File

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

View File

@ -0,0 +1,76 @@
using SoftwareInstallationContracts.BindingModels;
using SoftwareInstallationContracts.ViewModels;
using SoftwareInstallationDataModels.Enums;
using SoftwareInstallationDataModels.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SoftwareInstallationDatabaseImplement
{
public class Order : IOrderModel
{
public int Id { get; private set; }
public int PackageId { get; private set; }
[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 Package Package { get; set; }
public static Order? Create(OrderBindingModel? model)
{
if (model == null)
{
return null;
}
return new Order()
{
Id = model.Id,
PackageId = model.PackageId,
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;
DateImplement = model.DateImplement;
}
public OrderViewModel GetViewModel => new()
{
Id = Id,
PackageId = PackageId,
Count = Count,
Sum = Sum,
Status = Status,
DateCreate = DateCreate,
DateImplement = DateImplement
};
}
}

View File

@ -0,0 +1,82 @@
using Microsoft.EntityFrameworkCore;
using SoftwareInstallationContracts.BindingModels;
using SoftwareInstallationContracts.SearchModels;
using SoftwareInstallationContracts.StoragesContracts;
using SoftwareInstallationContracts.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SoftwareInstallationDatabaseImplement
{
public class OrderStorage : IOrderStorage
{
public OrderViewModel? Delete(OrderBindingModel model)
{
using var context = new SoftwareInstallationDatabase();
var element = context.Orders.FirstOrDefault(x => x.Id == model.Id);
if (element != null)
{
context.Orders.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
public OrderViewModel? GetElement(OrderSearchModel model)
{
using var context = new SoftwareInstallationDatabase();
if (!model.Id.HasValue)
{
return null;
}
return context.Orders
.Include(x => x.Package)
.FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id)?.GetViewModel;
}
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
{
var result = GetElement(model);
return result != null ? new() { result } : new();
}
public List<OrderViewModel> GetFullList()
{
using var context = new SoftwareInstallationDatabase();
return context.Orders
.Include(x => x.Package)
.Select(x => x.GetViewModel)
.ToList();
}
public OrderViewModel? Insert(OrderBindingModel model)
{
var newOrder = Order.Create(model);
if (newOrder == null)
{
return null;
}
using var context = new SoftwareInstallationDatabase();
context.Orders.Add(newOrder);
context.SaveChanges();
return newOrder.GetViewModel;
}
public OrderViewModel? Update(OrderBindingModel model)
{
using var context = new SoftwareInstallationDatabase();
var order = context.Orders.Include(x => x.Package).FirstOrDefault(x => x.Id == model.Id);
if (order == null)
{
return null;
}
order.Update(model);
context.SaveChanges();
return order.GetViewModel;
}
}
}

View File

@ -0,0 +1,106 @@
using SoftwareInstallationContracts.BindingModels;
using SoftwareInstallationContracts.ViewModels;
using SoftwareInstallationDataModels.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 SoftwareInstallationDatabaseImplement
{
public class Package : IPackageModel
{
public int Id { get; set; }
[Required]
public string PackageName { get; set; } = string.Empty;
[Required]
public double Price { get; set; }
private Dictionary<int, (IComponentModel, int)>? _packageComponents = null;
[NotMapped]
public Dictionary<int, (IComponentModel, int)> PackageComponents
{
get
{
if (_packageComponents == null)
{
_packageComponents = Components.ToDictionary(recPC => recPC.ComponentId, recPC => (recPC.Component as IComponentModel, recPC.Count));
}
return _packageComponents;
}
}
[ForeignKey("PackageId")]
public virtual List<PackageComponent> Components { get; set; } = new();
[ForeignKey("PackageId")]
public virtual List<Order> Orders { get; set; } = new();
public static Package Create(SoftwareInstallationDatabase context, PackageBindingModel model)
{
return new Package()
{
Id = model.Id,
PackageName = model.PackageName,
Price = model.Price,
Components = model.PackageComponents.Select(x => new PackageComponent
{
Component = context.Components.First(y => y.Id == x.Key),
Count = x.Value.Item2
}).ToList()
};
}
public void Update(PackageBindingModel model)
{
PackageName = model.PackageName;
Price = model.Price;
}
public PackageViewModel GetViewModel => new()
{
Id = Id,
PackageName = PackageName,
Price = Price,
PackageComponents = PackageComponents
};
public void UpdateComponents(SoftwareInstallationDatabase context, PackageBindingModel model)
{
var packageComponents = context.PackageComponents.Where(rec => rec.PackageId == model.Id).ToList();
if (packageComponents != null && packageComponents.Count > 0)
{ // удалили те, которых нет в модели
context.PackageComponents.RemoveRange(packageComponents.Where(rec => !model.PackageComponents.ContainsKey(rec.ComponentId)));
context.SaveChanges();
// обновили количество у существующих записей
foreach (var updateComponent in packageComponents)
{
updateComponent.Count = model.PackageComponents[updateComponent.ComponentId].Item2;
model.PackageComponents.Remove(updateComponent.ComponentId);
}
context.SaveChanges();
}
var package = context.Packages.First(x => x.Id == Id);
foreach (var pc in model.PackageComponents)
{
context.PackageComponents.Add(new PackageComponent
{
Package = package,
Component = context.Components.First(x => x.Id == pc.Key),
Count = pc.Value.Item2
});
context.SaveChanges();
}
_packageComponents = 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 SoftwareInstallationDatabaseImplement
{
public class PackageComponent
{
public int Id { get; set; }
[Required]
public int PackageId { get; set; }
[Required]
public int ComponentId { get; set; }
[Required]
public int Count { get; set; }
public virtual Component Component { get; set; } = new();
public virtual Package Package { get; set; } = new();
}
}

View File

@ -0,0 +1,109 @@
using Microsoft.EntityFrameworkCore;
using SoftwareInstallationContracts.BindingModels;
using SoftwareInstallationContracts.SearchModels;
using SoftwareInstallationContracts.StoragesContracts;
using SoftwareInstallationContracts.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SoftwareInstallationDatabaseImplement
{
public class PackageStorage : IPackageStorage
{
public List<PackageViewModel> GetFullList()
{
using var context = new SoftwareInstallationDatabase();
return context.Packages
.Include(x => x.Components)
.ThenInclude(x => x.Component)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public List<PackageViewModel> GetFilteredList(PackageSearchModel model)
{
if (string.IsNullOrEmpty(model.PackageName))
{
return new();
}
using var context = new SoftwareInstallationDatabase();
return context.Packages
.Include(x => x.Components)
.ThenInclude(x => x.Component)
.Where(x => x.PackageName.Contains(model.PackageName))
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public PackageViewModel? GetElement(PackageSearchModel model)
{
if (string.IsNullOrEmpty(model.PackageName) &&
!model.Id.HasValue)
{
return null;
}
using var context = new SoftwareInstallationDatabase();
return context.Packages
.Include(x => x.Components)
.ThenInclude(x => x.Component)
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.PackageName) &&
x.PackageName == model.PackageName) ||
(model.Id.HasValue && x.Id ==
model.Id))
?.GetViewModel;
}
public PackageViewModel? Insert(PackageBindingModel model)
{
using var context = new SoftwareInstallationDatabase();
var newPackage = Package.Create(context, model);
if (newPackage == null)
{
return null;
}
context.Packages.Add(newPackage);
context.SaveChanges();
return newPackage.GetViewModel;
}
public PackageViewModel? Update(PackageBindingModel model)
{
using var context = new SoftwareInstallationDatabase();
using var transaction = context.Database.BeginTransaction();
try
{
var Package = context.Packages.FirstOrDefault(rec =>
rec.Id == model.Id);
if (Package == null)
{
return null;
}
Package.Update(model);
context.SaveChanges();
Package.UpdateComponents(context, model);
transaction.Commit();
return Package.GetViewModel;
}
catch
{
transaction.Rollback();
throw;
}
}
public PackageViewModel? Delete(PackageBindingModel model)
{
using var context = new SoftwareInstallationDatabase();
var element = context.Packages
.Include(x => x.Components)
.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Packages.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}

View File

@ -0,0 +1,26 @@
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SoftwareInstallationDatabaseImplement
{
public class SoftwareInstallationDatabase : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder
optionsBuilder)
{
if (optionsBuilder.IsConfigured == false)
{
optionsBuilder.UseSqlServer(@"Data Source=WIN-45522256GVD;Initial Catalog=SoftwareInstallationDatabase;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True");
}
base.OnConfiguring(optionsBuilder);
}
public virtual DbSet<Component> Components { set; get; }
public virtual DbSet<Package> Packages { set; get; }
public virtual DbSet<PackageComponent> PackageComponents { set; get; }
public virtual DbSet<Order> Orders { set; get; }
}
}

View File

@ -0,0 +1,23 @@
<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.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.3">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\SoftwareInstallationContracts\SoftwareInstallationContracts.csproj" />
<ProjectReference Include="..\SoftwareInstallationDataModels\SoftwareInstallationDataModels.csproj" />
</ItemGroup>
</Project>

View File

@ -9,6 +9,10 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.3">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.0" />
<PackageReference Include="NLog.Extensions.Logging" Version="5.3.8" />
</ItemGroup>
@ -16,6 +20,7 @@
<ItemGroup>
<ProjectReference Include="..\SoftwareInstallationBusinessLogic\SoftwareInstallationBusinessLogic.csproj" />
<ProjectReference Include="..\SoftwareInstallationContracts\SoftwareInstallationContracts.csproj" />
<ProjectReference Include="..\SoftwareInstallationDatabaseImplement\SoftwareInstallationDatabaseImplement.csproj" />
<ProjectReference Include="..\SoftwareInstallationFileImplement\SoftwareInstallationFileImplement.csproj" />
<ProjectReference Include="..\SoftwareInstallationListImplement\SoftwareInstallationListImplement.csproj" />
</ItemGroup>