diff --git a/Diner/Diner.sln b/Diner/Diner.sln index cbcfed1..9faf364 100644 --- a/Diner/Diner.sln +++ b/Diner/Diner.sln @@ -15,7 +15,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DineryBusinessLogic", "Dine EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DinerFileImplement", "DinerShopImplement\DinerFileImplement.csproj", "{E46007E3-F3EC-4551-A203-19361E7B1B8D}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DinerDataBaseImplement", "DinerDataBaseImplement\DinerDataBaseImplement.csproj", "{42A59EB5-A475-4CEF-AE2F-1396D6BFD143}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DinerDataBaseImplement", "DinerDataBaseImplement\DinerDataBaseImplement.csproj", "{42A59EB5-A475-4CEF-AE2F-1396D6BFD143}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DinerRestApi", "DinerRestAPI\DinerRestApi.csproj", "{68493262-499C-4677-ADAD-F7FFEF4F448F}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -51,6 +53,10 @@ Global {42A59EB5-A475-4CEF-AE2F-1396D6BFD143}.Debug|Any CPU.Build.0 = Debug|Any CPU {42A59EB5-A475-4CEF-AE2F-1396D6BFD143}.Release|Any CPU.ActiveCfg = Release|Any CPU {42A59EB5-A475-4CEF-AE2F-1396D6BFD143}.Release|Any CPU.Build.0 = Release|Any CPU + {68493262-499C-4677-ADAD-F7FFEF4F448F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {68493262-499C-4677-ADAD-F7FFEF4F448F}.Debug|Any CPU.Build.0 = Debug|Any CPU + {68493262-499C-4677-ADAD-F7FFEF4F448F}.Release|Any CPU.ActiveCfg = Release|Any CPU + {68493262-499C-4677-ADAD-F7FFEF4F448F}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/Diner/DinerDataBaseImplement/DinerDataBase.cs b/Diner/DinerDataBaseImplement/DinerDataBase.cs index d41515e..b5acea2 100644 --- a/Diner/DinerDataBaseImplement/DinerDataBase.cs +++ b/Diner/DinerDataBaseImplement/DinerDataBase.cs @@ -15,5 +15,6 @@ namespace DinerDataBaseImplement public virtual DbSet Products { get; set; } public virtual DbSet ProductComponents { get; set; } public virtual DbSet Orders { get; set; } + public virtual DbSet Clients { get; set; } } } \ No newline at end of file diff --git a/Diner/DinerDataBaseImplement/Implements/ClientStorage.cs b/Diner/DinerDataBaseImplement/Implements/ClientStorage.cs index c93f047..9a3f12e 100644 --- a/Diner/DinerDataBaseImplement/Implements/ClientStorage.cs +++ b/Diner/DinerDataBaseImplement/Implements/ClientStorage.cs @@ -2,6 +2,7 @@ using DinerContracts.SearchModels; using DinerContracts.StoragesContracts; using DinerContracts.ViewModels; +using DinerDataBaseImplement.Models; using System; using System.Collections.Generic; using System.Linq; @@ -10,28 +11,61 @@ using System.Threading.Tasks; namespace DinerDataBaseImplement.Implements { public class ClientStorage : IClientStorage { - public ClientViewModel? Delete(ClientBindingModel model) { - throw new NotImplementedException(); - } - - public ClientViewModel? GetElement(ClientSearchModel model) { - throw new NotImplementedException(); - } - - public List GetFilteredList(ClientSearchModel model) { - throw new NotImplementedException(); - } - - public List GetFullList() { - throw new NotImplementedException(); - } public ClientViewModel? Insert(ClientBindingModel model) { - throw new NotImplementedException(); + var newClient = Client.Create(model); + if (newClient == null) { + return null; + } + using var context = new DinerDataBase(); + context.Clients.Add(newClient); + context.SaveChanges(); + return newClient.GetViewModel; } public ClientViewModel? Update(ClientBindingModel model) { - throw new NotImplementedException(); + using var context = new DinerDataBase(); + 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 DinerDataBase(); + var element = context.Clients.FirstOrDefault(x => x.ID == model.ID); + if (element != null) { + context.Clients.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + + public ClientViewModel? GetElement(ClientSearchModel model) { + if (string.IsNullOrEmpty(model.ClientFIO) && !model.ID.HasValue) { + return null; + } + using var context = new DinerDataBase(); + return context.Clients.FirstOrDefault(x => (!string.IsNullOrEmpty(model.ClientFIO) && + x.ClientFIO == model.ClientFIO) || model.ID.HasValue && + x.ID == model.ID)?.GetViewModel; + } + + public List GetFilteredList(ClientSearchModel model) { + if (string.IsNullOrEmpty(model.ClientFIO)) { + return new(); + } + using var context = new DinerDataBase(); + return context.Clients.Where(x => x.ClientFIO.Contains(model.ClientFIO)).Select(x => x.GetViewModel).ToList(); + } + + public List GetFullList() { + using var context = new DinerDataBase(); + return context.Clients.Select(x => x.GetViewModel).ToList(); } } } diff --git a/Diner/DinerDataBaseImplement/Implements/FoodStorage.cs b/Diner/DinerDataBaseImplement/Implements/FoodStorage.cs index 2478e2c..44d0bbc 100644 --- a/Diner/DinerDataBaseImplement/Implements/FoodStorage.cs +++ b/Diner/DinerDataBaseImplement/Implements/FoodStorage.cs @@ -2,7 +2,6 @@ using DinerContracts.SearchModels; using DinerContracts.StoragesContracts; using DinerContracts.ViewModels; -using DinerDataBaseImplement.Migrations; using DinerDataBaseImplement.Models; using System; using System.Collections.Generic; diff --git a/Diner/DinerDataBaseImplement/Migrations/20240321182759_InitMigration.Designer.cs b/Diner/DinerDataBaseImplement/Migrations/20240321182759_InitMigration.Designer.cs deleted file mode 100644 index 95d8f50..0000000 --- a/Diner/DinerDataBaseImplement/Migrations/20240321182759_InitMigration.Designer.cs +++ /dev/null @@ -1,157 +0,0 @@ -// -using System; -using DinerDataBaseImplement; -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Infrastructure; -using Microsoft.EntityFrameworkCore.Metadata; -using Microsoft.EntityFrameworkCore.Migrations; -using Microsoft.EntityFrameworkCore.Storage.ValueConversion; - -#nullable disable - -namespace DinerDataBaseImplement.Migrations -{ - [DbContext(typeof(DinerDataBase))] - [Migration("20240321182759_InitMigration")] - partial class InitMigration - { - /// - 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("DinerDataBaseImplement.Models.Food", b => - { - b.Property("ID") - .ValueGeneratedOnAdd() - .HasColumnType("int"); - - SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("ID")); - - b.Property("ProductName") - .IsRequired() - .HasColumnType("nvarchar(max)"); - - b.Property("Price") - .HasColumnType("float"); - - b.HasKey("ID"); - - b.ToTable("Components"); - }); - - modelBuilder.Entity("DinerDataBaseImplement.Models.Order", b => - { - b.Property("ID") - .ValueGeneratedOnAdd() - .HasColumnType("int"); - - SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("ID")); - - b.Property("Count") - .HasColumnType("int"); - - b.Property("DateCreate") - .HasColumnType("datetime2"); - - b.Property("DateImplement") - .IsRequired() - .HasColumnType("datetime2"); - - b.Property("ProductID") - .HasColumnType("int"); - - b.Property("Status") - .HasColumnType("int"); - - b.Property("Sum") - .HasColumnType("float"); - - b.HasKey("ID"); - - b.ToTable("Orders"); - }); - - modelBuilder.Entity("DinerDataBaseImplement.Models.Snack", b => - { - b.Property("ID") - .ValueGeneratedOnAdd() - .HasColumnType("int"); - - SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("ID")); - - b.Property("Price") - .HasColumnType("float"); - - b.Property("ProductName") - .IsRequired() - .HasColumnType("nvarchar(max)"); - - b.HasKey("ID"); - - b.ToTable("Components"); - }); - - modelBuilder.Entity("DinerDataBaseImplement.Models.SnackFood", b => - { - b.Property("ID") - .ValueGeneratedOnAdd() - .HasColumnType("int"); - - SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("ID")); - - b.Property("ComponentID") - .HasColumnType("int"); - - b.Property("Count") - .HasColumnType("int"); - - b.Property("ProductID") - .HasColumnType("int"); - - b.HasKey("ID"); - - b.HasIndex("ComponentID"); - - b.HasIndex("ProductID"); - - b.ToTable("ProductComponents"); - }); - - modelBuilder.Entity("DinerDataBaseImplement.Models.SnackFood", b => - { - b.HasOne("DinerDataBaseImplement.Models.Food", "Component") - .WithMany("SnackFood") - .HasForeignKey("ComponentID") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.HasOne("DinerDataBaseImplement.Models.Snack", "Product") - .WithMany("Components") - .HasForeignKey("ProductID") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.Navigation("Component"); - - b.Navigation("Product"); - }); - - modelBuilder.Entity("DinerDataBaseImplement.Models.Food", b => - { - b.Navigation("SnackFood"); - }); - - modelBuilder.Entity("DinerDataBaseImplement.Models.Snack", b => - { - b.Navigation("Components"); - }); -#pragma warning restore 612, 618 - } - } -} diff --git a/Diner/DinerDataBaseImplement/Migrations/20240403124835_Migration02.Designer.cs b/Diner/DinerDataBaseImplement/Migrations/20240403124835_Migration02.Designer.cs deleted file mode 100644 index a388e4b..0000000 --- a/Diner/DinerDataBaseImplement/Migrations/20240403124835_Migration02.Designer.cs +++ /dev/null @@ -1,156 +0,0 @@ -// -using System; -using DinerDataBaseImplement; -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Infrastructure; -using Microsoft.EntityFrameworkCore.Metadata; -using Microsoft.EntityFrameworkCore.Migrations; -using Microsoft.EntityFrameworkCore.Storage.ValueConversion; - -#nullable disable - -namespace DinerDataBaseImplement.Migrations -{ - [DbContext(typeof(DinerDataBase))] - [Migration("20240403124835_Migration02")] - partial class Migration02 - { - /// - 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("DinerDataBaseImplement.Models.Food", b => - { - b.Property("ID") - .ValueGeneratedOnAdd() - .HasColumnType("int"); - - SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("ID")); - - b.Property("ProductName") - .IsRequired() - .HasColumnType("nvarchar(max)"); - - b.Property("Price") - .HasColumnType("float"); - - b.HasKey("ID"); - - b.ToTable("Components"); - }); - - modelBuilder.Entity("DinerDataBaseImplement.Models.Order", b => - { - b.Property("ID") - .ValueGeneratedOnAdd() - .HasColumnType("int"); - - SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("ID")); - - b.Property("Count") - .HasColumnType("int"); - - b.Property("DateCreate") - .HasColumnType("datetime2"); - - b.Property("DateImplement") - .HasColumnType("datetime2"); - - b.Property("ProductID") - .HasColumnType("int"); - - b.Property("Status") - .HasColumnType("int"); - - b.Property("Sum") - .HasColumnType("float"); - - b.HasKey("ID"); - - b.ToTable("Orders"); - }); - - modelBuilder.Entity("DinerDataBaseImplement.Models.Snack", b => - { - b.Property("ID") - .ValueGeneratedOnAdd() - .HasColumnType("int"); - - SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("ID")); - - b.Property("Price") - .HasColumnType("float"); - - b.Property("ProductName") - .IsRequired() - .HasColumnType("nvarchar(max)"); - - b.HasKey("ID"); - - b.ToTable("Components"); - }); - - modelBuilder.Entity("DinerDataBaseImplement.Models.SnackFood", b => - { - b.Property("ID") - .ValueGeneratedOnAdd() - .HasColumnType("int"); - - SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("ID")); - - b.Property("ComponentID") - .HasColumnType("int"); - - b.Property("Count") - .HasColumnType("int"); - - b.Property("ProductID") - .HasColumnType("int"); - - b.HasKey("ID"); - - b.HasIndex("ComponentID"); - - b.HasIndex("ProductID"); - - b.ToTable("ProductComponents"); - }); - - modelBuilder.Entity("DinerDataBaseImplement.Models.SnackFood", b => - { - b.HasOne("DinerDataBaseImplement.Models.Food", "Component") - .WithMany("SnackFood") - .HasForeignKey("ComponentID") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.HasOne("DinerDataBaseImplement.Models.Snack", "Product") - .WithMany("Components") - .HasForeignKey("ProductID") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.Navigation("Component"); - - b.Navigation("Product"); - }); - - modelBuilder.Entity("DinerDataBaseImplement.Models.Food", b => - { - b.Navigation("SnackFood"); - }); - - modelBuilder.Entity("DinerDataBaseImplement.Models.Snack", b => - { - b.Navigation("Components"); - }); -#pragma warning restore 612, 618 - } - } -} diff --git a/Diner/DinerDataBaseImplement/Migrations/20240403124835_Migration02.cs b/Diner/DinerDataBaseImplement/Migrations/20240403124835_Migration02.cs deleted file mode 100644 index 9b056c2..0000000 --- a/Diner/DinerDataBaseImplement/Migrations/20240403124835_Migration02.cs +++ /dev/null @@ -1,37 +0,0 @@ -using System; -using Microsoft.EntityFrameworkCore.Migrations; - -#nullable disable - -namespace DinerDataBaseImplement.Migrations -{ - /// - public partial class Migration02 : Migration - { - /// - protected override void Up(MigrationBuilder migrationBuilder) - { - migrationBuilder.AlterColumn( - name: "DateImplement", - table: "Orders", - type: "datetime2", - nullable: true, - oldClrType: typeof(DateTime), - oldType: "datetime2"); - } - - /// - protected override void Down(MigrationBuilder migrationBuilder) - { - migrationBuilder.AlterColumn( - name: "DateImplement", - table: "Orders", - type: "datetime2", - nullable: false, - defaultValue: new DateTime(1, 1, 1, 0, 0, 0, 0, DateTimeKind.Unspecified), - oldClrType: typeof(DateTime), - oldType: "datetime2", - oldNullable: true); - } - } -} diff --git a/Diner/DinerDataBaseImplement/Migrations/20240405063803_Migration03.cs b/Diner/DinerDataBaseImplement/Migrations/20240405063803_Migration03.cs deleted file mode 100644 index affb26b..0000000 --- a/Diner/DinerDataBaseImplement/Migrations/20240405063803_Migration03.cs +++ /dev/null @@ -1,48 +0,0 @@ -using Microsoft.EntityFrameworkCore.Migrations; - -#nullable disable - -namespace DinerDataBaseImplement.Migrations -{ - /// - public partial class Migration03 : Migration - { - /// - protected override void Up(MigrationBuilder migrationBuilder) - { - migrationBuilder.AddColumn( - name: "SnackID", - table: "Orders", - type: "int", - nullable: true); - - migrationBuilder.CreateIndex( - name: "IX_Orders_SnackID", - table: "Orders", - column: "SnackID"); - - migrationBuilder.AddForeignKey( - name: "FK_Orders_Products_SnackID", - table: "Orders", - column: "SnackID", - principalTable: "Components", - principalColumn: "ID"); - } - - /// - protected override void Down(MigrationBuilder migrationBuilder) - { - migrationBuilder.DropForeignKey( - name: "FK_Orders_Products_SnackID", - table: "Orders"); - - migrationBuilder.DropIndex( - name: "IX_Orders_SnackID", - table: "Orders"); - - migrationBuilder.DropColumn( - name: "SnackID", - table: "Orders"); - } - } -} diff --git a/Diner/DinerDataBaseImplement/Migrations/20240405063803_Migration03.Designer.cs b/Diner/DinerDataBaseImplement/Migrations/20240501160144_InitMigration.Designer.cs similarity index 78% rename from Diner/DinerDataBaseImplement/Migrations/20240405063803_Migration03.Designer.cs rename to Diner/DinerDataBaseImplement/Migrations/20240501160144_InitMigration.Designer.cs index fd51043..51ddcd2 100644 --- a/Diner/DinerDataBaseImplement/Migrations/20240405063803_Migration03.Designer.cs +++ b/Diner/DinerDataBaseImplement/Migrations/20240501160144_InitMigration.Designer.cs @@ -12,8 +12,8 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion; namespace DinerDataBaseImplement.Migrations { [DbContext(typeof(DinerDataBase))] - [Migration("20240405063803_Migration03")] - partial class Migration03 + [Migration("20240501160144_InitMigration")] + partial class InitMigration { /// protected override void BuildTargetModel(ModelBuilder modelBuilder) @@ -25,6 +25,31 @@ namespace DinerDataBaseImplement.Migrations SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + modelBuilder.Entity("DinerDataBaseImplement.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("DinerDataBaseImplement.Models.Food", b => { b.Property("ID") @@ -33,7 +58,7 @@ namespace DinerDataBaseImplement.Migrations SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("ID")); - b.Property("ProductName") + b.Property("ComponentName") .IsRequired() .HasColumnType("nvarchar(max)"); @@ -53,6 +78,9 @@ namespace DinerDataBaseImplement.Migrations SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("ID")); + b.Property("ClientID") + .HasColumnType("int"); + b.Property("Count") .HasColumnType("int"); @@ -76,6 +104,8 @@ namespace DinerDataBaseImplement.Migrations b.HasKey("ID"); + b.HasIndex("ClientID"); + b.HasIndex("SnackID"); b.ToTable("Orders"); @@ -98,7 +128,7 @@ namespace DinerDataBaseImplement.Migrations b.HasKey("ID"); - b.ToTable("Components"); + b.ToTable("Products"); }); modelBuilder.Entity("DinerDataBaseImplement.Models.SnackFood", b => @@ -129,6 +159,10 @@ namespace DinerDataBaseImplement.Migrations modelBuilder.Entity("DinerDataBaseImplement.Models.Order", b => { + b.HasOne("DinerDataBaseImplement.Models.Client", null) + .WithMany("Orders") + .HasForeignKey("ClientID"); + b.HasOne("DinerDataBaseImplement.Models.Snack", "Snack") .WithMany() .HasForeignKey("SnackID"); @@ -155,6 +189,11 @@ namespace DinerDataBaseImplement.Migrations b.Navigation("Product"); }); + modelBuilder.Entity("DinerDataBaseImplement.Models.Client", b => + { + b.Navigation("Orders"); + }); + modelBuilder.Entity("DinerDataBaseImplement.Models.Food", b => { b.Navigation("SnackFood"); diff --git a/Diner/DinerDataBaseImplement/Migrations/20240321182759_InitMigration.cs b/Diner/DinerDataBaseImplement/Migrations/20240501160144_InitMigration.cs similarity index 70% rename from Diner/DinerDataBaseImplement/Migrations/20240321182759_InitMigration.cs rename to Diner/DinerDataBaseImplement/Migrations/20240501160144_InitMigration.cs index 4e58fa7..e2bc385 100644 --- a/Diner/DinerDataBaseImplement/Migrations/20240321182759_InitMigration.cs +++ b/Diner/DinerDataBaseImplement/Migrations/20240501160144_InitMigration.cs @@ -11,6 +11,21 @@ namespace DinerDataBaseImplement.Migrations /// protected override void Up(MigrationBuilder migrationBuilder) { + migrationBuilder.CreateTable( + name: "Clients", + columns: table => new + { + ID = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + ClientFIO = table.Column(type: "nvarchar(max)", nullable: false), + Email = table.Column(type: "nvarchar(max)", nullable: false), + Password = table.Column(type: "nvarchar(max)", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Clients", x => x.ID); + }); + migrationBuilder.CreateTable( name: "Components", columns: table => new @@ -26,25 +41,7 @@ namespace DinerDataBaseImplement.Migrations }); migrationBuilder.CreateTable( - name: "Orders", - columns: table => new - { - ID = table.Column(type: "int", nullable: false) - .Annotation("SqlServer:Identity", "1, 1"), - ProductID = table.Column(type: "int", nullable: false), - Count = table.Column(type: "int", nullable: false), - Sum = table.Column(type: "float", nullable: false), - Status = table.Column(type: "int", nullable: false), - DateCreate = table.Column(type: "datetime2", nullable: false), - DateImplement = table.Column(type: "datetime2", nullable: false) - }, - constraints: table => - { - table.PrimaryKey("PK_Orders", x => x.ID); - }); - - migrationBuilder.CreateTable( - name: "Components", + name: "Products", columns: table => new { ID = table.Column(type: "int", nullable: false) @@ -57,6 +54,36 @@ namespace DinerDataBaseImplement.Migrations table.PrimaryKey("PK_Products", x => x.ID); }); + migrationBuilder.CreateTable( + name: "Orders", + columns: table => new + { + ID = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + ProductID = table.Column(type: "int", nullable: false), + Count = table.Column(type: "int", nullable: false), + Sum = table.Column(type: "float", nullable: false), + Status = table.Column(type: "int", nullable: false), + DateCreate = table.Column(type: "datetime2", nullable: false), + DateImplement = table.Column(type: "datetime2", nullable: true), + SnackID = table.Column(type: "int", nullable: true), + ClientID = table.Column(type: "int", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_Orders", x => x.ID); + table.ForeignKey( + name: "FK_Orders_Clients_ClientID", + column: x => x.ClientID, + principalTable: "Clients", + principalColumn: "ID"); + table.ForeignKey( + name: "FK_Orders_Products_SnackID", + column: x => x.SnackID, + principalTable: "Products", + principalColumn: "ID"); + }); + migrationBuilder.CreateTable( name: "ProductComponents", columns: table => new @@ -79,11 +106,21 @@ namespace DinerDataBaseImplement.Migrations table.ForeignKey( name: "FK_ProductComponents_Products_ProductID", column: x => x.ProductID, - principalTable: "Components", + principalTable: "Products", principalColumn: "ID", onDelete: ReferentialAction.Cascade); }); + migrationBuilder.CreateIndex( + name: "IX_Orders_ClientID", + table: "Orders", + column: "ClientID"); + + migrationBuilder.CreateIndex( + name: "IX_Orders_SnackID", + table: "Orders", + column: "SnackID"); + migrationBuilder.CreateIndex( name: "IX_ProductComponents_ComponentID", table: "ProductComponents", @@ -105,10 +142,13 @@ namespace DinerDataBaseImplement.Migrations name: "ProductComponents"); migrationBuilder.DropTable( - name: "Components"); + name: "Clients"); migrationBuilder.DropTable( name: "Components"); + + migrationBuilder.DropTable( + name: "Products"); } } } diff --git a/Diner/DinerDataBaseImplement/Migrations/DinerDataBaseModelSnapshot.cs b/Diner/DinerDataBaseImplement/Migrations/DinerDataBaseModelSnapshot.cs index 8d14502..046482b 100644 --- a/Diner/DinerDataBaseImplement/Migrations/DinerDataBaseModelSnapshot.cs +++ b/Diner/DinerDataBaseImplement/Migrations/DinerDataBaseModelSnapshot.cs @@ -22,6 +22,31 @@ namespace DinerDataBaseImplement.Migrations SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + modelBuilder.Entity("DinerDataBaseImplement.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("DinerDataBaseImplement.Models.Food", b => { b.Property("ID") @@ -30,7 +55,7 @@ namespace DinerDataBaseImplement.Migrations SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("ID")); - b.Property("ProductName") + b.Property("ComponentName") .IsRequired() .HasColumnType("nvarchar(max)"); @@ -39,7 +64,7 @@ namespace DinerDataBaseImplement.Migrations b.HasKey("ID"); - b.ToTable("Components", (string)null); + b.ToTable("Components"); }); modelBuilder.Entity("DinerDataBaseImplement.Models.Order", b => @@ -50,6 +75,9 @@ namespace DinerDataBaseImplement.Migrations SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("ID")); + b.Property("ClientID") + .HasColumnType("int"); + b.Property("Count") .HasColumnType("int"); @@ -73,9 +101,11 @@ namespace DinerDataBaseImplement.Migrations b.HasKey("ID"); + b.HasIndex("ClientID"); + b.HasIndex("SnackID"); - b.ToTable("Orders", (string)null); + b.ToTable("Orders"); }); modelBuilder.Entity("DinerDataBaseImplement.Models.Snack", b => @@ -95,7 +125,7 @@ namespace DinerDataBaseImplement.Migrations b.HasKey("ID"); - b.ToTable("Components", (string)null); + b.ToTable("Products"); }); modelBuilder.Entity("DinerDataBaseImplement.Models.SnackFood", b => @@ -121,11 +151,15 @@ namespace DinerDataBaseImplement.Migrations b.HasIndex("ProductID"); - b.ToTable("ProductComponents", (string)null); + b.ToTable("ProductComponents"); }); modelBuilder.Entity("DinerDataBaseImplement.Models.Order", b => { + b.HasOne("DinerDataBaseImplement.Models.Client", null) + .WithMany("Orders") + .HasForeignKey("ClientID"); + b.HasOne("DinerDataBaseImplement.Models.Snack", "Snack") .WithMany() .HasForeignKey("SnackID"); @@ -152,6 +186,11 @@ namespace DinerDataBaseImplement.Migrations b.Navigation("Product"); }); + modelBuilder.Entity("DinerDataBaseImplement.Models.Client", b => + { + b.Navigation("Orders"); + }); + modelBuilder.Entity("DinerDataBaseImplement.Models.Food", b => { b.Navigation("SnackFood"); diff --git a/Diner/DinerDataBaseImplement/Models/Client.cs b/Diner/DinerDataBaseImplement/Models/Client.cs new file mode 100644 index 0000000..04ff8c3 --- /dev/null +++ b/Diner/DinerDataBaseImplement/Models/Client.cs @@ -0,0 +1,57 @@ +using DinerContracts.BindingModels; +using DinerContracts.ViewModels; +using DinerDataModels.Models; +using Microsoft.EntityFrameworkCore.Query.Internal; +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 DinerDataBaseImplement.Models { + public class Client : IClientModel { + + [Required] + public string ClientFIO { get; set; } = string.Empty; + + [Required] + public string Email { get; set; } = string.Empty; + + [Required] + public string Password { get; set; } = string.Empty; + + public int ID { get; private set; } + + [ForeignKey("ClientID")] + public virtual List Orders { get; set; } = new(); + + public static Client? Create(ClientBindingModel? model) { + if (model == null) { + return null; + } + return new Client() { + ClientFIO = model.ClientFIO, + Email = model.Email, + Password = model.Password, + ID = model.ID, + }; + } + + public void Update(ClientBindingModel? model) { + if (model == null) { + return; + } + ClientFIO = model.ClientFIO; + Email = model.Email; + Password = model.Password; + } + public ClientViewModel GetViewModel => new() { + ClientFIO = ClientFIO, + Email = Email, + Password = Password, + ID = ID + }; + } +} diff --git a/Diner/DinerListImplement/DataListSingleton.cs b/Diner/DinerListImplement/DataListSingleton.cs index 0341e42..37ff0c8 100644 --- a/Diner/DinerListImplement/DataListSingleton.cs +++ b/Diner/DinerListImplement/DataListSingleton.cs @@ -14,11 +14,13 @@ namespace DinerListImplement public List Foods { get; set; } public List Snacks { get; set; } public List Orders { get; set; } + public List Clients { get; set; } private DataListSingleton() { Foods = new List(); Snacks = new List(); Orders = new List(); + Clients = new List(); } public static DataListSingleton GetInstance() { if (_instance == null) _instance = new DataListSingleton(); diff --git a/Diner/DinerListImplement/Implements/ClientStorage.cs b/Diner/DinerListImplement/Implements/ClientStorage.cs index b911498..5b96ff5 100644 --- a/Diner/DinerListImplement/Implements/ClientStorage.cs +++ b/Diner/DinerListImplement/Implements/ClientStorage.cs @@ -2,6 +2,7 @@ using DinerContracts.SearchModels; using DinerContracts.StoragesContracts; using DinerContracts.ViewModels; +using DinerListImplement.Models; using System; using System.Collections.Generic; using System.Linq; @@ -18,27 +19,74 @@ namespace DinerListImplement.Implements { } public ClientViewModel? Insert(ClientBindingModel model) { - throw new NotImplementedException(); + model.ID = 1; + foreach (var client in _source.Clients) { + if (model.ID <= client.ID) { + model.ID = client.ID; + } + } + var newClient = Client.Create(model); + if (newClient == null) { + return null; + } + _source.Clients.Add(newClient); + return newClient.GetViewModel; } public ClientViewModel? Update(ClientBindingModel model) { - throw new NotImplementedException(); + foreach (var client in _source.Clients) { + if (client.ID == model.ID) { + client.Update(model); + return client.GetViewModel; + } + } + return null; } public ClientViewModel? Delete(ClientBindingModel model) { - throw new NotImplementedException(); + 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; } public ClientViewModel? GetElement(ClientSearchModel model) { - throw new NotImplementedException(); + if (string.IsNullOrEmpty(model.ClientFIO) && !model.ID.HasValue) { + return null; + } + foreach (var client in _source.Clients) { + if ((!string.IsNullOrEmpty(model.ClientFIO) && + client.ClientFIO == model.ClientFIO) || + (model.ID.HasValue && client.ID == model.ID)) { + return client.GetViewModel; + } + } + return null; } public List GetFilteredList(ClientSearchModel model) { - throw new NotImplementedException(); + var result = new List(); + if (string.IsNullOrEmpty(model.ClientFIO)) { + return result; + } + foreach (var client in _source.Clients) { + if (client.ClientFIO.Contains(model.ClientFIO)) { + result.Add(client.GetViewModel); + } + } + return result; } public List GetFullList() { - throw new NotImplementedException(); + var result = new List(); + foreach (var client in _source.Clients) { + result.Add(client.GetViewModel); + } + return result; } } } diff --git a/Diner/DinerListImplement/Models/Client.cs b/Diner/DinerListImplement/Models/Client.cs new file mode 100644 index 0000000..ea36145 --- /dev/null +++ b/Diner/DinerListImplement/Models/Client.cs @@ -0,0 +1,44 @@ +using DinerContracts.BindingModels; +using DinerContracts.ViewModels; +using DinerDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DinerListImplement.Models { + public class Client : IClientModel { + 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 int ID { get; private set; } + + public static Client? Create(ClientBindingModel? model) { + if (model == null) { + return null; + } + return new Client() { + ClientFIO = model.ClientFIO, + Email = model.Email, + Password = model.Password, + ID = model.ID, + }; + } + public void Update(ClientBindingModel? model) { + if (model == null) return; + ClientFIO = model.ClientFIO; + Email = model.Email; + Password = model.Password; + } + public ClientViewModel GetViewModel => new() { + ClientFIO = ClientFIO, + Email = Email, + Password = Password, + ID = ID + }; + } +} diff --git a/Diner/DinerListImplement/Models/Food.cs b/Diner/DinerListImplement/Models/Food.cs index 074ba20..c5910b5 100644 --- a/Diner/DinerListImplement/Models/Food.cs +++ b/Diner/DinerListImplement/Models/Food.cs @@ -33,7 +33,7 @@ namespace DinerListImplement.Models public FoodViewModel GetViewModel => new(){ ID = ID, ComponentName = ComponentName, - Price = Price, + Price = Price }; } } diff --git a/Diner/DinerListImplement/Models/Order.cs b/Diner/DinerListImplement/Models/Order.cs index 9e73f3e..ad9e99e 100644 --- a/Diner/DinerListImplement/Models/Order.cs +++ b/Diner/DinerListImplement/Models/Order.cs @@ -52,7 +52,7 @@ namespace DinerListImplement.Models Sum = Sum, Status = Status, DateCreate = DateCreate, - DateImplement = DateImplement, + DateImplement = DateImplement }; } } diff --git a/Diner/DinerRestAPI/Controllers/ClientController.cs b/Diner/DinerRestAPI/Controllers/ClientController.cs new file mode 100644 index 0000000..f7f931b --- /dev/null +++ b/Diner/DinerRestAPI/Controllers/ClientController.cs @@ -0,0 +1,59 @@ +using DinerContracts.BindingModels; +using DinerContracts.BusinessLogicsContracts; +using DinerContracts.SearchModels; +using DinerContracts.ViewModels; +using Microsoft.AspNetCore.Mvc; + +namespace DinerRestApi.Controllers { + + [Route("Api/[controller]/[action]")] + [ApiController] + + public class ClientController : Controller { + + private readonly ILogger _logger; + private readonly IClientLogic _logic; + + public ClientController(ILogger logger, IClientLogic logic) { + _logger = logger; + _logic = logic; + } + + [HttpGet] + + public ClientViewModel? Login(string login, string password) { + try { + return _logic.ReadELement(new ClientSearchModel { + Email = login, + Password = password + }); + } + catch (Exception ex) { + _logger.LogError(ex, "Ошибка входа в систему"); + throw; + } + } + + [HttpPost] + public void Register(ClientBindingModel model) { + try { + _logic.Create(model); + } + catch (Exception ex) { + _logger.LogError(ex, "Ошибка регистрации"); + throw; + } + } + + [HttpPost] + public void UpdateData(ClientBindingModel model) { + try { + _logic.Update(model); + } + catch (Exception ex) { + _logger.LogError(ex, "Ошибка обновления данных"); + throw; + } + } + } +} diff --git a/Diner/DinerRestAPI/Controllers/MainController.cs b/Diner/DinerRestAPI/Controllers/MainController.cs new file mode 100644 index 0000000..78c1ef0 --- /dev/null +++ b/Diner/DinerRestAPI/Controllers/MainController.cs @@ -0,0 +1,66 @@ +using DinerContracts.BindingModels; +using DinerContracts.BusinessLogicsContacts; +using DinerContracts.SearchModels; +using DinerContracts.ViewModels; +using Microsoft.AspNetCore.Mvc; + +namespace DinerRestApi.Controllers { + + [Route("api/[controller]/[action]")] + [ApiController] + public class MainController : Controller { + private readonly ILogger _logger; + private readonly IOrderLogic _order; + private readonly ISnackLogic _product; + + public MainController(ILogger logger, IOrderLogic order, ISnackLogic product) { + _logger = logger; + _order = order; + _product = product; + } + + [HttpGet] + public List? GetProductLis() { + try { + return _product.ReadList(null); + } + catch (Exception ex) { + _logger.LogError(ex, "Ошибка получения списка продукции"); + throw; + } + } + + [HttpGet] + public SnackViewModel? GetProduct(int ProductID) { + try { + return _product.ReadElement(new SnackSearchModel { ID = ProductID }); + } + catch (Exception ex) { + _logger.LogError(ex, $"Ошибка получения продуктапо ID={ProductID}"); + throw; + } + } + + [HttpGet] + public List? GetOrders(int ClientID) { + try { + return _order.ReadList(new OrderSearchModel { ID = ClientID }); + } + catch (Exception ex) { + _logger.LogError(ex, $"Ошибка получения списка закаозов клиента ID={ClientID}"); + throw; + } + } + + [HttpPost] + public void CreateOrder(OrderBindingModel model) { + try { + _order.CreateOrder(model); + } + catch (Exception ex) { + _logger.LogError(ex, "Ошибка создания заказа"); + throw; + } + } + } +} diff --git a/Diner/DinerRestAPI/DinerRestAPI.http b/Diner/DinerRestAPI/DinerRestAPI.http new file mode 100644 index 0000000..88d895c --- /dev/null +++ b/Diner/DinerRestAPI/DinerRestAPI.http @@ -0,0 +1,6 @@ +@DinerRestAPI_HostAddress = http://localhost:5023 + +GET {{DinerRestAPI_HostAddress}}/weatherforecast/ +Accept: application/json + +### diff --git a/Diner/DinerRestAPI/DinerRestApi.csproj b/Diner/DinerRestAPI/DinerRestApi.csproj new file mode 100644 index 0000000..b22e1a5 --- /dev/null +++ b/Diner/DinerRestAPI/DinerRestApi.csproj @@ -0,0 +1,20 @@ + + + + net8.0 + enable + enable + + + + + + + + + + + + + + diff --git a/Diner/DinerRestAPI/Program.cs b/Diner/DinerRestAPI/Program.cs new file mode 100644 index 0000000..a37e41e --- /dev/null +++ b/Diner/DinerRestAPI/Program.cs @@ -0,0 +1,50 @@ + +using DinerContracts.BusinessLogicsContacts; +using DinerContracts.BusinessLogicsContracts; +using DinerContracts.StoragesContracts; +using DinerDataBaseImplement.Implements; +using DineryBusinessLogic.BusinessLogic; +using Microsoft.OpenApi.Models; + +namespace DinerRestAPI { + public class Program { + public static void Main(string[] args) { + var builder = WebApplication.CreateBuilder(args); + + builder.Logging.SetMinimumLevel(LogLevel.Trace); + builder.Logging.AddLog4Net("log4net.config"); + + builder.Services.AddTransient(); + builder.Services.AddTransient(); + builder.Services.AddTransient(); + + builder.Services.AddTransient(); + builder.Services.AddTransient(); + builder.Services.AddTransient(); + + // Add services to the container. + + builder.Services.AddControllers(); + // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle + builder.Services.AddEndpointsApiExplorer(); + builder.Services.AddSwaggerGen(x => x.SwaggerDoc("v1", new OpenApiInfo { Title = "DinerRestApi", Version = "v1" })); + + var app = builder.Build(); + + // Configure the HTTP request pipeline. + if (app.Environment.IsDevelopment()) { + app.UseSwagger(); + app.UseSwaggerUI(x => x.SwaggerEndpoint("/swagger/v1/swagger.json", "DinerRestApi v1")); + } + + app.UseHttpsRedirection(); + + app.UseAuthorization(); + + + app.MapControllers(); + + app.Run(); + } + } +} diff --git a/Diner/DinerRestAPI/Properties/launchSettings.json b/Diner/DinerRestAPI/Properties/launchSettings.json new file mode 100644 index 0000000..a63b537 --- /dev/null +++ b/Diner/DinerRestAPI/Properties/launchSettings.json @@ -0,0 +1,41 @@ +{ + "$schema": "http://json.schemastore.org/launchsettings.json", + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:21216", + "sslPort": 44347 + } + }, + "profiles": { + "http": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "launchUrl": "swagger", + "applicationUrl": "http://localhost:5023", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "https": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "launchUrl": "swagger", + "applicationUrl": "https://localhost:7267;http://localhost:5023", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "launchUrl": "swagger", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/Diner/DinerRestAPI/appsettings.Development.json b/Diner/DinerRestAPI/appsettings.Development.json new file mode 100644 index 0000000..0c208ae --- /dev/null +++ b/Diner/DinerRestAPI/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/Diner/DinerRestAPI/appsettings.json b/Diner/DinerRestAPI/appsettings.json new file mode 100644 index 0000000..10f68b8 --- /dev/null +++ b/Diner/DinerRestAPI/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +} diff --git a/Diner/DinerRestAPI/log4net.config b/Diner/DinerRestAPI/log4net.config new file mode 100644 index 0000000..2b819fc --- /dev/null +++ b/Diner/DinerRestAPI/log4net.config @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Diner/DinerShopImplement/DataFileSingleton.cs b/Diner/DinerShopImplement/DataFileSingleton.cs index 4edd6e5..eb85557 100644 --- a/Diner/DinerShopImplement/DataFileSingleton.cs +++ b/Diner/DinerShopImplement/DataFileSingleton.cs @@ -18,14 +18,18 @@ namespace DinerFileImplement private readonly string SnackFileName = "Snack.xml"; + private readonly string ClientFileName = "Client.xml"; + public List Foods { get; set; } public List Orders { get; private set; } public List Snacks { get; private set; } + public List Clients { get; private set; } private DataFileSingleton() { Foods = LoadData(FoodFileName, "Food", x => Food.Create(x)!)!; Snacks = LoadData(SnackFileName, "Snack", x => Snack.Create(x)!)!; Orders = LoadData(OrderFileName, "Order", x => Order.Create(x)!)!; + Clients = LoadData(ClientFileName, "Client", x => Client.Create(x)!)!; } public static DataFileSingleton GetInstance() { if (instance == null) instance = new DataFileSingleton(); @@ -37,7 +41,10 @@ namespace DinerFileImplement x => x.GetXElement); public void SaveOrder() => SaveData(Orders, OrderFileName, "Orders", x => x.GetXElement); - private static void SaveData(List data, string filename, string xmlNodeName, + public void SaveClient() => SaveData(Clients, ClientFileName, "Clients", + x => x.GetXElement); + + private static void SaveData(List data, string filename, string xmlNodeName, Func selectFunction) { if (data != null) { diff --git a/Diner/DinerShopImplement/Implements/ClientStorage.cs b/Diner/DinerShopImplement/Implements/ClientStorage.cs index c72c66f..20fe010 100644 --- a/Diner/DinerShopImplement/Implements/ClientStorage.cs +++ b/Diner/DinerShopImplement/Implements/ClientStorage.cs @@ -2,6 +2,7 @@ using DinerContracts.SearchModels; using DinerContracts.StoragesContracts; using DinerContracts.ViewModels; +using DinerFileImplement.Models; using System; using System.Collections.Generic; using System.Linq; @@ -10,28 +11,63 @@ using System.Threading.Tasks; namespace DinerFileImplement.Implements { public class ClientStorage : IClientStorage { - public ClientViewModel? Delete(ClientBindingModel model) { - throw new NotImplementedException(); - } - public ClientViewModel? GetElement(ClientSearchModel model) { - throw new NotImplementedException(); - } + private readonly DataFileSingleton _source; - public List GetFilteredList(ClientSearchModel model) { - throw new NotImplementedException(); - } - - public List GetFullList() { - throw new NotImplementedException(); + public ClientStorage() { + _source = DataFileSingleton.GetInstance(); } public ClientViewModel? Insert(ClientBindingModel model) { - throw new NotImplementedException(); + 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.SaveClient(); + return newClient.GetViewModel; } public ClientViewModel? Update(ClientBindingModel model) { - throw new NotImplementedException(); + var client = _source.Clients.FirstOrDefault(x => x.ID == model.ID); + if (client == null) { + return null; + } + client.Update(model); + _source.SaveClient(); + 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.SaveClient(); + return element.GetViewModel; + } + return null; + } + + public ClientViewModel? GetElement(ClientSearchModel model) { + if (string.IsNullOrEmpty(model.ClientFIO) && !model.ID.HasValue) { + return null; + } + return _source.Clients.FirstOrDefault(x => (!string.IsNullOrEmpty(model.ClientFIO) && + x.ClientFIO == model.ClientFIO) || model.ID.HasValue && + x.ID == model.ID)?.GetViewModel; + } + + public List GetFilteredList(ClientSearchModel model) { + if (string.IsNullOrEmpty(model.ClientFIO)) { + return new(); + } + return _source.Clients.Where(x => x.ClientFIO.Contains(model.ClientFIO)) + .Select(x => x.GetViewModel).ToList(); + } + + public List GetFullList() { + return _source.Clients.Select(x => x.GetViewModel).ToList(); } } } diff --git a/Diner/DinerShopImplement/Models/Client.cs b/Diner/DinerShopImplement/Models/Client.cs new file mode 100644 index 0000000..9d198de --- /dev/null +++ b/Diner/DinerShopImplement/Models/Client.cs @@ -0,0 +1,62 @@ +using DinerContracts.BindingModels; +using DinerContracts.ViewModels; +using DinerDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; + +namespace DinerFileImplement.Models { + public class Client : IClientModel { + public string ClientFIO { get; set; } = string.Empty; + + public string Email { get; set; } = string.Empty; + + public string Password { get; set; } = string.Empty; + + public int ID { get; set; } + + public static Client? Create(ClientBindingModel? model) { + if (model == null) { + return null; + } + return new Client() { + ClientFIO = model.ClientFIO, + Email = model.Email, + Password = model.Password, + ID = model.ID, + }; + } + public static Client? Create(XElement element) { + if (element == null) { + return null; + } + return new Client() { + ClientFIO = element.Element("ClientFIO")!.Value, + Email = element.Element("Email")!.Value, + Password = element.Element("Password")!.Value, + ID = Convert.ToInt32(element.Attribute("ID")!.Value) + }; + } + public void Update (ClientBindingModel? model) { + if (model == null) return; + ClientFIO = model.ClientFIO; + Email = model.Email; + Password = model.Password; + } + + public ClientViewModel GetViewModel => new() { + ClientFIO = ClientFIO, + Email = Email, + Password = Password, + ID = ID + }; + + public XElement GetXElement => new("Client", new XAttribute("ID", ID), + new XElement("ClientFIO", ClientFIO), + new XElement("Email", Email), + new XElement("Password", Password.ToString())); + } +} diff --git a/Diner/DinerView/FormClient.Designer.cs b/Diner/DinerView/FormClient.Designer.cs new file mode 100644 index 0000000..d21ce9b --- /dev/null +++ b/Diner/DinerView/FormClient.Designer.cs @@ -0,0 +1,89 @@ +namespace DinerView { + partial class FormClient { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) { + if (disposing && (components != null)) { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() { + dataGridView = new DataGridView(); + buttonDelete = new Button(); + buttonUpdate = new Button(); + ((System.ComponentModel.ISupportInitialize)dataGridView).BeginInit(); + SuspendLayout(); + // + // dataGridView + // + dataGridView.AllowUserToAddRows = false; + dataGridView.AllowUserToDeleteRows = false; + dataGridView.BackgroundColor = SystemColors.ButtonFace; + dataGridView.BorderStyle = BorderStyle.None; + dataGridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize; + dataGridView.Dock = DockStyle.Left; + dataGridView.Location = new Point(0, 0); + dataGridView.Name = "dataGridView"; + dataGridView.RowHeadersVisible = false; + dataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect; + dataGridView.Size = new Size(468, 450); + dataGridView.TabIndex = 0; + // + // buttonDelete + // + buttonDelete.Location = new Point(509, 12); + buttonDelete.Name = "buttonDelete"; + buttonDelete.Size = new Size(95, 35); + buttonDelete.TabIndex = 1; + buttonDelete.Text = "Удалить"; + buttonDelete.UseVisualStyleBackColor = true; + buttonDelete.Click += buttonDelete_Click; + // + // buttonUpdate + // + buttonUpdate.Location = new Point(509, 53); + buttonUpdate.Name = "buttonUpdate"; + buttonUpdate.Size = new Size(95, 35); + buttonUpdate.TabIndex = 2; + buttonUpdate.Text = "Обновить"; + buttonUpdate.UseVisualStyleBackColor = true; + buttonUpdate.Click += buttonUpdate_Click; + // + // FormClient + // + AutoScaleDimensions = new SizeF(7F, 15F); + AutoScaleMode = AutoScaleMode.Font; + BackColor = SystemColors.ActiveBorder; + ClientSize = new Size(635, 450); + Controls.Add(buttonUpdate); + Controls.Add(buttonDelete); + Controls.Add(dataGridView); + Name = "FormClient"; + Text = "Обновить"; + Load += FormClient_Load; + ((System.ComponentModel.ISupportInitialize)dataGridView).EndInit(); + ResumeLayout(false); + } + + #endregion + + private DataGridView dataGridView; + private Button buttonDelete; + private Button buttonUpdate; + } +} \ No newline at end of file diff --git a/Diner/DinerView/FormClient.cs b/Diner/DinerView/FormClient.cs new file mode 100644 index 0000000..13ab4ec --- /dev/null +++ b/Diner/DinerView/FormClient.cs @@ -0,0 +1,69 @@ +using DinerContracts.BindingModels; +using DinerContracts.BusinessLogicsContracts; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace DinerView { + public partial class FormClient : Form { + + private readonly ILogger _logger; + private readonly IClientLogic _logic; + + public FormClient(ILogger logger, IClientLogic logic) { + InitializeComponent(); + _logger = logger; + _logic = logic; + } + + private void LoadData() { + try { + var list = _logic.ReadList(null); + if (list != null) { + dataGridView.DataSource = list; + dataGridView.Columns["ID"].Visible = false; + dataGridView.Columns["ClientFIO"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; + } + _logger.LogInformation("Загрузка клиентов"); + } + catch (Exception ex) { + _logger.LogError(ex, "Ошибка загрузки клиентов"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + + private void FormClient_Load(object sender, EventArgs e) { + LoadData(); + } + + private void buttonDelete_Click(object sender, EventArgs e) { + if (dataGridView.SelectedRows.Count == 1) { + if (MessageBox.Show("Удалить запись?", "Вопрос", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { + int id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["ID"].Value); + try { + if (!_logic.Delete(new ClientBindingModel { ID = id })) { + throw new Exception("Ошиюка при удалении. Дополнительная информация в логах."); + } + _logger.LogInformation("Удаление клиента"); + } + catch (Exception ex) { + _logger.LogError(ex, "Ошибка удалении клиента"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + LoadData(); + } + } + } + + private void buttonUpdate_Click(object sender, EventArgs e) { + LoadData(); + } + } +} diff --git a/Diner/DinerView/FormClient.resx b/Diner/DinerView/FormClient.resx new file mode 100644 index 0000000..af32865 --- /dev/null +++ b/Diner/DinerView/FormClient.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/Diner/DinerView/Program.cs b/Diner/DinerView/Program.cs index d328b7c..69d9bf1 100644 --- a/Diner/DinerView/Program.cs +++ b/Diner/DinerView/Program.cs @@ -40,7 +40,9 @@ namespace DinerView services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); + services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); @@ -59,6 +61,7 @@ namespace DinerView services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); } } } \ No newline at end of file