fix "order for skip/take" warning. some refactoring.

This commit is contained in:
Фролов 2021-08-13 17:26:19 +05:00
parent 1956eafb32
commit 2e01c4334f
10 changed files with 62 additions and 56 deletions

View File

@ -2,12 +2,12 @@
namespace AsbCloudApp.Data
{
public class FilePropertiesDto
public class FileInfoDto
{
public int Id { get; set; }
public string Name { get; set; }
public int IdCategory { get; set; }
public DateTime UploadDate { get; set; }
public string UserName { get; set; }
public string AuthorName { get; set; }
}
}

View File

@ -13,11 +13,11 @@ namespace AsbCloudApp.Services
int idCategory, IEnumerable<(string fileName, int idWell, int idCategory,
DateTime date, int idUser)> filesInfo);
Task<PaginationContainer<FilePropertiesDto>> GetFilesInfoAsync(int idWell,
Task<PaginationContainer<FileInfoDto>> GetFilesInfoAsync(int idWell,
int idCategory, DateTime begin, DateTime end,
int skip, int take, CancellationToken token = default);
Task<(int Id, string Name, int IdCategory)?> GetFileInfoAsync(int fileId,
Task<FileInfoDto> GetFileInfoAsync(int fileId,
CancellationToken token);
}
}

View File

@ -21,7 +21,7 @@ namespace AsbCloudDb.Model
public virtual DbSet<UserRole> UserRoles { get; set; }
public virtual DbSet<Well> Wells { get; set; }
public virtual DbSet<ReportProperties> ReportProperties { get; set; }
public virtual DbSet<File> Files { get; set; }
public virtual DbSet<FileInfo> Files { get; set; }
public virtual DbSet<FileCategory> FileCategories { get; set; }
public virtual DbSet<Telemetry> Telemetries { get; set; }
public virtual DbSet<TelemetryEvent> TelemetryEvents { get; set; }

View File

@ -6,8 +6,8 @@ using System.Text.Json.Serialization;
namespace AsbCloudDb.Model
{
[Table("t_files"), Comment("Файлы всех категорий")]
public class File : IId, IIdWell
[Table("t_file_info"), Comment("Файлы всех категорий")]
public class FileInfo : IId, IIdWell
{
[Key]
[Column("id")]
@ -26,7 +26,7 @@ namespace AsbCloudDb.Model
public string Name { get; set; }
[Column("date", TypeName = "timestamp with time zone")]
public DateTime Date { get; set; }
public DateTime UploadDate { get; set; }
[Column("is_deleted"), Comment("Удален ли файл")]
public bool IsDeleted { get; set; }
@ -37,7 +37,7 @@ namespace AsbCloudDb.Model
[JsonIgnore]
[ForeignKey(nameof(IdAuthor))]
public virtual User User { get; set; }
public virtual User Author { get; set; }
[JsonIgnore]
[ForeignKey(nameof(IdCategory))]

View File

@ -18,7 +18,7 @@ namespace AsbCloudDb.Model
DbSet<User> Users { get; set; }
DbSet<UserRole> UserRoles { get; set; }
DbSet<ReportProperties> ReportProperties { get; set; }
DbSet<File> Files { get; set; }
DbSet<FileInfo> Files { get; set; }
DbSet<FileCategory> FileCategories { get; set; }
DbSet<Telemetry> Telemetries { get; set; }
DbSet<TelemetryUser> TelemetryUsers { get; set; }

View File

@ -34,6 +34,6 @@ namespace AsbCloudDb.Model
public virtual Well Well { get; set; }
[ForeignKey(nameof(IdFile))]
public virtual File File { get; set; }
public virtual FileInfo File { get; set; }
}
}

View File

@ -55,7 +55,9 @@ namespace AsbCloudDb.Model
[InverseProperty(nameof(Model.UserRole.Users))]
public virtual UserRole Role { get; set; }
public virtual ICollection<File> Files { get; set; }
[InverseProperty(nameof(Model.FileInfo.Author))]
public virtual ICollection<FileInfo> Files { get; set; }
public string MakeDisplayName()
{

View File

@ -1,6 +1,7 @@
using AsbCloudApp.Data;
using AsbCloudApp.Services;
using AsbCloudDb.Model;
using Mapster;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
@ -28,12 +29,12 @@ namespace AsbCloudInfrastructure.Services
foreach (var fileInfo in filesInfo)
{
var file = new File()
var file = new FileInfo()
{
Name = fileInfo.fileName,
IdWell = fileInfo.idWell,
IdCategory = fileInfo.idCategory,
Date = fileInfo.date,
UploadDate = fileInfo.date,
IdAuthor = fileInfo.idUser
};
@ -45,66 +46,69 @@ namespace AsbCloudInfrastructure.Services
return fileIdsToNames;
}
public async Task<PaginationContainer<FilePropertiesDto>> GetFilesInfoAsync(int idWell,
public async Task<PaginationContainer<FileInfoDto>> GetFilesInfoAsync(int idWell,
int idCategory, DateTime begin = default, DateTime end = default,
int skip = 0, int take = 32, CancellationToken token = default)
{
var filesInfoQuery = db.Files.Include(f => f.User)
.Where(f => f.IdWell == idWell &&
f.IdCategory == idCategory).AsNoTracking();
var query = db.Files
.Include(f => f.Author)
.Where(e => e.IdWell == idWell &&
e.IdCategory == idCategory);
if (!filesInfoQuery.Any())
return null;
var result = new PaginationContainer<FilePropertiesDto>() { Skip = skip, Take = take };
query = query.Where(e => !e.IsDeleted);
if (begin != default)
filesInfoQuery = filesInfoQuery.Where(m => m.Date >= begin).AsNoTracking();
query = query.Where(e => e.UploadDate >= begin);
if (end != default)
filesInfoQuery = filesInfoQuery.Where(m => m.Date <= end).AsNoTracking();
query = query.Where(e => e.UploadDate <= end);
result.Count = await filesInfoQuery.CountAsync(token)
.ConfigureAwait(false);
var count = await query.CountAsync(token).ConfigureAwait(false);
if (skip > 0)
filesInfoQuery = filesInfoQuery.Skip(skip).AsNoTracking();
var result = new PaginationContainer<FileInfoDto>(count) {
Skip = skip,
Take = take,
Count = count,
};
var filesInfoList = await filesInfoQuery.OrderBy(f => f.Date)
.Take(take).ToListAsync(token)
.ConfigureAwait(false);
if (filesInfoList.Count == 0)
if (count <= skip)
return result;
foreach (var fileInfo in filesInfoList)
{
var messageDto = new FilePropertiesDto
{
Id = fileInfo.Id,
Name = fileInfo.Name,
IdCategory = fileInfo.IdCategory,
UploadDate = fileInfo.Date,
UserName = fileInfo.User.Name
};
query = query.OrderBy(e => e.UploadDate);
result.Items.Add(messageDto);
if (skip > 0)
query = query.Skip(skip);
query = query.Take(take);
var entities = await query
.Take(take).AsNoTracking().ToListAsync(token)
.ConfigureAwait(false);
foreach (var item in entities)
{
var filePropertiesDto = item.Adapt<FileInfoDto>();
filePropertiesDto.AuthorName = item.Author.Name;
result.Items.Add(filePropertiesDto);
}
return result;
}
public async Task<(int Id, string Name, int IdCategory)?> GetFileInfoAsync(int fileId,
public async Task<FileInfoDto> GetFileInfoAsync(int fileId,
CancellationToken token = default)
{
var fileInfo = await db.Files.AsNoTracking()
var entity = await db.Files
.Include(f => f.Author)
.AsNoTracking()
.FirstOrDefaultAsync(f => f.Id == fileId, token)
.ConfigureAwait(false);
if (fileInfo is null)
if (entity is null)
return null;
return (fileInfo.Id, fileInfo.Name, fileInfo.IdCategory);
var dto = entity.Adapt<FileInfoDto>();
dto.AuthorName = entity.Author.Name;
return dto;
}
}
}

View File

@ -56,13 +56,13 @@ namespace AsbCloudInfrastructure.Services
var shorReportName = Path.GetFileName(newReportName);
reportNameHandler.Invoke(shorReportName, id);
var newReportFile = new AsbCloudDb.Model.File
var newReportFile = new AsbCloudDb.Model.FileInfo
{
IdWell = idWell,
IdAuthor = idUser,
IdCategory = ReportCategoryId,
Name = newReportName,
Date = DateTime.Now,
UploadDate = DateTime.Now,
};
context.Files.Add(newReportFile);
@ -104,7 +104,7 @@ namespace AsbCloudInfrastructure.Services
Name = Path.GetFileName(r.File.Name),
FullName = r.File.Name,
IdWell = r.IdWell,
Date = r.File.Date,
Date = r.File.UploadDate,
Begin = r.Begin,
End = r.End,
Step = r.Step,
@ -155,7 +155,7 @@ namespace AsbCloudInfrastructure.Services
&& r.End <= end
&& r.Step <= stepSeconds
&& r.Format == format
select r).OrderBy(o => o.File.Date)
select r).OrderBy(o => o.File.UploadDate)
.AsNoTracking()
.Take(512).ToListAsync(token)
.ConfigureAwait(false);

View File

@ -85,7 +85,7 @@ namespace AsbCloudWebApi.Controllers
/// <returns>Список информации о файлах в этой категории</returns>
[HttpGet]
[Route("{idWell}")]
[ProducesResponseType(typeof(PaginationContainer<FilePropertiesDto>), (int)System.Net.HttpStatusCode.OK)]
[ProducesResponseType(typeof(PaginationContainer<FileInfoDto>), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> GetFilesInfoAsync([FromRoute] int idWell,
int skip = 0, int take = 32, int idCategory = default,
DateTime begin = default, DateTime end = default, CancellationToken token = default)
@ -135,9 +135,9 @@ namespace AsbCloudWebApi.Controllers
throw new FileNotFoundException();
// TODO: словарь content typoв
var relativePath = Path.Combine(fileService.RootPath, $"{idWell}", $"{fileInfo.Value.IdCategory}",
$"{fileInfo.Value.Id}" + Path.GetExtension($"{fileInfo.Value.Name}"));
return PhysicalFile(Path.GetFullPath(relativePath), "application/octet-stream", fileInfo.Value.Name);
var relativePath = Path.Combine(fileService.RootPath, $"{idWell}", $"{fileInfo.IdCategory}",
$"{fileInfo.Id}" + Path.GetExtension($"{fileInfo.Name}"));
return PhysicalFile(Path.GetFullPath(relativePath), "application/octet-stream", fileInfo.Name);
}
catch (FileNotFoundException ex)
{