diff --git a/FoodOrders/AbstractFoodOrdersContracts/Attributes/ColumnAttribute.cs b/FoodOrders/AbstractFoodOrdersContracts/Attributes/ColumnAttribute.cs new file mode 100644 index 0000000..d2d38e2 --- /dev/null +++ b/FoodOrders/AbstractFoodOrdersContracts/Attributes/ColumnAttribute.cs @@ -0,0 +1,28 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AbstractFoodOrdersContracts.Attributes +{ + [AttributeUsage(AttributeTargets.Property)] + public class ColumnAttribute : Attribute + { + public ColumnAttribute(string title = "", bool visible = true, int width + = 0, GridViewAutoSize gridViewAutoSize = GridViewAutoSize.None, bool + isUseAutoSize = false) + { + Title = title; + Visible = visible; + Width = width; + GridViewAutoSize = gridViewAutoSize; + IsUseAutoSize = isUseAutoSize; + } + public string Title { get; private set; } + public bool Visible { get; private set; } + public int Width { get; private set; } + public GridViewAutoSize GridViewAutoSize { get; private set; } + public bool IsUseAutoSize { get; private set; } + } +} diff --git a/FoodOrders/AbstractFoodOrdersContracts/Attributes/GridViewAutoSize.cs b/FoodOrders/AbstractFoodOrdersContracts/Attributes/GridViewAutoSize.cs new file mode 100644 index 0000000..0d619c2 --- /dev/null +++ b/FoodOrders/AbstractFoodOrdersContracts/Attributes/GridViewAutoSize.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AbstractFoodOrdersContracts.Attributes +{ + public enum GridViewAutoSize + { + NotSet = 0, + None = 1, + ColumnHeader = 2, + AllCellsExceptHeader = 4, + AllCells = 6, + DisplayedCellsExceptHeader = 8, + DisplayedCells = 10, + Fill = 16 + } +} diff --git a/FoodOrders/AbstractFoodOrdersContracts/BindingModels/MessageInfoBindingModel.cs b/FoodOrders/AbstractFoodOrdersContracts/BindingModels/MessageInfoBindingModel.cs index 5ec9cd6..4f749c3 100644 --- a/FoodOrders/AbstractFoodOrdersContracts/BindingModels/MessageInfoBindingModel.cs +++ b/FoodOrders/AbstractFoodOrdersContracts/BindingModels/MessageInfoBindingModel.cs @@ -15,5 +15,6 @@ namespace AbstractFoodOrdersContracts.BindingModels public DateTime DateDelivery { get; set; } public string Subject { get; set; } = string.Empty; public string Body { get; set; } = string.Empty; + public int Id { get; set; } } } diff --git a/FoodOrders/AbstractFoodOrdersContracts/ViewModels/ClientViewModel.cs b/FoodOrders/AbstractFoodOrdersContracts/ViewModels/ClientViewModel.cs index 2420e0d..9cd4815 100644 --- a/FoodOrders/AbstractFoodOrdersContracts/ViewModels/ClientViewModel.cs +++ b/FoodOrders/AbstractFoodOrdersContracts/ViewModels/ClientViewModel.cs @@ -1,4 +1,5 @@ -using AbstractFoodOrdersDataModels.Models; +using AbstractFoodOrdersContracts.Attributes; +using AbstractFoodOrdersDataModels.Models; using System; using System.Collections.Generic; using System.ComponentModel; @@ -10,13 +11,14 @@ namespace AbstractFoodOrdersContracts.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; + [Column(visible: false)] + public int Id { get; set; } + [Column(title: "ФИО клиента", gridViewAutoSize: GridViewAutoSize.Fill, isUseAutoSize: true)] + public string ClientFIO { get; set; } = string.Empty; + [Column(title: "Логин (эл. почта)", width: 200)] + public string Email { get; set; } = string.Empty; + [Column(title: "Пароль", width: 150)] + public string Password { get; set; } = string.Empty; - } + } } diff --git a/FoodOrders/AbstractFoodOrdersContracts/ViewModels/ComponentViewModel.cs b/FoodOrders/AbstractFoodOrdersContracts/ViewModels/ComponentViewModel.cs index 1aa5c75..1b11479 100644 --- a/FoodOrders/AbstractFoodOrdersContracts/ViewModels/ComponentViewModel.cs +++ b/FoodOrders/AbstractFoodOrdersContracts/ViewModels/ComponentViewModel.cs @@ -1,4 +1,5 @@ -using AbstractFoodOrdersDataModels.Models; +using AbstractFoodOrdersContracts.Attributes; +using AbstractFoodOrdersDataModels.Models; using System; using System.Collections.Generic; using System.ComponentModel; @@ -10,11 +11,12 @@ namespace AbstractFoodOrdersContracts.ViewModels { public class ComponentViewModel : IComponentModel { - public int Id { get; set; } - [DisplayName("Название компонента")] - public string ComponentName { get; set; } = string.Empty; - [DisplayName("Цена")] - public double Cost { get; set; } - } + [Column(visible: false)] + public int Id { get; set; } + [Column(title: "Название компонента", gridViewAutoSize: GridViewAutoSize.Fill, isUseAutoSize: true)] + public string ComponentName { get; set; } = string.Empty; + [Column(title: "Цена", width: 50)] + public double Cost { get; set; } + } } diff --git a/FoodOrders/AbstractFoodOrdersContracts/ViewModels/DishViewModel.cs b/FoodOrders/AbstractFoodOrdersContracts/ViewModels/DishViewModel.cs index dfab78c..0ffb0e1 100644 --- a/FoodOrders/AbstractFoodOrdersContracts/ViewModels/DishViewModel.cs +++ b/FoodOrders/AbstractFoodOrdersContracts/ViewModels/DishViewModel.cs @@ -1,4 +1,5 @@ -using AbstractFoodOrdersDataModels.Models; +using AbstractFoodOrdersContracts.Attributes; +using AbstractFoodOrdersDataModels.Models; using System; using System.Collections.Generic; using System.ComponentModel; @@ -10,12 +11,18 @@ namespace AbstractFoodOrdersContracts.ViewModels { public class DishViewModel : IDishModel { - public int Id { get; set; } - [DisplayName("Название изделия")] - public string DishName { get; set; } = string.Empty; - [DisplayName("Цена")] - public double Price { get; set; } - public Dictionary DishComponents { get; set; } = new(); - } + [Column(visible: false)] + public int Id { get; set; } + [Column(title: "Название блюда", gridViewAutoSize: GridViewAutoSize.Fill, isUseAutoSize: true)] + public string DishName { get; set; } = string.Empty; + [Column(title: "Цена", width: 75)] + public double Price { get; set; } + [Column(visible: false)] + public Dictionary DishComponents + { + get; + set; + } = new(); + } } diff --git a/FoodOrders/AbstractFoodOrdersContracts/ViewModels/ImplementerViewModel.cs b/FoodOrders/AbstractFoodOrdersContracts/ViewModels/ImplementerViewModel.cs index 6d592c1..16a76fc 100644 --- a/FoodOrders/AbstractFoodOrdersContracts/ViewModels/ImplementerViewModel.cs +++ b/FoodOrders/AbstractFoodOrdersContracts/ViewModels/ImplementerViewModel.cs @@ -1,4 +1,5 @@ -using AbstractFoodOrdersDataModels.Models; +using AbstractFoodOrdersContracts.Attributes; +using AbstractFoodOrdersDataModels.Models; using System; using System.Collections.Generic; using System.ComponentModel; @@ -10,14 +11,15 @@ namespace AbstractFoodOrdersContracts.ViewModels { public class ImplementerViewModel:IImplementerModel { + [Column(visible: false)] public int Id { get; set; } - [DisplayName("ФИО")] + [Column(title: "ФИО", gridViewAutoSize: GridViewAutoSize.Fill, isUseAutoSize: true)] public string ImplementerFIO { get; set; } = String.Empty; - [DisplayName("Пароль")] + [Column(title: "Пароль", width: 150)] public string Password { get; set; } = String.Empty; - [DisplayName("Опыт работы")] + [Column(title: "Опыт работы", width: 120)] public int WorkExperience { get; set; } - [DisplayName("Квалификация")] + [Column(title: "Квалификация", width: 120)] public int Qualification { get; set; } } } diff --git a/FoodOrders/AbstractFoodOrdersContracts/ViewModels/MessageInfoViewModel.cs b/FoodOrders/AbstractFoodOrdersContracts/ViewModels/MessageInfoViewModel.cs index 37be3df..6d3f505 100644 --- a/FoodOrders/AbstractFoodOrdersContracts/ViewModels/MessageInfoViewModel.cs +++ b/FoodOrders/AbstractFoodOrdersContracts/ViewModels/MessageInfoViewModel.cs @@ -1,4 +1,5 @@ -using AbstractFoodOrdersDataModels.Models; +using AbstractFoodOrdersContracts.Attributes; +using AbstractFoodOrdersDataModels.Models; using System; using System.Collections.Generic; using System.ComponentModel; @@ -10,15 +11,20 @@ namespace AbstractFoodOrdersContracts.ViewModels { public class MessageInfoViewModel:IMessageInfoModel { + [Column(visible: false)] public string MessageId { get; set; } = string.Empty; + [Column(visible: false)] public int? ClientId { get; set; } - [DisplayName("Отправитель")] + [Column(title: "Отправитель", width: 75)] public string SenderName { get; set; } = string.Empty; - [DisplayName("Доставлено")] + [Column(title: "Доставлено")] public DateTime DateDelivery { get; set; } - [DisplayName("Заголовок")] + [Column(title: "Заголовок", width: 75)] public string Subject { get; set; } = string.Empty; - [DisplayName("Текст")] + [Column(title: "Текст", gridViewAutoSize: GridViewAutoSize.Fill, isUseAutoSize: true)] public string Body { get; set; } = string.Empty; + + [Column(visible: false)] + public int Id { get; set; } } } diff --git a/FoodOrders/AbstractFoodOrdersContracts/ViewModels/OrderViewModel.cs b/FoodOrders/AbstractFoodOrdersContracts/ViewModels/OrderViewModel.cs index 994623e..fce7a1b 100644 --- a/FoodOrders/AbstractFoodOrdersContracts/ViewModels/OrderViewModel.cs +++ b/FoodOrders/AbstractFoodOrdersContracts/ViewModels/OrderViewModel.cs @@ -1,4 +1,5 @@ -using AbstractFoodOrdersDataModels.Enums; +using AbstractFoodOrdersContracts.Attributes; +using AbstractFoodOrdersDataModels.Enums; using AbstractFoodOrdersDataModels.Models; using System; using System.Collections.Generic; @@ -11,27 +12,31 @@ namespace AbstractFoodOrdersContracts.ViewModels { public class OrderViewModel : IOrderModel { - [DisplayName("Номер")] - public int Id { get; set; } - public int DishId { get; set; } - [DisplayName("Изделие")] - public string DishName { get; set; } = string.Empty; + [Column(title: "Номер")] + public int Id { get; set; } + [Column(visible: false)] + public int DishId { get; set; } + [Column(title: "Блюдо", gridViewAutoSize: GridViewAutoSize.Fill, isUseAutoSize: true)] + public string DishName { get; set; } = string.Empty; + [Column(visible: false)] public int? ImplementerId { get; set; } - [DisplayName("Исполнитель")] + [Column(title: "Исполнитель", gridViewAutoSize: GridViewAutoSize.Fill, isUseAutoSize: true)] public string? ImplementerFIO { get; set; } + [Column(visible: false)] public int ClientId { get; set; } - [DisplayName("ФИО клиента")] - public string ClientFIO { get; set; } = string.Empty; + [Column(title: "ФИО клиента", gridViewAutoSize: GridViewAutoSize.Fill, isUseAutoSize: true)] + public string ClientFIO { get; set; } = string.Empty; + [Column(visible: false)] public string ClientEmail { get; set; } = string.Empty; - [DisplayName("Количество")] - public int Count { get; set; } - [DisplayName("Сумма")] - public double Sum { get; set; } - [DisplayName("Статус")] - public OrderStatus Status { get; set; } = OrderStatus.Неизвестен; - [DisplayName("Дата создания")] - public DateTime DateCreate { get; set; } = DateTime.Now; - [DisplayName("Дата выполнения")] - public DateTime? DateImplement { get; set; } - } + [Column(title: "Количество", width: 100)] + public int Count { get; set; } + [Column(title: "Сумма", width: 100)] + public double Sum { get; set; } + [Column(title: "Статус", width: 125)] + public OrderStatus Status { get; set; } = OrderStatus.Неизвестен; + [Column(title: "Дата создания", width: 120)] + public DateTime DateCreate { get; set; } = DateTime.Now; + [Column(title: "Дата выполнения", width: 120)] + public DateTime? DateImplement { get; set; } + } } diff --git a/FoodOrders/AbstractFoodOrdersDataModels/Models/IMessageInfoModel.cs b/FoodOrders/AbstractFoodOrdersDataModels/Models/IMessageInfoModel.cs index 668b834..730e5a9 100644 --- a/FoodOrders/AbstractFoodOrdersDataModels/Models/IMessageInfoModel.cs +++ b/FoodOrders/AbstractFoodOrdersDataModels/Models/IMessageInfoModel.cs @@ -6,7 +6,7 @@ using System.Threading.Tasks; namespace AbstractFoodOrdersDataModels.Models { - public interface IMessageInfoModel + public interface IMessageInfoModel:IId { string MessageId { get; } int? ClientId { get; } diff --git a/FoodOrders/AbstractFoodOrdersDatabaseImplement/Models/MessageInfo.cs b/FoodOrders/AbstractFoodOrdersDatabaseImplement/Models/MessageInfo.cs index a677df4..3fe1ed4 100644 --- a/FoodOrders/AbstractFoodOrdersDatabaseImplement/Models/MessageInfo.cs +++ b/FoodOrders/AbstractFoodOrdersDatabaseImplement/Models/MessageInfo.cs @@ -26,6 +26,9 @@ namespace AbstractFoodOrdersDatabaseImplement.Models [Required] public string Body { get; set; } = string.Empty; + [NotMapped] + public int Id { get; private set; } + public static MessageInfo? Create(MessageInfoBindingModel model) { if (model == null) diff --git a/FoodOrders/FoodOrders/DataGridViewExtension.cs b/FoodOrders/FoodOrders/DataGridViewExtension.cs new file mode 100644 index 0000000..19db3ae --- /dev/null +++ b/FoodOrders/FoodOrders/DataGridViewExtension.cs @@ -0,0 +1,55 @@ +using AbstractFoodOrdersContracts.Attributes; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FoodOrders +{ + public static class DataGridViewExtension + { + public static void FillAndConfigGrid(this DataGridView grid, List? data) + { + if (data == null) + { + return; + } + grid.DataSource = data; + var type = typeof(T); + var properties = type.GetProperties(); + foreach (DataGridViewColumn column in grid.Columns) + { + var property = properties.FirstOrDefault(x => x.Name == + column.Name); + if (property == null) + { + throw new InvalidOperationException($"В типе {type.Name} не найдено свойство с именем { column.Name }"); + } + var attribute = + property.GetCustomAttributes(typeof(ColumnAttribute), true)?.SingleOrDefault(); + if (attribute == null) + { + throw new InvalidOperationException($"Не найден атрибут тип ColumnAttribute для свойства { property.Name }"); + } + // ищем нужный нам атрибут + if (attribute is ColumnAttribute columnAttr) + { + column.HeaderText = columnAttr.Title; + column.Visible = columnAttr.Visible; + if (columnAttr.IsUseAutoSize) + { + column.AutoSizeMode = + (DataGridViewAutoSizeColumnMode)Enum.Parse(typeof(DataGridViewAutoSizeColumnMode) + , columnAttr.GridViewAutoSize.ToString()); + } + else + { + column.Width = columnAttr.Width; + } + } + } + } + + } +} diff --git a/FoodOrders/FoodOrders/FormClients.cs b/FoodOrders/FoodOrders/FormClients.cs index 72e82e3..758b1eb 100644 --- a/FoodOrders/FoodOrders/FormClients.cs +++ b/FoodOrders/FoodOrders/FormClients.cs @@ -32,15 +32,9 @@ namespace FoodOrders private void LoadData() { - try + try { - var list = _logic.ReadList(null); - if (list != null) - { - dataGridViewClients.DataSource = list; - dataGridViewClients.Columns["Id"].Visible = false; - dataGridViewClients.Columns["ClientFIO"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; - } + dataGridViewClients.FillAndConfigGrid(_logic.ReadList(null)); _logger.LogInformation("Загрузка клиентов"); } catch (Exception ex) diff --git a/FoodOrders/FoodOrders/FormComponents.cs b/FoodOrders/FoodOrders/FormComponents.cs index 17680cc..4a1577e 100644 --- a/FoodOrders/FoodOrders/FormComponents.cs +++ b/FoodOrders/FoodOrders/FormComponents.cs @@ -30,24 +30,17 @@ namespace FoodOrders } private void LoadData() { - try - { - var list = _logic.ReadList(null); - if (list != null) - { - dataGridView.DataSource = list; - dataGridView.Columns["Id"].Visible = false; - dataGridView.Columns["ComponentName"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; - } - _logger.LogInformation("Загрузка компонентов"); - } - catch (Exception ex) - { - _logger.LogError(ex, "Ошибка загрузки компонентов"); - MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, - MessageBoxIcon.Error); - } - } + try + { + dataGridView.FillAndConfigGrid(_logic.ReadList(null)); + _logger.LogInformation("Загрузка компонентов"); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка загрузки компонент"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } private void ButtonAdd_Click(object sender, EventArgs e) { var service = Program.ServiceProvider?.GetService(typeof(FormComponent)); diff --git a/FoodOrders/FoodOrders/FormDish.cs b/FoodOrders/FoodOrders/FormDish.cs index 41bf6c7..c4d583c 100644 --- a/FoodOrders/FoodOrders/FormDish.cs +++ b/FoodOrders/FoodOrders/FormDish.cs @@ -38,8 +38,7 @@ namespace FoodOrders { var view = _logic.ReadElement(new DishSearchModel { - Id = - _id.Value + Id = _id.Value }); if (view != null) { diff --git a/FoodOrders/FoodOrders/FormDishes.cs b/FoodOrders/FoodOrders/FormDishes.cs index d7edcc9..50c050a 100644 --- a/FoodOrders/FoodOrders/FormDishes.cs +++ b/FoodOrders/FoodOrders/FormDishes.cs @@ -32,24 +32,17 @@ namespace FoodOrders private void LoadData() { - try - { - var list = _logic.ReadList(null); - if (list != null) - { - dataGridView.DataSource = list; - dataGridView.Columns["Id"].Visible = false; - dataGridView.Columns["DishComponents"].Visible = false; - dataGridView.Columns["DishName"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; - } - _logger.LogInformation("Загрузка изделий"); - } - catch (Exception ex) - { - _logger.LogError(ex, "Ошибка загрузки изделий"); - MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); - } - } + try + { + dataGridView.FillAndConfigGrid(_logic.ReadList(null)); + _logger.LogInformation("Загрузка изделий"); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка загрузки изделий"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } private void buttonAdd_Click(object sender, EventArgs e) { diff --git a/FoodOrders/FoodOrders/FormImplementers.cs b/FoodOrders/FoodOrders/FormImplementers.cs index 312ae9e..d3ee27b 100644 --- a/FoodOrders/FoodOrders/FormImplementers.cs +++ b/FoodOrders/FoodOrders/FormImplementers.cs @@ -33,13 +33,7 @@ namespace FoodOrders { try { - var list = _logic.ReadList(null); - if (list != null) - { - dataGridView.DataSource = list; - dataGridView.Columns["Id"].Visible = false; - dataGridView.Columns["ImplementerFIO"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; - } + dataGridView.FillAndConfigGrid(_logic.ReadList(null)); _logger.LogInformation("Загрузка исполнителей"); } catch (Exception ex) diff --git a/FoodOrders/FoodOrders/FormMails.cs b/FoodOrders/FoodOrders/FormMails.cs index d1ed859..b7cc14b 100644 --- a/FoodOrders/FoodOrders/FormMails.cs +++ b/FoodOrders/FoodOrders/FormMails.cs @@ -27,14 +27,7 @@ namespace FoodOrders { try { - var list = _logic.ReadList(null); - if (list != null) - { - dataGridView.DataSource = list; - dataGridView.Columns["MessageId"].Visible = false; - dataGridView.Columns["ClientId"].Visible = false; - dataGridView.Columns["Body"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; - } + dataGridView.FillAndConfigGrid(_logic.ReadList(null)); _logger.LogInformation("Загрузка писем"); } catch (Exception ex) diff --git a/FoodOrders/FoodOrders/FormMain.cs b/FoodOrders/FoodOrders/FormMain.cs index a224318..c97a32d 100644 --- a/FoodOrders/FoodOrders/FormMain.cs +++ b/FoodOrders/FoodOrders/FormMain.cs @@ -34,22 +34,15 @@ namespace FoodOrders } private void LoadData() { + try { - var list = _orderLogic.ReadList(null); - if (list != null) - { - dataGridView.DataSource = list; - dataGridView.Columns["ClientId"].Visible = false; - dataGridView.Columns["DishId"].Visible = false; - dataGridView.Columns["ImplementerId"].Visible = false; - dataGridView.Columns["ClientEmail"].Visible = false; - } + dataGridView.FillAndConfigGrid(_orderLogic.ReadList(null)); _logger.LogInformation("Загрузка заказов"); } catch (Exception ex) { - _logger.LogError(ex, "Ошибка загрузки заказов"); + _logger.LogError(ex, "Ошибка загрузки изделий"); MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); } }