готово вроде

This commit is contained in:
Kirill 2024-04-10 01:11:33 +04:00
parent 40b82024d2
commit bcabbc17bd
16 changed files with 1060 additions and 12 deletions

View File

@ -13,7 +13,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ClothShopBusinessLogic", "C
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ClothShopListImplement", "ClothShopListImplement\ClothShopListImplement.csproj", "{43F6977E-E37D-4D9C-BF55-6526520A04EB}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ClothShopFileImplement", "ClothShopFileImplement\ClothShopFileImplement.csproj", "{EDA6F63F-1DF0-4075-BDBE-B0B71B76D384}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ClothShopFileImplement", "ClothShopFileImplement\ClothShopFileImplement.csproj", "{EDA6F63F-1DF0-4075-BDBE-B0B71B76D384}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ClothShopDatabaseImplement", "ClothShopDatabaseImplement\ClothShopDatabaseImplement.csproj", "{297D7AB6-9010-4CC6-819D-0A25CD2C9905}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -45,6 +47,10 @@ Global
{EDA6F63F-1DF0-4075-BDBE-B0B71B76D384}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EDA6F63F-1DF0-4075-BDBE-B0B71B76D384}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EDA6F63F-1DF0-4075-BDBE-B0B71B76D384}.Release|Any CPU.Build.0 = Release|Any CPU
{297D7AB6-9010-4CC6-819D-0A25CD2C9905}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{297D7AB6-9010-4CC6-819D-0A25CD2C9905}.Debug|Any CPU.Build.0 = Debug|Any CPU
{297D7AB6-9010-4CC6-819D-0A25CD2C9905}.Release|Any CPU.ActiveCfg = Release|Any CPU
{297D7AB6-9010-4CC6-819D-0A25CD2C9905}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

View File

@ -0,0 +1,22 @@
using ClothShopDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
namespace ClothShopDatabaseImplement
{
public class ClothShopDatabase : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (optionsBuilder.IsConfigured == false)
{
optionsBuilder.UseSqlServer(@"Data Source=firsovpk;Initial Catalog=ClothShopDataBase;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"
);
}
base.OnConfiguring(optionsBuilder);
}
public virtual DbSet<Component> Components { set; get; }
public virtual DbSet<Textile> Textiles { set; get; }
public virtual DbSet<TextileComponent> TextileComponents { set; get; }
public virtual DbSet<Order> Orders { set; get; }
}
}

View File

@ -0,0 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.28" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.28" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.28">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="migr8.npgsql" Version="0.33.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ClothShopContracts\ClothShopContracts.csproj" />
<ProjectReference Include="..\ClothShopDataModels\ClothShopDataModels.csproj" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,90 @@
using ClothShopContracts.BindingModels;
using ClothShopContracts.SearchModels;
using ClothShopContracts.StoragesContracts;
using ClothShopContracts.ViewModels;
using ClothShopDatabaseImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ClothShopDatabaseImplement.Implements
{
public class ComponentStorage : IComponentStorage
{
public List<ComponentViewModel> GetFullList()
{
using var context = new ClothShopDatabase();
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 ClothShopDatabase();
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 ClothShopDatabase();
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 ClothShopDatabase();
context.Components.Add(newComponent);
context.SaveChanges();
return newComponent.GetViewModel;
}
public ComponentViewModel? Update(ComponentBindingModel model)
{
using var context = new ClothShopDatabase();
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 ClothShopDatabase();
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,82 @@
using ClothShopContracts.BindingModels;
using ClothShopContracts.SearchModels;
using ClothShopContracts.StoragesContracts;
using ClothShopContracts.ViewModels;
using ClothShopDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ClothShopDatabaseImplement.Implements
{
public class OrderStorage : IOrderStorage
{
public List<OrderViewModel> GetFullList()
{
using var context = new ClothShopDatabase();
return context.Orders.Include(x => x.Textiles)
.Select(x => x.GetViewModel)
.ToList();
}
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
{
if (!model.Id.HasValue)
{
return new();
}
using var context = new ClothShopDatabase();
return context.Orders.Include(x => x.Textiles)
.Where(x => x.Id == model.Id)
.Select(x => x.GetViewModel)
.ToList();
}
public OrderViewModel? GetElement(OrderSearchModel model)
{
if (!model.Id.HasValue)
{
return null;
}
using var context = new ClothShopDatabase();
return context.Orders.Include(x => x.Textiles).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 ClothShopDatabase();
context.Orders.Add(newOrder);
context.SaveChanges();
return newOrder.GetViewModel;
}
public OrderViewModel? Update(OrderBindingModel model)
{
using var context = new ClothShopDatabase();
var order = context.Orders.Include(x => x.Textiles).FirstOrDefault(x => x.Id == model.Id);
if (order == null)
{
return null;
}
order.Update(model);
context.SaveChanges();
return order.GetViewModel;
}
public OrderViewModel? Delete(OrderBindingModel model)
{
using var context = new ClothShopDatabase();
var element = context.Orders.Include(x => x.Textiles).FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Orders.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}

View File

@ -0,0 +1,111 @@
using ClothShopContracts.BindingModels;
using ClothShopContracts.SearchModels;
using ClothShopContracts.StoragesContracts;
using ClothShopContracts.ViewModels;
using ClothShopDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ClothShopDatabaseImplement.Implements
{
public class TextileStorage : ITextileStorage
{
public List<TextileViewModel> GetFullList()
{
using var context = new ClothShopDatabase();
return context.Textiles
.Include(x => x.Components)
.ThenInclude(x => x.Component)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public List<TextileViewModel> GetFilteredList(TextileSearchModel model)
{
if (string.IsNullOrEmpty(model.TextileName))
{
return new();
}
using var context = new ClothShopDatabase();
return context.Textiles
.Include(x => x.Components)
.ThenInclude(x => x.Component)
.Where(x => x.TextileName.Contains(model.TextileName))
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public TextileViewModel? GetElement(TextileSearchModel model)
{
if (string.IsNullOrEmpty(model.TextileName) &&
!model.Id.HasValue)
{
return null;
}
using var context = new ClothShopDatabase();
return context.Textiles
.Include(x => x.Components)
.ThenInclude(x => x.Component)
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.TextileName) &&
x.TextileName == model.TextileName) ||
(model.Id.HasValue && x.Id ==
model.Id))
?.GetViewModel;
}
public TextileViewModel? Insert(TextileBindingModel model)
{
using var context = new ClothShopDatabase();
var newComputer = Textile.Create(context, model);
if (newComputer == null)
{
return null;
}
context.Textiles.Add(newComputer);
context.SaveChanges();
return newComputer.GetViewModel;
}
public TextileViewModel? Update(TextileBindingModel model)
{
using var context = new ClothShopDatabase();
using var transaction = context.Database.BeginTransaction();
try
{
var Computer = context.Textiles.FirstOrDefault(rec =>
rec.Id == model.Id);
if (Computer == null)
{
return null;
}
Computer.Update(model);
context.SaveChanges();
Computer.UpdateComponents(context, model);
transaction.Commit();
return Computer.GetViewModel;
}
catch
{
transaction.Rollback();
throw;
}
}
public TextileViewModel? Delete(TextileBindingModel model)
{
using var context = new ClothShopDatabase();
var element = context.Textiles
.Include(x => x.Components)
.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Textiles.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}

View File

@ -0,0 +1,170 @@
// <auto-generated />
using System;
using ClothShopDatabaseImplement;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
#nullable disable
namespace ClothShopDatabaseImplement.Migrations
{
[DbContext(typeof(ClothShopDatabase))]
[Migration("20240407194221_InitMigration")]
partial class InitMigration
{
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "6.0.28")
.HasAnnotation("Relational:MaxIdentifierLength", 128);
SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder, 1L, 1);
modelBuilder.Entity("ClothShopDatabaseImplement.Models.Component", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"), 1L, 1);
b.Property<string>("ComponentName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<double>("Cost")
.HasColumnType("float");
b.HasKey("Id");
b.ToTable("Components");
});
modelBuilder.Entity("ClothShopDatabaseImplement.Models.Order", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"), 1L, 1);
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>("TextileId")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("TextileId");
b.ToTable("Orders");
});
modelBuilder.Entity("ClothShopDatabaseImplement.Models.Textile", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"), 1L, 1);
b.Property<double>("Price")
.HasColumnType("float");
b.Property<string>("TextileName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.HasKey("Id");
b.ToTable("Textiles");
});
modelBuilder.Entity("ClothShopDatabaseImplement.Models.TextileComponent", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"), 1L, 1);
b.Property<int>("ComponentId")
.HasColumnType("int");
b.Property<int>("Count")
.HasColumnType("int");
b.Property<int>("TextileId")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("ComponentId");
b.HasIndex("TextileId");
b.ToTable("TextileComponents");
});
modelBuilder.Entity("ClothShopDatabaseImplement.Models.Order", b =>
{
b.HasOne("ClothShopDatabaseImplement.Models.Textile", "Textiles")
.WithMany("Orders")
.HasForeignKey("TextileId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Textiles");
});
modelBuilder.Entity("ClothShopDatabaseImplement.Models.TextileComponent", b =>
{
b.HasOne("ClothShopDatabaseImplement.Models.Component", "Component")
.WithMany("TextileComponents")
.HasForeignKey("ComponentId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("ClothShopDatabaseImplement.Models.Textile", "Textile")
.WithMany("Components")
.HasForeignKey("TextileId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Component");
b.Navigation("Textile");
});
modelBuilder.Entity("ClothShopDatabaseImplement.Models.Component", b =>
{
b.Navigation("TextileComponents");
});
modelBuilder.Entity("ClothShopDatabaseImplement.Models.Textile", b =>
{
b.Navigation("Components");
b.Navigation("Orders");
});
#pragma warning restore 612, 618
}
}
}

View File

@ -0,0 +1,122 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace ClothShopDatabaseImplement.Migrations
{
public partial class InitMigration : Migration
{
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: "Textiles",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
TextileName = table.Column<string>(type: "nvarchar(max)", nullable: false),
Price = table.Column<double>(type: "float", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Textiles", 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),
TextileId = table.Column<int>(type: "int", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Orders", x => x.Id);
table.ForeignKey(
name: "FK_Orders_Textiles_TextileId",
column: x => x.TextileId,
principalTable: "Textiles",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
name: "TextileComponents",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
TextileId = 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_TextileComponents", x => x.Id);
table.ForeignKey(
name: "FK_TextileComponents_Components_ComponentId",
column: x => x.ComponentId,
principalTable: "Components",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "FK_TextileComponents_Textiles_TextileId",
column: x => x.TextileId,
principalTable: "Textiles",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateIndex(
name: "IX_Orders_TextileId",
table: "Orders",
column: "TextileId");
migrationBuilder.CreateIndex(
name: "IX_TextileComponents_ComponentId",
table: "TextileComponents",
column: "ComponentId");
migrationBuilder.CreateIndex(
name: "IX_TextileComponents_TextileId",
table: "TextileComponents",
column: "TextileId");
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Orders");
migrationBuilder.DropTable(
name: "TextileComponents");
migrationBuilder.DropTable(
name: "Components");
migrationBuilder.DropTable(
name: "Textiles");
}
}
}

View File

@ -0,0 +1,168 @@
// <auto-generated />
using System;
using ClothShopDatabaseImplement;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
#nullable disable
namespace ClothShopDatabaseImplement.Migrations
{
[DbContext(typeof(ClothShopDatabase))]
partial class ClothShopDatabaseModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "6.0.28")
.HasAnnotation("Relational:MaxIdentifierLength", 128);
SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder, 1L, 1);
modelBuilder.Entity("ClothShopDatabaseImplement.Models.Component", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"), 1L, 1);
b.Property<string>("ComponentName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<double>("Cost")
.HasColumnType("float");
b.HasKey("Id");
b.ToTable("Components");
});
modelBuilder.Entity("ClothShopDatabaseImplement.Models.Order", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"), 1L, 1);
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>("TextileId")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("TextileId");
b.ToTable("Orders");
});
modelBuilder.Entity("ClothShopDatabaseImplement.Models.Textile", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"), 1L, 1);
b.Property<double>("Price")
.HasColumnType("float");
b.Property<string>("TextileName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.HasKey("Id");
b.ToTable("Textiles");
});
modelBuilder.Entity("ClothShopDatabaseImplement.Models.TextileComponent", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"), 1L, 1);
b.Property<int>("ComponentId")
.HasColumnType("int");
b.Property<int>("Count")
.HasColumnType("int");
b.Property<int>("TextileId")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("ComponentId");
b.HasIndex("TextileId");
b.ToTable("TextileComponents");
});
modelBuilder.Entity("ClothShopDatabaseImplement.Models.Order", b =>
{
b.HasOne("ClothShopDatabaseImplement.Models.Textile", "Textiles")
.WithMany("Orders")
.HasForeignKey("TextileId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Textiles");
});
modelBuilder.Entity("ClothShopDatabaseImplement.Models.TextileComponent", b =>
{
b.HasOne("ClothShopDatabaseImplement.Models.Component", "Component")
.WithMany("TextileComponents")
.HasForeignKey("ComponentId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("ClothShopDatabaseImplement.Models.Textile", "Textile")
.WithMany("Components")
.HasForeignKey("TextileId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Component");
b.Navigation("Textile");
});
modelBuilder.Entity("ClothShopDatabaseImplement.Models.Component", b =>
{
b.Navigation("TextileComponents");
});
modelBuilder.Entity("ClothShopDatabaseImplement.Models.Textile", b =>
{
b.Navigation("Components");
b.Navigation("Orders");
});
#pragma warning restore 612, 618
}
}
}

View File

@ -0,0 +1,62 @@
using ClothShopContracts.BindingModels;
using ClothShopContracts.ViewModels;
using ClothShopDataModels.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 ClothShopDatabaseImplement.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<TextileComponent> TextileComponents { 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,65 @@
using ClothShopContracts.BindingModels;
using ClothShopContracts.ViewModels;
using ClothShopDataModels.Enums;
using System.ComponentModel.DataAnnotations;
namespace ClothShopDatabaseImplement.Models
{
public class Order
{
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 TextileId { get; private set; }
public virtual Textile? Textiles { 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,
TextileId = model.TextileId,
};
}
public void Update(OrderBindingModel? model)
{
if (model == null)
{
return;
}
Status = model.Status;
DateImplement = model.DateImplement;
}
public OrderViewModel GetViewModel => new()
{
TextileId = TextileId,
Count = Count,
Sum = Sum,
Status = Status,
DateCreate = DateCreate,
DateImplement = DateImplement,
Id = Id,
TextileName = Textiles?.TextileName ?? string.Empty
};
}
}

View File

@ -0,0 +1,98 @@
using ClothShopContracts.BindingModels;
using ClothShopContracts.ViewModels;
using ClothShopDataModels.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 ClothShopDatabaseImplement.Models
{
public class Textile : ITextileModel
{
public int Id { get; set; }
[Required]
public string TextileName { get; set; } = string.Empty;
[Required]
public double Price { get; set; }
private Dictionary<int, (IComponentModel, int)>? _textileComponents = null;
[NotMapped]
public Dictionary<int, (IComponentModel, int)> TextileComponents
{
get
{
if (_textileComponents == null)
{
_textileComponents = Components
.ToDictionary(recPC => recPC.ComponentId, recPC =>
(recPC.Component as IComponentModel, recPC.Count));
}
return _textileComponents;
}
}
[ForeignKey("TextileId")]
public virtual List<TextileComponent> Components { get; set; } = new();
[ForeignKey("TextileId")]
public virtual List<Order> Orders { get; set; } = new();
public static Textile Create(ClothShopDatabase context, TextileBindingModel model)
{
return new Textile()
{
Id = model.Id,
TextileName = model.TextileName,
Price = model.Price,
Components = model.TextileComponents.Select(x => new TextileComponent
{
Component = context.Components.First(y => y.Id == x.Key),
Count = x.Value.Item2
}).ToList()
};
}
public void Update(TextileBindingModel model)
{
TextileName = model.TextileName;
Price = model.Price;
}
public TextileViewModel GetViewModel => new()
{
Id = Id,
TextileName = TextileName,
Price = Price,
TextileComponents = TextileComponents
};
public void UpdateComponents(ClothShopDatabase context,
TextileBindingModel model)
{
var TextileComponents = context.TextileComponents.Where(rec => rec.TextileId == model.Id).ToList();
if (TextileComponents != null && TextileComponents.Count > 0)
{ // удалили те, которых нет в модели
context.TextileComponents.RemoveRange(TextileComponents.Where(rec
=> !model.TextileComponents.ContainsKey(rec.ComponentId)));
context.SaveChanges();
// обновили количество у существующих записей
foreach (var updateComponent in TextileComponents)
{
updateComponent.Count = model.TextileComponents[updateComponent.ComponentId].Item2;
model.TextileComponents.Remove(updateComponent.ComponentId);
}
context.SaveChanges();
}
var Textile = context.Textiles.First(x => x.Id == Id);
foreach (var pc in model.TextileComponents)
{
context.TextileComponents.Add(new TextileComponent
{
Textile = Textile,
Component = context.Components.First(x => x.Id == pc.Key),
Count = pc.Value.Item2
});
context.SaveChanges();
}
_textileComponents = null;
}
}
}

View File

@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ClothShopDatabaseImplement.Models
{
public class TextileComponent
{
public int Id { get; set; }
[Required]
public int TextileId { get; set; }
[Required]
public int ComponentId { get; set; }
[Required]
public int Count { get; set; }
public virtual Component Component { get; set; } = new();
public virtual Textile Textile { get; set; } = new();
}
}

View File

@ -16,7 +16,7 @@ namespace ClothShopFileImplement.Implements
public List<OrderViewModel> GetFullList()
{
return source.Orders
.Select(x => AccessComputerStorage(x.GetViewModel))
.Select(x => AccessTextileStorage(x.GetViewModel))
.ToList();
}
@ -28,7 +28,7 @@ namespace ClothShopFileImplement.Implements
}
return source.Orders
.Where(x => x.Id == model.Id)
.Select(x => AccessComputerStorage(x.GetViewModel))
.Select(x => AccessTextileStorage(x.GetViewModel))
.ToList();
}
@ -38,7 +38,7 @@ namespace ClothShopFileImplement.Implements
{
return null;
}
return AccessComputerStorage(source.Orders.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?.GetViewModel);
return AccessTextileStorage(source.Orders.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?.GetViewModel);
}
public OrderViewModel? Insert(OrderBindingModel model)
@ -51,7 +51,7 @@ namespace ClothShopFileImplement.Implements
}
source.Orders.Add(newOrder);
source.SaveOrders();
return AccessComputerStorage(newOrder.GetViewModel);
return AccessTextileStorage(newOrder.GetViewModel);
}
public OrderViewModel? Update(OrderBindingModel model)
@ -63,7 +63,7 @@ namespace ClothShopFileImplement.Implements
}
order.Update(model);
source.SaveOrders();
return AccessComputerStorage(order.GetViewModel);
return AccessTextileStorage(order.GetViewModel);
}
public OrderViewModel? Delete(OrderBindingModel model)
{
@ -73,20 +73,20 @@ namespace ClothShopFileImplement.Implements
{
source.Orders.Remove(element);
source.SaveOrders();
return AccessComputerStorage(element.GetViewModel);
return AccessTextileStorage(element.GetViewModel);
}
return null;
}
public OrderViewModel AccessComputerStorage(OrderViewModel model)
public OrderViewModel AccessTextileStorage(OrderViewModel model)
{
if (model == null)
return null;
foreach (var Computer in source.Textiles)
foreach (var Textile in source.Textiles)
{
if (Computer.Id == model.TextileId)
if (Textile.Id == model.TextileId)
{
model.TextileName = Computer.TextileName;
model.TextileName = Textile.TextileName;
break;
}
}

View File

@ -9,6 +9,10 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.28">
<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.Configuration.Json" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
@ -18,6 +22,7 @@
<ItemGroup>
<ProjectReference Include="..\ClothShopBusinessLogic\ClothShopBusinessLogic.csproj" />
<ProjectReference Include="..\ClothShopContracts\ClothShopContracts.csproj" />
<ProjectReference Include="..\ClothShopDatabaseImplement\ClothShopDatabaseImplement.csproj" />
<ProjectReference Include="..\ClothShopFileImplement\ClothShopFileImplement.csproj" />
<ProjectReference Include="..\ClothShopListImplement\ClothShopListImplement.csproj" />
</ItemGroup>

View File

@ -1,7 +1,7 @@
using ClothShopContracts.BusinessLogicContracts;
using ClothShopContracts.StoragesContracts;
using ClothShopBusinessLogic.BusinessLogics;
using ClothShopFileImplement.Implements;
using ClothShopDatabaseImplement.Implements;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using NLog.Extensions.Logging;