From 4057a48e0da73da9bbe759947e61c3c05f8a9560 Mon Sep 17 00:00:00 2001 From: antoc0der <1@DESKTOP-K1L8ND3> Date: Sun, 7 Apr 2024 14:10:31 +0400 Subject: [PATCH] =?UTF-8?q?=D0=BD=D0=B5=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82?= =?UTF-8?q?=D0=B0=D0=B5=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/MainController.cs | 84 ++++++++++++ .../Controllers/WeatherForecastController.cs | 33 ----- FlowerShopRestApi/WeatherForecast.cs | 13 -- ProjectFlowerShop/FormClients.Designer.cs | 74 +++++++++++ ProjectFlowerShop/FormClients.cs | 89 +++++++++++++ ProjectFlowerShop/FormClients.resx | 120 ++++++++++++++++++ ProjectFlowerShop/MainForm.Designer.cs | 15 ++- ProjectFlowerShop/MainForm.cs | 11 ++ ProjectFlowerShop/Program.cs | 1 + 9 files changed, 391 insertions(+), 49 deletions(-) create mode 100644 FlowerShopRestApi/Controllers/MainController.cs delete mode 100644 FlowerShopRestApi/Controllers/WeatherForecastController.cs delete mode 100644 FlowerShopRestApi/WeatherForecast.cs create mode 100644 ProjectFlowerShop/FormClients.Designer.cs create mode 100644 ProjectFlowerShop/FormClients.cs create mode 100644 ProjectFlowerShop/FormClients.resx diff --git a/FlowerShopRestApi/Controllers/MainController.cs b/FlowerShopRestApi/Controllers/MainController.cs new file mode 100644 index 0000000..9703b4a --- /dev/null +++ b/FlowerShopRestApi/Controllers/MainController.cs @@ -0,0 +1,84 @@ +using FlowerShopContracts.BindingModels; +using FlowerShopContracts.BusinessLogicsContracts; +using FlowerShopContracts.SearchModels; +using FlowerShopContracts.ViewModels; +using Microsoft.AspNetCore.Mvc; + +namespace FlowerShopRestApi.Controllers +{ + [Route("api/[controller]/[action]")] + [ApiController] + public class MainController : Controller + { + private readonly ILogger _logger; + private readonly IOrderLogic _order; + private readonly IFlowerLogic _flower; + public MainController(ILogger logger, IOrderLogic order, + IFlowerLogic flower) + { + _logger = logger; + _order = order; + _flower = flower; + } + [HttpGet] + public List? GetFlowerList() + { + try + { + return _flower.ReadList(null); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка получения списка цветов"); + throw; + } + } + [HttpGet] + public FlowerViewModel? GetFlower(int flowerId) + { + try + { + return _flower.ReadElement(new FlowerSearchModel + { + Id = + flowerId + }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка получения продукта по id={Id}", + flowerId); + 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/FlowerShopRestApi/Controllers/WeatherForecastController.cs b/FlowerShopRestApi/Controllers/WeatherForecastController.cs deleted file mode 100644 index 4b63d74..0000000 --- a/FlowerShopRestApi/Controllers/WeatherForecastController.cs +++ /dev/null @@ -1,33 +0,0 @@ -using Microsoft.AspNetCore.Mvc; - -namespace FlowerShopRestApi.Controllers -{ - [ApiController] - [Route("[controller]")] - public class WeatherForecastController : ControllerBase - { - private static readonly string[] Summaries = new[] - { - "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" - }; - - private readonly ILogger _logger; - - public WeatherForecastController(ILogger logger) - { - _logger = logger; - } - - [HttpGet(Name = "GetWeatherForecast")] - public IEnumerable Get() - { - return Enumerable.Range(1, 5).Select(index => new WeatherForecast - { - Date = DateTime.Now.AddDays(index), - TemperatureC = Random.Shared.Next(-20, 55), - Summary = Summaries[Random.Shared.Next(Summaries.Length)] - }) - .ToArray(); - } - } -} \ No newline at end of file diff --git a/FlowerShopRestApi/WeatherForecast.cs b/FlowerShopRestApi/WeatherForecast.cs deleted file mode 100644 index 89e6125..0000000 --- a/FlowerShopRestApi/WeatherForecast.cs +++ /dev/null @@ -1,13 +0,0 @@ -namespace FlowerShopRestApi -{ - public class WeatherForecast - { - public DateTime Date { get; set; } - - public int TemperatureC { get; set; } - - public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); - - public string? Summary { get; set; } - } -} \ No newline at end of file diff --git a/ProjectFlowerShop/FormClients.Designer.cs b/ProjectFlowerShop/FormClients.Designer.cs new file mode 100644 index 0000000..dc6057d --- /dev/null +++ b/ProjectFlowerShop/FormClients.Designer.cs @@ -0,0 +1,74 @@ +namespace ProjectFlowerShop +{ + 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/ProjectFlowerShop/FormClients.cs b/ProjectFlowerShop/FormClients.cs new file mode 100644 index 0000000..48a61a6 --- /dev/null +++ b/ProjectFlowerShop/FormClients.cs @@ -0,0 +1,89 @@ +using FlowerShopContracts.BindingModels; +using FlowerShopContracts.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 ProjectFlowerShop +{ + 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/ProjectFlowerShop/FormClients.resx b/ProjectFlowerShop/FormClients.resx new file mode 100644 index 0000000..1af7de1 --- /dev/null +++ b/ProjectFlowerShop/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/ProjectFlowerShop/MainForm.Designer.cs b/ProjectFlowerShop/MainForm.Designer.cs index 0a9dee0..94c1534 100644 --- a/ProjectFlowerShop/MainForm.Designer.cs +++ b/ProjectFlowerShop/MainForm.Designer.cs @@ -32,6 +32,7 @@ ToolStripMenu = new ToolStripMenuItem(); КомпонентыStripMenuItem = new ToolStripMenuItem(); ЦветыStripMenuItem = new ToolStripMenuItem(); + клиентыToolStripMenuItem = new ToolStripMenuItem(); отчетыToolStripMenuItem = new ToolStripMenuItem(); списокКомпонентовToolStripMenuItem = new ToolStripMenuItem(); компонентыToolStripMenuItem = new ToolStripMenuItem(); @@ -58,7 +59,7 @@ // // ToolStripMenu // - ToolStripMenu.DropDownItems.AddRange(new ToolStripItem[] { КомпонентыStripMenuItem, ЦветыStripMenuItem }); + ToolStripMenu.DropDownItems.AddRange(new ToolStripItem[] { КомпонентыStripMenuItem, ЦветыStripMenuItem, клиентыToolStripMenuItem }); ToolStripMenu.Name = "ToolStripMenu"; ToolStripMenu.Size = new Size(117, 24); ToolStripMenu.Text = "Справочники"; @@ -66,17 +67,24 @@ // КомпонентыStripMenuItem // КомпонентыStripMenuItem.Name = "КомпонентыStripMenuItem"; - КомпонентыStripMenuItem.Size = new Size(182, 26); + КомпонентыStripMenuItem.Size = new Size(224, 26); КомпонентыStripMenuItem.Text = "Компоненты"; КомпонентыStripMenuItem.Click += КомпонентыStripMenuItem_Click; // // ЦветыStripMenuItem // ЦветыStripMenuItem.Name = "ЦветыStripMenuItem"; - ЦветыStripMenuItem.Size = new Size(182, 26); + ЦветыStripMenuItem.Size = new Size(224, 26); ЦветыStripMenuItem.Text = "Цветы"; ЦветыStripMenuItem.Click += ЦветыStripMenuItem_Click; // + // клиентыToolStripMenuItem + // + клиентыToolStripMenuItem.Name = "клиентыToolStripMenuItem"; + клиентыToolStripMenuItem.Size = new Size(224, 26); + клиентыToolStripMenuItem.Text = "Клиенты"; + клиентыToolStripMenuItem.Click += клиентыToolStripMenuItem_Click; + // // отчетыToolStripMenuItem // отчетыToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { списокКомпонентовToolStripMenuItem, компонентыToolStripMenuItem, списокЗаказовToolStripMenuItem }); @@ -203,5 +211,6 @@ private ToolStripMenuItem списокКомпонентовToolStripMenuItem; private ToolStripMenuItem компонентыToolStripMenuItem; private ToolStripMenuItem списокЗаказовToolStripMenuItem; + private ToolStripMenuItem клиентыToolStripMenuItem; } } \ No newline at end of file diff --git a/ProjectFlowerShop/MainForm.cs b/ProjectFlowerShop/MainForm.cs index f7f2661..3eeee0b 100644 --- a/ProjectFlowerShop/MainForm.cs +++ b/ProjectFlowerShop/MainForm.cs @@ -55,6 +55,7 @@ namespace ProjectFlowerShop { DataGridView.DataSource = list; DataGridView.Columns["FlowerId"].Visible = false; + DataGridView.Columns["ClientId"].Visible = false; DataGridView.Columns["FlowerName"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; } _logger.LogInformation("Загрузка заказов"); @@ -91,6 +92,7 @@ namespace ProjectFlowerShop { Id = id, FlowerId = Convert.ToInt32(DataGridView.SelectedRows[0].Cells["FlowerId"].Value), + ClientId = Convert.ToInt32(DataGridView.SelectedRows[0].Cells["ClientId"].Value), Status = Enum.Parse(DataGridView.SelectedRows[0].Cells["Status"].Value.ToString()), Count = Convert.ToInt32(DataGridView.SelectedRows[0].Cells["Count"].Value), Sum = double.Parse(DataGridView.SelectedRows[0].Cells["Sum"].Value.ToString()), @@ -215,5 +217,14 @@ namespace ProjectFlowerShop form.ShowDialog(); } } + + private void клиентыToolStripMenuItem_Click(object sender, EventArgs e) + { + var service = Program.ServiceProvider?.GetService(typeof(FormClients)); + if (service is FormClients form) + { + form.ShowDialog(); + } + } } } diff --git a/ProjectFlowerShop/Program.cs b/ProjectFlowerShop/Program.cs index cbca06d..ed336d3 100644 --- a/ProjectFlowerShop/Program.cs +++ b/ProjectFlowerShop/Program.cs @@ -51,6 +51,7 @@ namespace ProjectFlowerShop services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient();