From 2e01c4334f182d5327a502c9533cd236246918a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A4=D1=80=D0=BE=D0=BB=D0=BE=D0=B2?= Date: Fri, 13 Aug 2021 17:26:19 +0500 Subject: [PATCH] fix "order for skip/take" warning. some refactoring. --- .../{FilePropertiesDto.cs => FileInfoDto.cs} | 4 +- AsbCloudApp/Services/IFileService.cs | 4 +- AsbCloudDb/Model/AsbCloudDbContext.cs | 2 +- AsbCloudDb/Model/{File.cs => FileInfo.cs} | 8 +- AsbCloudDb/Model/IAsbCloudDbContext.cs | 2 +- AsbCloudDb/Model/ReportProperties.cs | 2 +- AsbCloudDb/Model/User.cs | 4 +- .../Services/FileService.cs | 76 ++++++++++--------- .../Services/ReportService.cs | 8 +- AsbCloudWebApi/Controllers/FileController.cs | 8 +- 10 files changed, 62 insertions(+), 56 deletions(-) rename AsbCloudApp/Data/{FilePropertiesDto.cs => FileInfoDto.cs} (74%) rename AsbCloudDb/Model/{File.cs => FileInfo.cs} (85%) diff --git a/AsbCloudApp/Data/FilePropertiesDto.cs b/AsbCloudApp/Data/FileInfoDto.cs similarity index 74% rename from AsbCloudApp/Data/FilePropertiesDto.cs rename to AsbCloudApp/Data/FileInfoDto.cs index 816cbb86..e42bd8a5 100644 --- a/AsbCloudApp/Data/FilePropertiesDto.cs +++ b/AsbCloudApp/Data/FileInfoDto.cs @@ -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; } } } diff --git a/AsbCloudApp/Services/IFileService.cs b/AsbCloudApp/Services/IFileService.cs index 91d6681e..9a293d01 100644 --- a/AsbCloudApp/Services/IFileService.cs +++ b/AsbCloudApp/Services/IFileService.cs @@ -13,11 +13,11 @@ namespace AsbCloudApp.Services int idCategory, IEnumerable<(string fileName, int idWell, int idCategory, DateTime date, int idUser)> filesInfo); - Task> GetFilesInfoAsync(int idWell, + Task> 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 GetFileInfoAsync(int fileId, CancellationToken token); } } diff --git a/AsbCloudDb/Model/AsbCloudDbContext.cs b/AsbCloudDb/Model/AsbCloudDbContext.cs index 15c79cac..5a6ed4a1 100644 --- a/AsbCloudDb/Model/AsbCloudDbContext.cs +++ b/AsbCloudDb/Model/AsbCloudDbContext.cs @@ -21,7 +21,7 @@ namespace AsbCloudDb.Model public virtual DbSet UserRoles { get; set; } public virtual DbSet Wells { get; set; } public virtual DbSet ReportProperties { get; set; } - public virtual DbSet Files { get; set; } + public virtual DbSet Files { get; set; } public virtual DbSet FileCategories { get; set; } public virtual DbSet Telemetries { get; set; } public virtual DbSet TelemetryEvents { get; set; } diff --git a/AsbCloudDb/Model/File.cs b/AsbCloudDb/Model/FileInfo.cs similarity index 85% rename from AsbCloudDb/Model/File.cs rename to AsbCloudDb/Model/FileInfo.cs index 5d390a5c..7dfcf8ab 100644 --- a/AsbCloudDb/Model/File.cs +++ b/AsbCloudDb/Model/FileInfo.cs @@ -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))] diff --git a/AsbCloudDb/Model/IAsbCloudDbContext.cs b/AsbCloudDb/Model/IAsbCloudDbContext.cs index bf292a7b..d1ea46dc 100644 --- a/AsbCloudDb/Model/IAsbCloudDbContext.cs +++ b/AsbCloudDb/Model/IAsbCloudDbContext.cs @@ -18,7 +18,7 @@ namespace AsbCloudDb.Model DbSet Users { get; set; } DbSet UserRoles { get; set; } DbSet ReportProperties { get; set; } - DbSet Files { get; set; } + DbSet Files { get; set; } DbSet FileCategories { get; set; } DbSet Telemetries { get; set; } DbSet TelemetryUsers { get; set; } diff --git a/AsbCloudDb/Model/ReportProperties.cs b/AsbCloudDb/Model/ReportProperties.cs index 59c71110..39a05019 100644 --- a/AsbCloudDb/Model/ReportProperties.cs +++ b/AsbCloudDb/Model/ReportProperties.cs @@ -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; } } } diff --git a/AsbCloudDb/Model/User.cs b/AsbCloudDb/Model/User.cs index 6304b437..d671423b 100644 --- a/AsbCloudDb/Model/User.cs +++ b/AsbCloudDb/Model/User.cs @@ -55,7 +55,9 @@ namespace AsbCloudDb.Model [InverseProperty(nameof(Model.UserRole.Users))] public virtual UserRole Role { get; set; } - public virtual ICollection Files { get; set; } + + [InverseProperty(nameof(Model.FileInfo.Author))] + public virtual ICollection Files { get; set; } public string MakeDisplayName() { diff --git a/AsbCloudInfrastructure/Services/FileService.cs b/AsbCloudInfrastructure/Services/FileService.cs index 73ee07c8..7e560160 100644 --- a/AsbCloudInfrastructure/Services/FileService.cs +++ b/AsbCloudInfrastructure/Services/FileService.cs @@ -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> GetFilesInfoAsync(int idWell, + public async Task> 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() { 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(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(); + 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 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(); + dto.AuthorName = entity.Author.Name; + return dto; } } } diff --git a/AsbCloudInfrastructure/Services/ReportService.cs b/AsbCloudInfrastructure/Services/ReportService.cs index 7a9ba05a..7747629e 100644 --- a/AsbCloudInfrastructure/Services/ReportService.cs +++ b/AsbCloudInfrastructure/Services/ReportService.cs @@ -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); diff --git a/AsbCloudWebApi/Controllers/FileController.cs b/AsbCloudWebApi/Controllers/FileController.cs index 336041f6..aca12d3d 100644 --- a/AsbCloudWebApi/Controllers/FileController.cs +++ b/AsbCloudWebApi/Controllers/FileController.cs @@ -85,7 +85,7 @@ namespace AsbCloudWebApi.Controllers /// Список информации о файлах в этой категории [HttpGet] [Route("{idWell}")] - [ProducesResponseType(typeof(PaginationContainer), (int)System.Net.HttpStatusCode.OK)] + [ProducesResponseType(typeof(PaginationContainer), (int)System.Net.HttpStatusCode.OK)] public async Task 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) {