diff --git a/AsbCloudApp/Repositories/IWellFinalDocumentsRepository.cs b/AsbCloudApp/Repositories/IWellFinalDocumentsRepository.cs index 9e803029..69c19f98 100644 --- a/AsbCloudApp/Repositories/IWellFinalDocumentsRepository.cs +++ b/AsbCloudApp/Repositories/IWellFinalDocumentsRepository.cs @@ -38,14 +38,14 @@ namespace AsbCloudApp.Repositories Task> GetAvailableUsersAsync(int idWell, CancellationToken token); /// - /// Сохранение категории файла + /// Возвращаент категорию файла /// /// /// /// /// /// - Task SaveCategoryAsync(int idWell, int idCategory, int idUser, CancellationToken token); + Task GetCategoryAsync(int idWell, int idCategory, int idUser, CancellationToken token); } #nullable disable } diff --git a/AsbCloudInfrastructure/Repository/WellFinalDocumentsRepository.cs b/AsbCloudInfrastructure/Repository/WellFinalDocumentsRepository.cs index 8af4f375..f34294a7 100644 --- a/AsbCloudInfrastructure/Repository/WellFinalDocumentsRepository.cs +++ b/AsbCloudInfrastructure/Repository/WellFinalDocumentsRepository.cs @@ -33,27 +33,27 @@ namespace AsbCloudInfrastructure.Repository /// public async Task> UpdateRangeAsync(int idWell, IEnumerable? dtos, CancellationToken token) { - if (dtos is not null) - { - var entities = dtos - .Where(dto => dto.IdsPublishers?.Any() == true) - .SelectMany(dto => dto.IdsPublishers - .Select(idUser => new WellFinalDocument - { - IdCategory = dto.IdCategory, - IdWell = idWell, - IdUser = idUser - })); + if (dtos is null) + throw new ArgumentInvalidException("Данные по категориям отсутствуют."); - var itemsToDelete = context.WellFinalDocuments.Where(d => d.IdWell == idWell); - context.WellFinalDocuments.RemoveRange(itemsToDelete); + var entities = dtos + .Where(dto => dto.IdsPublishers?.Any() == true) + .SelectMany(dto => dto.IdsPublishers + .Select(idUser => new WellFinalDocument + { + IdCategory = dto.IdCategory, + IdWell = idWell, + IdUser = idUser + })); - await context.WellFinalDocuments.AddRangeAsync(entities).ConfigureAwait(false); - await context.SaveChangesAsync(token).ConfigureAwait(false); + var itemsToDelete = context.WellFinalDocuments.Where(d => d.IdWell == idWell); + context.WellFinalDocuments.RemoveRange(itemsToDelete); + + context.WellFinalDocuments.AddRange(entities); + await context.SaveChangesAsync(token).ConfigureAwait(false); + + return entities.Adapt>(); - return entities.Adapt>(); - } - throw new ArgumentInvalidException("Данные по категориям отсутствуют."); } /// @@ -108,6 +108,7 @@ namespace AsbCloudInfrastructure.Repository public async Task> GetAvailableUsersAsync(int idWell, CancellationToken token) { var companyIds = await context.RelationCompaniesWells + .AsNoTracking() .Where(x => x.IdWell == idWell).Select(x => x.IdCompany) .ToListAsync(token) .ConfigureAwait(false); @@ -123,7 +124,7 @@ namespace AsbCloudInfrastructure.Repository } /// - public async Task SaveCategoryAsync(int idWell, int idCategory, int idUser, CancellationToken token) + public async Task GetCategoryAsync(int idWell, int idCategory, int idUser, CancellationToken token) { var entity = await context.WellFinalDocuments .AsNoTracking() diff --git a/AsbCloudInfrastructure/Services/WellFinalDocumentsService.cs b/AsbCloudInfrastructure/Services/WellFinalDocumentsService.cs index d495a567..1da4b617 100644 --- a/AsbCloudInfrastructure/Services/WellFinalDocumentsService.cs +++ b/AsbCloudInfrastructure/Services/WellFinalDocumentsService.cs @@ -4,7 +4,6 @@ using AsbCloudApp.Repositories; using AsbCloudApp.Requests; using AsbCloudApp.Services; using Microsoft.Extensions.Configuration; -using System; using System.Collections.Generic; using System.IO; using System.Linq; @@ -51,11 +50,8 @@ namespace AsbCloudInfrastructure.Services { var data = await wellFinalDocumentsRepository.UpdateRangeAsync(idWell, dtos, token); - if (data.Any()) - { - var message = "от Вас ожидается загрузка на портал документа «{0}»"; - await GenerateMessageAsync(data, message, token); - } + var message = "от Вас ожидается загрузка на портал документа «{0}»"; + await GenerateMessageAsync(data, message, token); return data.Count(); } @@ -63,13 +59,16 @@ namespace AsbCloudInfrastructure.Services /// public async Task SaveCategoryFileAsync(int idWell, int idCategory, int idUser, Stream fileStream, string fileName, CancellationToken token) { - var dto = await wellFinalDocumentsRepository.SaveCategoryAsync(idWell, idCategory, idUser, token) + var dto = await wellFinalDocumentsRepository.GetCategoryAsync(idWell, idCategory, idUser, token) .ConfigureAwait(false); var file = await fileService.SaveAsync(dto.IdWell, dto.IdUser, dto.IdCategory, fileName, fileStream, token).ConfigureAwait(false); - return file?.Id ?? FileServiceThrewException; //TODO: изменить когда файловый сервис будет переведен на nullable + if (file is not null) + return file.Id; + + return FileServiceThrewException; } /// @@ -130,12 +129,12 @@ namespace AsbCloudInfrastructure.Services var category = await fileCategoryService.GetOrDefaultAsync(item.IdCategory, token); var well = await wellService.GetOrDefaultAsync(item.IdWell, token); - SendMessage(well, user, category.Name, message, token); + SendMessage(well, user, category.Name, message); } } } - private void SendMessage(WellDto? well, UserDto user, string documentCategory, string message, CancellationToken token) + private void SendMessage(WellDto? well, UserDto user, string documentCategory, string message) { var factory = new WellFinalDocumentMailBodyFactory(configuration); var subject = factory.MakeSubject(well, documentCategory); diff --git a/AsbCloudWebApi.Tests/ServicesTests/WellFinalDocumentsServiceTest.cs b/AsbCloudWebApi.Tests/ServicesTests/WellFinalDocumentsServiceTest.cs index 8d31d48c..290922cd 100644 --- a/AsbCloudWebApi.Tests/ServicesTests/WellFinalDocumentsServiceTest.cs +++ b/AsbCloudWebApi.Tests/ServicesTests/WellFinalDocumentsServiceTest.cs @@ -76,7 +76,7 @@ namespace AsbCloudWebApi.Tests.ServicesTests wellFinalDocumentsRepository.Setup(r => r.GetByWellIdAsync(It.IsAny(), It.IsAny(), It.IsAny())) .ReturnsAsync(wellCaseDto); - wellFinalDocumentsRepository.Setup(r => r.SaveCategoryAsync(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())) + wellFinalDocumentsRepository.Setup(r => r.GetCategoryAsync(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())) .ReturnsAsync(wellFinalDocumentDBDto); fileRepositoryMock = new Mock(); diff --git a/AsbCloudWebApi/Controllers/WellFinalDocumentsController.cs b/AsbCloudWebApi/Controllers/WellFinalDocumentsController.cs index d2263e9a..15c714da 100644 --- a/AsbCloudWebApi/Controllers/WellFinalDocumentsController.cs +++ b/AsbCloudWebApi/Controllers/WellFinalDocumentsController.cs @@ -52,7 +52,7 @@ namespace AsbCloudWebApi.Controllers return Forbid(); var idUser = User?.GetUserId(); - var data = await wellFinalDocumentsRepository.GetByWellIdAsync(idWell, idUser ?? default, token); + var data = await wellFinalDocumentsRepository.GetByWellIdAsync(idWell, (int)idUser!, token); return Ok(data); } @@ -89,8 +89,8 @@ namespace AsbCloudWebApi.Controllers if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false)) return Forbid(); - var idUser = User.GetUserId() ?? -1; - var data = await wellFinalDocumentsService.UpdateRangeAsync(idWell, idUser, dtos, token); + var idUser = User.GetUserId(); + var data = await wellFinalDocumentsService.UpdateRangeAsync(idWell, (int)idUser!, dtos, token); return Ok(data); }