SPMS_API/SPMS.Infrastructure/Persistence/Repositories/NoticeRepository.cs

32 lines
929 B
C#

using Microsoft.EntityFrameworkCore;
using SPMS.Domain.Entities;
using SPMS.Domain.Interfaces;
namespace SPMS.Infrastructure.Persistence.Repositories;
public class NoticeRepository : Repository<Notice>, INoticeRepository
{
public NoticeRepository(AppDbContext context) : base(context) { }
public async Task<(IReadOnlyList<Notice> Items, int TotalCount)> GetActivePagedAsync(int page, int size)
{
var query = _dbSet.Where(n => n.IsActive);
var totalCount = await query.CountAsync();
var items = await query
.OrderByDescending(n => n.IsPinned)
.ThenByDescending(n => n.CreatedAt)
.Skip((page - 1) * size)
.Take(size)
.ToListAsync();
return (items, totalCount);
}
public async Task<Notice?> GetActiveByIdAsync(long id)
{
return await _dbSet.FirstOrDefaultAsync(n => n.Id == id && n.IsActive);
}
}