diff --git a/SushiBar/SushiBar.sln b/SushiBar/SushiBar.sln index e3ec6dc..f2f42d1 100644 --- a/SushiBar/SushiBar.sln +++ b/SushiBar/SushiBar.sln @@ -17,6 +17,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SushiBarFileImplement", "Su EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SushiBarDatabaseImplement", "SushiBarDatabaseImplement\SushiBarDatabaseImplement.csproj", "{C9C66AFC-1246-4A0C-AB70-8D5C0CF311D9}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SushiBarRestApi", "SushiBarRestApi\SushiBarRestApi.csproj", "{0895AC4A-6C0A-4244-92AB-CD3AFD8B691A}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -51,6 +53,10 @@ Global {C9C66AFC-1246-4A0C-AB70-8D5C0CF311D9}.Debug|Any CPU.Build.0 = Debug|Any CPU {C9C66AFC-1246-4A0C-AB70-8D5C0CF311D9}.Release|Any CPU.ActiveCfg = Release|Any CPU {C9C66AFC-1246-4A0C-AB70-8D5C0CF311D9}.Release|Any CPU.Build.0 = Release|Any CPU + {0895AC4A-6C0A-4244-92AB-CD3AFD8B691A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {0895AC4A-6C0A-4244-92AB-CD3AFD8B691A}.Debug|Any CPU.Build.0 = Debug|Any CPU + {0895AC4A-6C0A-4244-92AB-CD3AFD8B691A}.Release|Any CPU.ActiveCfg = Release|Any CPU + {0895AC4A-6C0A-4244-92AB-CD3AFD8B691A}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/SushiBar/SushiBar/Forms/FormClients.Designer.cs b/SushiBar/SushiBar/Forms/FormClients.Designer.cs new file mode 100644 index 0000000..2cd8cae --- /dev/null +++ b/SushiBar/SushiBar/Forms/FormClients.Designer.cs @@ -0,0 +1,74 @@ +namespace SushiBar.Forms +{ + partial class FormClients + { + /// + /// 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(); + DeleteButton = new Button(); + ((System.ComponentModel.ISupportInitialize)DataGridView).BeginInit(); + SuspendLayout(); + // + // DataGridView + // + DataGridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize; + DataGridView.Location = new Point(12, 34); + DataGridView.Name = "DataGridView"; + DataGridView.RowTemplate.Height = 25; + DataGridView.Size = new Size(776, 404); + DataGridView.TabIndex = 0; + // + // DeleteButton + // + DeleteButton.Location = new Point(12, 5); + DeleteButton.Name = "DeleteButton"; + DeleteButton.Size = new Size(123, 23); + DeleteButton.TabIndex = 1; + DeleteButton.Text = "Удалить"; + DeleteButton.UseVisualStyleBackColor = true; + DeleteButton.Click += DeleteButton_Click; + // + // ClientsForm + // + AutoScaleDimensions = new SizeF(7F, 15F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(800, 450); + Controls.Add(DeleteButton); + Controls.Add(DataGridView); + Name = "ClientsForm"; + Text = "Клиенты"; + Load += ClientsForm_Load; + ((System.ComponentModel.ISupportInitialize)DataGridView).EndInit(); + ResumeLayout(false); + } + + #endregion + + private DataGridView DataGridView; + private Button DeleteButton; + } +} \ No newline at end of file diff --git a/SushiBar/SushiBar/Forms/FormClients.cs b/SushiBar/SushiBar/Forms/FormClients.cs new file mode 100644 index 0000000..dc6d81a --- /dev/null +++ b/SushiBar/SushiBar/Forms/FormClients.cs @@ -0,0 +1,77 @@ +using Microsoft.Extensions.Logging; +using SushiBarContracts.BindingModels; +using SushiBarContracts.BusinessLogicsContracts; + +namespace SushiBar.Forms +{ + public partial class FormClients : Form + { + private readonly ILogger _logger; + private readonly IClientLogic _logic; + public FormClients(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; + DataGridView.Columns["Email"].AutoSizeMode = + DataGridViewAutoSizeColumnMode.Fill; + DataGridView.Columns["Password"].AutoSizeMode = + DataGridViewAutoSizeColumnMode.Fill; + } + _logger.LogInformation("Загрузка клиентов"); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка загрузки клиентов"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + + private void ClientsForm_Load(object sender, EventArgs e) + { + LoadData(); + } + + private void DeleteButton_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); + _logger.LogInformation("Удаление клиента"); + try + { + if (!_logic.Delete(new ClientBindingModel + { + Id = id + })) + { + throw new Exception("Ошибка при удалении. Дополнительная информация в логах."); + } + LoadData(); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка удаления компонента"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } + } + } +} diff --git a/SushiBar/SushiBar/Forms/FormClients.resx b/SushiBar/SushiBar/Forms/FormClients.resx new file mode 100644 index 0000000..af32865 --- /dev/null +++ b/SushiBar/SushiBar/Forms/FormClients.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/SushiBar/SushiBar/Program.cs b/SushiBar/SushiBar/Program.cs index 2b7ab08..5513291 100644 --- a/SushiBar/SushiBar/Program.cs +++ b/SushiBar/SushiBar/Program.cs @@ -39,16 +39,19 @@ namespace SushiBar services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); services.AddTransient(); services.AddTransient(); 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/SushiBar/SushiBarBusinessLogic/BusinessLogics/ClientLogic.cs b/SushiBar/SushiBarBusinessLogic/BusinessLogics/ClientLogic.cs new file mode 100644 index 0000000..bf3775d --- /dev/null +++ b/SushiBar/SushiBarBusinessLogic/BusinessLogics/ClientLogic.cs @@ -0,0 +1,117 @@ +using Microsoft.Extensions.Logging; +using SushiBarContracts.BindingModels; +using SushiBarContracts.BusinessLogicsContracts; +using SushiBarContracts.SearchModels; +using SushiBarContracts.StoragesContracts; +using SushiBarContracts.ViewModels; + +namespace SushiBarBusinessLogic.BusinessLogics +{ + public class ClientLogic : IClientLogic + { + private readonly ILogger _logger; + private readonly IClientStorage _clientStorage; + public ClientLogic(ILogger logger, IClientStorage + componentStorage) + { + _logger = logger; + _clientStorage = componentStorage; + } + 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("Нет Email клиента", + nameof(model.ClientFIO)); + } + if (string.IsNullOrEmpty(model.Password)) + { + throw new ArgumentNullException("Нет пароля клиента", + nameof(model.ClientFIO)); + } + _logger.LogInformation("Client. ClientFIO:{ClientFIO}." + + "Email:{ Email}. Password:{ Password}. Id: { Id} ", model.ClientFIO, model.Email, model.Password, model.Id); + var element = _clientStorage.GetElement(new ClientSearchModel + { + Email = model.Email, + }); + if (element != null && element.Id != model.Id) + { + throw new InvalidOperationException("Клиент с таким лоигном уже есть"); + } + } + } +} diff --git a/SushiBar/SushiBarContracts/BindingModels/OrderBindingModel.cs b/SushiBar/SushiBarContracts/BindingModels/OrderBindingModel.cs index 04c26e9..bff7347 100644 --- a/SushiBar/SushiBarContracts/BindingModels/OrderBindingModel.cs +++ b/SushiBar/SushiBarContracts/BindingModels/OrderBindingModel.cs @@ -7,6 +7,7 @@ namespace SushiBarContracts.BindingModels { public int Id { get; set; } public int SushiId { 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/SushiBar/SushiBarContracts/SearchModels/ClientSearchModel.cs b/SushiBar/SushiBarContracts/SearchModels/ClientSearchModel.cs index ead7631..6d93c78 100644 --- a/SushiBar/SushiBarContracts/SearchModels/ClientSearchModel.cs +++ b/SushiBar/SushiBarContracts/SearchModels/ClientSearchModel.cs @@ -5,5 +5,6 @@ public int? Id { get; set; } public string? ClientFIO { get; set; } public string? Email { get; set; } + public string? Password { get; set; } } } diff --git a/SushiBar/SushiBarContracts/SearchModels/OrderSearchModel.cs b/SushiBar/SushiBarContracts/SearchModels/OrderSearchModel.cs index bab174d..942b1fd 100644 --- a/SushiBar/SushiBarContracts/SearchModels/OrderSearchModel.cs +++ b/SushiBar/SushiBarContracts/SearchModels/OrderSearchModel.cs @@ -3,6 +3,7 @@ public class OrderSearchModel { public int? Id { get; set; } + public int? ClientId { get; set; } public DateTime? DateFrom { get; set; } public DateTime? DateTo { get; set; } } diff --git a/SushiBar/SushiBarContracts/ViewModels/OrderViewModel.cs b/SushiBar/SushiBarContracts/ViewModels/OrderViewModel.cs index 0d9b086..65096f9 100644 --- a/SushiBar/SushiBarContracts/ViewModels/OrderViewModel.cs +++ b/SushiBar/SushiBarContracts/ViewModels/OrderViewModel.cs @@ -10,6 +10,9 @@ namespace SushiBarContracts.ViewModels public int Id { get; set; } public int SushiId { get; set; } [DisplayName("Суши")] + public int ClientId { get; set; } + [DisplayName("Клиент")] + public string ClientFIO { get; set; } = string.Empty; public string SushiName { get; set; } = string.Empty; [DisplayName("Количество")] public int Count { get; set; } diff --git a/SushiBar/SushiBarDataModels/Models/IOrderModel.cs b/SushiBar/SushiBarDataModels/Models/IOrderModel.cs index 3770e12..7330013 100644 --- a/SushiBar/SushiBarDataModels/Models/IOrderModel.cs +++ b/SushiBar/SushiBarDataModels/Models/IOrderModel.cs @@ -5,6 +5,7 @@ namespace SushiBarDataModels.Models public interface IOrderModel : IId { int SushiId { get; } + int ClientId { get; } int Count { get; } double Sum { get; } OrderStatus Status { get; } diff --git a/SushiBar/SushiBarDatabaseImplement/Implements/ClientStorage.cs b/SushiBar/SushiBarDatabaseImplement/Implements/ClientStorage.cs index 998a57f..6148da9 100644 --- a/SushiBar/SushiBarDatabaseImplement/Implements/ClientStorage.cs +++ b/SushiBar/SushiBarDatabaseImplement/Implements/ClientStorage.cs @@ -20,10 +20,10 @@ namespace SushiBarDatabaseImplement.Implements { using var context = new SushiBarDatabase(); return context.Clients - .Where(c => - (model.Id.HasValue && c.Id == model.Id) || - (!string.IsNullOrEmpty(model.ClientFIO) && model.ClientFIO == c.ClientFIO) || - (!string.IsNullOrEmpty(model.Email) && model.Email == c.Email)) + .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(); } diff --git a/SushiBar/SushiBarDatabaseImplement/Implements/OrderStorage.cs b/SushiBar/SushiBarDatabaseImplement/Implements/OrderStorage.cs index aaa3770..78b8b67 100644 --- a/SushiBar/SushiBarDatabaseImplement/Implements/OrderStorage.cs +++ b/SushiBar/SushiBarDatabaseImplement/Implements/OrderStorage.cs @@ -15,6 +15,7 @@ namespace SushiBarDatabaseImplement.Implements using var context = new SushiBarDatabase(); return context.Orders .Include(o => o.Sushi) + .Include(o => o.Client) .Select(o => o.GetViewModel) .ToList(); } @@ -23,9 +24,13 @@ namespace SushiBarDatabaseImplement.Implements using var context = new SushiBarDatabase(); return context.Orders .Include(o => o.Sushi) - .Where(o => - (model.Id.HasValue && o.Id == model.Id) || - (model.DateFrom.HasValue && model.DateTo.HasValue && model.DateFrom < o.DateCreate && o.DateCreate < model.DateTo)) + .Include(o => o.Client) + .Where(o => ( + (!model.Id.HasValue || o.Id == model.Id) && + (!model.DateFrom.HasValue || o.DateCreate >= model.DateFrom) && + (!model.DateTo.HasValue || o.DateCreate <= model.DateTo) && + (!model.ClientId.HasValue || o.ClientId == model.ClientId) + )) .Select(o => o.GetViewModel) .ToList(); } @@ -38,6 +43,7 @@ namespace SushiBarDatabaseImplement.Implements using var context = new SushiBarDatabase(); return context.Orders .Include (o => o.Sushi) + .Include(o => o.Client) .FirstOrDefault(o => o.Id == model.Id)?.GetViewModel; } public OrderViewModel? Insert(OrderBindingModel model) @@ -58,8 +64,10 @@ namespace SushiBarDatabaseImplement.Implements using var transaction = context.Database.BeginTransaction(); try { - var order = context.Orders.Include(o => o.Sushi).FirstOrDefault(rec => - rec.Id == model.Id); + var order = context.Orders + .Include(o => o.Sushi) + .Include(o => o.Client) + .FirstOrDefault(o => o.Id == model.Id); if (order == null) { return null; @@ -80,6 +88,7 @@ namespace SushiBarDatabaseImplement.Implements using var context = new SushiBarDatabase(); var element = context.Orders .Include(o => o.Sushi) + .Include(o => o.Client) .FirstOrDefault(o => o.Id == model.Id); if (element != null) { diff --git a/SushiBar/SushiBarDatabaseImplement/Migrations/20240424132544_InitialCreate.Designer.cs b/SushiBar/SushiBarDatabaseImplement/Migrations/20240504135651_rezero.Designer.cs similarity index 80% rename from SushiBar/SushiBarDatabaseImplement/Migrations/20240424132544_InitialCreate.Designer.cs rename to SushiBar/SushiBarDatabaseImplement/Migrations/20240504135651_rezero.Designer.cs index 1aa7242..6e8dc33 100644 --- a/SushiBar/SushiBarDatabaseImplement/Migrations/20240424132544_InitialCreate.Designer.cs +++ b/SushiBar/SushiBarDatabaseImplement/Migrations/20240504135651_rezero.Designer.cs @@ -12,8 +12,8 @@ using SushiBarDatabaseImplement; namespace SushiBarDatabaseImplement.Migrations { [DbContext(typeof(SushiBarDatabase))] - [Migration("20240424132544_InitialCreate")] - partial class InitialCreate + [Migration("20240504135651_rezero")] + partial class rezero { /// protected override void BuildTargetModel(ModelBuilder modelBuilder) @@ -25,6 +25,31 @@ namespace SushiBarDatabaseImplement.Migrations NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); + modelBuilder.Entity("SushiBarDatabaseImplement.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("SushiBarDatabaseImplement.Models.Component", b => { b.Property("Id") @@ -53,6 +78,9 @@ namespace SushiBarDatabaseImplement.Migrations NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + b.Property("ClientId") + .HasColumnType("integer"); + b.Property("Count") .HasColumnType("integer"); @@ -73,6 +101,8 @@ namespace SushiBarDatabaseImplement.Migrations b.HasKey("Id"); + b.HasIndex("ClientId"); + b.HasIndex("SushiId"); b.ToTable("Orders"); @@ -126,12 +156,20 @@ namespace SushiBarDatabaseImplement.Migrations modelBuilder.Entity("SushiBarDatabaseImplement.Models.Order", b => { + b.HasOne("SushiBarDatabaseImplement.Models.Client", "Client") + .WithMany() + .HasForeignKey("ClientId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + b.HasOne("SushiBarDatabaseImplement.Models.Sushi", "Sushi") .WithMany("Orders") .HasForeignKey("SushiId") .OnDelete(DeleteBehavior.Cascade) .IsRequired(); + b.Navigation("Client"); + b.Navigation("Sushi"); }); diff --git a/SushiBar/SushiBarDatabaseImplement/Migrations/20240424132544_InitialCreate.cs b/SushiBar/SushiBarDatabaseImplement/Migrations/20240504135651_rezero.cs similarity index 78% rename from SushiBar/SushiBarDatabaseImplement/Migrations/20240424132544_InitialCreate.cs rename to SushiBar/SushiBarDatabaseImplement/Migrations/20240504135651_rezero.cs index 545b5b1..b67f128 100644 --- a/SushiBar/SushiBarDatabaseImplement/Migrations/20240424132544_InitialCreate.cs +++ b/SushiBar/SushiBarDatabaseImplement/Migrations/20240504135651_rezero.cs @@ -7,11 +7,26 @@ using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; namespace SushiBarDatabaseImplement.Migrations { /// - public partial class InitialCreate : Migration + public partial class rezero : Migration { /// protected override void Up(MigrationBuilder migrationBuilder) { + 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.CreateTable( name: "Components", columns: table => new @@ -51,11 +66,18 @@ namespace SushiBarDatabaseImplement.Migrations Status = table.Column(type: "integer", nullable: false), DateCreate = table.Column(type: "timestamp without time zone", nullable: false), DateImplement = table.Column(type: "timestamp without time zone", nullable: true), - SushiId = table.Column(type: "integer", nullable: false) + SushiId = table.Column(type: "integer", nullable: false), + ClientId = table.Column(type: "integer", nullable: false) }, constraints: table => { table.PrimaryKey("PK_Orders", x => x.Id); + table.ForeignKey( + name: "FK_Orders_Clients_ClientId", + column: x => x.ClientId, + principalTable: "Clients", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); table.ForeignKey( name: "FK_Orders_Sushis_SushiId", column: x => x.SushiId, @@ -91,6 +113,11 @@ namespace SushiBarDatabaseImplement.Migrations onDelete: ReferentialAction.Cascade); }); + migrationBuilder.CreateIndex( + name: "IX_Orders_ClientId", + table: "Orders", + column: "ClientId"); + migrationBuilder.CreateIndex( name: "IX_Orders_SushiId", table: "Orders", @@ -116,6 +143,9 @@ namespace SushiBarDatabaseImplement.Migrations migrationBuilder.DropTable( name: "SushiComponents"); + migrationBuilder.DropTable( + name: "Clients"); + migrationBuilder.DropTable( name: "Components"); diff --git a/SushiBar/SushiBarDatabaseImplement/Migrations/SushiBarDatabaseModelSnapshot.cs b/SushiBar/SushiBarDatabaseImplement/Migrations/SushiBarDatabaseModelSnapshot.cs index be024d5..35a4e29 100644 --- a/SushiBar/SushiBarDatabaseImplement/Migrations/SushiBarDatabaseModelSnapshot.cs +++ b/SushiBar/SushiBarDatabaseImplement/Migrations/SushiBarDatabaseModelSnapshot.cs @@ -22,6 +22,31 @@ namespace SushiBarDatabaseImplement.Migrations NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); + modelBuilder.Entity("SushiBarDatabaseImplement.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("SushiBarDatabaseImplement.Models.Component", b => { b.Property("Id") @@ -50,6 +75,9 @@ namespace SushiBarDatabaseImplement.Migrations NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + b.Property("ClientId") + .HasColumnType("integer"); + b.Property("Count") .HasColumnType("integer"); @@ -70,6 +98,8 @@ namespace SushiBarDatabaseImplement.Migrations b.HasKey("Id"); + b.HasIndex("ClientId"); + b.HasIndex("SushiId"); b.ToTable("Orders"); @@ -123,12 +153,20 @@ namespace SushiBarDatabaseImplement.Migrations modelBuilder.Entity("SushiBarDatabaseImplement.Models.Order", b => { + b.HasOne("SushiBarDatabaseImplement.Models.Client", "Client") + .WithMany() + .HasForeignKey("ClientId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + b.HasOne("SushiBarDatabaseImplement.Models.Sushi", "Sushi") .WithMany("Orders") .HasForeignKey("SushiId") .OnDelete(DeleteBehavior.Cascade) .IsRequired(); + b.Navigation("Client"); + b.Navigation("Sushi"); }); diff --git a/SushiBar/SushiBarDatabaseImplement/Models/Order.cs b/SushiBar/SushiBarDatabaseImplement/Models/Order.cs index b40afa2..47c2809 100644 --- a/SushiBar/SushiBarDatabaseImplement/Models/Order.cs +++ b/SushiBar/SushiBarDatabaseImplement/Models/Order.cs @@ -21,6 +21,9 @@ namespace SushiBarDatabaseImplement.Models [Required] public int SushiId { get; private set; } public virtual Sushi Sushi { get; set; } + + public int ClientId { get; private set; } + public virtual Client Client { get; private set; } public static Order Create(SushiBarDatabase context, OrderBindingModel model) { if (model == null) @@ -36,7 +39,9 @@ namespace SushiBarDatabaseImplement.Models DateCreate = model.DateCreate, DateImplement = model.DateImplement, SushiId = model.SushiId, - Sushi = context.Sushis.First(x => x.Id == model.SushiId) + Sushi = context.Sushis.First(x => x.Id == model.SushiId), + ClientId = model.ClientId, + Client = context.Clients.FirstOrDefault(x => x.Id == model.ClientId) }; } public void Update(OrderBindingModel model) @@ -51,13 +56,15 @@ namespace SushiBarDatabaseImplement.Models public OrderViewModel GetViewModel => new() { Id = Id, - SushiId = SushiId, + SushiId = Sushi.Id, Count = Count, Sum = Sum, Status = Status, DateCreate = DateCreate, DateImplement = DateImplement, - SushiName = Sushi.SushiName + SushiName = Sushi.SushiName, + ClientId = Client.Id, + ClientFIO = Client.ClientFIO }; } } diff --git a/SushiBar/SushiBarDatabaseImplement/SushiBarDatabase.cs b/SushiBar/SushiBarDatabaseImplement/SushiBarDatabase.cs index 68f3807..d9138bc 100644 --- a/SushiBar/SushiBarDatabaseImplement/SushiBarDatabase.cs +++ b/SushiBar/SushiBarDatabaseImplement/SushiBarDatabase.cs @@ -9,7 +9,7 @@ namespace SushiBarDatabaseImplement { if (optionsBuilder.IsConfigured == false) { - optionsBuilder.UseNpgsql(@"Host=localhost;Database=SushiBar_db;Username=postgres;Password=postgres"); + optionsBuilder.UseNpgsql(@"Host=localhost;Database=SushiBar_db5;Username=postgres;Password=postgres"); } base.OnConfiguring(optionsBuilder); AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true); diff --git a/SushiBar/SushiBarFileImplement/Models/Order.cs b/SushiBar/SushiBarFileImplement/Models/Order.cs index 6251638..7bdda2a 100644 --- a/SushiBar/SushiBarFileImplement/Models/Order.cs +++ b/SushiBar/SushiBarFileImplement/Models/Order.cs @@ -2,11 +2,6 @@ using SushiBarContracts.ViewModels; using SushiBarDataModels.Enums; using SushiBarDataModels.Models; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; using System.Xml.Linq; namespace SushiBarFileImplement.Models @@ -15,6 +10,7 @@ namespace SushiBarFileImplement.Models { public int Id { get; private set; } public int SushiId { get; private set; } + public int ClientId { get; private set; } public int Count { get; private set; } public double Sum { get; private set; } public OrderStatus Status { get; private set; } = OrderStatus.Неизвестен; @@ -82,6 +78,6 @@ namespace SushiBarFileImplement.Models new XElement("Status", Status.ToString()), new XElement("DateCreate", DateCreate.ToString()), new XElement("DateImplement", DateImplement.ToString()) - ); + ); } } diff --git a/SushiBar/SushiBarListImplement/Models/Order.cs b/SushiBar/SushiBarListImplement/Models/Order.cs index 5897bf7..b5668ee 100644 --- a/SushiBar/SushiBarListImplement/Models/Order.cs +++ b/SushiBar/SushiBarListImplement/Models/Order.cs @@ -14,6 +14,7 @@ namespace SushiBarListImplement.Models { public int Id { get; private set; } public int SushiId { get; private set; } + public int ClientId { get; private set; } public int Count { get; private set; } public double Sum { get; private set; } public OrderStatus Status { get; private set; } = OrderStatus.Неизвестен; diff --git a/SushiBar/SushiBarRestApi/Controllers/ClientController.cs b/SushiBar/SushiBarRestApi/Controllers/ClientController.cs new file mode 100644 index 0000000..14adf9d --- /dev/null +++ b/SushiBar/SushiBarRestApi/Controllers/ClientController.cs @@ -0,0 +1,64 @@ +using Microsoft.AspNetCore.Mvc; +using SushiBarContracts.BindingModels; +using SushiBarContracts.BusinessLogicsContracts; +using SushiBarContracts.SearchModels; +using SushiBarContracts.ViewModels; + +namespace SushiBarRestApi.Controllers +{ + [Route("api/[controller]/[action]")] + [ApiController] + public class ClientController : Controller + { + private readonly ILogger _logger; + private readonly IClientLogic _logic; + public ClientController(IClientLogic logic, ILogger logger) + { + _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; + } + } + } +} \ No newline at end of file diff --git a/SushiBar/SushiBarRestApi/Controllers/MainController.cs b/SushiBar/SushiBarRestApi/Controllers/MainController.cs new file mode 100644 index 0000000..98d41cb --- /dev/null +++ b/SushiBar/SushiBarRestApi/Controllers/MainController.cs @@ -0,0 +1,81 @@ +using Microsoft.AspNetCore.Mvc; +using SushiBarContracts.BindingModels; +using SushiBarContracts.BusinessLogicsContracts; +using SushiBarContracts.SearchModels; +using SushiBarContracts.ViewModels; + +namespace SushiBarRestApi.Controllers +{ + [Route("api/[controller]/[action]")] + [ApiController] + public class MainController : Controller + { + private readonly ILogger _logger; + private readonly IOrderLogic _order; + private readonly ISushiLogic _iceCream; + public MainController(ILogger logger, IOrderLogic order, ISushiLogic iceCream) + { + _logger = logger; + _order = order; + _iceCream = iceCream; + } + [HttpGet] + public List? GetSushiList() + { + try + { + return _iceCream.ReadList(null); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка получения списка мороженного"); + throw; + } + } + [HttpGet] + public SushiViewModel? GetSushi(int iceCreamId) + { + try + { + return _iceCream.ReadElement(new SushiSearchModel + { + Id = iceCreamId + }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка получения продукта по id={Id}", iceCreamId); + throw; + } + } + [HttpGet] + public List? GetOrders(int clientId) + { + try + { + return _order.ReadList(new OrderSearchModel + { + ClientId = clientId + }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка получения списка заказов клиента id ={ Id}", clientId); + throw; + } + } + [HttpPost] + public void CreateOrder(OrderBindingModel model) + { + try + { + _order.CreateOrder(model); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка создания заказа"); + throw; + } + } + } +} diff --git a/SushiBar/SushiBarRestApi/Program.cs b/SushiBar/SushiBarRestApi/Program.cs new file mode 100644 index 0000000..e926907 --- /dev/null +++ b/SushiBar/SushiBarRestApi/Program.cs @@ -0,0 +1,36 @@ +using SushiBarBusinessLogic.BusinessLogics; +using SushiBarContracts.BusinessLogicsContracts; +using SushiBarContracts.StoragesContracts; +using SushiBarDatabaseImplement.Implements; +var builder = WebApplication.CreateBuilder(args); + +builder.Logging.SetMinimumLevel(LogLevel.Information); +builder.Logging.AddLog4Net("log4net.config"); + +// Add services to the container. +builder.Services.AddSingleton(); +builder.Services.AddSingleton(); +builder.Services.AddSingleton(); +builder.Services.AddSingleton(); +builder.Services.AddSingleton(); +builder.Services.AddSingleton(); +builder.Services.AddControllers(); + +// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle +builder.Services.AddEndpointsApiExplorer(); +builder.Services.AddSwaggerGen(); + +var app = builder.Build(); + +// Configure the HTTP request pipeline. +if (app.Environment.IsDevelopment()) +{ + app.UseSwagger(); + app.UseSwaggerUI(); +} + +app.UseAuthorization(); + +app.MapControllers(); + +app.Run(); diff --git a/SushiBar/SushiBarRestApi/Properties/launchSettings.json b/SushiBar/SushiBarRestApi/Properties/launchSettings.json new file mode 100644 index 0000000..9b3713c --- /dev/null +++ b/SushiBar/SushiBarRestApi/Properties/launchSettings.json @@ -0,0 +1,41 @@ +{ + "$schema": "http://json.schemastore.org/launchsettings.json", + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:29766", + "sslPort": 44390 + } + }, + "profiles": { + "http": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "launchUrl": "swagger", + "applicationUrl": "http://localhost:5056", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "https": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "launchUrl": "swagger", + "applicationUrl": "https://localhost:7070;http://localhost:5056", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "launchUrl": "swagger", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/SushiBar/SushiBarRestApi/SushiBarRestApi.csproj b/SushiBar/SushiBarRestApi/SushiBarRestApi.csproj new file mode 100644 index 0000000..29b2c7d --- /dev/null +++ b/SushiBar/SushiBarRestApi/SushiBarRestApi.csproj @@ -0,0 +1,20 @@ + + + + net8.0 + enable + enable + + + + + + + + + + + + + + diff --git a/SushiBar/SushiBarRestApi/appsettings.json b/SushiBar/SushiBarRestApi/appsettings.json new file mode 100644 index 0000000..10f68b8 --- /dev/null +++ b/SushiBar/SushiBarRestApi/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +} diff --git a/SushiBar/SushiBarRestApi/log4net.config b/SushiBar/SushiBarRestApi/log4net.config new file mode 100644 index 0000000..e0ed8e0 --- /dev/null +++ b/SushiBar/SushiBarRestApi/log4net.config @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + + \ No newline at end of file