From 9072abe1521801cff9c747f1b0332e03721272bd Mon Sep 17 00:00:00 2001 From: KharchenkoVV Date: Mon, 26 Jul 2021 11:54:50 +0500 Subject: [PATCH] =?UTF-8?q?CS2-38:=20=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B0=20=D0=BF=D0=B0=D0=B3=D0=B8=D0=BD=D0=B0=D1=86?= =?UTF-8?q?=D0=B8=D1=8F=20=D0=B4=D0=BB=D1=8F=20=D1=82=D0=B0=D0=B1=D0=BB?= =?UTF-8?q?=D0=B8=D1=86=D1=8B=20=D1=81=20=D1=84=D0=B0=D0=B9=D0=BB=D0=B0?= =?UTF-8?q?=D0=BC=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AsbCloudApp/Services/IFileService.cs | 6 +- AsbCloudDb/Model/File.cs | 23 +++++-- AsbCloudDb/Model/Report.cs | 6 +- .../Services/FileService.cs | 65 +++++++++++++++---- AsbCloudWebApi/Controllers/FileController.cs | 18 +++-- 5 files changed, 89 insertions(+), 29 deletions(-) diff --git a/AsbCloudApp/Services/IFileService.cs b/AsbCloudApp/Services/IFileService.cs index 0f6ffea8..f14b4e6a 100644 --- a/AsbCloudApp/Services/IFileService.cs +++ b/AsbCloudApp/Services/IFileService.cs @@ -8,10 +8,12 @@ namespace AsbCloudApp.Services { string RootPath { get; } IDictionary 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 GetFilesInfo(int wellId, int idCategory); + PaginationContainer GetFilesInfo(int wellId, + int idCategory, DateTime begin, DateTime end, + int skip, int take); (int Id, string Name, int IdCategory)? GetFileInfo(int fileId); } diff --git a/AsbCloudDb/Model/File.cs b/AsbCloudDb/Model/File.cs index c557a95f..b3810922 100644 --- a/AsbCloudDb/Model/File.cs +++ b/AsbCloudDb/Model/File.cs @@ -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; } } } \ No newline at end of file diff --git a/AsbCloudDb/Model/Report.cs b/AsbCloudDb/Model/Report.cs index 41bc2e62..4c521d86 100644 --- a/AsbCloudDb/Model/Report.cs +++ b/AsbCloudDb/Model/Report.cs @@ -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; } diff --git a/AsbCloudInfrastructure/Services/FileService.cs b/AsbCloudInfrastructure/Services/FileService.cs index e1828818..f9c8cd73 100644 --- a/AsbCloudInfrastructure/Services/FileService.cs +++ b/AsbCloudInfrastructure/Services/FileService.cs @@ -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 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(); @@ -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 GetFilesInfo(int wellId, int idCategory) + public PaginationContainer 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() { 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) diff --git a/AsbCloudWebApi/Controllers/FileController.cs b/AsbCloudWebApi/Controllers/FileController.cs index bdc24449..7a441684 100644 --- a/AsbCloudWebApi/Controllers/FileController.cs +++ b/AsbCloudWebApi/Controllers/FileController.cs @@ -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 /// /// id скважины /// id категории файла + /// дата начала + /// дата окончания + /// для пагинации кол-во записей пропустить + /// для пагинации кол-во записей взять /// Список информации о файлах в этой категории [HttpGet] [Route("{wellId}/filesInfo")] - [ProducesResponseType(typeof(IEnumerable), (int)System.Net.HttpStatusCode.OK)] - public IActionResult GetFilesInfo([FromRoute] int wellId, int idCategory) + [ProducesResponseType(typeof(PaginationContainer), (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);