diff --git a/SushiBar/SushiBar/FormClients.Designer.cs b/SushiBar/SushiBar/FormClients.Designer.cs new file mode 100644 index 0000000..b291b84 --- /dev/null +++ b/SushiBar/SushiBar/FormClients.Designer.cs @@ -0,0 +1,75 @@ +namespace SushiBar +{ + 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() + { + this.dataGridView = new System.Windows.Forms.DataGridView(); + this.buttonDelete = new System.Windows.Forms.Button(); + ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).BeginInit(); + this.SuspendLayout(); + // + // dataGridView + // + this.dataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; + this.dataGridView.Location = new System.Drawing.Point(12, 12); + this.dataGridView.Name = "dataGridView"; + this.dataGridView.RowTemplate.Height = 25; + this.dataGridView.Size = new System.Drawing.Size(649, 426); + this.dataGridView.TabIndex = 0; + // + // buttonDelete + // + this.buttonDelete.Location = new System.Drawing.Point(667, 12); + this.buttonDelete.Name = "buttonDelete"; + this.buttonDelete.Size = new System.Drawing.Size(121, 23); + this.buttonDelete.TabIndex = 1; + this.buttonDelete.Text = "Delete"; + this.buttonDelete.UseVisualStyleBackColor = true; + this.buttonDelete.Click += new System.EventHandler(this.ButtonDelete_Click); + // + // FormClients + // + this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(800, 450); + this.Controls.Add(this.buttonDelete); + this.Controls.Add(this.dataGridView); + this.Name = "FormClients"; + this.Text = "FormClient"; + this.Load += new System.EventHandler(this.FormClient_Load); + ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).EndInit(); + this.ResumeLayout(false); + + } + + #endregion + + private DataGridView dataGridView; + private Button buttonDelete; + } +} \ No newline at end of file diff --git a/SushiBar/SushiBar/FormClients.cs b/SushiBar/SushiBar/FormClients.cs new file mode 100644 index 0000000..795dd16 --- /dev/null +++ b/SushiBar/SushiBar/FormClients.cs @@ -0,0 +1,73 @@ +using Microsoft.Extensions.Logging; +using SushiBarContracts.BindingModels; +using SushiBarContracts.BusinessLogicsContracts; +using System.Windows.Forms; + +namespace SushiBar +{ + 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; + } + _logger.LogInformation("Load clients"); + } + catch (Exception ex) + { + _logger.LogError(ex, "Error load clients"); + MessageBox.Show(ex.Message, "Error", 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("Delete client?", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) + { + int id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); + _logger.LogInformation("Delete client"); + try + { + if (!_logic.Delete(new ClientBindingModel + { + Id = id + })) + { + throw new Exception("Error on deleting. Additional info below."); + } + LoadData(); + } + catch (Exception ex) + { + _logger.LogError(ex, "Error delete client"); + MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } + } + } +} diff --git a/SushiBar/SushiBar/FormClients.resx b/SushiBar/SushiBar/FormClients.resx new file mode 100644 index 0000000..f298a7b --- /dev/null +++ b/SushiBar/SushiBar/FormClients.resx @@ -0,0 +1,60 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 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/FormCreateOrder.Designer.cs b/SushiBar/SushiBar/FormCreateOrder.Designer.cs index 1437009..dc0d5ee 100644 --- a/SushiBar/SushiBar/FormCreateOrder.Designer.cs +++ b/SushiBar/SushiBar/FormCreateOrder.Designer.cs @@ -36,6 +36,8 @@ this.textBoxSum = new System.Windows.Forms.TextBox(); this.buttonSave = new System.Windows.Forms.Button(); this.buttonCancel = new System.Windows.Forms.Button(); + this.comboBoxClients = new System.Windows.Forms.ComboBox(); + this.label1 = new System.Windows.Forms.Label(); this.SuspendLayout(); // // labelSushi @@ -59,7 +61,7 @@ // labelCount // this.labelCount.AutoSize = true; - this.labelCount.Location = new System.Drawing.Point(12, 41); + this.labelCount.Location = new System.Drawing.Point(12, 67); this.labelCount.Name = "labelCount"; this.labelCount.Size = new System.Drawing.Size(40, 15); this.labelCount.TabIndex = 2; @@ -67,7 +69,7 @@ // // textBoxCount // - this.textBoxCount.Location = new System.Drawing.Point(53, 38); + this.textBoxCount.Location = new System.Drawing.Point(53, 64); this.textBoxCount.Name = "textBoxCount"; this.textBoxCount.Size = new System.Drawing.Size(197, 23); this.textBoxCount.TabIndex = 3; @@ -76,7 +78,7 @@ // labelSum // this.labelSum.AutoSize = true; - this.labelSum.Location = new System.Drawing.Point(12, 74); + this.labelSum.Location = new System.Drawing.Point(12, 100); this.labelSum.Name = "labelSum"; this.labelSum.Size = new System.Drawing.Size(31, 15); this.labelSum.TabIndex = 4; @@ -84,14 +86,14 @@ // // textBoxSum // - this.textBoxSum.Location = new System.Drawing.Point(53, 71); + this.textBoxSum.Location = new System.Drawing.Point(53, 97); this.textBoxSum.Name = "textBoxSum"; this.textBoxSum.Size = new System.Drawing.Size(197, 23); this.textBoxSum.TabIndex = 5; // // buttonSave // - this.buttonSave.Location = new System.Drawing.Point(12, 100); + this.buttonSave.Location = new System.Drawing.Point(12, 126); this.buttonSave.Name = "buttonSave"; this.buttonSave.Size = new System.Drawing.Size(105, 23); this.buttonSave.TabIndex = 6; @@ -101,7 +103,7 @@ // // buttonCancel // - this.buttonCancel.Location = new System.Drawing.Point(123, 100); + this.buttonCancel.Location = new System.Drawing.Point(123, 126); this.buttonCancel.Name = "buttonCancel"; this.buttonCancel.Size = new System.Drawing.Size(127, 23); this.buttonCancel.TabIndex = 7; @@ -109,11 +111,30 @@ this.buttonCancel.UseVisualStyleBackColor = true; this.buttonCancel.Click += new System.EventHandler(this.ButtonCancel_Click); // + // comboBoxClients + // + this.comboBoxClients.FormattingEnabled = true; + this.comboBoxClients.Location = new System.Drawing.Point(53, 35); + this.comboBoxClients.Name = "comboBoxClients"; + this.comboBoxClients.Size = new System.Drawing.Size(197, 23); + this.comboBoxClients.TabIndex = 9; + // + // label1 + // + this.label1.AutoSize = true; + this.label1.Location = new System.Drawing.Point(12, 38); + this.label1.Name = "label1"; + this.label1.Size = new System.Drawing.Size(38, 15); + this.label1.TabIndex = 8; + this.label1.Text = "Client"; + // // FormCreateOrder // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(262, 130); + this.ClientSize = new System.Drawing.Size(262, 159); + this.Controls.Add(this.comboBoxClients); + this.Controls.Add(this.label1); this.Controls.Add(this.buttonCancel); this.Controls.Add(this.buttonSave); this.Controls.Add(this.textBoxSum); @@ -140,5 +161,7 @@ private TextBox textBoxSum; private Button buttonSave; private Button buttonCancel; + private ComboBox comboBoxClients; + private Label label1; } } \ No newline at end of file diff --git a/SushiBar/SushiBar/FormCreateOrder.cs b/SushiBar/SushiBar/FormCreateOrder.cs index 0e8909b..8292383 100644 --- a/SushiBar/SushiBar/FormCreateOrder.cs +++ b/SushiBar/SushiBar/FormCreateOrder.cs @@ -10,13 +10,19 @@ namespace SushiBar private readonly ILogger _logger; private readonly ISushiLogic _logicP; private readonly IOrderLogic _logicO; + private readonly IClientLogic _logicC; - public FormCreateOrder(ILogger logger, ISushiLogic logicP, IOrderLogic logicO) + public FormCreateOrder( + ILogger logger, + ISushiLogic logicP, + IOrderLogic logicO, + IClientLogic logicC) { InitializeComponent(); _logger = logger; _logicP = logicP; _logicO = logicO; + _logicC = logicC; } private void CalcSum() @@ -51,11 +57,19 @@ namespace SushiBar comboBoxSushi.DataSource = list; comboBoxSushi.SelectedItem = null; } + var clientList = _logicC.ReadList(null); + if (clientList != null) + { + comboBoxClients.DisplayMember = "ClientFio"; + comboBoxClients.ValueMember = "Id"; + comboBoxClients.DataSource = clientList; + comboBoxClients.SelectedItem = null; + } } catch (Exception ex) { - _logger.LogError(ex, "Error on loading sushi"); + _logger.LogError(ex, "Error on loading sushi or client"); MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } @@ -78,10 +92,11 @@ namespace SushiBar var operationResult = _logicO.CreateOrder(new OrderBindingModel { SushiId = Convert.ToInt32(comboBoxSushi.SelectedValue), + ClientId = Convert.ToInt32(comboBoxClients.SelectedValue), SushiName = comboBoxSushi.Text, Count = Convert.ToInt32(textBoxCount.Text), Sum = Convert.ToDouble(textBoxSum.Text) - }); + }) ; if (!operationResult) { throw new Exception("Error on creating order. Additional info below."); diff --git a/SushiBar/SushiBar/FormMain.cs b/SushiBar/SushiBar/FormMain.cs index ec7bade..0c981b3 100644 --- a/SushiBar/SushiBar/FormMain.cs +++ b/SushiBar/SushiBar/FormMain.cs @@ -30,6 +30,7 @@ namespace SushiBar { dataGridView.DataSource = list; dataGridView.Columns["SushiId"].Visible = false; + dataGridView.Columns[""] } } catch (Exception ex) diff --git a/SushiBar/SushiBarContracts/BindingModels/OrderBindingModel.cs b/SushiBar/SushiBarContracts/BindingModels/OrderBindingModel.cs index a0a2b1b..93e8437 100644 --- a/SushiBar/SushiBarContracts/BindingModels/OrderBindingModel.cs +++ b/SushiBar/SushiBarContracts/BindingModels/OrderBindingModel.cs @@ -7,7 +7,7 @@ namespace SushiBarContracts.BindingModels { public int Id { get; set; } public int SushiId { get; set; } - public int ClientId { get; } + public int ClientId { get; set; } public string SushiName { get; set; } = string.Empty; public int Count { get; set; } public double Sum { get; set; } diff --git a/SushiBar/SushiBarDatabaseImplement/Migrations/20230326132549_AddClient.Designer.cs b/SushiBar/SushiBarDatabaseImplement/Migrations/20230326132549_AddClient.Designer.cs new file mode 100644 index 0000000..224f558 --- /dev/null +++ b/SushiBar/SushiBarDatabaseImplement/Migrations/20230326132549_AddClient.Designer.cs @@ -0,0 +1,213 @@ +// +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using SushiBarDatabaseImplement; + +#nullable disable + +namespace SushiBarDatabaseImplement.Migrations +{ + [DbContext(typeof(SushiBarDatabase))] + [Migration("20230326132549_AddClient")] + partial class AddClient + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.3") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("SushiBarDatabaseImplement.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("SushiBarDatabaseImplement.Models.Component", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComponentName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Cost") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Components"); + }); + + modelBuilder.Entity("SushiBarDatabaseImplement.Models.Order", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ClientId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("DateCreate") + .HasColumnType("datetime2"); + + b.Property("DateImplement") + .HasColumnType("datetime2"); + + b.Property("Status") + .HasColumnType("int"); + + b.Property("Sum") + .HasColumnType("float"); + + b.Property("SushiId") + .HasColumnType("int"); + + b.Property("SushiName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.HasIndex("ClientId"); + + b.HasIndex("SushiId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("SushiBarDatabaseImplement.Models.Sushi", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Price") + .HasColumnType("float"); + + b.Property("SushiName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Sushi"); + }); + + modelBuilder.Entity("SushiBarDatabaseImplement.Models.SushiComponent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComponentId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("SushiId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ComponentId"); + + b.HasIndex("SushiId"); + + b.ToTable("SushiComponents"); + }); + + 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"); + }); + + modelBuilder.Entity("SushiBarDatabaseImplement.Models.SushiComponent", b => + { + b.HasOne("SushiBarDatabaseImplement.Models.Component", "Component") + .WithMany("SushiComponent") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("SushiBarDatabaseImplement.Models.Sushi", "Sushi") + .WithMany("Components") + .HasForeignKey("SushiId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Component"); + + b.Navigation("Sushi"); + }); + + modelBuilder.Entity("SushiBarDatabaseImplement.Models.Component", b => + { + b.Navigation("SushiComponent"); + }); + + modelBuilder.Entity("SushiBarDatabaseImplement.Models.Sushi", b => + { + b.Navigation("Components"); + + b.Navigation("Orders"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/SushiBar/SushiBarDatabaseImplement/Migrations/20230326132549_AddClient.cs b/SushiBar/SushiBarDatabaseImplement/Migrations/20230326132549_AddClient.cs new file mode 100644 index 0000000..83da700 --- /dev/null +++ b/SushiBar/SushiBarDatabaseImplement/Migrations/20230326132549_AddClient.cs @@ -0,0 +1,68 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace SushiBarDatabaseImplement.Migrations +{ + /// + public partial class AddClient : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.AddColumn( + name: "ClientId", + table: "Orders", + type: "int", + nullable: false, + defaultValue: 0); + + 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.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/SushiBar/SushiBarDatabaseImplement/Migrations/SushiDatabaseModelSnapshot.cs b/SushiBar/SushiBarDatabaseImplement/Migrations/SushiDatabaseModelSnapshot.cs index 6583fcb..71843f6 100644 --- a/SushiBar/SushiBarDatabaseImplement/Migrations/SushiDatabaseModelSnapshot.cs +++ b/SushiBar/SushiBarDatabaseImplement/Migrations/SushiDatabaseModelSnapshot.cs @@ -22,6 +22,31 @@ namespace SushiBarDatabaseImplement.Migrations SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + modelBuilder.Entity("SushiBarDatabaseImplement.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("SushiBarDatabaseImplement.Models.Component", b => { b.Property("Id") @@ -50,6 +75,9 @@ namespace SushiBarDatabaseImplement.Migrations SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + b.Property("ClientId") + .HasColumnType("int"); + b.Property("Count") .HasColumnType("int"); @@ -74,6 +102,8 @@ namespace SushiBarDatabaseImplement.Migrations b.HasKey("Id"); + b.HasIndex("ClientId"); + b.HasIndex("SushiId"); b.ToTable("Orders"); @@ -127,12 +157,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"); });