using System; using System.Linq; using System.Collections.Generic; using Microsoft.EntityFrameworkCore; using AsbCloudApp.Data; using AsbCloudApp.Services; using AsbCloudDb.Model; namespace AsbCloudInfrastructure.Services { public class FileService : IFileService { public string RootPath { get; private set; } private readonly IAsbCloudDbContext db; private readonly ITelemetryService telemetryService; 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 idWell, int idCategory, DateTime date, int idUser)> filesInfo) { var fileIdsToNames = new Dictionary(); foreach(var fileInfo in filesInfo) { var file = new File() { Name = fileInfo.fileName, IdWell = fileInfo.idWell, IdCategory = fileInfo.idCategory, Date = fileInfo.date, IdUser = fileInfo.idUser }; db.Files.Add(file); db.SaveChanges(); fileIdsToNames.Add(file.Name, file.Id); } return fileIdsToNames; } public PaginationContainer GetFilesInfo(int wellId, int idCategory, DateTime begin = default, DateTime end = default, int skip = 0, int take = 32) { var telemetry = telemetryService.GetTelemetryByWellId(wellId); if (telemetry is null) return null; 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) { var fileInfo = db.Files.FirstOrDefault(f => f.Id == fileId); if (fileInfo is null) return null; return (fileInfo.Id, fileInfo.Name, fileInfo.IdCategory); } } }