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 int Id { get; set; }
public string Caption { 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 DateTime UploadDate { get; set; }
public long Size { get; set; } public long Size { get; set; }
public UserDto Author { get; set; } public UserDto Author { get; set; }
public string Company { get; set; }
} }
} }

View File

@ -9,5 +9,7 @@
public int? IdRole { get; set; } public int? IdRole { get; set; }
public string Password { 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<FileInfoDto> SaveAsync(int idWell, int idUser, int idCategory, string fileFullName, Stream fileStream, CancellationToken token = default);
Task<PaginationContainer<FileInfoDto>> GetInfosAsync(int idWell, Task<PaginationContainer<FileInfoDto>> GetInfosAsync(int idWell,
int idCategory, IEnumerable<int> companies, string fileName, DateTime begin, DateTime end, int idCategory, string companyName = default, string fileName = default, DateTime begin = default, DateTime end = default,
int skip, int take, CancellationToken token = default); int skip = 0, int take = 32, CancellationToken token = default);
Task<FileInfoDto> GetInfoAsync(int fileId, Task<FileInfoDto> GetInfoAsync(int fileId,
CancellationToken token); CancellationToken token);

View File

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

View File

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