create attributes for viewmodels
This commit is contained in:
parent
ec31fa4311
commit
513bcc2ec0
47
FlowerShop/FlowerShop/DataGridViewExtension.cs
Normal file
47
FlowerShop/FlowerShop/DataGridViewExtension.cs
Normal file
@ -0,0 +1,47 @@
|
||||
using FlowerShopContracts.Attributes;
|
||||
|
||||
namespace FlowerShopView
|
||||
{
|
||||
public static class DataGridViewExtension
|
||||
{
|
||||
public static void FillandConfigGrid<T>(this DataGridView grid, List<T>? 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -24,12 +24,7 @@ namespace FlowerShopView
|
||||
{
|
||||
try
|
||||
{
|
||||
var list = _logic.ReadList(null);
|
||||
if (list != null)
|
||||
{
|
||||
dataGridView.DataSource = list;
|
||||
dataGridView.Columns["Id"].Visible = false;
|
||||
}
|
||||
dataGridView.FillandConfigGrid(_logic.ReadList(null));
|
||||
_logger.LogInformation("Загрузка клиентов");
|
||||
}
|
||||
catch (Exception ex)
|
||||
|
@ -22,14 +22,7 @@ namespace FlowerShopView
|
||||
{
|
||||
try
|
||||
{
|
||||
var list = _logic.ReadList(null);
|
||||
if (list != null)
|
||||
{
|
||||
dataGridView.DataSource = list;
|
||||
dataGridView.Columns["Id"].Visible = false;
|
||||
dataGridView.Columns["ComponentName"].AutoSizeMode =
|
||||
DataGridViewAutoSizeColumnMode.Fill;
|
||||
}
|
||||
dataGridView.FillandConfigGrid(_logic.ReadList(null));
|
||||
_logger.LogInformation("Загрузка компонентов");
|
||||
}
|
||||
catch (Exception ex)
|
||||
|
@ -24,19 +24,12 @@ namespace FlowerShopView
|
||||
{
|
||||
try
|
||||
{
|
||||
var list = _logic.ReadList(null);
|
||||
if (list != null)
|
||||
{
|
||||
dataGridView.DataSource = list;
|
||||
dataGridView.Columns["Id"].Visible = false;
|
||||
dataGridView.Columns["FlowerComponents"].Visible = false;
|
||||
dataGridView.Columns["FlowerName"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
|
||||
}
|
||||
_logger.LogInformation("Загрузка компонентов");
|
||||
dataGridView.FillandConfigGrid(_logic.ReadList(null));
|
||||
_logger.LogInformation("Загрузка цветов");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка загрузки компонентов");
|
||||
_logger.LogError(ex, "Ошибка загрузки цветов");
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -21,15 +21,7 @@ namespace FlowerShopView
|
||||
{
|
||||
try
|
||||
{
|
||||
var List = _implementerLogic.ReadList(null);
|
||||
|
||||
if (List != null)
|
||||
{
|
||||
DataGridView.DataSource = List;
|
||||
DataGridView.Columns["Id"].Visible = false;
|
||||
DataGridView.Columns["ImplementerFIO"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
|
||||
}
|
||||
|
||||
DataGridView.FillandConfigGrid(_implementerLogic.ReadList(null));
|
||||
_logger.LogInformation("Загрузка исполнителей");
|
||||
}
|
||||
catch (Exception ex)
|
||||
|
@ -20,16 +20,7 @@ namespace FlowerShopView
|
||||
{
|
||||
try
|
||||
{
|
||||
var List = _messageLogic.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(_messageLogic.ReadList(null));
|
||||
_logger.LogInformation("Загрузка почтовых собщений");
|
||||
}
|
||||
catch (Exception ex)
|
||||
|
@ -25,18 +25,10 @@ namespace FlowerShopView
|
||||
}
|
||||
private void LoadData()
|
||||
{
|
||||
_logger.LogInformation("Загрузка заказов");
|
||||
try
|
||||
{
|
||||
var list = _orderLogic.ReadList(null);
|
||||
if (list != null)
|
||||
{
|
||||
dataGridView.DataSource = list;
|
||||
dataGridView.Columns["FlowerId"].Visible = false;
|
||||
dataGridView.Columns["ClientId"].Visible = false;
|
||||
dataGridView.Columns["ClientEmail"].Visible = false;
|
||||
dataGridView.Columns["ImplementerId"].Visible = false;
|
||||
}
|
||||
dataGridView.FillandConfigGrid(_orderLogic.ReadList(null));
|
||||
_logger.LogInformation("Загрузка заказов");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
22
FlowerShop/FlowerShopContracts/Attributes/ColumnAttribute.cs
Normal file
22
FlowerShop/FlowerShopContracts/Attributes/ColumnAttribute.cs
Normal file
@ -0,0 +1,22 @@
|
||||
namespace FlowerShopContracts.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; }
|
||||
}
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
namespace FlowerShopContracts.Attributes
|
||||
{
|
||||
public enum GridViewAutoSize
|
||||
{
|
||||
NotSet = 0,
|
||||
None = 1,
|
||||
ColumnHeader = 2,
|
||||
AllCellsExceptHeader = 4,
|
||||
AllCells = 6,
|
||||
DisplayedCellsExceptHeader = 8,
|
||||
DisplayedCells = 10,
|
||||
Fill = 16
|
||||
}
|
||||
}
|
@ -1,17 +1,18 @@
|
||||
using FlowerShopDataModels.Models;
|
||||
using System.ComponentModel;
|
||||
using FlowerShopContracts.Attributes;
|
||||
using FlowerShopDataModels.Models;
|
||||
|
||||
namespace FlowerShopContracts.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: "ФИО клиента", width: 150)]
|
||||
public string ClientFIO { get; set; } = string.Empty;
|
||||
[Column(title: "Логин (эл. почта)", gridViewAutoSize: GridViewAutoSize.Fill, isUseAutoSize: true)]
|
||||
public string Email { get; set; } = string.Empty;
|
||||
[Column(title: "Пароль", width: 150)]
|
||||
public string Password { get; set; } = string.Empty;
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -1,16 +1,16 @@
|
||||
using FlowerShopDataModels.Models;
|
||||
using System.ComponentModel;
|
||||
using FlowerShopContracts.Attributes;
|
||||
using FlowerShopDataModels.Models;
|
||||
|
||||
namespace FlowerShopContracts.ViewModels
|
||||
{
|
||||
public class ComponentViewModel : IComponentModel
|
||||
{
|
||||
public int Id { get; set; }
|
||||
[Column(visible: false)]
|
||||
public int Id { get; set; }
|
||||
|
||||
[DisplayName("Название компонента")]
|
||||
public string ComponentName { get; set; } = string.Empty;
|
||||
|
||||
[DisplayName("Цена")]
|
||||
public double Cost { get; set; }
|
||||
[Column(title: "Название компонента", gridViewAutoSize: GridViewAutoSize.Fill, isUseAutoSize: true)]
|
||||
public string ComponentName { get; set; } = string.Empty;
|
||||
[Column(title: "Цена", width: 150)]
|
||||
public double Cost { get; set; }
|
||||
}
|
||||
}
|
||||
|
@ -1,16 +1,18 @@
|
||||
using FlowerShopDataModels.Models;
|
||||
using System.ComponentModel;
|
||||
using FlowerShopContracts.Attributes;
|
||||
using FlowerShopDataModels.Models;
|
||||
|
||||
namespace FlowerShopContracts.ViewModels
|
||||
{
|
||||
public class FlowerViewModel : IFlowerModel
|
||||
{
|
||||
[Column(visible: false)]
|
||||
public int Id { get; set; }
|
||||
[DisplayName("Название изделия")]
|
||||
[Column(title: "Название изделия", gridViewAutoSize: GridViewAutoSize.Fill, isUseAutoSize: true)]
|
||||
public string FlowerName { get; set; } = string.Empty;
|
||||
[DisplayName("Цена")]
|
||||
[Column(title: "Цена", width: 150)]
|
||||
public double Price { get; set; }
|
||||
public Dictionary<int, (IComponentModel, int)> FlowerComponents
|
||||
[Column(visible: false)]
|
||||
public Dictionary<int, (IComponentModel, int)> FlowerComponents
|
||||
{
|
||||
get;
|
||||
set;
|
||||
|
@ -1,18 +1,19 @@
|
||||
using FlowerShopDataModels.Models;
|
||||
using System.ComponentModel;
|
||||
using FlowerShopContracts.Attributes;
|
||||
using FlowerShopDataModels.Models;
|
||||
|
||||
namespace FlowerShopContracts.ViewModels
|
||||
{
|
||||
public class ImplementerViewModel : IImplementerModel
|
||||
{
|
||||
[Column(visible: false)]
|
||||
public int Id { get; set; }
|
||||
[DisplayName("ФИО исполнителя")]
|
||||
[Column(title: "ФИО исполнителя", gridViewAutoSize: GridViewAutoSize.AllCells, isUseAutoSize: true)]
|
||||
public string ImplementerFIO { get; set; } = string.Empty;
|
||||
[DisplayName("Пароль")]
|
||||
[Column(title: "Пароль", width: 100)]
|
||||
public string Password { get; set; } = string.Empty;
|
||||
[DisplayName("Опыт исполнителя")]
|
||||
[Column(title: "Опыт исполнителя" ,gridViewAutoSize: GridViewAutoSize.Fill, isUseAutoSize: true)]
|
||||
public int WorkExperience { get; set; }
|
||||
[DisplayName("Квалификация исполнителя")]
|
||||
[Column(title: "Квалификация исполнителя", gridViewAutoSize: GridViewAutoSize.Fill, isUseAutoSize: true)]
|
||||
public int Qualification { get; set; }
|
||||
}
|
||||
}
|
||||
|
@ -1,19 +1,23 @@
|
||||
using FlowerShopDataModels.Models;
|
||||
using System.ComponentModel;
|
||||
using FlowerShopContracts.Attributes;
|
||||
using FlowerShopDataModels.Models;
|
||||
|
||||
namespace FlowerShopContracts.ViewModels
|
||||
{
|
||||
public class MessageInfoViewModel : IMessageInfoModel
|
||||
{
|
||||
public string MessageId { get; set; } = string.Empty;
|
||||
public int? ClientId { get; set; }
|
||||
[DisplayName("Отправитель")]
|
||||
[Column(visible: false)]
|
||||
public int Id { get; set; }
|
||||
[Column(visible: false)]
|
||||
public string MessageId { get; set; } = string.Empty;
|
||||
[Column(visible: false)]
|
||||
public int? ClientId { get; set; }
|
||||
[Column(title: "Отправитель" ,width: 150)]
|
||||
public string SenderName { get; set; } = string.Empty;
|
||||
[DisplayName("Дата доставки")]
|
||||
[Column(title: "Дата доставки", width: 120)]
|
||||
public DateTime DateDelivery { get; set; }
|
||||
[DisplayName("Тема")]
|
||||
[Column(title: "Тема", width: 120)]
|
||||
public string Subject { get; set; } = string.Empty;
|
||||
[DisplayName("Содержание")]
|
||||
[Column(title: "Содержание", gridViewAutoSize: GridViewAutoSize.Fill, isUseAutoSize: true)]
|
||||
public string Body { get; set; } = string.Empty;
|
||||
}
|
||||
}
|
||||
|
@ -1,33 +1,36 @@
|
||||
using FlowerShopDataModels.Enums;
|
||||
using FlowerShopContracts.Attributes;
|
||||
using FlowerShopDataModels.Enums;
|
||||
using FlowerShopDataModels.Models;
|
||||
using System.ComponentModel;
|
||||
|
||||
namespace FlowerShopContracts.ViewModels
|
||||
{
|
||||
public class OrderViewModel : IOrderModel
|
||||
{
|
||||
[DisplayName("Номер")]
|
||||
[Column(title: "Номер", gridViewAutoSize: GridViewAutoSize.AllCells, isUseAutoSize: true)]
|
||||
public int Id { get; set; }
|
||||
[Column(visible: false)]
|
||||
public int FlowerId { get; set; }
|
||||
public int ClientId { get; set; }
|
||||
public int? ImplementerId { get; set; }
|
||||
[DisplayName("Изделие")]
|
||||
[Column(visible: false)]
|
||||
public int ClientId { get; set; }
|
||||
[Column(visible: false)]
|
||||
public int? ImplementerId { get; set; }
|
||||
[Column(title: "Изделие", gridViewAutoSize: GridViewAutoSize.AllCells, isUseAutoSize: true)]
|
||||
public string FlowerName { get; set; } = string.Empty;
|
||||
[DisplayName("ФИО клиента")]
|
||||
[Column(title: "ФИО клиента", width: 120)]
|
||||
public string ClientFIO { get; set; } = string.Empty;
|
||||
[DisplayName("Почта клиента")]
|
||||
[Column(title: "Почта клиента", width: 190]
|
||||
public string ClientEmail { get; set; } = string.Empty;
|
||||
[DisplayName("ФИО исполнителя")]
|
||||
[Column(title: "ФИО исполнителя", width: 120)]
|
||||
public string ImplementerFIO { get; set; }=string.Empty;
|
||||
[DisplayName("Количество")]
|
||||
[Column(title: "Количество", width: 100)]
|
||||
public int Count { get; set; }
|
||||
[DisplayName("Сумма")]
|
||||
[Column(title: "Сумма", width: 75)]
|
||||
public double Sum { get; set; }
|
||||
[DisplayName("Статус")]
|
||||
[Column(title: "Статус", width:70)]
|
||||
public OrderStatus Status { get; set; } = OrderStatus.Неизвестен;
|
||||
[DisplayName("Дата создания")]
|
||||
[Column(title: "Дата создания", width: 120)]
|
||||
public DateTime DateCreate { get; set; } = DateTime.Now;
|
||||
[DisplayName("Дата выполнения")]
|
||||
[Column(title: "Дата выполнения", width:120)]
|
||||
public DateTime? DateImplement { get; set; }
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user