DD.WellWorkover.Cloud/AsbCloudInfrastructure/Repository/FileRepository.cs

288 lines
8.5 KiB
C#
Raw Normal View History

using AsbCloudApp.Data;
2022-10-06 14:37:03 +05:00
using AsbCloudApp.Repositories;
using AsbCloudApp.Requests;
2022-10-17 14:42:47 +05:00
using AsbCloudDb;
using AsbCloudDb.Model;
using Mapster;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
2024-08-19 10:01:07 +05:00
namespace AsbCloudInfrastructure.Repository;
public class FileRepository : IFileRepository
{
2024-08-19 10:01:07 +05:00
private readonly IQueryable<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);
}
2024-08-19 10:01:07 +05:00
private IQueryable<FileInfo> BuildQuery(FileRequest request)
{
2024-08-19 10:01:07 +05:00
var query = dbSetConfigured
.Where(e => e.IdWell == request.IdWell);
2024-08-19 10:01:07 +05:00
if (request.IdCategory is not null)
query = query.Where(x => x.IdCategory == request.IdCategory);
if (request.IsDeleted is not null)
query = query.Where(x => x.IsDeleted == request.IsDeleted);
if (request.CompanyNamePart is not null)
query = query.Where(e => e.Author != null && e.Author.Company.Caption.ToLower().Contains(request.CompanyNamePart.ToLower()));
if (request.FileNamePart is not null)
query = query.Where(e => e.Name.ToLower().Contains(request.FileNamePart.ToLower()));
if (request.Begin is not null)
{
2024-08-19 10:01:07 +05:00
var beginUtc = request.Begin.Value.ToUniversalTime();
query = query.Where(e => e.UploadDate >= beginUtc);
}
2024-08-19 10:01:07 +05:00
if (request.End is not null)
{
2024-08-19 10:01:07 +05:00
var endUtc = request.End.Value.ToUniversalTime();
query = query.Where(e => e.UploadDate <= endUtc);
2022-10-17 14:42:47 +05:00
}
2024-08-19 10:01:07 +05:00
if (request?.SortFields?.Any() == true)
2022-10-17 14:42:47 +05:00
{
2024-08-19 10:01:07 +05:00
query = query.SortBy(request.SortFields);
}
2024-08-19 10:01:07 +05:00
else
query = query
.OrderBy(o => o.UploadDate);
2024-08-19 10:01:07 +05:00
return query;
}
public async Task<IEnumerable<FileInfoDto>> GetInfosAsync(FileRequest request, CancellationToken token)
{
var query = BuildQuery(request);
2024-08-19 10:01:07 +05:00
var entities = await query
.SkipTake(request.Skip, request.Take)
.AsNoTracking()
.ToListAsync(token)
.ConfigureAwait(false);
2022-10-17 14:42:47 +05:00
2024-08-19 10:01:07 +05:00
var dtos = entities.Select(e => Convert(e));
return dtos;
}
2024-08-19 10:01:07 +05:00
public async Task<PaginationContainer<FileInfoDto>> GetInfosPaginatedAsync(FileRequest request, CancellationToken token = default)
{
var skip = request.Skip ?? 0;
var take = request.Take ?? 32;
2024-08-19 10:01:07 +05:00
var query = BuildQuery(request);
2024-08-19 10:01:07 +05:00
var result = new PaginationContainer<FileInfoDto>()
{
Skip = skip,
Take = take,
Count = await query.CountAsync(token),
};
2022-10-17 14:42:47 +05:00
2024-08-19 10:01:07 +05:00
if (result.Count == 0)
return result;
2024-08-19 10:01:07 +05:00
var entities = await query
.SkipTake(skip, take)
.AsNoTracking()
.ToListAsync(token)
.ConfigureAwait(false);
2024-08-19 10:01:07 +05:00
result.Items = entities.Select(e => Convert(e));
2024-08-19 10:01:07 +05:00
return result;
}
2024-08-19 10:01:07 +05:00
public async Task<IEnumerable<FileInfoDto>> GetInfoByIdsAsync(IEnumerable<int> idsFile, CancellationToken token)
{
var result = Enumerable.Empty<FileInfoDto>();
2024-08-19 10:01:07 +05:00
var entities = await dbSetConfigured
.AsNoTracking()
.Where(f => idsFile.Contains(f.Id))
.ToListAsync(token)
.ConfigureAwait(false);
2024-08-19 10:01:07 +05:00
if (entities is not null)
result = entities.Select(entity => Convert(entity));
2024-08-19 10:01:07 +05:00
return result;
}
2024-08-19 10:01:07 +05:00
public async Task<int> MarkAsDeletedAsync(int idFile, CancellationToken token = default)
{
var fileInfo = await db.Files.FirstOrDefaultAsync(f => f.Id == idFile, token).ConfigureAwait(false);
2024-08-19 10:01:07 +05:00
if (fileInfo is null)
return 0;
2024-08-19 10:01:07 +05:00
fileInfo.IsDeleted = true;
2024-08-19 10:01:07 +05:00
return await db.SaveChangesAsync(token).ConfigureAwait(false);
}
2024-08-19 10:01:07 +05:00
public async Task<IEnumerable<FileInfoDto>> DeleteAsync(IEnumerable<int> ids, CancellationToken token)
{
var query = dbSetConfigured
.Where(f => ids.Contains(f.Id));
2024-08-19 10:01:07 +05:00
var files = await query.ToListAsync(token);
2024-08-19 10:01:07 +05:00
var filesDtos = files.Select(x => Convert(x));
2024-08-19 10:01:07 +05:00
db.Files.RemoveRange(query);
await db.SaveChangesAsync(token).ConfigureAwait(false);
2024-08-19 10:01:07 +05:00
return filesDtos;
}
2024-08-19 10:01:07 +05:00
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);
2024-08-19 10:01:07 +05:00
FileInfoDto dto = Convert(entity!);
return dto;
}
2024-08-19 10:01:07 +05:00
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 = DateTimeOffset.UtcNow;
newFileMark.IdUser = idUser;
newFileMark.User = null!;
db.FileMarks.Add(newFileMark);
return await db.SaveChangesAsync(token);
}
2024-08-19 10:01:07 +05:00
public async Task<int> MarkFileMarkAsDeletedAsync(IEnumerable<int> idsMarks, CancellationToken token)
{
var fileMarkQuery = db.FileMarks
.Where(m => idsMarks.Contains(m.Id));
2022-09-30 10:49:40 +05:00
2024-08-19 10:01:07 +05:00
foreach (var fileMark in fileMarkQuery)
fileMark.IsDeleted = true;
2022-09-30 10:49:40 +05:00
2024-08-19 10:01:07 +05:00
return await db.SaveChangesAsync(token);
}
2022-09-30 10:49:40 +05:00
2024-08-19 10:01:07 +05:00
public async Task<IEnumerable<FileInfoDto>> GetAllAsync(CancellationToken token)
=> await dbSetConfigured.AsNoTracking()
.Select(x => Convert(x))
.ToListAsync(token)
.ConfigureAwait(false);
2022-09-30 10:49:40 +05:00
2024-08-19 10:01:07 +05:00
public async Task<FileInfoDto?> GetOrDefaultAsync(int id, CancellationToken token)
{
var entity = await dbSetConfigured
.AsNoTracking()
.FirstOrDefaultAsync(f => f.Id == id, token)
.ConfigureAwait(false);
2022-09-30 10:49:40 +05:00
2024-08-19 10:01:07 +05:00
if (entity is null)
return null;
2022-09-30 10:49:40 +05:00
2024-08-19 10:01:07 +05:00
var dto = Convert(entity);
return dto;
}
2022-09-30 10:49:40 +05:00
2024-08-19 10:01:07 +05:00
public FileInfoDto? GetOrDefault(int id)
{
var entity = dbSetConfigured
.AsNoTracking()
.FirstOrDefault(f => f.Id == id);
2022-09-30 10:49:40 +05:00
2024-08-19 10:01:07 +05:00
if (entity is null)
return null;
2022-09-30 10:49:40 +05:00
2024-08-19 10:01:07 +05:00
var dto = Convert(entity);
return dto;
}
2022-09-30 10:49:40 +05:00
2024-08-19 10:01:07 +05:00
public async Task<int> InsertAsync(FileInfoDto newItem, CancellationToken token)
{
var fileInfo = new FileInfo()
2022-09-30 10:49:40 +05:00
{
2024-08-19 10:01:07 +05:00
IdWell = newItem.IdWell,
IdAuthor = newItem.IdAuthor,
IdCategory = newItem.IdCategory,
Name = newItem.Name,
UploadDate = DateTimeOffset.UtcNow,
IsDeleted = false,
Size = newItem.Size,
};
var entry = db.Files.Add(fileInfo);
await db.SaveChangesAsync(token).ConfigureAwait(false);
return entry.Entity.Id;
}
2024-08-19 10:01:07 +05:00
public Task<int> InsertRangeAsync(IEnumerable<FileInfoDto> newItems, CancellationToken token)
{
throw new NotImplementedException();
}
2024-08-19 10:01:07 +05:00
public Task<int> UpdateAsync(FileInfoDto item, CancellationToken token)
{
throw new NotImplementedException();
}
2024-08-19 10:01:07 +05:00
public Task<int> DeleteAsync(int id, CancellationToken token)
{
throw new NotImplementedException();
}
private static FileInfoDto Convert(FileInfo entity)
{
var timezoneOffset = entity.Well.Timezone?.Hours ?? 5;
return Convert(entity, timezoneOffset);
}
private static FileInfoDto Convert(FileInfo entity, double timezoneOffset)
{
var dto = entity.Adapt<FileInfoDto>();
dto.UploadDate = entity.UploadDate.ToOffset(TimeSpan.FromHours(timezoneOffset));
dto.FileMarks = entity.FileMarks.Select(m =>
{
var mark = m.Adapt<FileMarkDto>();
mark.DateCreated = m.DateCreated.ToOffset(TimeSpan.FromHours(timezoneOffset));
return mark;
});
return dto;
}
}