Agliullov D. A. Lab Work 7 Hard #20

Closed
d.agliullov wants to merge 34 commits from Lab7_Hard into Lab6_Hard
8 changed files with 152 additions and 7 deletions
Showing only changes of commit 57aecf6cb6 - Show all commits

View File

@ -3,6 +3,7 @@ using ConfectioneryContracts.BusinessLogicsContracts;
using ConfectioneryContracts.SearchModels;
using ConfectioneryContracts.StoragesContract;
using ConfectioneryContracts.ViewModels;
using DocumentFormat.OpenXml.EMMA;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
@ -16,10 +17,10 @@ namespace ConfectioneryBusinessLogic
{
private readonly ILogger _logger;
private readonly IMessageInfoStorage _messageInfoStorage;
public MessageInfoLogic(ILogger<MessageInfoLogic> logger, IMessageInfoStorage MessageInfoStorage)
public MessageInfoLogic(ILogger<MessageInfoLogic> logger, IMessageInfoStorage messageInfoStorage)
{
_logger = logger;
_messageInfoStorage = MessageInfoStorage;
_messageInfoStorage = messageInfoStorage;
}
public bool Create(MessageInfoBindingModel model)
@ -44,5 +45,18 @@ namespace ConfectioneryBusinessLogic
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
return list;
}
public List<MessageInfoViewModel>? ReadPage(int page, int pageSize)
{
_logger.LogInformation("ReadPage. page: {page}, pageSize: {pageSize} ", page, pageSize);
var list = _messageInfoStorage.GetListOnPage(page, pageSize);
if (list == null)
{
_logger.LogWarning("ReadPage. Uncorrect range messages for page");
return null;
}
_logger.LogInformation("ReadPage. Count:{Count}", list.Count);
return list;
}
}
}

View File

@ -24,6 +24,17 @@ namespace ConfectioneryFileImplement
return null;
}
public List<MessageInfoViewModel>? GetListOnPage(int page, int pageSize)
{
if (page * pageSize >= _source.Messages.Count)
{
return null;
}
return _source.Messages.Skip((page - 1) * pageSize).Take(pageSize)
.Select(x => x.GetViewModel)
.ToList();
}
public List<MessageInfoViewModel> GetFilteredList(MessageInfoSearchModel model)
{
return _source.Messages

View File

@ -24,6 +24,20 @@ namespace ConfectioneryListImplement
return null;
}
public List<MessageInfoViewModel>? GetListOnPage(int page, int pageSize)
{
if (page * pageSize >= _source.Messages.Count)
{
return null;
}
List<MessageInfoViewModel> result = new();
for (var i = (page - 1) * pageSize; i < page * pageSize; i++)
{
result.Add(_source.Messages[i].GetViewModel);
}
return result;
}
public List<MessageInfoViewModel> GetFilteredList(MessageInfoSearchModel model)
{
List<MessageInfoViewModel> result = new();

View File

@ -29,24 +29,62 @@
private void InitializeComponent()
{
dataGridView = new DataGridView();
buttonPrevPage = new Button();
buttonNextPage = new Button();
labelInfoPages = new Label();
((System.ComponentModel.ISupportInitialize)dataGridView).BeginInit();
SuspendLayout();
//
// 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.Name = "dataGridView";
dataGridView.RowTemplate.Height = 25;
dataGridView.Size = new Size(803, 450);
dataGridView.Size = new Size(730, 454);
dataGridView.TabIndex = 0;
//
// buttonPrevPage
//
buttonPrevPage.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
buttonPrevPage.Location = new Point(12, 460);
buttonPrevPage.Name = "buttonPrevPage";
buttonPrevPage.Size = new Size(75, 23);
buttonPrevPage.TabIndex = 1;
buttonPrevPage.Text = "<<<";
buttonPrevPage.UseVisualStyleBackColor = true;
buttonPrevPage.Click += ButtonPrevPage_Click;
//
// buttonNextPage
//
buttonNextPage.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
buttonNextPage.Location = new Point(203, 460);
buttonNextPage.Name = "buttonNextPage";
buttonNextPage.Size = new Size(75, 23);
buttonNextPage.TabIndex = 2;
buttonNextPage.Text = ">>>";
buttonNextPage.UseVisualStyleBackColor = true;
buttonNextPage.Click += ButtonNextPage_Click;
//
// labelInfoPages
//
labelInfoPages.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
labelInfoPages.Location = new Point(93, 464);
labelInfoPages.Name = "labelInfoPages";
labelInfoPages.Size = new Size(104, 19);
labelInfoPages.TabIndex = 3;
labelInfoPages.Text = "{0} страница";
labelInfoPages.TextAlign = ContentAlignment.MiddleCenter;
//
// FormViewMail
//
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(800, 450);
ClientSize = new Size(727, 498);
Controls.Add(labelInfoPages);
Controls.Add(buttonNextPage);
Controls.Add(buttonPrevPage);
Controls.Add(dataGridView);
Name = "FormViewMail";
Text = "Письма";
@ -58,5 +96,8 @@
#endregion
private DataGridView dataGridView;
private Button buttonPrevPage;
private Button buttonNextPage;
private Label labelInfoPages;
}
}

View File

@ -10,6 +10,7 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ConfectioneryContracts.ViewModels;
namespace ConfectioneryView
{
@ -17,19 +18,27 @@ namespace ConfectioneryView
{
private readonly ILogger _logger;
private readonly IMessageInfoLogic _logic;
private int currentPage = 1;
public int pageSize = 5;
public FormViewMail(ILogger<FormViewMail> 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.ReadPage(currentPage, pageSize);
if (list != null)
{
dataGridView.DataSource = list;
@ -38,12 +47,49 @@ namespace ConfectioneryView
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<MessageInfoViewModel>)dataGridView.DataSource).Count == 0)
{
_logger.LogWarning("Out of range messages");
currentPage--;
MailLoad();
buttonNextPage.Enabled = false;
}
else
{
buttonPrevPage.Enabled = true;
}
}
}

View File

@ -14,5 +14,7 @@ namespace ConfectioneryContracts.BusinessLogicsContracts
List<MessageInfoViewModel>? ReadList(MessageInfoSearchModel? model);
bool Create(MessageInfoBindingModel model);
public List<MessageInfoViewModel>? ReadPage(int page, int pageSize);
}
}

View File

@ -13,6 +13,8 @@ namespace ConfectioneryContracts.StoragesContract
{
List<MessageInfoViewModel> GetFullList();
List<MessageInfoViewModel>? GetListOnPage(int page, int pageSize);
List<MessageInfoViewModel> GetFilteredList(MessageInfoSearchModel model);
MessageInfoViewModel? GetElement(MessageInfoSearchModel model);

View File

@ -19,6 +19,21 @@ namespace ConfectioneryDatabaseImplement
return null;
}
public List<MessageInfoViewModel>? GetListOnPage(int page, int pageSize)
{
using var context = new ConfectioneryDatabase();
try
{
return context.Messages.Skip((page - 1) * pageSize).Take(pageSize)
.Select(x => x.GetViewModel)
.ToList();
}
catch (Exception)
{
return null;
}
}
public List<MessageInfoViewModel> GetFilteredList(MessageInfoSearchModel model)
{
using var context = new ConfectioneryDatabase();