CS2-38: Добавлена пагинация для таблицы с файлами

This commit is contained in:
KharchenkoVV 2021-07-26 11:54:50 +05:00
parent 99242f6995
commit 9072abe152
5 changed files with 89 additions and 29 deletions

View File

@ -8,10 +8,12 @@ namespace AsbCloudApp.Services
{ {
string RootPath { get; } string RootPath { get; }
IDictionary<string, int> SaveFilesPropertiesToDb(int wellId, IDictionary<string, int> SaveFilesPropertiesToDb(int wellId,
int idCategory, IEnumerable<(string fileName, int idCategory, int idCategory, IEnumerable<(string fileName, int idWell, int idCategory,
DateTime date, int idUser)> filesInfo); DateTime date, int idUser)> filesInfo);
IEnumerable<FilePropertiesDto> GetFilesInfo(int wellId, int idCategory); PaginationContainer<FilePropertiesDto> GetFilesInfo(int wellId,
int idCategory, DateTime begin, DateTime end,
int skip, int take);
(int Id, string Name, int IdCategory)? GetFileInfo(int fileId); (int Id, string Name, int IdCategory)? GetFileInfo(int fileId);
} }

View File

@ -2,6 +2,7 @@
using System; using System;
using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema; using System.ComponentModel.DataAnnotations.Schema;
using System.Text.Json.Serialization;
namespace AsbCloudDb.Model namespace AsbCloudDb.Model
{ {
@ -12,19 +13,31 @@ namespace AsbCloudDb.Model
[Column("id")] [Column("id")]
public int Id { get; set; } public int Id { get; set; }
[Column("name"), Comment("Название файла")]
public string Name { get; set; }
[Column("id_well"), Comment("id скважины")] [Column("id_well"), Comment("id скважины")]
public int IdWell { get; set; } public int IdWell { get; set; }
[Column("id_user"), Comment("Id пользователя, загрузившего файл")]
public int IdUser { get; set; }
[Column("id_category"), Comment("id категории файла")] [Column("id_category"), Comment("id категории файла")]
public int IdCategory { get; set; } public int IdCategory { get; set; }
[Column("name"), Comment("Название файла")]
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 Date { get; set; }
[Column("id_user"), Comment("Id пользователя, загрузившего файл")] [JsonIgnore]
public int IdUser { get; set; } [ForeignKey(nameof(IdWell))]
public virtual Well Well { get; set; }
[JsonIgnore]
[ForeignKey(nameof(IdUser))]
public virtual User User { get; set; }
[JsonIgnore]
[ForeignKey(nameof(IdCategory))]
public virtual FileCategory FileCategory { get; set; }
} }
} }

View File

@ -12,12 +12,12 @@ namespace AsbCloudDb.Model
[Column("id")] [Column("id")]
public int Id { get; set; } public int Id { get; set; }
[Column("name"), Comment("Название отчета (файла)")]
public string Name { get; set; }
[Column("id_well"), Comment("id скважины")] [Column("id_well"), Comment("id скважины")]
public int IdWell { get; set; } public int IdWell { get; set; }
[Column("name"), Comment("Название отчета (файла)")]
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 Date { get; set; }

View File

@ -1,6 +1,7 @@
using System; using System;
using System.Linq; using System.Linq;
using System.Collections.Generic; using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;
using AsbCloudApp.Data; using AsbCloudApp.Data;
using AsbCloudApp.Services; using AsbCloudApp.Services;
using AsbCloudDb.Model; using AsbCloudDb.Model;
@ -11,15 +12,17 @@ namespace AsbCloudInfrastructure.Services
{ {
public string RootPath { get; private set; } public string RootPath { get; private set; }
private readonly IAsbCloudDbContext db; private readonly IAsbCloudDbContext db;
private readonly ITelemetryService telemetryService;
public FileService(IAsbCloudDbContext db) public FileService(IAsbCloudDbContext db, ITelemetryService telemetryService)
{ {
RootPath = "files"; RootPath = "files";
this.db = db; this.db = db;
this.telemetryService = telemetryService;
} }
public IDictionary<string, int> SaveFilesPropertiesToDb(int wellId, int idCategory, public IDictionary<string, int> SaveFilesPropertiesToDb(int wellId, int idCategory,
IEnumerable<(string fileName, int idCategory, DateTime date, int idUser)> filesInfo) IEnumerable<(string fileName, int idWell, int idCategory, DateTime date, int idUser)> filesInfo)
{ {
var fileIdsToNames = new Dictionary<string, int>(); var fileIdsToNames = new Dictionary<string, int>();
@ -28,6 +31,7 @@ namespace AsbCloudInfrastructure.Services
var file = new File() var file = new File()
{ {
Name = fileInfo.fileName, Name = fileInfo.fileName,
IdWell = fileInfo.idWell,
IdCategory = fileInfo.idCategory, IdCategory = fileInfo.idCategory,
Date = fileInfo.date, Date = fileInfo.date,
IdUser = fileInfo.idUser IdUser = fileInfo.idUser
@ -41,19 +45,54 @@ namespace AsbCloudInfrastructure.Services
return fileIdsToNames; return fileIdsToNames;
} }
public IEnumerable<FilePropertiesDto> GetFilesInfo(int wellId, int idCategory) public PaginationContainer<FilePropertiesDto> GetFilesInfo(int wellId,
int idCategory, DateTime begin = default, DateTime end = default,
int skip = 0, int take = 32)
{ {
var fileInfoFromDb = db.Files.Where(f => f.IdWell == wellId && var telemetry = telemetryService.GetTelemetryByWellId(wellId);
f.IdCategory == idCategory).Select(file => new FilePropertiesDto if (telemetry is null)
{ return null;
Id = file.Id,
Name = file.Name,
IdCategory = file.IdCategory,
UploadDate = file.Date,
UserName = file.IdUser.ToString()
}).ToList();
return fileInfoFromDb; var filesInfoFromDb = db.Files.Include(f => f.User)
.Where(f => f.IdWell == wellId &&
f.IdCategory == idCategory);
if (!filesInfoFromDb.Any())
return null;
var result = new PaginationContainer<FilePropertiesDto>() { Skip = skip, Take = take };
if (begin != default)
filesInfoFromDb = filesInfoFromDb.Where(m => m.Date >= begin);
if (end != default)
filesInfoFromDb = filesInfoFromDb.Where(m => m.Date <= end);
result.Count = filesInfoFromDb.Count();
if (skip > 0)
filesInfoFromDb = filesInfoFromDb.Skip(skip);
var filesInfoList = filesInfoFromDb.Take(take).ToList();
if (filesInfoList.Count == 0)
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
};
result.Items.Add(messageDto);
}
return result;
} }
public (int Id, string Name, int IdCategory)? GetFileInfo(int fileId) public (int Id, string Name, int IdCategory)? GetFileInfo(int fileId)

View File

@ -1,5 +1,4 @@
using System; using System;
using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.IO; using System.IO;
using AsbCloudApp.Data; using AsbCloudApp.Data;
@ -47,7 +46,7 @@ namespace AsbCloudWebApi.Controllers
return Forbid(); return Forbid();
var fileInfoCollection = files.Select(f => var fileInfoCollection = files.Select(f =>
(f.FileName, idCategory, DateTime.Now, idUser)); (f.FileName, wellId, idCategory, DateTime.Now, idUser));
var fileNamesAndIds = fileService.SaveFilesPropertiesToDb(wellId, var fileNamesAndIds = fileService.SaveFilesPropertiesToDb(wellId,
idCategory, fileInfoCollection); idCategory, fileInfoCollection);
@ -74,20 +73,27 @@ namespace AsbCloudWebApi.Controllers
/// </summary> /// </summary>
/// <param name="wellId">id скважины</param> /// <param name="wellId">id скважины</param>
/// <param name="idCategory">id категории файла</param> /// <param name="idCategory">id категории файла</param>
/// <param name="begin">дата начала</param>
/// <param name="end">дата окончания</param>
/// <param name="skip">для пагинации кол-во записей пропустить</param>
/// <param name="take">для пагинации кол-во записей взять </param>
/// <returns>Список информации о файлах в этой категории</returns> /// <returns>Список информации о файлах в этой категории</returns>
[HttpGet] [HttpGet]
[Route("{wellId}/filesInfo")] [Route("{wellId}/filesInfo")]
[ProducesResponseType(typeof(IEnumerable<FilePropertiesDto>), (int)System.Net.HttpStatusCode.OK)] [ProducesResponseType(typeof(PaginationContainer<FilePropertiesDto>), (int)System.Net.HttpStatusCode.OK)]
public IActionResult GetFilesInfo([FromRoute] int wellId, int idCategory) public IActionResult GetFilesInfo([FromRoute] int wellId,
int skip = 0, int take = 32, int idCategory = default,
DateTime begin = default, DateTime end = default)
{ {
int? idCompany = User.GetCompanyId(); int? idCompany = User.GetCompanyId();
if (idCompany is null || !wellService.CheckWellOwnership((int)idCompany, wellId)) if (idCompany is null || !wellService.CheckWellOwnership((int)idCompany, wellId))
return Forbid(); return Forbid();
var filesInfo = fileService.GetFilesInfo(wellId, idCategory); var filesInfo = fileService.GetFilesInfo(wellId, idCategory,
begin, end, skip, take);
if (!filesInfo.Any()) if (filesInfo is null || !filesInfo.Items.Any())
return NoContent(); return NoContent();
return Ok(filesInfo); return Ok(filesInfo);