This commit is contained in:
ai.astrakhantsev 2022-09-13 11:54:45 +05:00
commit 5bb2d3ea10
6 changed files with 80 additions and 45 deletions

View File

@ -16,6 +16,6 @@ namespace AsbCloudApp.Services
/// Получение справочника категорий файлов
/// </summary>
/// <returns></returns>
Task<IEnumerable<FileCategoryDto>> GetWellCategoryAsync(CancellationToken token);
Task<IEnumerable<FileCategoryDto>> GetWellCaseCategoriesAsync(CancellationToken token);
}
}

View File

@ -2,8 +2,6 @@
using AsbCloudApp.Services;
using AsbCloudDb.Model;
using AsbCloudInfrastructure.Repository;
using DocumentFormat.OpenXml.InkML;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
@ -13,37 +11,21 @@ namespace AsbCloudInfrastructure.Services
{
public class FileCategoryService : CrudCacheServiceBase<FileCategoryDto, FileCategory>, IFileCategoryService
{
private readonly IAsbCloudDbContext db;
public FileCategoryService(IAsbCloudDbContext context)
: base(context)
{
this.db = context;
}
public async Task<IEnumerable<FileCategoryDto>> GetWellCategoryAsync(CancellationToken token)
public async Task<IEnumerable<FileCategoryDto>> GetWellCaseCategoriesAsync(CancellationToken token)
{
var data = await (from category in db.FileCategories
where category.Id >= 10000 && category.Id <= 20000
select new FileCategoryDto
{
Id = category.Id,
Name = category.Name,
ShortName = category.ShortName
})
.ToListAsync(token)
.ConfigureAwait(false);
var cache = await GetCacheAsync(token)
.ConfigureAwait(false);
var dtos = cache
.Where(kv => kv.Key >= 10000)
.Where(kv => kv.Key <= 20000)
.Select(kv => kv.Value);
return data.ToList();
}
public override async Task<FileCategoryDto> GetOrDefaultAsync(int id, CancellationToken token)
{
var entity = await db.FileCategories
.FirstOrDefaultAsync(x => x.Id == id)
.ConfigureAwait(false);
var dto = Convert(entity);
return dto;
return dtos;
}
}
}

View File

@ -2,7 +2,6 @@
using AsbCloudApp.Exceptions;
using AsbCloudApp.Services;
using AsbCloudDb.Model;
using AsbCloudInfrastructure.Repository;
using Mapster;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
@ -77,7 +76,7 @@ namespace AsbCloudInfrastructure.Services
throw new ArgumentInvalidException("Данные по категориям отсутствуют.");
}
public async Task<WellCaseDto> GetByWellId(int idWell, int idUser, CancellationToken token)
public async Task<WellCaseDto> GetByWellId_old(int idWell, int idUser, CancellationToken token)
{
var wellFinalDocuments = new List<WellFinalDocumentDto>();
@ -140,6 +139,52 @@ namespace AsbCloudInfrastructure.Services
};
}
public async Task<WellCaseDto> GetByWellId(int idWell, int idUser, CancellationToken token)
{
var entities = await context.WellFinalDocuments
.Include(d => d.Category)
.Include(d => d.User)
.Where(d => d.IdWell == idWell)
.AsNoTracking()
.ToArrayAsync(token)
.ConfigureAwait(false);
var entitiesGroups = entities
.GroupBy(d => d.Category);
var categoriesIds = entitiesGroups
.Select(g => g.Key.Id);
var files = (await fileService
.GetInfosByWellIdAsync(idWell, token)
.ConfigureAwait(false))
.Where(f => categoriesIds.Contains(f.IdCategory))
.ToArray();
var docs = entitiesGroups.Select((g) => new WellFinalDocumentDto
{
IdCategory = g.Key.Id,
FilesCount = files
.Where(f => f.IdCategory == g.Key.Id)
.Count(),
File = files
.Where(f => f.IdCategory == g.Key.Id)
.OrderBy(f => f.UploadDate)
.LastOrDefault(),
NameCategory = g.Key.Name,
Publishers = g.Select(i => i.User.Adapt<UserDto>()),
PermissionToUpload = g.Any(i => i.IdUser == idUser),
});
var result = new WellCaseDto
{
IdWell = idWell,
PermissionToSetPubliher = userService.HasPermission(idUser, "WellFinalDocument.editPublisher"),
WellFinalDocuments = docs,
};
return result;
}
public async Task<IEnumerable<UserDto>> GetAvailableUsersAsync(int idWell, CancellationToken token)
{
var companyIds = await context.RelationCompaniesWells

View File

@ -29,7 +29,7 @@ namespace AsbCloudWebApi.Tests.ServicesTests
[Fact]
public async Task GetWellCategoryAsync_return_cnt_file_category()
{
var cnt = (await service.GetWellCategoryAsync(0, CancellationToken.None)).Count();
var cnt = (await service.GetWellCaseCategoriesAsync(CancellationToken.None)).Count();
Assert.NotEqual(0, cnt);
}
}

View File

@ -2,9 +2,6 @@
using AsbCloudApp.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;
using System.Threading;
using System.Collections.Generic;
namespace AsbCloudWebApi.Controllers
{
@ -16,7 +13,6 @@ namespace AsbCloudWebApi.Controllers
[Authorize]
public class FileCategoryController : CrudController<FileCategoryDto, ICrudService<FileCategoryDto>>
{
private readonly IFileCategoryService fileCategoryService;
public FileCategoryController(ICrudService<FileCategoryDto> service, IFileCategoryService fileCategoryService)
: base(service)
{

View File

@ -6,7 +6,7 @@ using System.Threading.Tasks;
using System.Threading;
using System.Collections.Generic;
using Microsoft.AspNetCore.Http;
using AsbCloudInfrastructure.Services;
using System.ComponentModel.DataAnnotations;
namespace AsbCloudWebApi.Controllers
{
@ -32,13 +32,26 @@ namespace AsbCloudWebApi.Controllers
this.fileCategoryService = fileCategoryService;
}
/// <summary>
/// Получение справочника категорий файлов
/// </summary>
/// <returns></returns>
[HttpGet]
[Route("fileCategories")]
[ProducesResponseType(typeof(IEnumerable<FileCategoryDto>), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> GetFileCategories(CancellationToken token = default)
{
var data = await fileCategoryService.GetWellCaseCategoriesAsync(token);
return Ok(data);
}
/// <summary>
/// Получение всех записей
/// </summary>
/// <param name="idWell"></param>
/// <param name="token"></param>
/// <returns></returns>
[HttpGet]
[HttpGet("{idWell}")]
[Permission]
[ProducesResponseType(typeof(WellCaseDto), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> GetAsync(int idWell, CancellationToken token = default)
@ -57,9 +70,9 @@ namespace AsbCloudWebApi.Controllers
/// <param name="idWell"></param>
/// <param name="token"></param>
/// <returns></returns>
[HttpGet]
[HttpGet("{idWell}/availableUsers")]
[Permission]
[Route("availableUsers")]
[Route("publishers")]
[ProducesResponseType(typeof(IEnumerable<UserDto>), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> GetAvailableUsersAsync(int idWell, CancellationToken token = default)
{
@ -77,10 +90,10 @@ namespace AsbCloudWebApi.Controllers
/// <param name="dtos"></param>
/// <param name="token"></param>
/// <returns></returns>
[HttpPut]
[HttpPut("{idWell}")]
[Permission("WellFinalDocuments.editPublisher")]
[ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> UpdateRangeAsync(int idWell, IEnumerable<WellFinalDocumentInputDto> dtos, CancellationToken token = default)
public async Task<IActionResult> UpdateRangeAsync(int idWell, [Required] IEnumerable<WellFinalDocumentInputDto> dtos, CancellationToken token = default)
{
if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false))
return Forbid();
@ -96,12 +109,11 @@ namespace AsbCloudWebApi.Controllers
/// <param name="idCategory"></param>
/// <param name="token"></param>
/// <returns></returns>
[HttpGet]
[HttpGet("{idWell}/history")]
[Permission]
[Route("filesHistoryByIdCategory")]
[ProducesResponseType(typeof(WellFinalDocumentsHistoryDto), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> GetFilesHistoryByIdCategory(int idWell,
int idCategory,
[Required] int idCategory,
CancellationToken token = default)
{
if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false))
@ -119,10 +131,10 @@ namespace AsbCloudWebApi.Controllers
/// <param name="file"></param>
/// <param name="token"></param>
/// <returns></returns>
[HttpPost]
[HttpPost("{idWell}")]
[Permission]
[ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> SaveCategoryFile(int idWell, int idCategory, IFormFile file, CancellationToken token = default)
public async Task<IActionResult> SaveCategoryFile(int idWell, [Required] int idCategory, [Required] IFormFile file, CancellationToken token = default)
{
if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false))
return Forbid();