fileController search by companyName (not by companyId).

Add UserDto into FileInfoDto.
Add CompanyDto into UserDto.
This commit is contained in:
Фролов 2021-08-31 18:01:26 +05:00
parent 41660af49f
commit a7962492af
6 changed files with 27 additions and 37 deletions

View File

@ -4,6 +4,6 @@
{
public int Id { get; set; }
public string Caption { get; set; }
public string CompanyType { get; set; }
public string CompanyTypeCaption { get; set; }
}
}

View File

@ -12,6 +12,5 @@ namespace AsbCloudApp.Data
public DateTime UploadDate { get; set; }
public long Size { get; set; }
public UserDto Author { get; set; }
public string Company { get; set; }
}
}

View File

@ -9,5 +9,7 @@
public int? IdRole { get; set; }
public string Password { get; set; }
public CompanyDto Company { get; set; }
}
}

View File

@ -14,8 +14,8 @@ namespace AsbCloudApp.Services
Task<FileInfoDto> SaveAsync(int idWell, int idUser, int idCategory, string fileFullName, Stream fileStream, CancellationToken token = default);
Task<PaginationContainer<FileInfoDto>> GetInfosAsync(int idWell,
int idCategory, IEnumerable<int> companies, string fileName, DateTime begin, DateTime end,
int skip, int take, CancellationToken token = default);
int idCategory, string companyName = default, string fileName = default, DateTime begin = default, DateTime end = default,
int skip = 0, int take = 32, CancellationToken token = default);
Task<FileInfoDto> GetInfoAsync(int fileId,
CancellationToken token);

View File

@ -1,7 +1,6 @@
using AsbCloudApp.Data;
using AsbCloudApp.Services;
using AsbCloudDb.Model;
using AsbCloudInfrastructure.Services.Cache;
using Mapster;
using Microsoft.EntityFrameworkCore;
using System;
@ -16,14 +15,18 @@ namespace AsbCloudInfrastructure.Services
public class FileService : IFileService
{
public string RootPath { get; private set; }
private readonly IAsbCloudDbContext db;
private readonly CacheTable<Company> cacheCompanies;
public FileService(IAsbCloudDbContext db, Cache.CacheDb cacheDb)
private readonly IQueryable<AsbCloudDb.Model.FileInfo> dbSetConfigured;
private readonly IAsbCloudDbContext db;
public FileService(IAsbCloudDbContext db)
{
RootPath = "files";
this.db = db;
cacheCompanies = cacheDb.GetCachedTable<AsbCloudDb.Model.Company>((DbContext)db);
dbSetConfigured = db.Files
.Include(f => f.Author)
.ThenInclude(u => u.Company)
.ThenInclude(c => c.CompanyType);
}
public async Task<FileInfoDto> SaveAsync(int idWell, int idUser, int idCategory, string fileFullName, Stream fileStream, CancellationToken token = default)
@ -61,37 +64,31 @@ namespace AsbCloudInfrastructure.Services
public async Task<IEnumerable<FileInfoDto>> GetInfosByCategoryAsync(int idWell,
int idCategory, CancellationToken token = default)
{
var entities = await db.Files
.Include(f => f.Author)
var entities = await dbSetConfigured
.Where(e => e.IdWell == idWell && e.IdCategory == idCategory)
.AsNoTracking()
.ToListAsync(token)
.ConfigureAwait(false);
var dtos = entities.Adapt<FileInfoDto, AsbCloudDb.Model.FileInfo>(async (d,s) => {
d.Author = s.Author?.Adapt<UserDto>();
var company = await cacheCompanies.FirstOrDefaultAsync(c => c.Id == s.Author?.IdCompany, token)
.ConfigureAwait(false);
d.Company = company?.Caption;
});
var dtos = entities.Adapt<FileInfoDto>();
return dtos;
}
public async Task<PaginationContainer<FileInfoDto>> GetInfosAsync(int idWell,
int idCategory, IEnumerable<int> companies = default, string fileName = default, DateTime begin = default,
int idCategory, string companyName = default, string fileName = default, DateTime begin = default,
DateTime end = default, int skip = 0, int take = 32, CancellationToken token = default)
{
var query = db.Files
.Include(f => f.Author)
var query = dbSetConfigured
.Where(e => e.IdWell == idWell &&
e.IdCategory == idCategory);
query = query.Where(e => !e.IsDeleted);
if (companies.Any())
query.Include(file => file.Author).ThenInclude(a => a.Company)
.Where(e => companies.Contains(e.Author.Company.Id));
if (!string.IsNullOrEmpty(companyName))
query = query
.Include(file => file.Author)
.ThenInclude(a => a.Company)
.Where(e => (e.Author == null) || (e.Author.Company == null) || e.Author.Company.Caption.Contains(companyName));
if (!string.IsNullOrEmpty(fileName))
query = query.Where(e => e.Name.ToLower().Contains(fileName.ToLower()));
@ -124,13 +121,7 @@ namespace AsbCloudInfrastructure.Services
.Take(take).AsNoTracking().ToListAsync(token)
.ConfigureAwait(false);
var dtos = entities.Adapt<FileInfoDto, AsbCloudDb.Model.FileInfo>(async (d, s) =>
{
d.Author = s.Author?.Adapt<UserDto>();
var company = await cacheCompanies.FirstOrDefaultAsync(c => c.Id == s.Author?.IdCompany, token)
.ConfigureAwait(false);
d.Company = company?.Caption;
});
var dtos = entities.Adapt<FileInfoDto>();
result.Items.AddRange(dtos);
return result;
@ -139,8 +130,7 @@ namespace AsbCloudInfrastructure.Services
public async Task<FileInfoDto> GetInfoAsync(int fileId,
CancellationToken token = default)
{
var entity = await db.Files
.Include(f => f.Author)
var entity = await dbSetConfigured
.AsNoTracking()
.FirstOrDefaultAsync(f => f.Id == fileId, token)
.ConfigureAwait(false);
@ -149,7 +139,6 @@ namespace AsbCloudInfrastructure.Services
return null;
var dto = entity.Adapt<FileInfoDto>();
dto.Author = entity.Author?.Adapt<UserDto>();
return dto;
}

View File

@ -95,7 +95,7 @@ namespace AsbCloudWebApi.Controllers
/// </summary>
/// <param name="idWell">id скважины</param>
/// <param name="idCategory">id категории файла</param>
/// <param name="companies">id компаний для фильтрации возвращаемых файлов</param>
/// <param name="companyName">id компаний для фильтрации возвращаемых файлов</param>
/// <param name="fileName">часть имени файла для поиска</param>
/// <param name="begin">дата начала</param>
/// <param name="end">дата окончания</param>
@ -108,7 +108,7 @@ namespace AsbCloudWebApi.Controllers
public async Task<IActionResult> GetFilesInfoAsync(
[FromRoute] int idWell,
int idCategory = default,
[FromQuery] IEnumerable<int> companies = default,
string companyName = default,
string fileName = default,
DateTime begin = default,
DateTime end = default,
@ -123,7 +123,7 @@ namespace AsbCloudWebApi.Controllers
return Forbid();
var filesInfo = await fileService.GetInfosAsync(idWell, idCategory,
companies, fileName, begin, end, skip, take, token).ConfigureAwait(false);
companyName, fileName, begin, end, skip, take, token).ConfigureAwait(false);
return Ok(filesInfo);
}