135 lines
4.3 KiB
C#
135 lines
4.3 KiB
C#
using GozareshgirProgramManager.Domain.FileManagementAgg.Entities;
|
|
using GozareshgirProgramManager.Domain.FileManagementAgg.Enums;
|
|
using GozareshgirProgramManager.Domain.FileManagementAgg.Repositories;
|
|
using GozareshgirProgramManager.Infrastructure.Persistence.Context;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace GozareshgirProgramManager.Infrastructure.Persistence.Repositories.FileManagement;
|
|
|
|
public class UploadedFileRepository : IUploadedFileRepository
|
|
{
|
|
private readonly ProgramManagerDbContext _context;
|
|
|
|
public UploadedFileRepository(ProgramManagerDbContext context)
|
|
{
|
|
_context = context;
|
|
}
|
|
|
|
public async Task<UploadedFile?> GetByIdAsync(Guid fileId)
|
|
{
|
|
return await _context.UploadedFiles
|
|
.FirstOrDefaultAsync(x => x.Id == fileId);
|
|
}
|
|
|
|
public async Task<UploadedFile?> GetByUniqueFileNameAsync(string uniqueFileName)
|
|
{
|
|
return await _context.UploadedFiles
|
|
.FirstOrDefaultAsync(x => x.UniqueFileName == uniqueFileName);
|
|
}
|
|
|
|
public async Task<List<UploadedFile>> GetUserFilesAsync(long userId, int pageNumber, int pageSize)
|
|
{
|
|
return await _context.UploadedFiles
|
|
.Where(x => x.UploadedByUserId == userId)
|
|
.OrderByDescending(x => x.UploadDate)
|
|
.Skip((pageNumber - 1) * pageSize)
|
|
.Take(pageSize)
|
|
.ToListAsync();
|
|
}
|
|
|
|
public async Task<List<UploadedFile>> GetByCategoryAsync(FileCategory category, int pageNumber, int pageSize)
|
|
{
|
|
return await _context.UploadedFiles
|
|
.Where(x => x.Category == category)
|
|
.OrderByDescending(x => x.UploadDate)
|
|
.Skip((pageNumber - 1) * pageSize)
|
|
.Take(pageSize)
|
|
.ToListAsync();
|
|
}
|
|
|
|
public async Task<List<UploadedFile>> GetByStatusAsync(FileStatus status, int pageNumber, int pageSize)
|
|
{
|
|
return await _context.UploadedFiles
|
|
.Where(x => x.Status == status)
|
|
.OrderByDescending(x => x.UploadDate)
|
|
.Skip((pageNumber - 1) * pageSize)
|
|
.Take(pageSize)
|
|
.ToListAsync();
|
|
}
|
|
|
|
public async Task<List<UploadedFile>> GetByReferenceAsync(string entityType, string entityId)
|
|
{
|
|
return await _context.UploadedFiles
|
|
.Where(x => x.ReferenceEntityType == entityType && x.ReferenceEntityId == entityId)
|
|
.OrderByDescending(x => x.UploadDate)
|
|
.ToListAsync();
|
|
}
|
|
|
|
public async Task<List<UploadedFile>> SearchByNameAsync(string searchTerm, int pageNumber, int pageSize)
|
|
{
|
|
return await _context.UploadedFiles
|
|
.Where(x => x.OriginalFileName.Contains(searchTerm))
|
|
.OrderByDescending(x => x.UploadDate)
|
|
.Skip((pageNumber - 1) * pageSize)
|
|
.Take(pageSize)
|
|
.ToListAsync();
|
|
}
|
|
|
|
public async Task<int> GetUserFilesCountAsync(long userId)
|
|
{
|
|
return await _context.UploadedFiles
|
|
.CountAsync(x => x.UploadedByUserId == userId);
|
|
}
|
|
|
|
public async Task<long> GetUserTotalFileSizeAsync(long userId)
|
|
{
|
|
return await _context.UploadedFiles
|
|
.Where(x => x.UploadedByUserId == userId)
|
|
.SumAsync(x => x.FileSizeBytes);
|
|
}
|
|
|
|
public async Task<List<UploadedFile>> GetExpiredFilesAsync(DateTime olderThan)
|
|
{
|
|
return await _context.UploadedFiles
|
|
.IgnoreQueryFilters() // Include deleted files
|
|
.Where(x => x.IsDeleted && x.DeletedDate < olderThan)
|
|
.ToListAsync();
|
|
}
|
|
|
|
public async Task<UploadedFile> AddAsync(UploadedFile file)
|
|
{
|
|
await _context.UploadedFiles.AddAsync(file);
|
|
return file;
|
|
}
|
|
|
|
public Task UpdateAsync(UploadedFile file)
|
|
{
|
|
_context.UploadedFiles.Update(file);
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
public Task DeleteAsync(UploadedFile file)
|
|
{
|
|
_context.UploadedFiles.Remove(file);
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
public async Task<int> SaveChangesAsync()
|
|
{
|
|
return await _context.SaveChangesAsync();
|
|
}
|
|
|
|
public async Task<bool> ExistsAsync(Guid fileId)
|
|
{
|
|
return await _context.UploadedFiles
|
|
.AnyAsync(x => x.Id == fileId);
|
|
}
|
|
|
|
public async Task<bool> ExistsByUniqueFileNameAsync(string uniqueFileName)
|
|
{
|
|
return await _context.UploadedFiles
|
|
.AnyAsync(x => x.UniqueFileName == uniqueFileName);
|
|
}
|
|
}
|
|
|