forked from ddrilling/AsbCloudServer
276 lines
9.4 KiB
C#
276 lines
9.4 KiB
C#
using AsbCloudApp.Data;
|
|
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.Repository
|
|
{
|
|
public class FileRepository : IFileRepository
|
|
{
|
|
private readonly IQueryable<AsbCloudDb.Model.FileInfo> dbSetConfigured;
|
|
private readonly IAsbCloudDbContext db;
|
|
|
|
public FileRepository(IAsbCloudDbContext db)
|
|
{
|
|
this.db = db;
|
|
this.dbSetConfigured = db.Files
|
|
.Include(f => f.Author)
|
|
.ThenInclude(u => u.Company)
|
|
.ThenInclude(c => c.CompanyType)
|
|
.Include(f => f.FileMarks)
|
|
.ThenInclude(m => m.User)
|
|
.Include(f => f.Well);
|
|
}
|
|
|
|
public async Task<int> AddAsync(int idWell, int? idUser, int idCategory,
|
|
string destinationFileName, long fileSize, CancellationToken token = default)
|
|
{
|
|
var fileInfo = new AsbCloudDb.Model.FileInfo()
|
|
{
|
|
IdWell = idWell,
|
|
IdAuthor = idUser,
|
|
IdCategory = idCategory,
|
|
Name = destinationFileName,
|
|
UploadDate = DateTime.UtcNow,
|
|
IsDeleted = false,
|
|
Size = fileSize,
|
|
};
|
|
|
|
var entry = db.Files.Add(fileInfo);
|
|
await db.SaveChangesAsync(token).ConfigureAwait(false);
|
|
return entry.Entity.Id;
|
|
}
|
|
|
|
public async Task<IEnumerable<FileInfoDto>> GetInfosByCategoryAsync(int idWell, int idCategory, CancellationToken token)
|
|
{
|
|
var entities = await dbSetConfigured
|
|
.Where(e => e.IdWell == idWell && e.IdCategory == idCategory && e.IsDeleted == false)
|
|
.AsNoTracking()
|
|
.ToListAsync(token)
|
|
.ConfigureAwait(false);
|
|
|
|
var dtos = entities.Select(e => Convert(e));
|
|
return dtos;
|
|
}
|
|
|
|
public async Task<PaginationContainer<FileInfoDto>> GetInfosAsync(int idWell,
|
|
int idCategory, string companyName = default, string fileName = default, DateTime begin = default,
|
|
DateTime end = default, int skip = 0, int take = 32, CancellationToken token = default)
|
|
{
|
|
var query = dbSetConfigured
|
|
.Where(e => e.IdWell == idWell &&
|
|
e.IdCategory == idCategory &&
|
|
!e.IsDeleted);
|
|
|
|
if (!string.IsNullOrEmpty(companyName))
|
|
query = query.Where(e => (e.Author == null) ||
|
|
(e.Author.Company == null) ||
|
|
e.Author.Company.Caption.Contains(companyName));
|
|
|
|
if (!string.IsNullOrEmpty(fileName))
|
|
query = query.Where(e => e.Name.ToLower().Contains(fileName.ToLower()));
|
|
|
|
var firstFile = await query.FirstOrDefaultAsync(token);
|
|
if (firstFile is null)
|
|
return new PaginationContainer<FileInfoDto>()
|
|
{
|
|
Skip = skip,
|
|
Take = take,
|
|
Count = 0,
|
|
};
|
|
|
|
var timezoneOffset = firstFile.Well.Timezone?.Hours ?? 5;
|
|
|
|
if (begin != default)
|
|
{
|
|
var beginUtc = begin.ToUtcDateTimeOffset(timezoneOffset);
|
|
query = query.Where(e => e.UploadDate >= beginUtc);
|
|
}
|
|
|
|
if (end != default)
|
|
{
|
|
var endUtc = end.ToUtcDateTimeOffset(timezoneOffset);
|
|
query = query.Where(e => e.UploadDate <= endUtc);
|
|
}
|
|
|
|
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);
|
|
|
|
var dtos = entities.Select(e => Convert(e, timezoneOffset));
|
|
result.Items.AddRange(dtos);
|
|
return result;
|
|
}
|
|
|
|
public async Task<FileInfoDto> GetInfoAsync(int idFile, CancellationToken token)
|
|
{
|
|
var entity = await dbSetConfigured
|
|
.AsNoTracking()
|
|
.FirstOrDefaultAsync(f => f.Id == idFile, token)
|
|
.ConfigureAwait(false);
|
|
|
|
if (entity is null)
|
|
{
|
|
throw new FileNotFoundException($"fileId:{idFile} not found");
|
|
}
|
|
|
|
var dto = Convert(entity);
|
|
return dto;
|
|
}
|
|
|
|
public async Task<IEnumerable<FileInfoDto>> GetInfoByIdsAsync(IEnumerable<int> idsFile, CancellationToken token)
|
|
{
|
|
var result = new List<FileInfoDto>();
|
|
|
|
var entities = await dbSetConfigured
|
|
.AsNoTracking()
|
|
.Where(f => idsFile.Contains(f.Id))
|
|
.ToListAsync(token)
|
|
.ConfigureAwait(false);
|
|
|
|
foreach (var entity in entities)
|
|
{
|
|
if (entity is null)
|
|
{
|
|
throw new FileNotFoundException($"fileId:{entity.Id} not found");
|
|
}
|
|
result.Add(Convert(entity));
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
public async Task<int> MarkAsDeletedAsync(int idFile, CancellationToken token = default)
|
|
{
|
|
var fileInfo = await db.Files.FirstOrDefaultAsync(f => f.Id == idFile, token).ConfigureAwait(false);
|
|
|
|
if (fileInfo is null)
|
|
return 0;
|
|
|
|
fileInfo.IsDeleted = true;
|
|
|
|
return await db.SaveChangesAsync(token).ConfigureAwait(false);
|
|
}
|
|
|
|
public async Task<IEnumerable<FileInfoDto>> DeleteAsync(IEnumerable<int> ids, CancellationToken token)
|
|
{
|
|
var filesQuery = db.Files
|
|
.Where(f => ids.Contains(f.Id));
|
|
|
|
var files = await filesQuery.ToListAsync(token);
|
|
|
|
var filesDtos = files.Select(x => new FileInfoDto {
|
|
Id = x.Id,
|
|
IdWell = x.Id,
|
|
IdCategory = x.IdCategory,
|
|
Name = x.Name
|
|
});
|
|
|
|
db.Files.RemoveRange(filesQuery);
|
|
await db.SaveChangesAsync(token).ConfigureAwait(false);
|
|
|
|
return filesDtos;
|
|
}
|
|
|
|
public async Task<FileInfoDto> GetByMarkId(int idMark,
|
|
CancellationToken token)
|
|
{
|
|
var entity = await dbSetConfigured
|
|
.FirstOrDefaultAsync(f => f.FileMarks.Any(m => m.Id == idMark), token)
|
|
.ConfigureAwait(false);
|
|
|
|
FileInfoDto dto = Convert(entity);
|
|
return dto;
|
|
}
|
|
|
|
public async Task<int> CreateFileMarkAsync(FileMarkDto fileMarkDto, int idUser, CancellationToken token)
|
|
{
|
|
var fileMark = await db.FileMarks
|
|
.FirstOrDefaultAsync(m => m.IdFile == fileMarkDto.IdFile &&
|
|
m.IdMarkType == fileMarkDto.IdMarkType &&
|
|
m.IdUser == idUser &&
|
|
m.IsDeleted == false,
|
|
token)
|
|
.ConfigureAwait(false);
|
|
|
|
if (fileMark is not null)
|
|
return 0;
|
|
|
|
var newFileMark = fileMarkDto.Adapt<FileMark>();
|
|
newFileMark.Id = default;
|
|
newFileMark.DateCreated = DateTime.UtcNow;
|
|
newFileMark.IdUser = idUser;
|
|
newFileMark.User = null;
|
|
|
|
db.FileMarks.Add(newFileMark);
|
|
return await db.SaveChangesAsync(token);
|
|
}
|
|
|
|
public async Task<int> MarkFileMarkAsDeletedAsync(IEnumerable<int> idsMarks, CancellationToken token)
|
|
{
|
|
var fileMarkQuery = db.FileMarks
|
|
.Where(m => idsMarks.Contains(m.Id));
|
|
|
|
foreach (var fileMark in fileMarkQuery)
|
|
fileMark.IsDeleted = true;
|
|
|
|
return await db.SaveChangesAsync(token);
|
|
}
|
|
|
|
public async Task<IEnumerable<FileInfoDto>> GetInfosByWellIdAsync(int idWell, CancellationToken token)
|
|
{
|
|
var entities = await dbSetConfigured
|
|
.Where(e => e.IdWell == idWell && e.IsDeleted == false)
|
|
.AsNoTracking()
|
|
.ToListAsync(token)
|
|
.ConfigureAwait(false);
|
|
|
|
var dtos = entities.Select(e => Convert(e));
|
|
return dtos;
|
|
}
|
|
|
|
private static FileInfoDto Convert(AsbCloudDb.Model.FileInfo entity)
|
|
{
|
|
var timezoneOffset = entity.Well.Timezone?.Hours ?? 5;
|
|
return Convert(entity, timezoneOffset);
|
|
}
|
|
|
|
private static FileInfoDto Convert(AsbCloudDb.Model.FileInfo entity, double timezoneOffset)
|
|
{
|
|
var dto = entity.Adapt<FileInfoDto>();
|
|
dto.UploadDate = entity.UploadDate.ToRemoteDateTime(timezoneOffset);
|
|
dto.FileMarks = entity.FileMarks.Select(m =>
|
|
{
|
|
var mark = m.Adapt<FileMarkDto>();
|
|
mark.DateCreated = m.DateCreated.ToRemoteDateTime(timezoneOffset);
|
|
return mark;
|
|
});
|
|
return dto;
|
|
}
|
|
}
|
|
}
|