From 4cf40944f5a7dd974484ff3e3cb1f203af4527f3 Mon Sep 17 00:00:00 2001 From: Ino Date: Wed, 3 May 2023 14:05:03 +0400 Subject: [PATCH] =?UTF-8?q?=D0=BD=D0=B0=D1=87=D0=B0=D0=BB=D0=BE=207=20?= =?UTF-8?q?=D1=83=D1=81=D0=BB=D0=BE=D0=B6=D0=BD=D0=B5=D0=BD=D0=BA=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BusinessLogics/MessageInfoLogic.cs | 23 +- .../BusinessLogics/OrderLogic.cs | 257 ++++++++---------- .../IceCreamShop/FormReplyMail.Designer.cs | 149 ++++++++++ IceCreamShop/IceCreamShop/FormReplyMail.cs | 73 +++++ IceCreamShop/IceCreamShop/FormReplyMail.resx | 60 ++++ .../IceCreamShop/FormViewMail.Designer.cs | 52 +++- IceCreamShop/IceCreamShop/FormViewMail.cs | 64 ++++- .../BindingModels/MessageInfoBindingModel.cs | 6 +- .../IMessageInfoLogic.cs | 6 +- .../SearchModels/MessageInfoSearchModel.cs | 6 +- .../StoragesContracts/IMessageInfoStorage.cs | 4 +- .../ViewModels/MessageInfoViewModel.cs | 8 +- .../Implements/MessageInfoStorage.cs | 36 ++- .../IceCreamShopDatabaseModelSnapshot.cs | 3 +- .../Models/MessageInfo.cs | 50 ++-- .../Implements/MessageInfoStorage.cs | 27 +- .../Models/MessageInfo.cs | 88 +++--- .../Implements/MessageInfoStorage.cs | 3 +- 18 files changed, 695 insertions(+), 220 deletions(-) create mode 100644 IceCreamShop/IceCreamShop/FormReplyMail.Designer.cs create mode 100644 IceCreamShop/IceCreamShop/FormReplyMail.cs create mode 100644 IceCreamShop/IceCreamShop/FormReplyMail.resx diff --git a/IceCreamShop/IceCreamBusinessLogic/BusinessLogics/MessageInfoLogic.cs b/IceCreamShop/IceCreamBusinessLogic/BusinessLogics/MessageInfoLogic.cs index 06f8576..ef9e706 100644 --- a/IceCreamShop/IceCreamBusinessLogic/BusinessLogics/MessageInfoLogic.cs +++ b/IceCreamShop/IceCreamBusinessLogic/BusinessLogics/MessageInfoLogic.cs @@ -27,7 +27,28 @@ namespace IceCreamBusinessLogic.BusinessLogics return true; } - public List? ReadList(MessageInfoSearchModel? model) + public MessageInfoViewModel? ReadElement(MessageInfoSearchModel model) + { + var res = _messageInfoStorage.GetElement(model); + if (res == null) + { + _logger.LogWarning("Read element operation failed"); + return null; + } + return res; + } + + public bool Update(MessageInfoBindingModel model) + { + if (_messageInfoStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } + + public List? ReadList(MessageInfoSearchModel? model) { _logger.LogInformation("ReadList. MessageId:{MessageId}.ClientId:{ClientId} ", model?.MessageId, model?.ClientId); var list = (model == null) ? _messageInfoStorage.GetFullList() : _messageInfoStorage.GetFilteredList(model); diff --git a/IceCreamShop/IceCreamBusinessLogic/BusinessLogics/OrderLogic.cs b/IceCreamShop/IceCreamBusinessLogic/BusinessLogics/OrderLogic.cs index 04d3ea8..7f262ff 100644 --- a/IceCreamShop/IceCreamBusinessLogic/BusinessLogics/OrderLogic.cs +++ b/IceCreamShop/IceCreamBusinessLogic/BusinessLogics/OrderLogic.cs @@ -25,21 +25,17 @@ namespace IceCreamBusinessLogic.BusinessLogics private readonly IShopLogic _shopLogic; private readonly IIceCreamStorage _iceCreamStorage; - public OrderLogic(ILogger logger, IOrderStorage orderStorage, AbstractMailWorker mailWorker, IClientLogic clientLogic) + public OrderLogic(ILogger logger, IOrderStorage orderStorage, IIceCreamStorage icecreamStorage, IShopLogic shopLogic, IClientLogic clientLogic, AbstractMailWorker mailWorker) { - public OrderLogic(IOrderStorage orderStorage, IShopStorage shopStorage, IShopLogic shopLogic, IIceCreamStorage iceCreamStorage, ILogger logger) - { - _orderStorage = orderStorage; - _shopStorage = shopStorage; - _logger = logger; + _logger = logger; + _shopLogic = shopLogic; + _iceCreamStorage = icecreamStorage; + _orderStorage = orderStorage; _mailWorker = mailWorker; _clientLogic = clientLogic; } - _shopLogic = shopLogic; - _iceCreamStorage = iceCreamStorage; - } - public bool CreateOrder(OrderBindingModel model) + public bool CreateOrder(OrderBindingModel model) { CheckModel(model); if(model.Status != OrderStatus.Неизвестен) @@ -111,149 +107,132 @@ namespace IceCreamBusinessLogic.BusinessLogics _logger.LogInformation("Order. OrderID:{Id}.Sum:{ Sum}. DocumentId: { DocumentId}", model.Id, model.Sum, model.IceCreamId); } - public bool SetNewStatus(OrderBindingModel rawModel, OrderStatus newStatus) - { - var viewModel = _orderStorage.GetElement(new OrderSearchModel - { - Id = rawModel.Id - }); + public bool SetNewStatus(OrderBindingModel rawModel, OrderStatus newStatus) + { + var viewModel = _orderStorage.GetElement(new OrderSearchModel + { + Id = rawModel.Id + }); - if (viewModel == null) - { - _logger.LogWarning("Order model not found"); - return false; - } + if (viewModel == null) + { + _logger.LogWarning("Order model not found"); + return false; + } - OrderBindingModel model = new OrderBindingModel - { - Id = viewModel.Id, - IceCreamId = viewModel.IceCreamId, - Status = viewModel.Status, - DateCreate = viewModel.DateCreate, - DateImplement = viewModel.DateImplement, - Count = viewModel.Count, - Sum = viewModel.Sum, - ImplementerId = viewModel.ImplementerId - }; - if (rawModel.ImplementerId.HasValue) - { - model.ImplementerId = rawModel.ImplementerId; - } - model.Status = orderStatus; - model.DateCreate = vmodel.DateCreate; - if (model.DateImplement == null) - model.DateImplement = vmodel.DateImplement; - if (vmodel.ImplementerId.HasValue) - model.ImplementerId = vmodel.ImplementerId; - model.IceCreamId = vmodel.IceCreamId; - model.Sum = vmodel.Sum; - model.Count = vmodel.Count; + OrderBindingModel model = new OrderBindingModel + { + Id = viewModel.Id, + IceCreamId = viewModel.IceCreamId, + Status = viewModel.Status, + DateCreate = viewModel.DateCreate, + DateImplement = viewModel.DateImplement, + Count = viewModel.Count, + Sum = viewModel.Sum, + ImplementerId = viewModel.ImplementerId + }; + if (rawModel.ImplementerId.HasValue) + { + model.ImplementerId = rawModel.ImplementerId; + } + + CheckModel(model); + if (model.Status + 1 != newStatus && model.Status != OrderStatus.Ожидается) + { + _logger.LogWarning("Status update to " + newStatus.ToString() + " operation failed. Order status incorrect."); + return false; + } + + if (newStatus == OrderStatus.Готов) + { + var icecream = _iceCreamStorage.GetElement(new IceCreamSearchModel() { Id = model.IceCreamId }); + if (icecream == null) + { + _logger.LogWarning("Status update to " + newStatus.ToString() + " operation failed. Icecream not found."); + return false; + } + if (CheckSupply(icecream, model.Count) == false) + { + _logger.LogWarning("Status update to " + newStatus.ToString() + " operation failed. Shop supply error."); + model.Status = OrderStatus.Ожидается; + _orderStorage.Update(model); + return false; + } + } + + model.Status = newStatus; + if (model.Status == OrderStatus.Выдан) model.DateImplement = DateTime.Now; var result = _orderStorage.Update(model); if (result == null) { + model.Status--; _logger.LogWarning("Update operation failed"); return false; } - SendOrderStatusMail(result.ClientId, $"Изменен статус заказа #{result.Id}", $"Заказ #{model.Id} изменен статус на {result.Status}"); + SendOrderStatusMail(result.ClientId, $"Изменен статус заказа #{result.Id}", $"Заказ #{result.Id} изменен статус на {result.Status}"); return true; } - - CheckModel(model); - if (model.Status + 1 != newStatus && model.Status != OrderStatus.Ожидается) - { - _logger.LogWarning("Status update to " + newStatus.ToString() + " operation failed. Order status incorrect."); - return false; - } + public bool CheckSupply(IIceCreamModel model, int count) + { + if (count <= 0) + { + _logger.LogWarning("Check then supply operation error. icecream count < 0."); + return false; + } - if (newStatus == OrderStatus.Готов) - { - var icecream = _iceCreamStorage.GetElement(new IceCreamSearchModel() { Id = model.IceCreamId }); - if (icecream == null) - { - _logger.LogWarning("Status update to " + newStatus.ToString() + " operation failed. Icecream not found."); - return false; - } - if (CheckSupply(icecream, model.Count) == false) - { - _logger.LogWarning("Status update to " + newStatus.ToString() + " operation failed. Shop supply error."); - model.Status = OrderStatus.Ожидается; - _orderStorage.Update(model); - return false; - } - } + int freeSpace = 0; + foreach (var shop in _shopStorage.GetFullList()) + { + freeSpace += shop.IceCreamMaxCount; + foreach (var icecream in shop.ShopIceCreams) + { + freeSpace -= icecream.Value.Item2; + } + } - model.Status = newStatus; - if (model.Status == OrderStatus.Выдан) model.DateImplement = DateTime.Now; - if (_orderStorage.Update(model) == null) - { - model.Status--; - _logger.LogWarning("Update operation failed"); - return false; - } - return true; - } + if (freeSpace - count < 0) + { + _logger.LogWarning("Check then supply operation error. There's no place for new icecreams in shops."); + return false; + } - public bool CheckSupply(IIceCreamModel model, int count) - { - if (count <= 0) - { - _logger.LogWarning("Check then supply operation error. icecream count < 0."); - return false; - } - - int freeSpace = 0; - foreach (var shop in _shopStorage.GetFullList()) - { - freeSpace += shop.IceCreamMaxCount; - foreach (var icecream in shop.ShopIceCreams) - { - freeSpace -= icecream.Value.Item2; - } - } - - if (freeSpace - count < 0) - { - _logger.LogWarning("Check then supply operation error. There's no place for new icecreams in shops."); - return false; - } - - foreach (var shop in _shopStorage.GetFullList()) - { - freeSpace = shop.IceCreamMaxCount; - foreach (var icecream in shop.ShopIceCreams) - { - freeSpace -= icecream.Value.Item2; - } - if (freeSpace == 0) - { - continue; - } - if (freeSpace - count >= 0) - { - if (_shopLogic.SupplyIceCreams(new() { Id = shop.Id }, model, count)) count = 0; - else - { - _logger.LogWarning("Supply error"); - return false; - } - } - if (freeSpace - count < 0) - { - if (_shopLogic.SupplyIceCreams(new() { Id = shop.Id }, model, freeSpace)) count -= freeSpace; - else - { - _logger.LogWarning("Supply error"); - return false; - } - } - if (count <= 0) - { - return true; - } - } - return false; - } + foreach (var shop in _shopStorage.GetFullList()) + { + freeSpace = shop.IceCreamMaxCount; + foreach (var icecream in shop.ShopIceCreams) + { + freeSpace -= icecream.Value.Item2; + } + if (freeSpace == 0) + { + continue; + } + if (freeSpace - count >= 0) + { + if (_shopLogic.SupplyIceCreams(new() { Id = shop.Id }, model, count)) count = 0; + else + { + _logger.LogWarning("Supply error"); + return false; + } + } + if (freeSpace - count < 0) + { + if (_shopLogic.SupplyIceCreams(new() { Id = shop.Id }, model, freeSpace)) count -= freeSpace; + else + { + _logger.LogWarning("Supply error"); + return false; + } + } + if (count <= 0) + { + return true; + } + } + return false; + } public OrderViewModel? ReadElement(OrderSearchModel model) { diff --git a/IceCreamShop/IceCreamShop/FormReplyMail.Designer.cs b/IceCreamShop/IceCreamShop/FormReplyMail.Designer.cs new file mode 100644 index 0000000..03cbcff --- /dev/null +++ b/IceCreamShop/IceCreamShop/FormReplyMail.Designer.cs @@ -0,0 +1,149 @@ +namespace IceCreamShopView +{ + partial class FormReplyMail + { + /// + /// 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() + { + textBoxReply = new TextBox(); + textBoxMainText = new TextBox(); + textBoxTitle = new TextBox(); + labelReply = new Label(); + labelMainText = new Label(); + labelTitle = new Label(); + buttonCancel = new Button(); + buttonSave = new Button(); + SuspendLayout(); + // + // textBoxReply + // + textBoxReply.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right; + textBoxReply.Location = new Point(137, 114); + textBoxReply.Multiline = true; + textBoxReply.Name = "textBoxReply"; + textBoxReply.Size = new Size(494, 182); + textBoxReply.TabIndex = 15; + // + // textBoxMainText + // + textBoxMainText.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; + textBoxMainText.Location = new Point(137, 32); + textBoxMainText.Multiline = true; + textBoxMainText.Name = "textBoxMainText"; + textBoxMainText.ReadOnly = true; + textBoxMainText.Size = new Size(494, 76); + textBoxMainText.TabIndex = 14; + // + // textBoxTitle + // + textBoxTitle.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; + textBoxTitle.Location = new Point(137, 2); + textBoxTitle.Name = "textBoxTitle"; + textBoxTitle.ReadOnly = true; + textBoxTitle.Size = new Size(494, 23); + textBoxTitle.TabIndex = 13; + // + // labelReply + // + labelReply.Location = new Point(14, 114); + labelReply.Name = "labelReply"; + labelReply.Size = new Size(117, 31); + labelReply.TabIndex = 12; + labelReply.Text = "Ответ на письмо:"; + labelReply.TextAlign = ContentAlignment.TopRight; + // + // labelMainText + // + labelMainText.Location = new Point(14, 35); + labelMainText.Name = "labelMainText"; + labelMainText.Size = new Size(117, 23); + labelMainText.TabIndex = 11; + labelMainText.Text = "Текст письма:"; + labelMainText.TextAlign = ContentAlignment.TopRight; + // + // labelTitle + // + labelTitle.Location = new Point(14, 6); + labelTitle.Name = "labelTitle"; + labelTitle.Size = new Size(117, 23); + labelTitle.TabIndex = 10; + labelTitle.Text = "Заголовок письма:"; + labelTitle.TextAlign = ContentAlignment.TopRight; + // + // buttonCancel + // + buttonCancel.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; + buttonCancel.Location = new Point(175, 302); + buttonCancel.Name = "buttonCancel"; + buttonCancel.Size = new Size(117, 30); + buttonCancel.TabIndex = 9; + buttonCancel.Text = "Отмена"; + buttonCancel.UseVisualStyleBackColor = true; + buttonCancel.Click += ButtonCancel_Click; + // + // buttonSave + // + buttonSave.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; + buttonSave.Location = new Point(298, 302); + buttonSave.Name = "buttonSave"; + buttonSave.Size = new Size(117, 30); + buttonSave.TabIndex = 8; + buttonSave.Text = "Сохранить"; + buttonSave.UseVisualStyleBackColor = true; + buttonSave.Click += ButtonSave_Click; + // + // FormReplyMail + // + AutoScaleDimensions = new SizeF(7F, 15F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(636, 344); + Controls.Add(textBoxReply); + Controls.Add(textBoxMainText); + Controls.Add(textBoxTitle); + Controls.Add(labelReply); + Controls.Add(labelMainText); + Controls.Add(labelTitle); + Controls.Add(buttonCancel); + Controls.Add(buttonSave); + Name = "FormReplyMail"; + Text = "Ответ на письмо"; + Load += FormReplyMail_Load; + ResumeLayout(false); + PerformLayout(); + } + + #endregion + + private TextBox textBoxReply; + private TextBox textBoxMainText; + private TextBox textBoxTitle; + private Label labelReply; + private Label labelMainText; + private Label labelTitle; + private Button buttonCancel; + private Button buttonSave; + } +} \ No newline at end of file diff --git a/IceCreamShop/IceCreamShop/FormReplyMail.cs b/IceCreamShop/IceCreamShop/FormReplyMail.cs new file mode 100644 index 0000000..b27a968 --- /dev/null +++ b/IceCreamShop/IceCreamShop/FormReplyMail.cs @@ -0,0 +1,73 @@ +using IceCreamBusinessLogic.MailWorker; +using IceCreamShopContracts.BusinessLogicsContracts; +using IceCreamShopContracts.ViewModels; +using Microsoft.Extensions.Logging; + +namespace IceCreamShopView +{ + public partial class FormReplyMail : Form + { + private readonly ILogger _logger; + private readonly AbstractMailWorker _mailWorker; + private readonly IMessageInfoLogic _logic; + private MessageInfoViewModel _message; + + public string MessageId { get; set; } = string.Empty; + + public FormReplyMail(ILogger logger, AbstractMailWorker mailWorker, IMessageInfoLogic logic) + { + InitializeComponent(); + _logger = logger; + _mailWorker = mailWorker; + _logic = logic; + } + + private void ButtonCancel_Click(object sender, EventArgs e) + { + DialogResult = DialogResult.Cancel; + Close(); + } + + private void ButtonSave_Click(object sender, EventArgs e) + { + _mailWorker.MailSendAsync(new() + { + MailAddress = _message.SenderName, + Subject = _message.Subject, + Text = textBoxReply.Text, + }); + _logic.Update(new() + { + MessageId = MessageId, + Reply = textBoxReply.Text, + HasRead = true, + }); + MessageBox.Show("Успешно отправлено письмо", "Отправка письма", MessageBoxButtons.OK); + DialogResult = DialogResult.OK; + Close(); + } + + private void FormReplyMail_Load(object sender, EventArgs e) + { + try + { + _message = _logic.ReadElement(new() { MessageId = MessageId }); + if (_message == null) + throw new ArgumentNullException("Письма с таким id не существует"); + Text += $"для {_message.SenderName}"; + textBoxTitle.Text = _message.Subject; + textBoxMainText.Text = _message.Body; + if (_message.HasRead is false) + { + _logic.Update(new() { MessageId = MessageId, HasRead = true, Reply = _message.Reply }); + } + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка получения собщения"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, + MessageBoxIcon.Error); + } + } + } +} diff --git a/IceCreamShop/IceCreamShop/FormReplyMail.resx b/IceCreamShop/IceCreamShop/FormReplyMail.resx new file mode 100644 index 0000000..f298a7b --- /dev/null +++ b/IceCreamShop/IceCreamShop/FormReplyMail.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/IceCreamShop/IceCreamShop/FormViewMail.Designer.cs b/IceCreamShop/IceCreamShop/FormViewMail.Designer.cs index 86ee139..87fe923 100644 --- a/IceCreamShop/IceCreamShop/FormViewMail.Designer.cs +++ b/IceCreamShop/IceCreamShop/FormViewMail.Designer.cs @@ -28,25 +28,64 @@ /// private void InitializeComponent() { + labelInfoPages = new Label(); + buttonNextPage = new Button(); + buttonPrevPage = new Button(); dataGridView = new DataGridView(); ((System.ComponentModel.ISupportInitialize)dataGridView).BeginInit(); SuspendLayout(); // + // labelInfoPages + // + labelInfoPages.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; + labelInfoPages.Location = new Point(99, 485); + labelInfoPages.Name = "labelInfoPages"; + labelInfoPages.Size = new Size(104, 19); + labelInfoPages.TabIndex = 7; + labelInfoPages.Text = "{0} страница"; + labelInfoPages.TextAlign = ContentAlignment.MiddleCenter; + // + // buttonNextPage + // + buttonNextPage.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; + buttonNextPage.Location = new Point(209, 481); + buttonNextPage.Name = "buttonNextPage"; + buttonNextPage.Size = new Size(75, 23); + buttonNextPage.TabIndex = 6; + buttonNextPage.Text = ">>>"; + buttonNextPage.UseVisualStyleBackColor = true; + buttonNextPage.Click += ButtonNextPage_Click; + // + // buttonPrevPage + // + buttonPrevPage.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; + buttonPrevPage.Location = new Point(18, 481); + buttonPrevPage.Name = "buttonPrevPage"; + buttonPrevPage.Size = new Size(75, 23); + buttonPrevPage.TabIndex = 5; + buttonPrevPage.Text = "<<<"; + buttonPrevPage.UseVisualStyleBackColor = true; + buttonPrevPage.Click += ButtonPrevPage_Click; + // // dataGridView // + dataGridView.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right; dataGridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize; - dataGridView.Dock = DockStyle.Fill; - dataGridView.Location = new Point(0, 0); + dataGridView.Location = new Point(12, 12); dataGridView.Name = "dataGridView"; dataGridView.RowTemplate.Height = 25; - dataGridView.Size = new Size(800, 450); - dataGridView.TabIndex = 1; + dataGridView.Size = new Size(872, 463); + dataGridView.TabIndex = 4; + dataGridView.RowHeaderMouseClick += dataGridView_RowHeaderMouseClick; // // FormViewMail // AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(800, 450); + ClientSize = new Size(896, 513); + Controls.Add(labelInfoPages); + Controls.Add(buttonNextPage); + Controls.Add(buttonPrevPage); Controls.Add(dataGridView); Name = "FormViewMail"; Text = "Письма"; @@ -57,6 +96,9 @@ #endregion + private Label labelInfoPages; + private Button buttonNextPage; + private Button buttonPrevPage; private DataGridView dataGridView; } } \ No newline at end of file diff --git a/IceCreamShop/IceCreamShop/FormViewMail.cs b/IceCreamShop/IceCreamShop/FormViewMail.cs index a802c58..1fe9917 100644 --- a/IceCreamShop/IceCreamShop/FormViewMail.cs +++ b/IceCreamShop/IceCreamShop/FormViewMail.cs @@ -1,4 +1,6 @@ -using IceCreamShopContracts.BusinessLogicsContracts; +using IceCreamShop; +using IceCreamShopContracts.BusinessLogicsContracts; +using IceCreamShopContracts.ViewModels; using Microsoft.Extensions.Logging; namespace IceCreamShopView @@ -7,19 +9,31 @@ namespace IceCreamShopView { private readonly ILogger _logger; private readonly IMessageInfoLogic _logic; + private int currentPage = 1; + public int pageSize = 5; public FormViewMail(ILogger logger, IMessageInfoLogic logic) { InitializeComponent(); _logger = logger; _logic = logic; + buttonPrevPage.Enabled = false; } private void FormViewMail_Load(object sender, EventArgs e) + { + MailLoad(); + } + + private bool MailLoad() { try { - var list = _logic.ReadList(null); + var list = _logic.ReadList(new() + { + Page = currentPage, + PageSize = pageSize, + }); if (list != null) { dataGridView.DataSource = list; @@ -28,12 +42,56 @@ namespace IceCreamShopView dataGridView.Columns["Body"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; } _logger.LogInformation("Загрузка списка писем"); + labelInfoPages.Text = $"{currentPage} страница"; + return true; } catch (Exception ex) { _logger.LogError(ex, "Ошибка загрузки писем"); MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, - MessageBoxIcon.Error); + MessageBoxIcon.Error); + return false; + } + } + + private void ButtonPrevPage_Click(object sender, EventArgs e) + { + if (currentPage == 1) + { + _logger.LogWarning("Неккоректный номер страницы {page}", currentPage - 1); + return; + } + currentPage--; + if (MailLoad()) + { + buttonNextPage.Enabled = true; + if (currentPage == 1) + buttonPrevPage.Enabled = false; + } + } + + private void ButtonNextPage_Click(object sender, EventArgs e) + { + currentPage++; + if (!MailLoad() || ((List)dataGridView.DataSource).Count == 0) + { + _logger.LogWarning("Out of range messages"); + currentPage--; + MailLoad(); + buttonNextPage.Enabled = false; + } + else + buttonPrevPage.Enabled = true; + } + + private void dataGridView_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e) + { + var service = Program.ServiceProvider?.GetService(typeof(FormReplyMail)); + if (service is FormReplyMail form) + { + form.MessageId = (string)dataGridView.Rows[e.RowIndex].Cells["MessageId"].Value; + form.ShowDialog(); + MailLoad(); } } } diff --git a/IceCreamShop/IceCreamShopContracts/BindingModels/MessageInfoBindingModel.cs b/IceCreamShop/IceCreamShopContracts/BindingModels/MessageInfoBindingModel.cs index 7bab4ca..f2a51d8 100644 --- a/IceCreamShop/IceCreamShopContracts/BindingModels/MessageInfoBindingModel.cs +++ b/IceCreamShop/IceCreamShopContracts/BindingModels/MessageInfoBindingModel.cs @@ -15,5 +15,9 @@ namespace IceCreamShopContracts.BindingModels public string Body { get; set; } = string.Empty; public DateTime DateDelivery { get; set; } - } + + public bool HasRead { get; set; } + + public string? Reply { get; set; } + } } \ No newline at end of file diff --git a/IceCreamShop/IceCreamShopContracts/BusinessLogicsContracts/IMessageInfoLogic.cs b/IceCreamShop/IceCreamShopContracts/BusinessLogicsContracts/IMessageInfoLogic.cs index 9ceba41..ee3d45e 100644 --- a/IceCreamShop/IceCreamShopContracts/BusinessLogicsContracts/IMessageInfoLogic.cs +++ b/IceCreamShop/IceCreamShopContracts/BusinessLogicsContracts/IMessageInfoLogic.cs @@ -9,5 +9,9 @@ namespace IceCreamShopContracts.BusinessLogicsContracts List? ReadList(MessageInfoSearchModel? model); bool Create(MessageInfoBindingModel model); - } + + bool Update(MessageInfoBindingModel model); + + MessageInfoViewModel? ReadElement(MessageInfoSearchModel model); + } } \ No newline at end of file diff --git a/IceCreamShop/IceCreamShopContracts/SearchModels/MessageInfoSearchModel.cs b/IceCreamShop/IceCreamShopContracts/SearchModels/MessageInfoSearchModel.cs index da8e399..d05f11f 100644 --- a/IceCreamShop/IceCreamShopContracts/SearchModels/MessageInfoSearchModel.cs +++ b/IceCreamShop/IceCreamShopContracts/SearchModels/MessageInfoSearchModel.cs @@ -5,5 +5,9 @@ public int? ClientId { get; set; } public string? MessageId { get; set; } - } + + public int? Page { get; set; } + + public int? PageSize { get; set; } + } } \ No newline at end of file diff --git a/IceCreamShop/IceCreamShopContracts/StoragesContracts/IMessageInfoStorage.cs b/IceCreamShop/IceCreamShopContracts/StoragesContracts/IMessageInfoStorage.cs index 9ac5688..97442f9 100644 --- a/IceCreamShop/IceCreamShopContracts/StoragesContracts/IMessageInfoStorage.cs +++ b/IceCreamShop/IceCreamShopContracts/StoragesContracts/IMessageInfoStorage.cs @@ -13,5 +13,7 @@ namespace IceCreamShopContracts.StoragesContracts MessageInfoViewModel? GetElement(MessageInfoSearchModel model); MessageInfoViewModel? Insert(MessageInfoBindingModel model); - } + + MessageInfoViewModel? Update(MessageInfoBindingModel model); + } } \ No newline at end of file diff --git a/IceCreamShop/IceCreamShopContracts/ViewModels/MessageInfoViewModel.cs b/IceCreamShop/IceCreamShopContracts/ViewModels/MessageInfoViewModel.cs index c4e4a23..9c252bb 100644 --- a/IceCreamShop/IceCreamShopContracts/ViewModels/MessageInfoViewModel.cs +++ b/IceCreamShop/IceCreamShopContracts/ViewModels/MessageInfoViewModel.cs @@ -20,5 +20,11 @@ namespace IceCreamShopContracts.ViewModels [DisplayName("Текст")] public string Body { get; set; } = string.Empty; - } + + [DisplayName("Прочитано")] + public bool HasRead { get; set; } + + [DisplayName("Ответ")] + public string? Reply { get; set; } + } } \ No newline at end of file diff --git a/IceCreamShop/IceCreamShopDatabaseImplement/Implements/MessageInfoStorage.cs b/IceCreamShop/IceCreamShopDatabaseImplement/Implements/MessageInfoStorage.cs index 4186d90..cbd80a1 100644 --- a/IceCreamShop/IceCreamShopDatabaseImplement/Implements/MessageInfoStorage.cs +++ b/IceCreamShop/IceCreamShopDatabaseImplement/Implements/MessageInfoStorage.cs @@ -19,16 +19,20 @@ namespace IceCreamShopDatabaseImplement.Implements return null; } - public List GetFilteredList(MessageInfoSearchModel model) - { - using var context = new IceCreamShopDatabase(); - return context.Messages - .Where(x => x.ClientId == model.ClientId) - .Select(x => x.GetViewModel) - .ToList(); - } + public List GetFilteredList(MessageInfoSearchModel model) + { + using var context = new IceCreamShopDatabase(); + var res = context.Messages + .Where(x => !model.ClientId.HasValue || x.ClientId == model.ClientId) + .Select(x => x.GetViewModel); + if (!(model.Page.HasValue && model.PageSize.HasValue)) + { + return res.ToList(); + } + return res.Skip((model.Page.Value - 1) * model.PageSize.Value).Take(model.PageSize.Value).ToList(); + } - public List GetFullList() + public List GetFullList() { using var context = new IceCreamShopDatabase(); return context.Messages @@ -48,5 +52,17 @@ namespace IceCreamShopDatabaseImplement.Implements context.SaveChanges(); return newMessage.GetViewModel; } - } + + public MessageInfoViewModel? Update(MessageInfoBindingModel model) + { + using var context = new IceCreamShopDatabase(); + var res = context.Messages.FirstOrDefault(x => x.MessageId.Equals(model.MessageId)); + if (res != null) + { + res.Update(model); + context.SaveChanges(); + } + return res?.GetViewModel; + } + } } diff --git a/IceCreamShop/IceCreamShopDatabaseImplement/Migrations/IceCreamShopDatabaseModelSnapshot.cs b/IceCreamShop/IceCreamShopDatabaseImplement/Migrations/IceCreamShopDatabaseModelSnapshot.cs index ed373d3..1094860 100644 --- a/IceCreamShop/IceCreamShopDatabaseImplement/Migrations/IceCreamShopDatabaseModelSnapshot.cs +++ b/IceCreamShop/IceCreamShopDatabaseImplement/Migrations/IceCreamShopDatabaseModelSnapshot.cs @@ -320,8 +320,9 @@ namespace IceCreamShopDatabaseImplement.Migrations b.Navigation("Client"); b.Navigation("IceCream"); + }); - modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.ShopIcecream", b => + modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.ShopIcecream", b => { b.HasOne("IceCreamShopDatabaseImplement.Models.IceCream", "IceCream") .WithMany() diff --git a/IceCreamShop/IceCreamShopDatabaseImplement/Models/MessageInfo.cs b/IceCreamShop/IceCreamShopDatabaseImplement/Models/MessageInfo.cs index 9c6b65b..c88ff01 100644 --- a/IceCreamShop/IceCreamShopDatabaseImplement/Models/MessageInfo.cs +++ b/IceCreamShop/IceCreamShopDatabaseImplement/Models/MessageInfo.cs @@ -22,7 +22,11 @@ namespace IceCreamShopDatabaseImplement.Models public Client? Client { get; private set; } - public static MessageInfo? Create(MessageInfoBindingModel model) + public bool HasRead { get; private set; } + + public string? Reply { get; private set; } + + public static MessageInfo? Create(MessageInfoBindingModel model) { if (model == null) { @@ -30,23 +34,37 @@ namespace IceCreamShopDatabaseImplement.Models } return new() { - Body = model.Body, - Subject = model.Subject, - ClientId = model.ClientId, - MessageId = model.MessageId, - SenderName = model.SenderName, - DateDelivery = model.DateDelivery, - }; + Body = model.Body, + Reply = model.Reply, + HasRead = model.HasRead, + Subject = model.Subject, + ClientId = model.ClientId, + MessageId = model.MessageId, + SenderName = model.SenderName, + DateDelivery = model.DateDelivery, + }; } - public MessageInfoViewModel GetViewModel => new() + public void Update(MessageInfoBindingModel model) + { + if (model == null) + { + return; + } + Reply = model.Reply; + HasRead = model.HasRead; + } + + public MessageInfoViewModel GetViewModel => new() { - Body = Body, - Subject = Subject, - ClientId = ClientId, - MessageId = MessageId, - SenderName = SenderName, - DateDelivery = DateDelivery, - }; + Body = Body, + Reply = Reply, + HasRead = HasRead, + Subject = Subject, + ClientId = ClientId, + MessageId = MessageId, + SenderName = SenderName, + DateDelivery = DateDelivery, + }; } } diff --git a/IceCreamShop/IceCreamShopFileImplement/Implements/MessageInfoStorage.cs b/IceCreamShop/IceCreamShopFileImplement/Implements/MessageInfoStorage.cs index 1ee91b0..72b4fe0 100644 --- a/IceCreamShop/IceCreamShopFileImplement/Implements/MessageInfoStorage.cs +++ b/IceCreamShop/IceCreamShopFileImplement/Implements/MessageInfoStorage.cs @@ -25,11 +25,15 @@ namespace IceCreamShopFileImplement.Implements public List GetFilteredList(MessageInfoSearchModel model) { - return _source.Messages - .Where(x => x.ClientId == model.ClientId) - .Select(x => x.GetViewModel) - .ToList(); - } + var res = _source.Messages + .Where(x => !model.ClientId.HasValue || x.ClientId == model.ClientId) + .Select(x => x.GetViewModel); + if (!(model.Page.HasValue && model.PageSize.HasValue)) + { + return res.ToList(); + } + return res.Skip((model.Page.Value - 1) * model.PageSize.Value).Take(model.PageSize.Value).ToList(); + } public List GetFullList() { @@ -49,5 +53,16 @@ namespace IceCreamShopFileImplement.Implements _source.SaveMessages(); return newMessage.GetViewModel; } - } + + public MessageInfoViewModel? Update(MessageInfoBindingModel model) + { + var res = _source.Messages.FirstOrDefault(x => x.MessageId.Equals(model.MessageId)); + if (res != null) + { + res.Update(model); + _source.SaveMessages(); + } + return res?.GetViewModel; + } + } } diff --git a/IceCreamShop/IceCreamShopFileImplement/Models/MessageInfo.cs b/IceCreamShop/IceCreamShopFileImplement/Models/MessageInfo.cs index aa31ba3..d72f0b1 100644 --- a/IceCreamShop/IceCreamShopFileImplement/Models/MessageInfo.cs +++ b/IceCreamShop/IceCreamShopFileImplement/Models/MessageInfo.cs @@ -19,7 +19,11 @@ namespace IceCreamShopFileImplement.Models public string Body { get; private set; } = string.Empty; - public static MessageInfo? Create(MessageInfoBindingModel model) + public bool HasRead { get; private set; } + + public string? Reply { get; private set; } + + public static MessageInfo? Create(MessageInfoBindingModel model) { if (model == null) { @@ -27,13 +31,15 @@ namespace IceCreamShopFileImplement.Models } return new() { - Body = model.Body, - Subject = model.Subject, - ClientId = model.ClientId, - MessageId = model.MessageId, - SenderName = model.SenderName, - DateDelivery = model.DateDelivery, - }; + Body = model.Body, + Reply = model.Reply, + HasRead = model.HasRead, + Subject = model.Subject, + ClientId = model.ClientId, + MessageId = model.MessageId, + SenderName = model.SenderName, + DateDelivery = model.DateDelivery, + }; } public static MessageInfo? Create(XElement element) @@ -44,32 +50,48 @@ namespace IceCreamShopFileImplement.Models } return new() { - Body = element.Attribute("Body")!.Value, - Subject = element.Attribute("Subject")!.Value, - ClientId = Convert.ToInt32(element.Attribute("ClientId")!.Value), - MessageId = element.Attribute("MessageId")!.Value, - SenderName = element.Attribute("SenderName")!.Value, - DateDelivery = Convert.ToDateTime(element.Attribute("DateDelivery")!.Value), - }; + Body = element.Attribute("Body")!.Value, + Reply = element.Attribute("Reply")!.Value, + HasRead = Convert.ToBoolean(element.Attribute("HasRead")!.Value), + Subject = element.Attribute("Subject")!.Value, + ClientId = Convert.ToInt32(element.Attribute("ClientId")!.Value), + MessageId = element.Attribute("MessageId")!.Value, + SenderName = element.Attribute("SenderName")!.Value, + DateDelivery = Convert.ToDateTime(element.Attribute("DateDelivery")!.Value), + }; } - public MessageInfoViewModel GetViewModel => new() - { - Body = Body, - Subject = Subject, - ClientId = ClientId, - MessageId = MessageId, - SenderName = SenderName, - DateDelivery = DateDelivery, - }; + public void Update(MessageInfoBindingModel model) + { + if (model == null) + { + return; + } + Reply = model.Reply; + HasRead = model.HasRead; + } - public XElement GetXElement => new("MessageInfo", - new XAttribute("Body", Body), - new XAttribute("Subject", Subject), - new XAttribute("ClientId", ClientId), - new XAttribute("MessageId", MessageId), - new XAttribute("SenderName", SenderName), - new XAttribute("DateDelivery", DateDelivery) - ); - } + public MessageInfoViewModel GetViewModel => new() + { + Body = Body, + Reply = Reply, + HasRead = HasRead, + Subject = Subject, + ClientId = ClientId, + MessageId = MessageId, + SenderName = SenderName, + DateDelivery = DateDelivery, + }; + + public XElement GetXElement => new("MessageInfo", + new XAttribute("Body", Body), + new XAttribute("Reply", Reply), + new XAttribute("HasRead", HasRead), + new XAttribute("Subject", Subject), + new XAttribute("ClientId", ClientId), + new XAttribute("MessageId", MessageId), + new XAttribute("SenderName", SenderName), + new XAttribute("DateDelivery", DateDelivery) + ); + } } diff --git a/IceCreamShop/IceCreamShopListImplement/Implements/MessageInfoStorage.cs b/IceCreamShop/IceCreamShopListImplement/Implements/MessageInfoStorage.cs index 3917486..80b496a 100644 --- a/IceCreamShop/IceCreamShopListImplement/Implements/MessageInfoStorage.cs +++ b/IceCreamShop/IceCreamShopListImplement/Implements/MessageInfoStorage.cs @@ -1,12 +1,13 @@ using IceCreamShopContracts.BindingModels; using IceCreamShopContracts.SearchModels; +using IceCreamShopContracts.StoragesContracts; using IceCreamShopContracts.ViewModels; using IceCreamShopListImplement.Models; namespace IceCreamShopListImplement.Implements { public class MessageInfoStorage - { + { private readonly DataListSingleton _source; public MessageInfoStorage() {