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 namespace AsbCloudApp.Data
{ {
public class FilePropertiesDto public class FileInfoDto
{ {
public int Id { get; set; } public int Id { get; set; }
public string Name { get; set; } public string Name { get; set; }
public int IdCategory { get; set; } public int IdCategory { get; set; }
public DateTime UploadDate { 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, int idCategory, IEnumerable<(string fileName, int idWell, int idCategory,
DateTime date, int idUser)> filesInfo); DateTime date, int idUser)> filesInfo);
Task<PaginationContainer<FilePropertiesDto>> GetFilesInfoAsync(int idWell, Task<PaginationContainer<FileInfoDto>> GetFilesInfoAsync(int idWell,
int idCategory, DateTime begin, DateTime end, int idCategory, DateTime begin, DateTime end,
int skip, int take, CancellationToken token = default); int skip, int take, CancellationToken token = default);
Task<(int Id, string Name, int IdCategory)?> GetFileInfoAsync(int fileId, Task<FileInfoDto> GetFileInfoAsync(int fileId,
CancellationToken token); CancellationToken token);
} }
} }

View File

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

View File

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

View File

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

View File

@ -34,6 +34,6 @@ namespace AsbCloudDb.Model
public virtual Well Well { get; set; } public virtual Well Well { get; set; }
[ForeignKey(nameof(IdFile))] [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))] [InverseProperty(nameof(Model.UserRole.Users))]
public virtual UserRole Role { get; set; } 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() public string MakeDisplayName()
{ {

View File

@ -1,6 +1,7 @@
using AsbCloudApp.Data; using AsbCloudApp.Data;
using AsbCloudApp.Services; using AsbCloudApp.Services;
using AsbCloudDb.Model; using AsbCloudDb.Model;
using Mapster;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
@ -28,12 +29,12 @@ namespace AsbCloudInfrastructure.Services
foreach (var fileInfo in filesInfo) foreach (var fileInfo in filesInfo)
{ {
var file = new File() var file = new FileInfo()
{ {
Name = fileInfo.fileName, Name = fileInfo.fileName,
IdWell = fileInfo.idWell, IdWell = fileInfo.idWell,
IdCategory = fileInfo.idCategory, IdCategory = fileInfo.idCategory,
Date = fileInfo.date, UploadDate = fileInfo.date,
IdAuthor = fileInfo.idUser IdAuthor = fileInfo.idUser
}; };
@ -45,66 +46,69 @@ namespace AsbCloudInfrastructure.Services
return fileIdsToNames; 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 idCategory, DateTime begin = default, DateTime end = default,
int skip = 0, int take = 32, CancellationToken token = default) int skip = 0, int take = 32, CancellationToken token = default)
{ {
var filesInfoQuery = db.Files.Include(f => f.User) var query = db.Files
.Where(f => f.IdWell == idWell && .Include(f => f.Author)
f.IdCategory == idCategory).AsNoTracking(); .Where(e => e.IdWell == idWell &&
e.IdCategory == idCategory);
if (!filesInfoQuery.Any()) query = query.Where(e => !e.IsDeleted);
return null;
var result = new PaginationContainer<FilePropertiesDto>() { Skip = skip, Take = take };
if (begin != default) if (begin != default)
filesInfoQuery = filesInfoQuery.Where(m => m.Date >= begin).AsNoTracking(); query = query.Where(e => e.UploadDate >= begin);
if (end != default) if (end != default)
filesInfoQuery = filesInfoQuery.Where(m => m.Date <= end).AsNoTracking(); query = query.Where(e => e.UploadDate <= end);
result.Count = await filesInfoQuery.CountAsync(token) var count = await query.CountAsync(token).ConfigureAwait(false);
.ConfigureAwait(false);
if (skip > 0) var result = new PaginationContainer<FileInfoDto>(count) {
filesInfoQuery = filesInfoQuery.Skip(skip).AsNoTracking(); Skip = skip,
Take = take,
Count = count,
};
var filesInfoList = await filesInfoQuery.OrderBy(f => f.Date) if (count <= skip)
.Take(take).ToListAsync(token)
.ConfigureAwait(false);
if (filesInfoList.Count == 0)
return result; return result;
foreach (var fileInfo in filesInfoList) query = query.OrderBy(e => e.UploadDate);
{
var messageDto = new FilePropertiesDto
{
Id = fileInfo.Id,
Name = fileInfo.Name,
IdCategory = fileInfo.IdCategory,
UploadDate = fileInfo.Date,
UserName = fileInfo.User.Name
};
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; return result;
} }
public async Task<(int Id, string Name, int IdCategory)?> GetFileInfoAsync(int fileId, public async Task<FileInfoDto> GetFileInfoAsync(int fileId,
CancellationToken token = default) 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) .FirstOrDefaultAsync(f => f.Id == fileId, token)
.ConfigureAwait(false); .ConfigureAwait(false);
if (fileInfo is null) if (entity is null)
return 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); var shorReportName = Path.GetFileName(newReportName);
reportNameHandler.Invoke(shorReportName, id); reportNameHandler.Invoke(shorReportName, id);
var newReportFile = new AsbCloudDb.Model.File var newReportFile = new AsbCloudDb.Model.FileInfo
{ {
IdWell = idWell, IdWell = idWell,
IdAuthor = idUser, IdAuthor = idUser,
IdCategory = ReportCategoryId, IdCategory = ReportCategoryId,
Name = newReportName, Name = newReportName,
Date = DateTime.Now, UploadDate = DateTime.Now,
}; };
context.Files.Add(newReportFile); context.Files.Add(newReportFile);
@ -104,7 +104,7 @@ namespace AsbCloudInfrastructure.Services
Name = Path.GetFileName(r.File.Name), Name = Path.GetFileName(r.File.Name),
FullName = r.File.Name, FullName = r.File.Name,
IdWell = r.IdWell, IdWell = r.IdWell,
Date = r.File.Date, Date = r.File.UploadDate,
Begin = r.Begin, Begin = r.Begin,
End = r.End, End = r.End,
Step = r.Step, Step = r.Step,
@ -155,7 +155,7 @@ namespace AsbCloudInfrastructure.Services
&& r.End <= end && r.End <= end
&& r.Step <= stepSeconds && r.Step <= stepSeconds
&& r.Format == format && r.Format == format
select r).OrderBy(o => o.File.Date) select r).OrderBy(o => o.File.UploadDate)
.AsNoTracking() .AsNoTracking()
.Take(512).ToListAsync(token) .Take(512).ToListAsync(token)
.ConfigureAwait(false); .ConfigureAwait(false);

View File

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