using AsbCloudApp.Data; using AsbCloudApp.Services; using AsbCloudDb.Model; using Mapster; using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; using System.Linq; using System.Threading; using System.Threading.Tasks; using System.IO; namespace AsbCloudInfrastructure.Services { public class FileService : IFileService { public string RootPath { get; private set; } private readonly IAsbCloudDbContext db; public FileService(IAsbCloudDbContext db) { RootPath = "files"; this.db = db; } public IDictionary SaveFileInfos(int idWell, int idUser, IEnumerable filesInfo) { var fileIdsToNames = new Dictionary(); foreach (var fileInfo in filesInfo) { var file = new AsbCloudDb.Model.FileInfo() { Name = fileInfo.Name, IdWell = idWell, IdCategory = fileInfo.IdCategory, UploadDate = fileInfo.UploadDate, IdAuthor = idUser }; db.Files.Add(file); db.SaveChanges(); fileIdsToNames.Add(file.Name, file.Id); } return fileIdsToNames; } public async Task SaveFile(int idWell, int idCategory, int fileId, string fileExtension, Stream fileStream) { var relativePath = Path.Combine(RootPath, $"{idWell}", $"{idCategory}", $"{fileId}" + $"{fileExtension}"); Directory.CreateDirectory(Path.GetDirectoryName(relativePath)); using (var newfileStream = new FileStream(relativePath, FileMode.Create)) { await fileStream.CopyToAsync(newfileStream); } } public async Task> GetFilesInfoAsync(int idWell, int idCategory, IEnumerable companies = default, DateTime begin = default, DateTime end = default, int skip = 0, int take = 32, CancellationToken token = default) { var query = db.Files .Include(f => f.Author) .Where(e => e.IdWell == idWell && e.IdCategory == idCategory); query = query.Where(e => !e.IsDeleted); if (companies.Any()) query.Include(file => file.Author).ThenInclude(a => a.Company) .Where(e => companies.Contains(e.Author.Company.Id)); if (begin != default) query = query.Where(e => e.UploadDate >= begin); if (end != default) query = query.Where(e => e.UploadDate <= end); var count = await query.CountAsync(token).ConfigureAwait(false); var result = new PaginationContainer(count) { Skip = skip, Take = take, Count = count, }; if (count <= skip) return result; query = query.OrderBy(e => e.UploadDate); 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; filePropertiesDto.CompanyId = item.Author?.IdCompany ?? 0; result.Items.Add(filePropertiesDto); } return result; } public async Task GetFileInfoAsync(int fileId, CancellationToken token = default) { var entity = await db.Files .Include(f => f.Author) .AsNoTracking() .FirstOrDefaultAsync(f => f.Id == fileId, token) .ConfigureAwait(false); if (entity is null) return null; var dto = entity.Adapt(); dto.AuthorName = entity.Author.Name; return dto; } public async Task DeleteFileAsync(int idFile, CancellationToken token = default) { var fileInfo = db.Files.FirstOrDefault(f => f.Id == idFile); if (fileInfo is null) return 0; fileInfo.IsDeleted = true; return await db.SaveChangesAsync(token); } } }