diff --git a/ConfectionaryBusinessLogic/MessageInfoLogic.cs b/ConfectionaryBusinessLogic/MessageInfoLogic.cs index 0e6cf2f..99b9bb6 100644 --- a/ConfectionaryBusinessLogic/MessageInfoLogic.cs +++ b/ConfectionaryBusinessLogic/MessageInfoLogic.cs @@ -45,18 +45,5 @@ namespace ConfectioneryBusinessLogic _logger.LogInformation("ReadList. Count:{Count}", list.Count); return list; } - - public List? 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; - } } } diff --git a/ConfectionaryFileImplement/MessageInfoStorage.cs b/ConfectionaryFileImplement/MessageInfoStorage.cs index ebbe603..75505d2 100644 --- a/ConfectionaryFileImplement/MessageInfoStorage.cs +++ b/ConfectionaryFileImplement/MessageInfoStorage.cs @@ -24,23 +24,17 @@ namespace ConfectioneryFileImplement return null; } - public List? 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 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() diff --git a/ConfectionaryListImplement/MessageInfoStorage.cs b/ConfectionaryListImplement/MessageInfoStorage.cs index 798bf2d..249ea29 100644 --- a/ConfectionaryListImplement/MessageInfoStorage.cs +++ b/ConfectionaryListImplement/MessageInfoStorage.cs @@ -3,6 +3,7 @@ using ConfectioneryContracts.SearchModels; using ConfectioneryContracts.StoragesContract; using ConfectioneryContracts.ViewModels; using ConfectioneryListImplement.Models; +using System.Collections.Generic; namespace ConfectioneryListImplement { @@ -24,20 +25,6 @@ namespace ConfectioneryListImplement return null; } - public List? GetListOnPage(int page, int pageSize) - { - if (page * pageSize >= _source.Messages.Count) - { - return null; - } - List result = new(); - for (var i = (page - 1) * pageSize; i < page * pageSize; i++) - { - result.Add(_source.Messages[i].GetViewModel); - } - return result; - } - public List GetFilteredList(MessageInfoSearchModel model) { List result = new(); @@ -48,7 +35,21 @@ namespace ConfectioneryListImplement result.Add(item.GetViewModel); } } - return result; + + if (!(model.Page.HasValue && model.PageSize.HasValue)) + { + return result; + } + if (model.Page * model.PageSize >= result.Count) + { + return null; + } + List filteredResult = new(); + for (var i = (model.Page.Value - 1) * model.PageSize.Value; i < model.Page.Value * model.PageSize.Value; i++) + { + filteredResult.Add(result[i]); + } + return filteredResult; } public List GetFullList() diff --git a/Confectionery/FormViewMail.cs b/Confectionery/FormViewMail.cs index cfa4eef..004b1b3 100644 --- a/Confectionery/FormViewMail.cs +++ b/Confectionery/FormViewMail.cs @@ -38,7 +38,11 @@ namespace ConfectioneryView { try { - var list = _logic.ReadPage(currentPage, pageSize); + var list = _logic.ReadList(new() + { + Page = currentPage, + PageSize = pageSize, + }); if (list != null) { dataGridView.DataSource = list; diff --git a/ConfectioneryContracts/BusinessLogicsContracts/IMessageInfoLogic.cs b/ConfectioneryContracts/BusinessLogicsContracts/IMessageInfoLogic.cs index 64280a6..ed4b54c 100644 --- a/ConfectioneryContracts/BusinessLogicsContracts/IMessageInfoLogic.cs +++ b/ConfectioneryContracts/BusinessLogicsContracts/IMessageInfoLogic.cs @@ -14,7 +14,5 @@ namespace ConfectioneryContracts.BusinessLogicsContracts List? ReadList(MessageInfoSearchModel? model); bool Create(MessageInfoBindingModel model); - - public List? ReadPage(int page, int pageSize); } } diff --git a/ConfectioneryContracts/SearchModels/MessageInfoSearchModel.cs b/ConfectioneryContracts/SearchModels/MessageInfoSearchModel.cs index e344281..ca96e1d 100644 --- a/ConfectioneryContracts/SearchModels/MessageInfoSearchModel.cs +++ b/ConfectioneryContracts/SearchModels/MessageInfoSearchModel.cs @@ -11,5 +11,9 @@ namespace ConfectioneryContracts.SearchModels public int? ClientId { get; set; } public string? MessageId { get; set; } + + public int? Page { get; set; } + + public int? PageSize { get; set; } } } diff --git a/ConfectioneryContracts/StoragesContract/IMessageInfoStorage.cs b/ConfectioneryContracts/StoragesContract/IMessageInfoStorage.cs index 7b3ef6c..6bb7375 100644 --- a/ConfectioneryContracts/StoragesContract/IMessageInfoStorage.cs +++ b/ConfectioneryContracts/StoragesContract/IMessageInfoStorage.cs @@ -13,8 +13,6 @@ namespace ConfectioneryContracts.StoragesContract { List GetFullList(); - List? GetListOnPage(int page, int pageSize); - List GetFilteredList(MessageInfoSearchModel model); MessageInfoViewModel? GetElement(MessageInfoSearchModel model); diff --git a/ConfectioneryDatabaseImplement/MessageInfoStorage.cs b/ConfectioneryDatabaseImplement/MessageInfoStorage.cs index 5ac50f7..d60e1c5 100644 --- a/ConfectioneryDatabaseImplement/MessageInfoStorage.cs +++ b/ConfectioneryDatabaseImplement/MessageInfoStorage.cs @@ -19,28 +19,17 @@ namespace ConfectioneryDatabaseImplement return null; } - public List? 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 GetFilteredList(MessageInfoSearchModel model) { using var context = new ConfectioneryDatabase(); - return context.Messages - .Where(x => x.ClientId == model.ClientId) - .Select(x => x.GetViewModel) - .ToList(); + 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()