diff --git a/AsbCloudApp/Data/PaginationContainer.cs b/AsbCloudApp/Data/PaginationContainer.cs index bdee31be..61840893 100644 --- a/AsbCloudApp/Data/PaginationContainer.cs +++ b/AsbCloudApp/Data/PaginationContainer.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using System.Linq; namespace AsbCloudApp.Data { @@ -13,16 +14,7 @@ namespace AsbCloudApp.Data /// public PaginationContainer() { - Items = new List(4); - } - - /// - /// конструктор - /// - /// - public PaginationContainer(int capacity) - { - Items = new List(capacity); + Items = Enumerable.Empty(); } /// @@ -43,6 +35,6 @@ namespace AsbCloudApp.Data /// /// Данные /// - public List Items { get; set; } + public IEnumerable Items { get; set; } } } diff --git a/AsbCloudApp/Repositories/IHelpPageRepository.cs b/AsbCloudApp/Repositories/IHelpPageRepository.cs index 26f9c69e..82dd96a4 100644 --- a/AsbCloudApp/Repositories/IHelpPageRepository.cs +++ b/AsbCloudApp/Repositories/IHelpPageRepository.cs @@ -13,11 +13,22 @@ public interface IHelpPageRepository : ICrudRepository /// /// Получение справки по url страницы и id категории /// - /// + /// /// /// /// - Task GetOrDefaultByUrlPageAndIdCategoryAsync(string urlPage, + Task GetOrDefaultByUrlPageAndIdCategoryAsync(string key, int idCategory, CancellationToken cancellationToken); + + /// + /// Проверяет наличие справки для страницы + /// + /// + /// + /// + /// + Task IsExistingAsync(string key, + int idCategory, + CancellationToken cancellationToken); } diff --git a/AsbCloudInfrastructure/Repository/FileRepository.cs b/AsbCloudInfrastructure/Repository/FileRepository.cs index 7b078e10..e75e7691 100644 --- a/AsbCloudInfrastructure/Repository/FileRepository.cs +++ b/AsbCloudInfrastructure/Repository/FileRepository.cs @@ -111,7 +111,7 @@ namespace AsbCloudInfrastructure.Repository .ToListAsync(token) .ConfigureAwait(false); - result.Items = entities.Select(e => Convert(e)).ToList(); + result.Items = entities.Select(e => Convert(e)); return result; } diff --git a/AsbCloudInfrastructure/Repository/HelpPageRepository.cs b/AsbCloudInfrastructure/Repository/HelpPageRepository.cs index dfc912a3..5742511f 100644 --- a/AsbCloudInfrastructure/Repository/HelpPageRepository.cs +++ b/AsbCloudInfrastructure/Repository/HelpPageRepository.cs @@ -16,13 +16,13 @@ public class HelpPageRepository : CrudRepositoryBase, { } - public async Task GetOrDefaultByUrlPageAndIdCategoryAsync(string urlPage, + public async Task GetOrDefaultByUrlPageAndIdCategoryAsync(string key, int idCategory, CancellationToken cancellationToken) { var helpPage = await dbSet.AsNoTracking() .SingleOrDefaultAsync(x => - x.UrlPage == urlPage && + x.UrlPage == key && x.IdCategory == idCategory, cancellationToken); @@ -31,4 +31,9 @@ public class HelpPageRepository : CrudRepositoryBase, return helpPage.Adapt(); } + + public Task IsExistingAsync(string key, int idCategory, CancellationToken cancellationToken) => + dbContext.HelpPages.AnyAsync(h => h.UrlPage == key && + h.IdCategory == idCategory, + cancellationToken); } diff --git a/AsbCloudInfrastructure/Repository/NotificationRepository.cs b/AsbCloudInfrastructure/Repository/NotificationRepository.cs index 90799208..8a44a418 100644 --- a/AsbCloudInfrastructure/Repository/NotificationRepository.cs +++ b/AsbCloudInfrastructure/Repository/NotificationRepository.cs @@ -70,14 +70,14 @@ public class NotificationRepository : CrudCacheRepositoryBase x.Adapt()) - .ToListAsync(cancellationToken); + .ToArrayAsync(cancellationToken); return result; } diff --git a/AsbCloudInfrastructure/Repository/WellOperationRepository.cs b/AsbCloudInfrastructure/Repository/WellOperationRepository.cs index 00c87dbb..5d4795cb 100644 --- a/AsbCloudInfrastructure/Repository/WellOperationRepository.cs +++ b/AsbCloudInfrastructure/Repository/WellOperationRepository.cs @@ -165,7 +165,7 @@ namespace AsbCloudInfrastructure.Repository .Skip(result.Skip) .Take(result.Take); - result.Items = await query.ToListAsync(token); + result.Items = await query.ToArrayAsync(token); return result; } diff --git a/AsbCloudInfrastructure/Services/SAUB/MessageService.cs b/AsbCloudInfrastructure/Services/SAUB/MessageService.cs index 7b49d5c8..82f0abfc 100644 --- a/AsbCloudInfrastructure/Services/SAUB/MessageService.cs +++ b/AsbCloudInfrastructure/Services/SAUB/MessageService.cs @@ -101,6 +101,8 @@ namespace AsbCloudInfrastructure.Services.SAUB var eventsDict = events.ToDictionary(x=>x.IdEvent, x => x); var usersDict = users.ToDictionary(x => x.IdUser, x => x); + var messagesDtoList = new List(); + foreach (var message in messagesList) { var messageDto = new MessageDto @@ -126,10 +128,11 @@ namespace AsbCloudInfrastructure.Services.SAUB messageDto.CategoryId = e.IdCategory; messageDto.Message = e.MakeMessageText(message); } - - result.Items.Add(messageDto); + messagesDtoList.Add(messageDto); } + result.Items = result.Items.Concat(messagesDtoList); + return result; } diff --git a/AsbCloudWebApi/Controllers/HelpPageController.cs b/AsbCloudWebApi/Controllers/HelpPageController.cs index bae2010e..c3a538a1 100644 --- a/AsbCloudWebApi/Controllers/HelpPageController.cs +++ b/AsbCloudWebApi/Controllers/HelpPageController.cs @@ -21,18 +21,21 @@ public class HelpPageController : ControllerBase { private readonly IHelpPageService helpPageService; private readonly IUserRepository userRepository; + private readonly IHelpPageRepository helpPageRepository; public HelpPageController(IHelpPageService helpPageService, - IUserRepository userRepository) + IUserRepository userRepository, + IHelpPageRepository helpPageRepository) { this.helpPageService = helpPageService; this.userRepository = userRepository; + this.helpPageRepository = helpPageRepository; } /// /// Загрузка файла справки /// - /// Url страницы + /// Ключ страницы /// Id категории файла. Допустимое значение параметра: 20000 /// Файл справки /// Токен для отмены задачи @@ -41,7 +44,7 @@ public class HelpPageController : ControllerBase [Permission] [ProducesResponseType(typeof(int), (int)HttpStatusCode.OK)] public async Task UploadAsync( - string urlPage, + [Required] string key, [Range(minimum: 20000, maximum: 20000, ErrorMessage = "Категория файла недопустима. Допустимые: 20000")] int idCategory, [Required] IFormFile file, @@ -57,7 +60,7 @@ public class HelpPageController : ControllerBase using var fileStream = file.OpenReadStream(); - int helpPageId = await helpPageService.AddOrUpdateAsync(urlPage, + int helpPageId = await helpPageService.AddOrUpdateAsync(key, idCategory, file.FileName, fileStream, @@ -69,19 +72,20 @@ public class HelpPageController : ControllerBase /// /// Получение файла справки /// - /// Url страницы + /// Ключ страницы /// Id категории файла. Допустимое значение параметра: 20000 /// Токен для отмены задачи /// [HttpGet] - [Route("{urlPage}/{idCategory}")] [ProducesResponseType(typeof(PhysicalFileResult), (int)HttpStatusCode.OK)] [ProducesResponseType(StatusCodes.Status204NoContent)] - public async Task GetFileAsync(string urlPage, - int idCategory, + public async Task GetFileAsync( + [Required] string key, + [Range(minimum: 20000, maximum: 20000, ErrorMessage = "Категория файла недопустима. Допустимые: 20000")] + int idCategory, CancellationToken cancellationToken) { - var file = await helpPageService.GetFileStreamAsync(urlPage, + var file = await helpPageService.GetFileStreamAsync(key, idCategory, cancellationToken); @@ -94,4 +98,27 @@ public class HelpPageController : ControllerBase return File(memoryStream, "application/pdf", file.fileName); } + + /// + /// Проверяет наличие справки для страницы + /// + /// Ключ страницы + /// Id категории файла. Допустимое значение параметра: 20000 + /// + /// + [HttpGet] + [Route("isExisting")] + [ProducesResponseType(typeof(bool), (int)HttpStatusCode.OK)] + public async Task IsExistingAsync( + [Required] string key, + [Range(minimum: 20000, maximum: 20000, ErrorMessage = "Категория файла недопустима. Допустимые: 20000")] + int idCategory, + CancellationToken cancellationToken) + { + var helpPage = await helpPageRepository.GetOrDefaultByUrlPageAndIdCategoryAsync(key, + idCategory, + cancellationToken); + + return Ok(helpPage != null); + } }