diff --git a/BlacksmithWorkshop/BlacksmithWorkshop.sln b/BlacksmithWorkshop/BlacksmithWorkshop.sln
index 3e3c374..fc5a9aa 100644
--- a/BlacksmithWorkshop/BlacksmithWorkshop.sln
+++ b/BlacksmithWorkshop/BlacksmithWorkshop.sln
@@ -15,7 +15,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BlacksmithWorkshopListImple
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BlacksmithWorkshopFileImplement", "BlacksmithWorkshopFileImplement\BlacksmithWorkshopFileImplement.csproj", "{0FFB9D8A-1B02-4622-AD31-DB88F4201350}"
EndProject
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BlacksmithWorkshopDatebaseImplement", "BlacksmithWorkshopDatebaseImplement\BlacksmithWorkshopDatebaseImplement.csproj", "{2DB15039-1244-4882-BF37-C3FD8C0B7CBB}"
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BlacksmithWorkshopDatabaseImplement", "BlacksmithWorkshopDatebaseImplement\BlacksmithWorkshopDatabaseImplement.csproj", "{2DB15039-1244-4882-BF37-C3FD8C0B7CBB}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
diff --git a/BlacksmithWorkshop/BlacksmithWorkshop/BlacksmithWorkshopView.csproj b/BlacksmithWorkshop/BlacksmithWorkshop/BlacksmithWorkshopView.csproj
index fe9d4c2..884c371 100644
--- a/BlacksmithWorkshop/BlacksmithWorkshop/BlacksmithWorkshopView.csproj
+++ b/BlacksmithWorkshop/BlacksmithWorkshop/BlacksmithWorkshopView.csproj
@@ -35,7 +35,7 @@
-
+
diff --git a/BlacksmithWorkshop/BlacksmithWorkshop/Program.cs b/BlacksmithWorkshop/BlacksmithWorkshop/Program.cs
index 4fd9534..fbcb514 100644
--- a/BlacksmithWorkshop/BlacksmithWorkshop/Program.cs
+++ b/BlacksmithWorkshop/BlacksmithWorkshop/Program.cs
@@ -1,3 +1,4 @@
+using BlacksmithWorkshopBusinessLogic.BusinessLogics;
using BlacksmithWorkshopBusinessLogic.OfficePackage;
using BlacksmithWorkshopBusinessLogic.OfficePackage.Implements;
using BlacksmithWorkShopBusinessLogic.BusinessLogics;
@@ -36,9 +37,11 @@ namespace BlacksmithWorkshopView
services.AddTransient();
services.AddTransient();
services.AddTransient();
+ services.AddTransient();
services.AddTransient();
services.AddTransient();
services.AddTransient();
+ services.AddTransient();
services.AddTransient();
services.AddTransient();
services.AddTransient();
diff --git a/BlacksmithWorkshop/BlacksmithWorkshopBusinessLogic/BusinessLogics/ClientLogic.cs b/BlacksmithWorkshop/BlacksmithWorkshopBusinessLogic/BusinessLogics/ClientLogic.cs
new file mode 100644
index 0000000..f4db7bd
--- /dev/null
+++ b/BlacksmithWorkshop/BlacksmithWorkshopBusinessLogic/BusinessLogics/ClientLogic.cs
@@ -0,0 +1,107 @@
+using BlacksmithWorkshopContracts.BindingModels;
+using BlacksmithWorkshopContracts.BusinessLogicsContracts;
+using BlacksmithWorkshopContracts.SearchModels;
+using BlacksmithWorkshopContracts.StoragesContracts;
+using BlacksmithWorkshopContracts.ViewModels;
+using Microsoft.Extensions.Logging;
+
+namespace BlacksmithWorkshopBusinessLogic.BusinessLogics
+{
+ public class ClientLogic : IClientLogic
+ {
+ private readonly ILogger _logger;
+ private readonly IClientStorage _clientStorage;
+ public ClientLogic(ILogger logger, IClientStorage clientStorage)
+ {
+ _logger= logger;
+ _clientStorage = clientStorage;
+ }
+ public List? ReadList(ClientSearchModel? model)
+ {
+ _logger.LogInformation("Readlist. Email: {Email}. Id: {Id}", model?.Email, model?.Id);
+ var list = model == null ? _clientStorage.GetFullList() : _clientStorage.GetFilteredList(model);
+ if (list == null)
+ {
+ _logger.LogWarning("ReadList return null list");
+ return null;
+ }
+ _logger.LogInformation("Readlist. Count: {Count}", list.Count);
+ return list;
+ }
+ public ClientViewModel? ReadElement(ClientSearchModel model)
+ {
+ if (model == null)
+ {
+ throw new ArgumentNullException(nameof(model));
+ }
+ _logger.LogInformation("ReadElement. Email: {Email}. Id: {Id}", model.Email, model.Id);
+ var element = _clientStorage.GetElement(model);
+ if (element == null)
+ {
+ _logger.LogWarning("ReadElement element not found");
+ return null;
+ }
+ _logger.LogInformation("ReadElement find. Id: {Id}", element.Id);
+ return element;
+ }
+ public bool Create(ClientBindingModel model)
+ {
+ CheckModel(model);
+ if (_clientStorage.Insert(model) == null)
+ {
+ _logger.LogWarning("Insert operation failed");
+ return false;
+ }
+ return true;
+ }
+ public bool Update(ClientBindingModel model)
+ {
+ CheckModel(model);
+ if (_clientStorage.Update(model) == null)
+ {
+ _logger.LogWarning("Update operation failed");
+ return false;
+ }
+ return true;
+ }
+ public bool Delete(ClientBindingModel model)
+ {
+ CheckModel(model, false);
+ _logger.LogInformation("Delete. Id:{Id}", model.Id);
+ if (_clientStorage.Delete(model) == null)
+ {
+ _logger.LogWarning("Delete operation failed");
+ return false;
+ }
+ return true;
+ }
+ private void CheckModel(ClientBindingModel model, bool withParams = true)
+ {
+ if (model == null)
+ {
+ throw new ArgumentNullException(nameof(model));
+ }
+ if (!withParams)
+ {
+ return;
+ }
+ if (string.IsNullOrEmpty(model.ClientFIO))
+ {
+ throw new ArgumentNullException("Нет ФИО клиента", nameof(model.ClientFIO));
+ }
+ if (string.IsNullOrEmpty(model.Email))
+ {
+ throw new ArgumentNullException("Нет почты (логина) клиента", nameof(model.Email));
+ }
+ _logger.LogInformation("Client. ClientFIO: {ClientFIO}. Email: {Email}. Id: {Id}", model.ClientFIO, model.Email, model.Id);
+ var element = _clientStorage.GetElement(new ClientSearchModel
+ {
+ Email = model.Email,
+ });
+ if (element != null && element.Id != model.Id)
+ {
+ throw new InvalidOperationException("Клиент с таким адресом электронной почты уже есть");
+ }
+ }
+ }
+}
diff --git a/BlacksmithWorkshop/BlacksmithWorkshopContracts/BindingModels/ClientBindingModel.cs b/BlacksmithWorkshop/BlacksmithWorkshopContracts/BindingModels/ClientBindingModel.cs
new file mode 100644
index 0000000..4366fcc
--- /dev/null
+++ b/BlacksmithWorkshop/BlacksmithWorkshopContracts/BindingModels/ClientBindingModel.cs
@@ -0,0 +1,12 @@
+using BlacksmithWorkshopDataModels.Models;
+
+namespace BlacksmithWorkshopContracts.BindingModels
+{
+ public class ClientBindingModel : IClientModel
+ {
+ public int Id { get; set; }
+ public string ClientFIO { get; set; } = string.Empty;
+ public string Email { get; set; } = string.Empty;
+ public string Password { get; set; } = string.Empty;
+ }
+}
diff --git a/BlacksmithWorkshop/BlacksmithWorkshopContracts/BusinessLogicsContracts/IClientLogic.cs b/BlacksmithWorkshop/BlacksmithWorkshopContracts/BusinessLogicsContracts/IClientLogic.cs
new file mode 100644
index 0000000..1316c1d
--- /dev/null
+++ b/BlacksmithWorkshop/BlacksmithWorkshopContracts/BusinessLogicsContracts/IClientLogic.cs
@@ -0,0 +1,15 @@
+using BlacksmithWorkshopContracts.BindingModels;
+using BlacksmithWorkshopContracts.SearchModels;
+using BlacksmithWorkshopContracts.ViewModels;
+
+namespace BlacksmithWorkshopContracts.BusinessLogicsContracts
+{
+ public interface IClientLogic
+ {
+ List? ReadList(ClientSearchModel? model);
+ ClientViewModel? ReadElement(ClientSearchModel model);
+ bool Create(ClientBindingModel model);
+ bool Update(ClientBindingModel model);
+ bool Delete(ClientBindingModel model);
+ }
+}
diff --git a/BlacksmithWorkshop/BlacksmithWorkshopContracts/SearchModels/ClientSearchModel.cs b/BlacksmithWorkshop/BlacksmithWorkshopContracts/SearchModels/ClientSearchModel.cs
new file mode 100644
index 0000000..16c9a9b
--- /dev/null
+++ b/BlacksmithWorkshop/BlacksmithWorkshopContracts/SearchModels/ClientSearchModel.cs
@@ -0,0 +1,10 @@
+namespace BlacksmithWorkshopContracts.SearchModels
+{
+ public class ClientSearchModel
+ {
+ public int? Id { get; set; }
+ public string? ClientFIO { get; set; }
+ public string? Email { get; set; }
+ public string? Password { get; set; }
+ }
+}
diff --git a/BlacksmithWorkshop/BlacksmithWorkshopContracts/StoragesContracts/IClientStorage.cs b/BlacksmithWorkshop/BlacksmithWorkshopContracts/StoragesContracts/IClientStorage.cs
new file mode 100644
index 0000000..6ea8af8
--- /dev/null
+++ b/BlacksmithWorkshop/BlacksmithWorkshopContracts/StoragesContracts/IClientStorage.cs
@@ -0,0 +1,16 @@
+using BlacksmithWorkshopContracts.BindingModels;
+using BlacksmithWorkshopContracts.SearchModels;
+using BlacksmithWorkshopContracts.ViewModels;
+
+namespace BlacksmithWorkshopContracts.StoragesContracts
+{
+ public interface IClientStorage
+ {
+ List GetFullList();
+ List GetFilteredList(ClientSearchModel model);
+ ClientViewModel? GetElement(ClientSearchModel model);
+ ClientViewModel? Insert(ClientBindingModel model);
+ ClientViewModel? Update(ClientBindingModel model);
+ ClientViewModel? Delete(ClientBindingModel model);
+ }
+}
diff --git a/BlacksmithWorkshop/BlacksmithWorkshopContracts/ViewModels/ClientViewModel.cs b/BlacksmithWorkshop/BlacksmithWorkshopContracts/ViewModels/ClientViewModel.cs
new file mode 100644
index 0000000..e6dbc79
--- /dev/null
+++ b/BlacksmithWorkshop/BlacksmithWorkshopContracts/ViewModels/ClientViewModel.cs
@@ -0,0 +1,16 @@
+using BlacksmithWorkshopDataModels.Models;
+using System.ComponentModel;
+
+namespace BlacksmithWorkshopContracts.ViewModels
+{
+ public class ClientViewModel : IClientModel
+ {
+ public int Id { get; set; }
+ [DisplayName("ФИО клиента")]
+ public string ClientFIO { get; set; } = string.Empty;
+ [DisplayName("Логин (эл. почта)")]
+ public string Email { get; set; } = string.Empty;
+ [DisplayName("Пароль")]
+ public string Password { get; set; } = string.Empty;
+ }
+}
diff --git a/BlacksmithWorkshop/BlacksmithWorkshopDataModels/Models/IClientModel.cs b/BlacksmithWorkshop/BlacksmithWorkshopDataModels/Models/IClientModel.cs
new file mode 100644
index 0000000..52ea5c0
--- /dev/null
+++ b/BlacksmithWorkshop/BlacksmithWorkshopDataModels/Models/IClientModel.cs
@@ -0,0 +1,9 @@
+namespace BlacksmithWorkshopDataModels.Models
+{
+ public interface IClientModel : IId
+ {
+ string ClientFIO { get; }
+ string Email { get; }
+ string Password { get; }
+ }
+}
diff --git a/BlacksmithWorkshop/BlacksmithWorkshopDatebaseImplement/BlacksmithWorkshopDatabase.cs b/BlacksmithWorkshop/BlacksmithWorkshopDatebaseImplement/BlacksmithWorkshopDatabase.cs
index 05d278c..430af4d 100644
--- a/BlacksmithWorkshop/BlacksmithWorkshopDatebaseImplement/BlacksmithWorkshopDatabase.cs
+++ b/BlacksmithWorkshop/BlacksmithWorkshopDatebaseImplement/BlacksmithWorkshopDatabase.cs
@@ -24,5 +24,6 @@ namespace BlacksmithWorkshopDatabaseImplement
public virtual DbSet Manufactures { set; get; }
public virtual DbSet ManufactureComponents { set; get; }
public virtual DbSet Orders { set; get; }
+ public virtual DbSet Clients { set; get; }
}
}
\ No newline at end of file
diff --git a/BlacksmithWorkshop/BlacksmithWorkshopDatebaseImplement/BlacksmithWorkshopDatebaseImplement.csproj b/BlacksmithWorkshop/BlacksmithWorkshopDatebaseImplement/BlacksmithWorkshopDatabaseImplement.csproj
similarity index 100%
rename from BlacksmithWorkshop/BlacksmithWorkshopDatebaseImplement/BlacksmithWorkshopDatebaseImplement.csproj
rename to BlacksmithWorkshop/BlacksmithWorkshopDatebaseImplement/BlacksmithWorkshopDatabaseImplement.csproj
diff --git a/BlacksmithWorkshop/BlacksmithWorkshopDatebaseImplement/Implements/ClientStorage.cs b/BlacksmithWorkshop/BlacksmithWorkshopDatebaseImplement/Implements/ClientStorage.cs
new file mode 100644
index 0000000..366aed9
--- /dev/null
+++ b/BlacksmithWorkshop/BlacksmithWorkshopDatebaseImplement/Implements/ClientStorage.cs
@@ -0,0 +1,74 @@
+using BlacksmithWorkshopContracts.BindingModels;
+using BlacksmithWorkshopContracts.SearchModels;
+using BlacksmithWorkshopContracts.StoragesContracts;
+using BlacksmithWorkshopContracts.ViewModels;
+using BlacksmithWorkshopDatabaseImplement.Models;
+
+namespace BlacksmithWorkshopDatabaseImplement.Implements
+{
+ public class ClientStorage : IClientStorage
+ {
+ public List GetFullList()
+ {
+ using var context = new BlacksmithWorkshopDatabase();
+ return context.Clients.Select(x => x.GetViewModel).ToList();
+ }
+ public List GetFilteredList(ClientSearchModel model)
+ {
+ if (string.IsNullOrEmpty(model.Email))
+ {
+ return new();
+ }
+ using var context = new BlacksmithWorkshopDatabase();
+ return context.Clients
+ .Where(x => x.Email.Contains(model.Email))
+ .Select(x => x.GetViewModel)
+ .ToList();
+ }
+ public ClientViewModel? GetElement(ClientSearchModel model)
+ {
+ using var context = new BlacksmithWorkshopDatabase();
+ if (!model.Id.HasValue && string.IsNullOrEmpty(model.Email) && string.IsNullOrEmpty(model.ClientFIO))
+ {
+ return null;
+ }
+ return context.Clients.FirstOrDefault(x => ((model.Id.HasValue && x.Id == model.Id) || (!string.IsNullOrEmpty(model.Email) && x.Email == model.Email) || (!string.IsNullOrEmpty(model.ClientFIO) && x.ClientFIO == model.ClientFIO)))?.GetViewModel;
+ }
+ public ClientViewModel? Insert(ClientBindingModel model)
+ {
+ using var context = new BlacksmithWorkshopDatabase();
+ var newClient = Client.Create(model);
+ if (newClient == null)
+ {
+ return null;
+ }
+ context.Clients.Add(newClient);
+ context.SaveChanges();
+ return newClient.GetViewModel;
+ }
+ public ClientViewModel? Update(ClientBindingModel model)
+ {
+ using var context = new BlacksmithWorkshopDatabase();
+ var client = context.Clients.FirstOrDefault(x => x.Id == model.Id);
+ if (client == null)
+ {
+ return null;
+ }
+ client.Update(model);
+ context.SaveChanges();
+ return client.GetViewModel;
+ }
+ public ClientViewModel? Delete(ClientBindingModel model)
+ {
+ using var context = new BlacksmithWorkshopDatabase();
+ var element = context.Clients.FirstOrDefault(x => x.Id == model.Id);
+ if (element == null)
+ {
+ return null;
+ }
+ context.Clients.Remove(element);
+ context.SaveChanges();
+ return element.GetViewModel;
+ }
+ }
+}
\ No newline at end of file
diff --git a/BlacksmithWorkshop/BlacksmithWorkshopDatebaseImplement/Migrations/20230327122648_Lab5Migration.Designer.cs b/BlacksmithWorkshop/BlacksmithWorkshopDatebaseImplement/Migrations/20230327122648_Lab5Migration.Designer.cs
new file mode 100644
index 0000000..fd513be
--- /dev/null
+++ b/BlacksmithWorkshop/BlacksmithWorkshopDatebaseImplement/Migrations/20230327122648_Lab5Migration.Designer.cs
@@ -0,0 +1,208 @@
+//
+using System;
+using BlacksmithWorkshopDatabaseImplement;
+using Microsoft.EntityFrameworkCore;
+using Microsoft.EntityFrameworkCore.Infrastructure;
+using Microsoft.EntityFrameworkCore.Metadata;
+using Microsoft.EntityFrameworkCore.Migrations;
+using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
+
+#nullable disable
+
+namespace BlacksmithWorkshopDatebaseImplement.Migrations
+{
+ [DbContext(typeof(BlacksmithWorkshopDatabase))]
+ [Migration("20230327122648_Lab5Migration")]
+ partial class Lab5Migration
+ {
+ ///
+ 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("BlacksmithWorkshopDatabaseImplement.Models.Client", b =>
+ {
+ b.Property("Id")
+ .ValueGeneratedOnAdd()
+ .HasColumnType("int");
+
+ SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id"));
+
+ b.Property("ClientFIO")
+ .IsRequired()
+ .HasColumnType("nvarchar(max)");
+
+ b.Property("Email")
+ .IsRequired()
+ .HasColumnType("nvarchar(max)");
+
+ b.Property("Password")
+ .IsRequired()
+ .HasColumnType("nvarchar(max)");
+
+ b.HasKey("Id");
+
+ b.ToTable("Clients");
+ });
+
+ modelBuilder.Entity("BlacksmithWorkshopDatabaseImplement.Models.Component", b =>
+ {
+ b.Property("Id")
+ .ValueGeneratedOnAdd()
+ .HasColumnType("int");
+
+ SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id"));
+
+ b.Property("ComponentName")
+ .IsRequired()
+ .HasColumnType("nvarchar(max)");
+
+ b.Property("Cost")
+ .HasColumnType("float");
+
+ b.HasKey("Id");
+
+ b.ToTable("Components");
+ });
+
+ modelBuilder.Entity("BlacksmithWorkshopDatabaseImplement.Models.Manufacture", b =>
+ {
+ b.Property("Id")
+ .ValueGeneratedOnAdd()
+ .HasColumnType("int");
+
+ SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id"));
+
+ b.Property("ManufactureName")
+ .IsRequired()
+ .HasColumnType("nvarchar(max)");
+
+ b.Property("Price")
+ .HasColumnType("float");
+
+ b.HasKey("Id");
+
+ b.ToTable("Manufactures");
+ });
+
+ modelBuilder.Entity("BlacksmithWorkshopDatabaseImplement.Models.ManufactureComponent", b =>
+ {
+ b.Property("Id")
+ .ValueGeneratedOnAdd()
+ .HasColumnType("int");
+
+ SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id"));
+
+ b.Property("ComponentId")
+ .HasColumnType("int");
+
+ b.Property("Count")
+ .HasColumnType("int");
+
+ b.Property("ManufactureId")
+ .HasColumnType("int");
+
+ b.HasKey("Id");
+
+ b.HasIndex("ComponentId");
+
+ b.HasIndex("ManufactureId");
+
+ b.ToTable("ManufactureComponents");
+ });
+
+ modelBuilder.Entity("BlacksmithWorkshopDatabaseImplement.Models.Order", b =>
+ {
+ b.Property("Id")
+ .ValueGeneratedOnAdd()
+ .HasColumnType("int");
+
+ SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id"));
+
+ b.Property("ClientId")
+ .HasColumnType("int");
+
+ b.Property("Count")
+ .HasColumnType("int");
+
+ b.Property("DateCreate")
+ .HasColumnType("datetime2");
+
+ b.Property("DateImplement")
+ .HasColumnType("datetime2");
+
+ b.Property("ManufactureId")
+ .HasColumnType("int");
+
+ b.Property("Status")
+ .HasColumnType("int");
+
+ b.Property("Sum")
+ .HasColumnType("float");
+
+ b.HasKey("Id");
+
+ b.HasIndex("ClientId");
+
+ b.HasIndex("ManufactureId");
+
+ b.ToTable("Orders");
+ });
+
+ modelBuilder.Entity("BlacksmithWorkshopDatabaseImplement.Models.ManufactureComponent", b =>
+ {
+ b.HasOne("BlacksmithWorkshopDatabaseImplement.Models.Component", "Component")
+ .WithMany("ManufactureComponents")
+ .HasForeignKey("ComponentId")
+ .OnDelete(DeleteBehavior.Cascade)
+ .IsRequired();
+
+ b.HasOne("BlacksmithWorkshopDatabaseImplement.Models.Manufacture", "Manufacture")
+ .WithMany("Components")
+ .HasForeignKey("ManufactureId")
+ .OnDelete(DeleteBehavior.Cascade)
+ .IsRequired();
+
+ b.Navigation("Component");
+
+ b.Navigation("Manufacture");
+ });
+
+ modelBuilder.Entity("BlacksmithWorkshopDatabaseImplement.Models.Order", b =>
+ {
+ b.HasOne("BlacksmithWorkshopDatabaseImplement.Models.Client", null)
+ .WithMany("Orders")
+ .HasForeignKey("ClientId");
+
+ b.HasOne("BlacksmithWorkshopDatabaseImplement.Models.Manufacture", null)
+ .WithMany("Orders")
+ .HasForeignKey("ManufactureId")
+ .OnDelete(DeleteBehavior.Cascade)
+ .IsRequired();
+ });
+
+ modelBuilder.Entity("BlacksmithWorkshopDatabaseImplement.Models.Client", b =>
+ {
+ b.Navigation("Orders");
+ });
+
+ modelBuilder.Entity("BlacksmithWorkshopDatabaseImplement.Models.Component", b =>
+ {
+ b.Navigation("ManufactureComponents");
+ });
+
+ modelBuilder.Entity("BlacksmithWorkshopDatabaseImplement.Models.Manufacture", b =>
+ {
+ b.Navigation("Components");
+
+ b.Navigation("Orders");
+ });
+#pragma warning restore 612, 618
+ }
+ }
+}
diff --git a/BlacksmithWorkshop/BlacksmithWorkshopDatebaseImplement/Migrations/20230327122648_Lab5Migration.cs b/BlacksmithWorkshop/BlacksmithWorkshopDatebaseImplement/Migrations/20230327122648_Lab5Migration.cs
new file mode 100644
index 0000000..47efb82
--- /dev/null
+++ b/BlacksmithWorkshop/BlacksmithWorkshopDatebaseImplement/Migrations/20230327122648_Lab5Migration.cs
@@ -0,0 +1,22 @@
+using Microsoft.EntityFrameworkCore.Migrations;
+
+#nullable disable
+
+namespace BlacksmithWorkshopDatebaseImplement.Migrations
+{
+ ///
+ public partial class Lab5Migration : Migration
+ {
+ ///
+ protected override void Up(MigrationBuilder migrationBuilder)
+ {
+
+ }
+
+ ///
+ protected override void Down(MigrationBuilder migrationBuilder)
+ {
+
+ }
+ }
+}
diff --git a/BlacksmithWorkshop/BlacksmithWorkshopDatebaseImplement/Migrations/BlacksmithWorkshopDatabaseModelSnapshot.cs b/BlacksmithWorkshop/BlacksmithWorkshopDatebaseImplement/Migrations/BlacksmithWorkshopDatabaseModelSnapshot.cs
index ddfbfaa..3d6b04f 100644
--- a/BlacksmithWorkshop/BlacksmithWorkshopDatebaseImplement/Migrations/BlacksmithWorkshopDatabaseModelSnapshot.cs
+++ b/BlacksmithWorkshop/BlacksmithWorkshopDatebaseImplement/Migrations/BlacksmithWorkshopDatabaseModelSnapshot.cs
@@ -22,6 +22,31 @@ namespace BlacksmithWorkshopDatebaseImplement.Migrations
SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);
+ modelBuilder.Entity("BlacksmithWorkshopDatabaseImplement.Models.Client", b =>
+ {
+ b.Property("Id")
+ .ValueGeneratedOnAdd()
+ .HasColumnType("int");
+
+ SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id"));
+
+ b.Property("ClientFIO")
+ .IsRequired()
+ .HasColumnType("nvarchar(max)");
+
+ b.Property("Email")
+ .IsRequired()
+ .HasColumnType("nvarchar(max)");
+
+ b.Property("Password")
+ .IsRequired()
+ .HasColumnType("nvarchar(max)");
+
+ b.HasKey("Id");
+
+ b.ToTable("Clients");
+ });
+
modelBuilder.Entity("BlacksmithWorkshopDatabaseImplement.Models.Component", b =>
{
b.Property("Id")
@@ -96,6 +121,9 @@ namespace BlacksmithWorkshopDatebaseImplement.Migrations
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id"));
+ b.Property("ClientId")
+ .HasColumnType("int");
+
b.Property("Count")
.HasColumnType("int");
@@ -116,6 +144,8 @@ namespace BlacksmithWorkshopDatebaseImplement.Migrations
b.HasKey("Id");
+ b.HasIndex("ClientId");
+
b.HasIndex("ManufactureId");
b.ToTable("Orders");
@@ -142,6 +172,10 @@ namespace BlacksmithWorkshopDatebaseImplement.Migrations
modelBuilder.Entity("BlacksmithWorkshopDatabaseImplement.Models.Order", b =>
{
+ b.HasOne("BlacksmithWorkshopDatabaseImplement.Models.Client", null)
+ .WithMany("Orders")
+ .HasForeignKey("ClientId");
+
b.HasOne("BlacksmithWorkshopDatabaseImplement.Models.Manufacture", null)
.WithMany("Orders")
.HasForeignKey("ManufactureId")
@@ -149,6 +183,11 @@ namespace BlacksmithWorkshopDatebaseImplement.Migrations
.IsRequired();
});
+ modelBuilder.Entity("BlacksmithWorkshopDatabaseImplement.Models.Client", b =>
+ {
+ b.Navigation("Orders");
+ });
+
modelBuilder.Entity("BlacksmithWorkshopDatabaseImplement.Models.Component", b =>
{
b.Navigation("ManufactureComponents");
diff --git a/BlacksmithWorkshop/BlacksmithWorkshopDatebaseImplement/Models/Client.cs b/BlacksmithWorkshop/BlacksmithWorkshopDatebaseImplement/Models/Client.cs
new file mode 100644
index 0000000..ccee351
--- /dev/null
+++ b/BlacksmithWorkshop/BlacksmithWorkshopDatebaseImplement/Models/Client.cs
@@ -0,0 +1,52 @@
+using BlacksmithWorkshopContracts.BindingModels;
+using BlacksmithWorkshopContracts.ViewModels;
+using BlacksmithWorkshopDataModels.Models;
+using System.ComponentModel.DataAnnotations;
+using System.ComponentModel.DataAnnotations.Schema;
+
+namespace BlacksmithWorkshopDatabaseImplement.Models
+{
+ public class Client : IClientModel
+ {
+ public int Id { get; private set; }
+ [Required]
+ public string ClientFIO { get; private set; } = string.Empty;
+ [Required]
+ public string Email { get; private set; } = string.Empty;
+ [Required]
+ public string Password { get; private set; } = string.Empty;
+ [ForeignKey("ClientId")]
+ public virtual List Orders { get; set; } = new();
+ public static Client? Create(ClientBindingModel model)
+ {
+ if (model == null)
+ {
+ return null;
+ }
+ return new()
+ {
+ Id = model.Id,
+ ClientFIO = model.ClientFIO,
+ Email = model.Email,
+ Password = model.Password
+ };
+ }
+ public void Update(ClientBindingModel model)
+ {
+ if (model == null)
+ {
+ return;
+ }
+ ClientFIO = model.ClientFIO;
+ Email = model.Email;
+ Password = model.Password;
+ }
+ public ClientViewModel GetViewModel => new()
+ {
+ Id = Id,
+ ClientFIO = ClientFIO,
+ Email = Email,
+ Password = Password,
+ };
+ }
+}
\ No newline at end of file
diff --git a/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/DataFileSingleton.cs b/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/DataFileSingleton.cs
index a2b8f6c..4c631a2 100644
--- a/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/DataFileSingleton.cs
+++ b/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/DataFileSingleton.cs
@@ -8,9 +8,11 @@ namespace BlacksmithWorkshopFileImplement
private readonly string ComponentFileName = "Component.xml";
private readonly string OrderFileName = "Order.xml";
private readonly string ManufactureFileName = "Manufacture.xml";
+ private readonly string ClientFileName = "Client.xml";
public List Components { get; private set; }
public List Orders { get; private set; }
public List Manufactures { get; private set; }
+ public List Clients { get; private set; }
public static DataFileSingleton GetInstance()
{
if (instance == null)
@@ -22,11 +24,13 @@ namespace BlacksmithWorkshopFileImplement
public void SaveComponents() => SaveData(Components, ComponentFileName, "Components", x => x.GetXElement);
public void SaveManufactures() => SaveData(Manufactures, ManufactureFileName, "Manufactures", x => x.GetXElement);
public void SaveOrders() => SaveData(Orders, OrderFileName, "Orders", x => x.GetXElement);
+ public void SaveClients() => SaveData(Clients, ClientFileName, "Clients", x => x.GetXElement);
private DataFileSingleton()
{
Components = LoadData(ComponentFileName, "Component", x => Component.Create(x)!)!;
Manufactures = LoadData(ManufactureFileName, "Manufacture", x => Manufacture.Create(x)!)!;
Orders = LoadData(OrderFileName, "Order", x => Order.Create(x)!)!;
+ Clients = LoadData(ClientFileName, "Client", x => Client.Create(x)!)!;
}
private static List? LoadData(string filename, string xmlNodeName, Func selectFunction)
{
diff --git a/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/Implements/ClientStorage.cs b/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/Implements/ClientStorage.cs
new file mode 100644
index 0000000..35dd67a
--- /dev/null
+++ b/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/Implements/ClientStorage.cs
@@ -0,0 +1,76 @@
+using BlacksmithWorkshopContracts.BindingModels;
+using BlacksmithWorkshopContracts.SearchModels;
+using BlacksmithWorkshopContracts.StoragesContracts;
+using BlacksmithWorkshopContracts.ViewModels;
+using BlacksmithWorkshopFileImplement.Models;
+
+namespace BlacksmithWorkshopFileImplement.Implements
+{
+ public class ClientStorage : IClientStorage
+ {
+ private readonly DataFileSingleton source;
+ public ClientStorage()
+ {
+ source = DataFileSingleton.GetInstance();
+ }
+ public List GetFullList()
+ {
+ return source.Clients
+ .Select(x => x.GetViewModel)
+ .ToList();
+ }
+ public List GetFilteredList(ClientSearchModel model)
+ {
+ if (string.IsNullOrEmpty(model.Email))
+ {
+ return new();
+ }
+ return source.Clients
+ .Where(x => x.Email.Contains(model.Email))
+ .Select(x => x.GetViewModel)
+ .ToList();
+ }
+ public ClientViewModel? GetElement(ClientSearchModel model)
+ {
+ if (!model.Id.HasValue && string.IsNullOrEmpty(model.Email) && string.IsNullOrEmpty(model.ClientFIO))
+ {
+ return null;
+ }
+ return source.Clients.FirstOrDefault(x => ((model.Id.HasValue && x.Id == model.Id) || (!string.IsNullOrEmpty(model.Email) && x.Email == model.Email) || (!string.IsNullOrEmpty(model.ClientFIO) && x.ClientFIO == model.ClientFIO)))?.GetViewModel;
+ }
+ public ClientViewModel? Insert(ClientBindingModel model)
+ {
+ model.Id = source.Clients.Count > 0 ? source.Clients.Max(x => x.Id) + 1 : 1;
+ var newClient = Client.Create(model);
+ if (newClient == null)
+ {
+ return null;
+ }
+ source.Clients.Add(newClient);
+ source.SaveClients();
+ return newClient.GetViewModel;
+ }
+ public ClientViewModel? Update(ClientBindingModel model)
+ {
+ var client = source.Clients.FirstOrDefault(x => x.Id == model.Id);
+ if (client == null)
+ {
+ return null;
+ }
+ client.Update(model);
+ source.SaveClients();
+ return client.GetViewModel;
+ }
+ public ClientViewModel? Delete(ClientBindingModel model)
+ {
+ var element = source.Clients.FirstOrDefault(x => x.Id == model.Id);
+ if (element != null)
+ {
+ source.Clients.Remove(element);
+ source.SaveClients();
+ return element.GetViewModel;
+ }
+ return null;
+ }
+ }
+}
\ No newline at end of file
diff --git a/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/Models/Client.cs b/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/Models/Client.cs
new file mode 100644
index 0000000..1a3e560
--- /dev/null
+++ b/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/Models/Client.cs
@@ -0,0 +1,68 @@
+using BlacksmithWorkshopContracts.BindingModels;
+using BlacksmithWorkshopContracts.ViewModels;
+using BlacksmithWorkshopDataModels.Models;
+using System.Xml.Linq;
+
+namespace BlacksmithWorkshopFileImplement.Models
+{
+ public class Client : IClientModel
+ {
+ public int Id { get; private set; }
+ public string ClientFIO { get; private set; } = string.Empty;
+ public string Email { get; private set; } = string.Empty;
+ public string Password { get; private set; } = string.Empty;
+ public static Client? Create(ClientBindingModel model)
+ {
+ if (model == null)
+ {
+ return null;
+ }
+ return new Client()
+ {
+ Id = model.Id,
+ ClientFIO = model.ClientFIO,
+ Email = model.Email,
+ Password = model.Password
+ };
+ }
+ public static Client? Create(XElement element)
+ {
+ if (element == null)
+ {
+ return null;
+ }
+ return new Client()
+ {
+ Id = Convert.ToInt32(element.Attribute("Id")!.Value),
+ ClientFIO = element.Element("ClientFIO")!.Value,
+ Email = element.Element("Email")!.Value,
+ Password = element.Element("Password")!.Value,
+ };
+ }
+ public void Update(ClientBindingModel model)
+ {
+ if (model == null)
+ {
+ return;
+ }
+ ClientFIO = model.ClientFIO;
+ Email = model.Email;
+ Password = model.Password;
+ }
+ public ClientViewModel GetViewModel => new()
+ {
+ Id = Id,
+ ClientFIO = ClientFIO,
+ Email = Email,
+ Password = Password
+ };
+ public XElement GetXElement => new
+ (
+ "Client",
+ new XAttribute("Id", Id),
+ new XElement("ClientFIO", ClientFIO),
+ new XElement("Email", Email),
+ new XElement("Password", Password)
+ );
+ }
+}
diff --git a/BlacksmithWorkshop/BlacksmithWorkshopListImplement/DataListSingleton.cs b/BlacksmithWorkshop/BlacksmithWorkshopListImplement/DataListSingleton.cs
index fef796c..062f7e3 100644
--- a/BlacksmithWorkshop/BlacksmithWorkshopListImplement/DataListSingleton.cs
+++ b/BlacksmithWorkshop/BlacksmithWorkshopListImplement/DataListSingleton.cs
@@ -8,11 +8,13 @@ namespace BlacksmithWorkshopListImplement
public List Components { get; set; }
public List Orders { get; set; }
public List Manufactures { get; set; }
+ public List Clients { get; set; }
private DataListSingleton()
{
Components = new List();
Orders = new List();
Manufactures = new List();
+ Clients = new List();
}
public static DataListSingleton GetInstance()
{
diff --git a/BlacksmithWorkshop/BlacksmithWorkshopListImplement/Implements/ClientStorage.cs b/BlacksmithWorkshop/BlacksmithWorkshopListImplement/Implements/ClientStorage.cs
new file mode 100644
index 0000000..422a8b4
--- /dev/null
+++ b/BlacksmithWorkshop/BlacksmithWorkshopListImplement/Implements/ClientStorage.cs
@@ -0,0 +1,110 @@
+using BlacksmithWorkshopContracts.BindingModels;
+using BlacksmithWorkshopContracts.SearchModels;
+using BlacksmithWorkshopContracts.StoragesContracts;
+using BlacksmithWorkshopContracts.ViewModels;
+using BlacksmithWorkshopListImplement.Models;
+
+namespace BlacksmithWorkshopListImplement.Implements
+{
+ public class ClientStorage : IClientStorage
+ {
+ private readonly DataListSingleton _source;
+ public ClientStorage()
+ {
+ _source = DataListSingleton.GetInstance();
+ }
+ public List GetFullList()
+ {
+ var result = new List();
+ foreach (var client in _source.Clients)
+ {
+ result.Add(client.GetViewModel);
+ }
+ return result;
+ }
+ public List GetFilteredList(ClientSearchModel model)
+ {
+ if (model == null)
+ {
+ return new();
+ }
+ var result = new List();
+ if (string.IsNullOrEmpty(model.ClientFIO) && string.IsNullOrEmpty(model.Email))
+ {
+ return result;
+ }
+ if (model.Email != null)
+ {
+ foreach (var client in _source.Clients)
+ {
+ if (client.Email.Contains(model.Email))
+ {
+ result.Add(client.GetViewModel);
+ }
+ }
+ }
+ return result;
+ }
+ public ClientViewModel? GetElement(ClientSearchModel model)
+ {
+ foreach (var client in _source.Clients)
+ {
+ if (model.Id.HasValue && model.Id == client.Id)//сначала ищем по айди
+ {
+ return client.GetViewModel;
+ }
+ if (model.Email != null && client.Email.Contains(model.Email))//затем по логину
+ {
+ return client.GetViewModel;
+ }
+ if (model.ClientFIO != null && client.ClientFIO.Contains(model.ClientFIO))//затем по ФИО
+ {
+ return client.GetViewModel;
+ }
+ }
+ return null;
+ }
+ public ClientViewModel? Insert(ClientBindingModel model)
+ {
+ model.Id = 1;
+ foreach (var client in _source.Clients)
+ {
+ if (model.Id <= client.Id)
+ {
+ model.Id = client.Id + 1;
+ }
+ }
+ var res = Client.Create(model);
+ if (res != null)
+ {
+ _source.Clients.Add(res);
+ }
+ return res?.GetViewModel;
+ }
+ public ClientViewModel? Update(ClientBindingModel model)
+ {
+ foreach (var client in _source.Clients)
+ {
+ if (client.Id == model.Id)
+ {
+ client.Update(model);
+ return client.GetViewModel;
+ }
+ }
+ return null;
+ }
+ public ClientViewModel? Delete(ClientBindingModel model)
+ {
+ for (int i = 0; i < _source.Clients.Count; ++i)
+ {
+ if (_source.Clients[i].Id == model.Id)
+ {
+ var element = _source.Clients[i];
+ _source.Clients.RemoveAt(i);
+ return element.GetViewModel;
+ }
+ }
+ return null;
+ }
+ }
+}
\ No newline at end of file
diff --git a/BlacksmithWorkshop/BlacksmithWorkshopListImplement/Models/Client.cs b/BlacksmithWorkshop/BlacksmithWorkshopListImplement/Models/Client.cs
new file mode 100644
index 0000000..c8a179e
--- /dev/null
+++ b/BlacksmithWorkshop/BlacksmithWorkshopListImplement/Models/Client.cs
@@ -0,0 +1,45 @@
+using BlacksmithWorkshopContracts.BindingModels;
+using BlacksmithWorkshopContracts.ViewModels;
+using BlacksmithWorkshopDataModels.Models;
+
+namespace BlacksmithWorkshopListImplement.Models
+{
+ public class Client : IClientModel
+ {
+ public int Id { get; private set; }
+ public string ClientFIO { get; private set; } = string.Empty;
+ public string Email { get; private set; } = string.Empty;
+ public string Password { get; private set; } = string.Empty;
+ public static Client? Create(ClientBindingModel model)
+ {
+ if (model == null)
+ {
+ return null;
+ }
+ return new()
+ {
+ Id = model.Id,
+ ClientFIO = model.ClientFIO,
+ Email = model.Email,
+ Password = model.Password
+ };
+ }
+ public void Update(ClientBindingModel model)
+ {
+ if (model == null)
+ {
+ return;
+ }
+ ClientFIO = model.ClientFIO;
+ Email = model.Email;
+ Password = model.Password;
+ }
+ public ClientViewModel GetViewModel => new()
+ {
+ Id = Id,
+ ClientFIO = ClientFIO,
+ Email = Email,
+ Password = Password,
+ };
+ }
+}