SPMS_API/SPMS.Infrastructure/Persistence/Repositories/NotificationRepository.cs
SEAN c29a48163d improvement: Notification 도메인 구축 (#247)
- Domain: NotificationCategory enum, Notification entity, INotificationRepository
- Infrastructure: NotificationConfiguration, NotificationRepository, AppDbContext/DI 등록
- Migration: AddNotificationTable 생성 및 적용
- Application: DTO 7개, INotificationService, NotificationService, DI 등록
- API: NotificationController (summary, list, read, read-all)

Closes #247
2026-02-26 09:44:28 +09:00

61 lines
2.1 KiB
C#

using Microsoft.EntityFrameworkCore;
using SPMS.Domain.Entities;
using SPMS.Domain.Enums;
using SPMS.Domain.Interfaces;
namespace SPMS.Infrastructure.Persistence.Repositories;
public class NotificationRepository : Repository<Notification>, INotificationRepository
{
public NotificationRepository(AppDbContext context) : base(context) { }
public async Task<(IReadOnlyList<Notification> Items, int TotalCount)> GetPagedAsync(
long adminId, NotificationCategory? category, DateTime? from, DateTime? to,
bool? isRead, int page, int size)
{
IQueryable<Notification> query = _dbSet.Where(n => n.TargetAdminId == adminId);
if (category.HasValue)
query = query.Where(n => n.Category == category.Value);
if (from.HasValue)
query = query.Where(n => n.CreatedAt >= from.Value);
if (to.HasValue)
query = query.Where(n => n.CreatedAt <= to.Value);
if (isRead.HasValue)
query = query.Where(n => n.IsRead == isRead.Value);
var totalCount = await query.CountAsync();
var items = await query
.OrderByDescending(n => n.CreatedAt)
.Skip((page - 1) * size)
.Take(size)
.ToListAsync();
return (items, totalCount);
}
public async Task<int> GetUnreadCountAsync(long adminId)
=> await _dbSet.CountAsync(n => n.TargetAdminId == adminId && !n.IsRead);
public async Task<IReadOnlyList<Notification>> GetRecentAsync(long adminId, int limit)
=> await _dbSet
.Where(n => n.TargetAdminId == adminId)
.OrderByDescending(n => n.CreatedAt)
.Take(limit)
.ToListAsync();
public async Task<Notification?> GetByIdAndAdminAsync(long id, long adminId)
=> await _dbSet.FirstOrDefaultAsync(n => n.Id == id && n.TargetAdminId == adminId);
public async Task<int> MarkAllAsReadAsync(long adminId)
=> await _dbSet
.Where(n => n.TargetAdminId == adminId && !n.IsRead)
.ExecuteUpdateAsync(s => s
.SetProperty(n => n.IsRead, true)
.SetProperty(n => n.ReadAt, DateTime.UtcNow));
}