diff --git a/AsbCloudApp/Data/CompanyDto.cs b/AsbCloudApp/Data/CompanyDto.cs index 795d4358..f53d6483 100644 --- a/AsbCloudApp/Data/CompanyDto.cs +++ b/AsbCloudApp/Data/CompanyDto.cs @@ -4,6 +4,6 @@ { public int Id { get; set; } public string Caption { get; set; } - public string CompanyType { get; set; } + public string CompanyTypeCaption { get; set; } } } diff --git a/AsbCloudApp/Data/FileInfoDto.cs b/AsbCloudApp/Data/FileInfoDto.cs index 2361e325..46c29e98 100644 --- a/AsbCloudApp/Data/FileInfoDto.cs +++ b/AsbCloudApp/Data/FileInfoDto.cs @@ -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; } } } diff --git a/AsbCloudApp/Data/UserDto.cs b/AsbCloudApp/Data/UserDto.cs index dd9234c6..98abcbb9 100644 --- a/AsbCloudApp/Data/UserDto.cs +++ b/AsbCloudApp/Data/UserDto.cs @@ -9,5 +9,7 @@ public int? IdRole { get; set; } public string Password { get; set; } + + public CompanyDto Company { get; set; } } } diff --git a/AsbCloudApp/Services/IFileService.cs b/AsbCloudApp/Services/IFileService.cs index 4b0ac894..77f9d3ca 100644 --- a/AsbCloudApp/Services/IFileService.cs +++ b/AsbCloudApp/Services/IFileService.cs @@ -14,8 +14,8 @@ namespace AsbCloudApp.Services Task SaveAsync(int idWell, int idUser, int idCategory, string fileFullName, Stream fileStream, CancellationToken token = default); Task> GetInfosAsync(int idWell, - int idCategory, IEnumerable 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 GetInfoAsync(int fileId, CancellationToken token); diff --git a/AsbCloudInfrastructure/Services/FileService.cs b/AsbCloudInfrastructure/Services/FileService.cs index c8ae19ce..e758a828 100644 --- a/AsbCloudInfrastructure/Services/FileService.cs +++ b/AsbCloudInfrastructure/Services/FileService.cs @@ -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 cacheCompanies; - public FileService(IAsbCloudDbContext db, Cache.CacheDb cacheDb) + private readonly IQueryable dbSetConfigured; + private readonly IAsbCloudDbContext db; + + public FileService(IAsbCloudDbContext db) { RootPath = "files"; this.db = db; - cacheCompanies = cacheDb.GetCachedTable((DbContext)db); + dbSetConfigured = db.Files + .Include(f => f.Author) + .ThenInclude(u => u.Company) + .ThenInclude(c => c.CompanyType); } public async Task SaveAsync(int idWell, int idUser, int idCategory, string fileFullName, Stream fileStream, CancellationToken token = default) @@ -61,37 +64,31 @@ namespace AsbCloudInfrastructure.Services public async Task> 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(async (d,s) => { - d.Author = s.Author?.Adapt(); - var company = await cacheCompanies.FirstOrDefaultAsync(c => c.Id == s.Author?.IdCompany, token) - .ConfigureAwait(false); - d.Company = company?.Caption; - }); - + var dtos = entities.Adapt(); return dtos; } public async Task> GetInfosAsync(int idWell, - int idCategory, IEnumerable 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(async (d, s) => - { - d.Author = s.Author?.Adapt(); - var company = await cacheCompanies.FirstOrDefaultAsync(c => c.Id == s.Author?.IdCompany, token) - .ConfigureAwait(false); - d.Company = company?.Caption; - }); + var dtos = entities.Adapt(); result.Items.AddRange(dtos); return result; @@ -139,8 +130,7 @@ namespace AsbCloudInfrastructure.Services public async Task 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(); - dto.Author = entity.Author?.Adapt(); return dto; } diff --git a/AsbCloudWebApi/Controllers/FileController.cs b/AsbCloudWebApi/Controllers/FileController.cs index 09e8143e..42058db1 100644 --- a/AsbCloudWebApi/Controllers/FileController.cs +++ b/AsbCloudWebApi/Controllers/FileController.cs @@ -95,7 +95,7 @@ namespace AsbCloudWebApi.Controllers /// /// id скважины /// id категории файла - /// id компаний для фильтрации возвращаемых файлов + /// id компаний для фильтрации возвращаемых файлов /// часть имени файла для поиска /// дата начала /// дата окончания @@ -108,7 +108,7 @@ namespace AsbCloudWebApi.Controllers public async Task GetFilesInfoAsync( [FromRoute] int idWell, int idCategory = default, - [FromQuery] IEnumerable 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); }