@@ -7,16 +7,16 @@ namespace AskFm.DAL.Repositories;
77
88public class NotificationRepository : INotificationRepository
99{
10- private readonly AppDbContext _context ;
11-
12- public NotificationRepository ( AppDbContext context )
10+ private readonly IUnitOfWork _unitOfWork ;
11+
12+ public NotificationRepository ( IUnitOfWork unitOfWork )
1313 {
14- _context = context ;
14+ _unitOfWork = unitOfWork ;
1515 }
1616
1717 public async Task < ( IEnumerable < Notification > notifications , int totalCount ) > GetAllNotifications ( int userId , int pageNumber , int pageSize )
1818 {
19- var query = _context . Notifications . Where ( n => n . UserId == userId ) ;
19+ var query = _unitOfWork . Notifications . FindAll ( n => n . UserId == userId ) ;
2020
2121 var totalCount = await query . CountAsync ( ) ;
2222
@@ -28,39 +28,11 @@ public NotificationRepository(AppDbContext context)
2828
2929 return ( notifications , totalCount ) ;
3030 }
31-
32- public async Task < Notification > GetNotificationById ( int notificationId )
33- {
34- var notification = await _context . Notifications . FirstOrDefaultAsync ( n => n . Id == notificationId ) ;
35-
36- if ( notification == null )
37- throw new InvalidOperationException ( $ "Notification with ID { notificationId } not found.") ;
38-
39- return notification ;
40- }
41-
42- public async Task UpdateNotification ( Notification notification )
43- {
44- if ( notification == null )
45- throw new ArgumentNullException ( nameof ( notification ) ) ;
46-
47- _context . Notifications . Update ( notification ) ;
48- await _context . SaveChangesAsync ( ) ;
49- }
50-
51- public async Task AddNotification ( Notification notification )
52- {
53- if ( notification == null )
54- throw new ArgumentNullException ( nameof ( notification ) ) ;
55-
56- await _context . Notifications . AddAsync ( notification ) ;
57- await _context . SaveChangesAsync ( ) ;
58- }
5931
6032 public async Task < ( IEnumerable < Notification > notifications , int totalCount ) > GetNotificationsByType ( int userId , NotificationStatus status , int pageNumber , int pageSize )
6133 {
62- var query = _context . Notifications . Where ( n => n . UserId == userId && n . Type == status ) ;
63-
34+ var query = _unitOfWork . Notifications . FindAll ( n => n . UserId == userId && n . Type == status ) ;
35+
6436 var totalCount = await query . CountAsync ( ) ;
6537
6638 var notifications = await query
@@ -72,69 +44,38 @@ public async Task AddNotification(Notification notification)
7244 return ( notifications , totalCount ) ;
7345 }
7446
75- public async Task MarkNotificationAsRead ( int notificationId )
76- {
77- var notification = await GetNotificationById ( notificationId ) ;
78- notification . isRead = true ;
79- //notification.UpdatedAt = DateTime.UtcNow;
80- await UpdateNotification ( notification ) ;
81- }
82-
83- public async Task MarkAllNotificationsAsRead ( int userId )
84- {
85- await _context . Notifications
86- . Where ( n => n . UserId == userId && ! n . isRead )
87- . ExecuteUpdateAsync ( n => n
88- . SetProperty ( x => x . isRead , true ) ) ;
89- // .SetProperty(x => x.UpdatedAt, DateTime.UtcNow))
90- }
91-
9247 public async Task < ApplicationUser ? > GetActorUserByResourceId ( int resourceId , NotificationStatus type )
9348 {
94- // In follow case the follow model does not have a follow id so we use the followedId to get the actor user
9549 if ( type == NotificationStatus . FOLLOW )
9650 {
97- return await _context . Follows
98- . Where ( f => f . FollowedId == resourceId )
99- . Select ( f => f . Follower )
100- . FirstOrDefaultAsync ( ) ;
51+ var follow = await _unitOfWork . Follows . FindAsync ( f => f . FollowedId == resourceId , new [ ] { "Follower" } ) ;
52+ return follow ? . Follower ;
10153 }
10254 else if ( type == NotificationStatus . QUESTION )
10355 {
104- return await _context . Threads
105- . Where ( t => t . Id == resourceId )
106- . Select ( t => t . Asker )
107- . FirstOrDefaultAsync ( ) ;
56+ var thread = await _unitOfWork . Threads . FindAsync ( t => t . Id == resourceId , new [ ] { "Asker" } ) ;
57+ return thread ? . Asker ;
10858 }
10959 else if ( type == NotificationStatus . ANSWER )
11060 {
111- return await _context . Threads
112- . Where ( t => t . Id == resourceId )
113- . Select ( t => t . Asked )
114- . FirstOrDefaultAsync ( ) ;
61+ var thread = await _unitOfWork . Threads . FindAsync ( t => t . Id == resourceId , new [ ] { "Asked" } ) ;
62+ return thread ? . Asked ;
11563 }
11664 else if ( type == NotificationStatus . COMMENT_LIKE )
11765 {
118- return await _context . CommentLikes
119- . Where ( cl => cl . CommentId == resourceId )
120- . Select ( cl => cl . User )
121- . FirstOrDefaultAsync ( ) ;
66+ var commentLike = await _unitOfWork . CommentLikes . FindAsync ( cl => cl . CommentId == resourceId , new [ ] { "User" } ) ;
67+ return commentLike ? . User ;
12268 }
12369 else if ( type == NotificationStatus . QUESTION_LIKE )
12470 {
125- return await _context . ThreadLikes
126- . Where ( tl => tl . ThreadId == resourceId )
127- . Select ( tl => tl . User )
128- . FirstOrDefaultAsync ( ) ;
71+ var threadLike = await _unitOfWork . ThreadLikes . FindAsync ( tl => tl . ThreadId == resourceId , new [ ] { "User" } ) ;
72+ return threadLike ? . User ;
12973 }
13074 else if ( type == NotificationStatus . REPLAY )
13175 {
132- return await _context . Comments
133- . Where ( c => c . Id == resourceId )
134- . Select ( c => c . User )
135- . FirstOrDefaultAsync ( ) ;
76+ var comment = await _unitOfWork . Comments . FindAsync ( c => c . Id == resourceId , new [ ] { "User" } ) ;
77+ return comment ? . User ;
13678 }
13779 return null ;
138-
13980 }
14081}
0 commit comments