diff --git a/Pizzeria/Pizzeria.sln b/Pizzeria/Pizzeria.sln index 6b98332..489356e 100644 --- a/Pizzeria/Pizzeria.sln +++ b/Pizzeria/Pizzeria.sln @@ -15,7 +15,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PizzeriaListImplement", "Pi EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PizzeriaFileImplement", "PizzeriaShopFileImplement\PizzeriaFileImplement.csproj", "{44B31B6F-2852-454B-9E7D-E965C9A348ED}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PizzeriaDatabaseImplement", "PizzeriaDatabaseImplement\PizzeriaDatabaseImplement.csproj", "{37411F99-74EC-476C-BE13-D20015ED52B2}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PizzeriaDatabaseImplement", "PizzeriaDatabaseImplement\PizzeriaDatabaseImplement.csproj", "{37411F99-74EC-476C-BE13-D20015ED52B2}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PizzeriaRestApi", "PizzeriaRestApi\PizzeriaRestApi.csproj", "{96F0E752-5060-48E9-9A98-2A7AA1AB63A7}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -51,6 +53,10 @@ Global {37411F99-74EC-476C-BE13-D20015ED52B2}.Debug|Any CPU.Build.0 = Debug|Any CPU {37411F99-74EC-476C-BE13-D20015ED52B2}.Release|Any CPU.ActiveCfg = Release|Any CPU {37411F99-74EC-476C-BE13-D20015ED52B2}.Release|Any CPU.Build.0 = Release|Any CPU + {96F0E752-5060-48E9-9A98-2A7AA1AB63A7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {96F0E752-5060-48E9-9A98-2A7AA1AB63A7}.Debug|Any CPU.Build.0 = Debug|Any CPU + {96F0E752-5060-48E9-9A98-2A7AA1AB63A7}.Release|Any CPU.ActiveCfg = Release|Any CPU + {96F0E752-5060-48E9-9A98-2A7AA1AB63A7}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/Pizzeria/PizzeriaBusinessLogic/ClientLogic.cs b/Pizzeria/PizzeriaBusinessLogic/ClientLogic.cs new file mode 100644 index 0000000..987bae6 --- /dev/null +++ b/Pizzeria/PizzeriaBusinessLogic/ClientLogic.cs @@ -0,0 +1,113 @@ +using PizzeriaContracts.BindingModels; +using PizzeriaContracts.BusinessLogicsContracts; +using PizzeriaContracts.SearchModels; +using PizzeriaContracts.StoragesContracts; +using PizzeriaContracts.ViewModels; +using Microsoft.Extensions.Logging; + + +namespace PizzeriaBusinessLogic +{ + 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. ClientFIO: {ClientFIO}. Id:{Id}", model?.ClientFIO, 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. ClientFIO:{ClientFIO}. Id:{Id}", model.ClientFIO, 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.ClientFIO)); + } + if (string.IsNullOrEmpty(model.Password)) + { + throw new ArgumentNullException("Нет пароля у клиента", nameof(model.ClientFIO)); + } + _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/Pizzeria/PizzeriaContracts/BindingModels/ClientBindingModel.cs b/Pizzeria/PizzeriaContracts/BindingModels/ClientBindingModel.cs new file mode 100644 index 0000000..aea22b3 --- /dev/null +++ b/Pizzeria/PizzeriaContracts/BindingModels/ClientBindingModel.cs @@ -0,0 +1,12 @@ +using PizzeriaDataModels.Models; +namespace PizzeriaContracts.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/Pizzeria/PizzeriaContracts/BindingModels/OrderBindingModel.cs b/Pizzeria/PizzeriaContracts/BindingModels/OrderBindingModel.cs index 877567f..abe6966 100644 --- a/Pizzeria/PizzeriaContracts/BindingModels/OrderBindingModel.cs +++ b/Pizzeria/PizzeriaContracts/BindingModels/OrderBindingModel.cs @@ -11,6 +11,7 @@ namespace PizzeriaContracts.BindingModels { public int Id { get; set; } public int PizzaId { get; set; } + public int ClientId { get; set; } public int Count { get; set; } public double Sum { get; set; } public OrderStatus Status { get; set; } = OrderStatus.Неизвестен; diff --git a/Pizzeria/PizzeriaContracts/BusinessLogicsContracts/IClientLogic.cs b/Pizzeria/PizzeriaContracts/BusinessLogicsContracts/IClientLogic.cs new file mode 100644 index 0000000..a662c55 --- /dev/null +++ b/Pizzeria/PizzeriaContracts/BusinessLogicsContracts/IClientLogic.cs @@ -0,0 +1,15 @@ +using PizzeriaContracts.BindingModels; +using PizzeriaContracts.SearchModels; +using PizzeriaContracts.ViewModels; + +namespace PizzeriaContracts.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/Pizzeria/PizzeriaContracts/SearchModels/ClientSearchModel.cs b/Pizzeria/PizzeriaContracts/SearchModels/ClientSearchModel.cs new file mode 100644 index 0000000..3e77319 --- /dev/null +++ b/Pizzeria/PizzeriaContracts/SearchModels/ClientSearchModel.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using PizzeriaDataModels.Models; + + +namespace PizzeriaContracts.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/Pizzeria/PizzeriaContracts/StoragesContracts/IClientStorage.cs b/Pizzeria/PizzeriaContracts/StoragesContracts/IClientStorage.cs new file mode 100644 index 0000000..d1ff9d7 --- /dev/null +++ b/Pizzeria/PizzeriaContracts/StoragesContracts/IClientStorage.cs @@ -0,0 +1,17 @@ +using PizzeriaContracts.BindingModels; +using PizzeriaContracts.SearchModels; +using PizzeriaContracts.ViewModels; + +namespace PizzeriaContracts.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/Pizzeria/PizzeriaContracts/ViewModels/ClientViewModel.cs b/Pizzeria/PizzeriaContracts/ViewModels/ClientViewModel.cs new file mode 100644 index 0000000..c0655fc --- /dev/null +++ b/Pizzeria/PizzeriaContracts/ViewModels/ClientViewModel.cs @@ -0,0 +1,17 @@ +using PizzeriaDataModels.Models; +using System.ComponentModel; + +namespace PizzeriaContracts.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/Pizzeria/PizzeriaContracts/ViewModels/OrderViewModel.cs b/Pizzeria/PizzeriaContracts/ViewModels/OrderViewModel.cs index 02a6c07..8929c5a 100644 --- a/Pizzeria/PizzeriaContracts/ViewModels/OrderViewModel.cs +++ b/Pizzeria/PizzeriaContracts/ViewModels/OrderViewModel.cs @@ -15,6 +15,10 @@ namespace PizzeriaContracts.ViewModels public int PizzaId { get; set; } [DisplayName("Изделие")] public string PizzaName { get; set; } = string.Empty; + public int ClientId { get; set; } + + [DisplayName("ФИО клиента")] + public string ClientFIO { get; set; } = string.Empty; [DisplayName("Количество")] public int Count { get; set; } [DisplayName("Сумма")] diff --git a/Pizzeria/PizzeriaDataModels/Models/IClientModel.cs b/Pizzeria/PizzeriaDataModels/Models/IClientModel.cs new file mode 100644 index 0000000..9bb9611 --- /dev/null +++ b/Pizzeria/PizzeriaDataModels/Models/IClientModel.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PizzeriaDataModels.Models +{ + public interface IClientModel : IId + { + string ClientFIO { get; } + string Email { get; } + string Password { get; } + } +} diff --git a/Pizzeria/PizzeriaDatabaseImplement/Implements/ClientStorage.cs b/Pizzeria/PizzeriaDatabaseImplement/Implements/ClientStorage.cs new file mode 100644 index 0000000..6999507 --- /dev/null +++ b/Pizzeria/PizzeriaDatabaseImplement/Implements/ClientStorage.cs @@ -0,0 +1,83 @@ +using PizzeriaContracts.BindingModels; +using PizzeriaContracts.SearchModels; +using PizzeriaContracts.StoragesContracts; +using PizzeriaContracts.ViewModels; +using PizzeriaDatabaseImplement.models; +namespace PizzeriaDatabaseImplement.Implements +{ + public class ClientStorage : IClientStorage + { + public List GetFullList() + { + using var context = new PizzeriaDatabase(); + return context.Clients + .Select(x => x.GetViewModel) + .ToList(); + } + public List GetFilteredList(ClientSearchModel model) + { + if (string.IsNullOrEmpty(model.ClientFIO) && + string.IsNullOrEmpty(model.Email) && + string.IsNullOrEmpty(model.Password)) + { + return new(); + } + using var context = new PizzeriaDatabase(); + return context.Clients + .Where(x => (string.IsNullOrEmpty(model.ClientFIO) || x.ClientFIO.Contains(model.ClientFIO) && + string.IsNullOrEmpty(model.Email) || x.ClientFIO.Contains(model.Email) && + string.IsNullOrEmpty(model.Password) || x.ClientFIO.Contains(model.Password))) + .Select(x => x.GetViewModel) + .ToList(); + } + public ClientViewModel? GetElement(ClientSearchModel model) + { + if (string.IsNullOrEmpty(model.ClientFIO) && !model.Id.HasValue) + { + return null; + } + using var context = new PizzeriaDatabase(); + return context.Clients + .FirstOrDefault(x => + (!string.IsNullOrEmpty(model.ClientFIO) && x.ClientFIO == model.ClientFIO) || + (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + } + public ClientViewModel? Insert(ClientBindingModel model) + { + var newClient = Client.Create(model); + if (newClient == null) + { + return null; + } + using var context = new PizzeriaDatabase(); + context.Clients.Add(newClient); + context.SaveChanges(); + return newClient.GetViewModel; + } + public ClientViewModel? Update(ClientBindingModel model) + { + using var context = new PizzeriaDatabase(); + 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 PizzeriaDatabase(); + var element = context.Clients.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Clients.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} \ No newline at end of file diff --git a/Pizzeria/PizzeriaDatabaseImplement/Migrations/20230327193948_AddClient.Designer.cs b/Pizzeria/PizzeriaDatabaseImplement/Migrations/20230327193948_AddClient.Designer.cs new file mode 100644 index 0000000..1fe92f1 --- /dev/null +++ b/Pizzeria/PizzeriaDatabaseImplement/Migrations/20230327193948_AddClient.Designer.cs @@ -0,0 +1,214 @@ +// +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; +using PizzeriaDatabaseImplement; + +#nullable disable + +namespace PizzeriaDatabaseImplement.Migrations +{ + [DbContext(typeof(PizzeriaDatabase))] + [Migration("20230327193948_AddClient")] + partial class AddClient + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.3") + .HasAnnotation("Relational:MaxIdentifierLength", 63); + + NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); + + modelBuilder.Entity("PizzeriaDatabaseImplement.models.Client", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("ClientFIO") + .IsRequired() + .HasColumnType("text"); + + b.Property("Email") + .IsRequired() + .HasColumnType("text"); + + b.Property("Password") + .IsRequired() + .HasColumnType("text"); + + b.HasKey("Id"); + + b.ToTable("Clients"); + }); + + modelBuilder.Entity("PizzeriaDatabaseImplement.models.Component", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("ComponentName") + .IsRequired() + .HasColumnType("text"); + + b.Property("Cost") + .HasColumnType("double precision"); + + b.HasKey("Id"); + + b.ToTable("Components"); + }); + + modelBuilder.Entity("PizzeriaDatabaseImplement.models.Order", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("ClientId") + .HasColumnType("integer"); + + b.Property("Count") + .HasColumnType("integer"); + + b.Property("DateCreate") + .HasColumnType("timestamp with time zone"); + + b.Property("DateImplement") + .HasColumnType("timestamp with time zone"); + + b.Property("PizzaId") + .HasColumnType("integer"); + + b.Property("Status") + .HasColumnType("integer"); + + b.Property("Sum") + .HasColumnType("double precision"); + + b.HasKey("Id"); + + b.HasIndex("ClientId"); + + b.HasIndex("PizzaId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("PizzeriaDatabaseImplement.models.Pizza", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("PizzaName") + .IsRequired() + .HasColumnType("text"); + + b.Property("Price") + .HasColumnType("double precision"); + + b.HasKey("Id"); + + b.ToTable("Pizzas"); + }); + + modelBuilder.Entity("PizzeriaDatabaseImplement.models.PizzaComponent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("ComponentId") + .HasColumnType("integer"); + + b.Property("Count") + .HasColumnType("integer"); + + b.Property("PizzaId") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("ComponentId"); + + b.HasIndex("PizzaId"); + + b.ToTable("PizzaComponents"); + }); + + modelBuilder.Entity("PizzeriaDatabaseImplement.models.Order", b => + { + b.HasOne("PizzeriaDatabaseImplement.models.Client", "Client") + .WithMany("Orders") + .HasForeignKey("ClientId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("PizzeriaDatabaseImplement.models.Pizza", "Pizza") + .WithMany("Orders") + .HasForeignKey("PizzaId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Client"); + + b.Navigation("Pizza"); + }); + + modelBuilder.Entity("PizzeriaDatabaseImplement.models.PizzaComponent", b => + { + b.HasOne("PizzeriaDatabaseImplement.models.Component", "Component") + .WithMany("PizzaComponents") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("PizzeriaDatabaseImplement.models.Pizza", "Pizza") + .WithMany("Components") + .HasForeignKey("PizzaId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Component"); + + b.Navigation("Pizza"); + }); + + modelBuilder.Entity("PizzeriaDatabaseImplement.models.Client", b => + { + b.Navigation("Orders"); + }); + + modelBuilder.Entity("PizzeriaDatabaseImplement.models.Component", b => + { + b.Navigation("PizzaComponents"); + }); + + modelBuilder.Entity("PizzeriaDatabaseImplement.models.Pizza", b => + { + b.Navigation("Components"); + + b.Navigation("Orders"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Pizzeria/PizzeriaDatabaseImplement/Migrations/20230327193948_AddClient.cs b/Pizzeria/PizzeriaDatabaseImplement/Migrations/20230327193948_AddClient.cs new file mode 100644 index 0000000..f3fcdec --- /dev/null +++ b/Pizzeria/PizzeriaDatabaseImplement/Migrations/20230327193948_AddClient.cs @@ -0,0 +1,69 @@ +using Microsoft.EntityFrameworkCore.Migrations; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +#nullable disable + +namespace PizzeriaDatabaseImplement.Migrations +{ + /// + public partial class AddClient : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.AddColumn( + name: "ClientId", + table: "Orders", + type: "integer", + nullable: false, + defaultValue: 0); + + migrationBuilder.CreateTable( + name: "Clients", + columns: table => new + { + Id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + ClientFIO = table.Column(type: "text", nullable: false), + Email = table.Column(type: "text", nullable: false), + Password = table.Column(type: "text", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Clients", x => x.Id); + }); + + migrationBuilder.CreateIndex( + name: "IX_Orders_ClientId", + table: "Orders", + column: "ClientId"); + + migrationBuilder.AddForeignKey( + name: "FK_Orders_Clients_ClientId", + table: "Orders", + column: "ClientId", + principalTable: "Clients", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropForeignKey( + name: "FK_Orders_Clients_ClientId", + table: "Orders"); + + migrationBuilder.DropTable( + name: "Clients"); + + migrationBuilder.DropIndex( + name: "IX_Orders_ClientId", + table: "Orders"); + + migrationBuilder.DropColumn( + name: "ClientId", + table: "Orders"); + } + } +} diff --git a/Pizzeria/PizzeriaDatabaseImplement/Migrations/PizzeriaDatabaseModelSnapshot.cs b/Pizzeria/PizzeriaDatabaseImplement/Migrations/PizzeriaDatabaseModelSnapshot.cs index ae2eb4b..c4d5582 100644 --- a/Pizzeria/PizzeriaDatabaseImplement/Migrations/PizzeriaDatabaseModelSnapshot.cs +++ b/Pizzeria/PizzeriaDatabaseImplement/Migrations/PizzeriaDatabaseModelSnapshot.cs @@ -22,6 +22,31 @@ namespace PizzeriaDatabaseImplement.Migrations NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); + modelBuilder.Entity("PizzeriaDatabaseImplement.models.Client", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("ClientFIO") + .IsRequired() + .HasColumnType("text"); + + b.Property("Email") + .IsRequired() + .HasColumnType("text"); + + b.Property("Password") + .IsRequired() + .HasColumnType("text"); + + b.HasKey("Id"); + + b.ToTable("Clients"); + }); + modelBuilder.Entity("PizzeriaDatabaseImplement.models.Component", b => { b.Property("Id") @@ -50,6 +75,9 @@ namespace PizzeriaDatabaseImplement.Migrations NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + b.Property("ClientId") + .HasColumnType("integer"); + b.Property("Count") .HasColumnType("integer"); @@ -70,6 +98,8 @@ namespace PizzeriaDatabaseImplement.Migrations b.HasKey("Id"); + b.HasIndex("ClientId"); + b.HasIndex("PizzaId"); b.ToTable("Orders"); @@ -123,12 +153,20 @@ namespace PizzeriaDatabaseImplement.Migrations modelBuilder.Entity("PizzeriaDatabaseImplement.models.Order", b => { + b.HasOne("PizzeriaDatabaseImplement.models.Client", "Client") + .WithMany("Orders") + .HasForeignKey("ClientId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + b.HasOne("PizzeriaDatabaseImplement.models.Pizza", "Pizza") .WithMany("Orders") .HasForeignKey("PizzaId") .OnDelete(DeleteBehavior.Cascade) .IsRequired(); + b.Navigation("Client"); + b.Navigation("Pizza"); }); @@ -151,6 +189,11 @@ namespace PizzeriaDatabaseImplement.Migrations b.Navigation("Pizza"); }); + modelBuilder.Entity("PizzeriaDatabaseImplement.models.Client", b => + { + b.Navigation("Orders"); + }); + modelBuilder.Entity("PizzeriaDatabaseImplement.models.Component", b => { b.Navigation("PizzaComponents"); diff --git a/Pizzeria/PizzeriaDatabaseImplement/PizzeriaDatabase.cs b/Pizzeria/PizzeriaDatabaseImplement/PizzeriaDatabase.cs index d7d0ce0..f0219dd 100644 --- a/Pizzeria/PizzeriaDatabaseImplement/PizzeriaDatabase.cs +++ b/Pizzeria/PizzeriaDatabaseImplement/PizzeriaDatabase.cs @@ -15,6 +15,7 @@ namespace PizzeriaDatabaseImplement } public virtual DbSet Components { set; get; } public virtual DbSet Pizzas { set; get; } + public virtual DbSet Clients { set; get; } public virtual DbSet PizzaComponents { set; get; } public virtual DbSet Orders { set; get; } } diff --git a/Pizzeria/PizzeriaDatabaseImplement/models/Client.cs b/Pizzeria/PizzeriaDatabaseImplement/models/Client.cs new file mode 100644 index 0000000..f4a6af4 --- /dev/null +++ b/Pizzeria/PizzeriaDatabaseImplement/models/Client.cs @@ -0,0 +1,64 @@ +using PizzeriaContracts.BindingModels; +using PizzeriaContracts.ViewModels; +using PizzeriaDataModels.Models; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; + +namespace PizzeriaDatabaseImplement.models +{ + public class Client : IClientModel + { + public int Id { get; private set; } + [Required] + public string ClientFIO { get; private set; } + [Required] + public string Email { get; private set; } + [Required] + public string Password { 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() + { + Id = model.Id, + ClientFIO = model.ClientFIO, + Email = model.Email, + Password = model.Password + }; + } + public static Client Create(ClientViewModel model) + { + return new Client + { + Id = model.Id, + ClientFIO = model.ClientFIO, + Email = model.Email, + Password = model.Password + }; + } + public void Update(ClientBindingModel model) + { + if (model == null) + { + return; + } + Id = model.Id; + ClientFIO = model.ClientFIO; + Email = model.Email; + Password = model.Password; + } + public ClientViewModel GetViewModel => new() + { + Id = Id, + ClientFIO = ClientFIO, + Email = Email, + Password = Password + }; + } +} diff --git a/Pizzeria/PizzeriaDatabaseImplement/models/Order.cs b/Pizzeria/PizzeriaDatabaseImplement/models/Order.cs index 37c386f..6553b00 100644 --- a/Pizzeria/PizzeriaDatabaseImplement/models/Order.cs +++ b/Pizzeria/PizzeriaDatabaseImplement/models/Order.cs @@ -10,6 +10,9 @@ namespace PizzeriaDatabaseImplement.models [Required] public int PizzaId { get; private set; } + [Required] + public int ClientId { get; private set; } + [Required] public int Count { get; private set; } @@ -25,6 +28,7 @@ namespace PizzeriaDatabaseImplement.models public DateTime? DateImplement { get; private set; } public virtual Pizza Pizza { get; set; } = new(); + public virtual Client Client { get; set; } = new(); public int Id { get; private set; } @@ -39,6 +43,8 @@ namespace PizzeriaDatabaseImplement.models Id = model.Id, PizzaId = model.PizzaId, Pizza = context.Pizzas.FirstOrDefault(x => x.Id == model.PizzaId), + ClientId = model.ClientId, + Client = context.Clients.FirstOrDefault(x => x.Id == model.ClientId), Count = model.Count, Sum = model.Sum, Status = model.Status, @@ -54,6 +60,7 @@ namespace PizzeriaDatabaseImplement.models return; } PizzaId = model.PizzaId; + ClientId = model.ClientId; Count = model.Count; Sum = model.Sum; Status = model.Status; @@ -65,6 +72,8 @@ namespace PizzeriaDatabaseImplement.models { Id = Id, PizzaId = PizzaId, + ClientId = ClientId, + ClientFIO = Client.ClientFIO, PizzaName = Pizza.PizzaName, Count = Count, Sum = Sum,