"красивости"

This commit is contained in:
ngfrolov 2022-09-13 11:47:12 +05:00
parent 44be18f5ed
commit 698803fbf8
6 changed files with 86 additions and 60 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(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,24 +13,9 @@ namespace AsbCloudWebApi.Controllers
[Authorize]
public class FileCategoryController : CrudController<FileCategoryDto, ICrudService<FileCategoryDto>>
{
private readonly IFileCategoryService fileCategoryService;
public FileCategoryController(ICrudService<FileCategoryDto> service, IFileCategoryService fileCategoryService)
: base(service)
{
this.fileCategoryService = fileCategoryService;
}
/// <summary>
/// Получение справочника категорий файлов
/// </summary>
/// <returns></returns>
[HttpGet]
[Route("/getWellFileCategory")]
//[Permission]
public async Task<IActionResult> GetWellFileCategory(CancellationToken token = default)
{
var data = await fileCategoryService.GetWellCategoryAsync(token);
return Ok(data);
}
}
}

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
{
@ -21,10 +21,29 @@ namespace AsbCloudWebApi.Controllers
{
private readonly IWellFinalDocumentsService wellFinalDocumentsService;
private readonly IWellService wellService;
public WellFinalDocumentsController(IWellFinalDocumentsService wellFinalDocumentsService, IWellService wellService)
private readonly IFileCategoryService fileCategoryService;
public WellFinalDocumentsController(
IWellFinalDocumentsService wellFinalDocumentsService,
IWellService wellService,
IFileCategoryService fileCategoryService)
{
this.wellFinalDocumentsService = wellFinalDocumentsService;
this.wellService = wellService;
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>
@ -33,7 +52,7 @@ namespace AsbCloudWebApi.Controllers
/// <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)
@ -52,9 +71,8 @@ namespace AsbCloudWebApi.Controllers
/// <param name="idWell"></param>
/// <param name="token"></param>
/// <returns></returns>
[HttpGet]
[HttpGet("{idWell}/availableUsers")]
[Permission]
[Route("publishers")]
[ProducesResponseType(typeof(IEnumerable<UserDto>), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> GetAvailableUsersAsync(int idWell, CancellationToken token = default)
{
@ -72,10 +90,10 @@ namespace AsbCloudWebApi.Controllers
/// <param name="dtos"></param>
/// <param name="token"></param>
/// <returns></returns>
[HttpPut]
[HttpPut("{idWell}")]
[Permission("WellFinalDocument.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();
@ -91,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))
@ -114,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();