FileController Add file size and fix search by file name

This commit is contained in:
Фролов 2021-08-31 12:29:27 +05:00
parent e87e959bca
commit 41660af49f
4 changed files with 25 additions and 17 deletions

View File

@ -10,7 +10,8 @@ namespace AsbCloudApp.Data
public int IdAuthor { get; set; }
public string Name { get; set; }
public DateTime UploadDate { get; set; }
public string AuthorName { get; set; }
public int CompanyId { get; set; }
public long Size { get; set; }
public UserDto Author { get; set; }
public string Company { get; set; }
}
}

View File

@ -28,6 +28,9 @@ namespace AsbCloudDb.Model
[Column("date", TypeName = "timestamp with time zone")]
public DateTime UploadDate { get; set; }
[Column("file_size"), Comment("Размер файла")]
public long Size { get; set; }
[Column("is_deleted"), Comment("Удален ли файл")]
public bool IsDeleted { get; set; }

View File

@ -1,6 +1,7 @@
using AsbCloudApp.Data;
using AsbCloudApp.Services;
using AsbCloudDb.Model;
using AsbCloudInfrastructure.Services.Cache;
using Mapster;
using Microsoft.EntityFrameworkCore;
using System;
@ -16,11 +17,13 @@ namespace AsbCloudInfrastructure.Services
{
public string RootPath { get; private set; }
private readonly IAsbCloudDbContext db;
private readonly CacheTable<Company> cacheCompanies;
public FileService(IAsbCloudDbContext db)
public FileService(IAsbCloudDbContext db, Cache.CacheDb cacheDb)
{
RootPath = "files";
this.db = db;
cacheCompanies = cacheDb.GetCachedTable<AsbCloudDb.Model.Company>((DbContext)db);
}
public async Task<FileInfoDto> SaveAsync(int idWell, int idUser, int idCategory, string fileFullName, Stream fileStream, CancellationToken token = default)
@ -34,6 +37,7 @@ namespace AsbCloudInfrastructure.Services
Name = Path.GetFileName(fileFullName),
UploadDate = DateTime.Now,
IsDeleted = false,
Size = fileStream.Length,
};
var entry = db.Files.Add(fileInfo);
@ -64,9 +68,11 @@ namespace AsbCloudInfrastructure.Services
.ToListAsync(token)
.ConfigureAwait(false);
var dtos = entities.Adapt<FileInfoDto, AsbCloudDb.Model.FileInfo>((d,s) => {
d.AuthorName = s.Author?.Name;
d.CompanyId = s.Author?.IdCompany ?? 0;
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;
});
return dtos;
@ -88,7 +94,7 @@ namespace AsbCloudInfrastructure.Services
.Where(e => companies.Contains(e.Author.Company.Id));
if (!string.IsNullOrEmpty(fileName))
query = query.Where(e => e.Name.Contains(fileName, StringComparison.OrdinalIgnoreCase));
query = query.Where(e => e.Name.ToLower().Contains(fileName.ToLower()));
if (begin != default)
query = query.Where(e => e.UploadDate >= begin);
@ -118,14 +124,15 @@ namespace AsbCloudInfrastructure.Services
.Take(take).AsNoTracking().ToListAsync(token)
.ConfigureAwait(false);
foreach (var item in entities)
var dtos = entities.Adapt<FileInfoDto, AsbCloudDb.Model.FileInfo>(async (d, s) =>
{
var filePropertiesDto = item.Adapt<FileInfoDto>();
filePropertiesDto.AuthorName = item.Author?.Name;
filePropertiesDto.CompanyId = item.Author?.IdCompany ?? 0;
result.Items.Add(filePropertiesDto);
}
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);
return result;
}
@ -142,7 +149,7 @@ namespace AsbCloudInfrastructure.Services
return null;
var dto = entity.Adapt<FileInfoDto>();
dto.AuthorName = entity.Author.Name;
dto.Author = entity.Author?.Adapt<UserDto>();
return dto;
}

View File

@ -125,9 +125,6 @@ namespace AsbCloudWebApi.Controllers
var filesInfo = await fileService.GetInfosAsync(idWell, idCategory,
companies, fileName, begin, end, skip, take, token).ConfigureAwait(false);
if (filesInfo is null || !filesInfo.Items.Any())
return NoContent();
return Ok(filesInfo);
}