forked from ddrilling/AsbCloudServer
CS2-38: Добавлена пагинация для таблицы с файлами
This commit is contained in:
parent
99242f6995
commit
9072abe152
@ -8,10 +8,12 @@ namespace AsbCloudApp.Services
|
||||
{
|
||||
string RootPath { get; }
|
||||
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);
|
||||
|
||||
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);
|
||||
}
|
||||
|
@ -2,6 +2,7 @@
|
||||
using System;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace AsbCloudDb.Model
|
||||
{
|
||||
@ -12,19 +13,31 @@ namespace AsbCloudDb.Model
|
||||
[Column("id")]
|
||||
public int Id { get; set; }
|
||||
|
||||
[Column("name"), Comment("Название файла")]
|
||||
public string Name { get; set; }
|
||||
|
||||
[Column("id_well"), Comment("id скважины")]
|
||||
public int IdWell { get; set; }
|
||||
|
||||
[Column("id_user"), Comment("Id пользователя, загрузившего файл")]
|
||||
public int IdUser { get; set; }
|
||||
|
||||
[Column("id_category"), Comment("id категории файла")]
|
||||
public int IdCategory { get; set; }
|
||||
|
||||
[Column("name"), Comment("Название файла")]
|
||||
public string Name { get; set; }
|
||||
|
||||
[Column("date", TypeName = "timestamp with time zone")]
|
||||
public DateTime Date { get; set; }
|
||||
|
||||
[Column("id_user"), Comment("Id пользователя, загрузившего файл")]
|
||||
public int IdUser { get; set; }
|
||||
[JsonIgnore]
|
||||
[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; }
|
||||
}
|
||||
}
|
@ -12,12 +12,12 @@ namespace AsbCloudDb.Model
|
||||
[Column("id")]
|
||||
public int Id { get; set; }
|
||||
|
||||
[Column("name"), Comment("Название отчета (файла)")]
|
||||
public string Name { get; set; }
|
||||
|
||||
[Column("id_well"), Comment("id скважины")]
|
||||
public int IdWell { get; set; }
|
||||
|
||||
[Column("name"), Comment("Название отчета (файла)")]
|
||||
public string Name { get; set; }
|
||||
|
||||
[Column("date", TypeName = "timestamp with time zone")]
|
||||
public DateTime Date { get; set; }
|
||||
|
||||
|
@ -1,6 +1,7 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using AsbCloudApp.Data;
|
||||
using AsbCloudApp.Services;
|
||||
using AsbCloudDb.Model;
|
||||
@ -11,15 +12,17 @@ namespace AsbCloudInfrastructure.Services
|
||||
{
|
||||
public string RootPath { get; private set; }
|
||||
private readonly IAsbCloudDbContext db;
|
||||
private readonly ITelemetryService telemetryService;
|
||||
|
||||
public FileService(IAsbCloudDbContext db)
|
||||
public FileService(IAsbCloudDbContext db, ITelemetryService telemetryService)
|
||||
{
|
||||
RootPath = "files";
|
||||
this.db = db;
|
||||
this.telemetryService = telemetryService;
|
||||
}
|
||||
|
||||
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>();
|
||||
|
||||
@ -28,6 +31,7 @@ namespace AsbCloudInfrastructure.Services
|
||||
var file = new File()
|
||||
{
|
||||
Name = fileInfo.fileName,
|
||||
IdWell = fileInfo.idWell,
|
||||
IdCategory = fileInfo.idCategory,
|
||||
Date = fileInfo.date,
|
||||
IdUser = fileInfo.idUser
|
||||
@ -41,19 +45,54 @@ namespace AsbCloudInfrastructure.Services
|
||||
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 &&
|
||||
f.IdCategory == idCategory).Select(file => new FilePropertiesDto
|
||||
{
|
||||
Id = file.Id,
|
||||
Name = file.Name,
|
||||
IdCategory = file.IdCategory,
|
||||
UploadDate = file.Date,
|
||||
UserName = file.IdUser.ToString()
|
||||
}).ToList();
|
||||
var telemetry = telemetryService.GetTelemetryByWellId(wellId);
|
||||
if (telemetry is null)
|
||||
return null;
|
||||
|
||||
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)
|
||||
|
@ -1,5 +1,4 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.IO;
|
||||
using AsbCloudApp.Data;
|
||||
@ -47,7 +46,7 @@ namespace AsbCloudWebApi.Controllers
|
||||
return Forbid();
|
||||
|
||||
var fileInfoCollection = files.Select(f =>
|
||||
(f.FileName, idCategory, DateTime.Now, idUser));
|
||||
(f.FileName, wellId, idCategory, DateTime.Now, idUser));
|
||||
|
||||
var fileNamesAndIds = fileService.SaveFilesPropertiesToDb(wellId,
|
||||
idCategory, fileInfoCollection);
|
||||
@ -74,20 +73,27 @@ namespace AsbCloudWebApi.Controllers
|
||||
/// </summary>
|
||||
/// <param name="wellId">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>
|
||||
[HttpGet]
|
||||
[Route("{wellId}/filesInfo")]
|
||||
[ProducesResponseType(typeof(IEnumerable<FilePropertiesDto>), (int)System.Net.HttpStatusCode.OK)]
|
||||
public IActionResult GetFilesInfo([FromRoute] int wellId, int idCategory)
|
||||
[ProducesResponseType(typeof(PaginationContainer<FilePropertiesDto>), (int)System.Net.HttpStatusCode.OK)]
|
||||
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();
|
||||
|
||||
if (idCompany is null || !wellService.CheckWellOwnership((int)idCompany, wellId))
|
||||
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 Ok(filesInfo);
|
||||
|
Loading…
Reference in New Issue
Block a user