48 lines
1.6 KiB
C#
48 lines
1.6 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
using SPMS.Domain.Entities;
|
|
using SPMS.Domain.Interfaces;
|
|
|
|
namespace SPMS.Infrastructure.Persistence.Repositories;
|
|
|
|
public class FileRepository : Repository<FileEntity>, IFileRepository
|
|
{
|
|
public FileRepository(AppDbContext context) : base(context) { }
|
|
|
|
public async Task<FileEntity?> GetByFileNameAsync(long serviceId, string fileName)
|
|
{
|
|
return await _dbSet
|
|
.FirstOrDefaultAsync(f => f.ServiceId == serviceId && f.FileName == fileName && !f.IsDeleted);
|
|
}
|
|
|
|
public async Task<bool> FileExistsAsync(long serviceId, string fileName)
|
|
{
|
|
return await _dbSet
|
|
.AnyAsync(f => f.ServiceId == serviceId && f.FileName == fileName && !f.IsDeleted);
|
|
}
|
|
|
|
public async Task<FileEntity?> GetByIdAndServiceAsync(long id, long serviceId)
|
|
{
|
|
return await _dbSet
|
|
.FirstOrDefaultAsync(f => f.Id == id && f.ServiceId == serviceId);
|
|
}
|
|
|
|
public async Task<(IReadOnlyList<FileEntity> Items, int TotalCount)> GetPagedByServiceAsync(
|
|
long serviceId, int page, int size, string? fileType = null)
|
|
{
|
|
var query = _dbSet.Where(f => f.ServiceId == serviceId && !f.IsDeleted);
|
|
|
|
if (!string.IsNullOrWhiteSpace(fileType))
|
|
query = query.Where(f => f.FileType == fileType.ToLowerInvariant());
|
|
|
|
var totalCount = await query.CountAsync();
|
|
|
|
var items = await query
|
|
.OrderByDescending(f => f.CreatedAt)
|
|
.Skip((page - 1) * size)
|
|
.Take(size)
|
|
.ToListAsync();
|
|
|
|
return (items, totalCount);
|
|
}
|
|
}
|