DD.WellWorkover.Cloud/AsbCloudInfrastructure/Services/FileService.cs

149 lines
4.6 KiB
C#
Raw Normal View History

using AsbCloudApp.Data;
using AsbCloudApp.Services;
using AsbCloudDb.Model;
using Mapster;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
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;
}
2021-08-19 17:32:22 +05:00
public IDictionary<string, int> SaveFileInfos(int idWell, int idUser,
IEnumerable<FileInfoDto> filesInfo)
{
var fileIdsToNames = new Dictionary<string, int>();
foreach (var fileInfo in filesInfo)
{
var file = new AsbCloudDb.Model.FileInfo()
{
2021-08-19 17:32:22 +05:00
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<PaginationContainer<FileInfoDto>> GetFilesInfoAsync(int idWell,
int idCategory, IEnumerable<int> 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<FileInfoDto>(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<FileInfoDto>();
filePropertiesDto.AuthorName = item.Author?.Name;
filePropertiesDto.CompanyId = item.Author?.IdCompany ?? 0;
result.Items.Add(filePropertiesDto);
}
return result;
}
public async Task<FileInfoDto> 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<FileInfoDto>();
dto.AuthorName = entity.Author.Name;
return dto;
}
public async Task<int> 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);
}
}
}