Skip to content

Commit bf3cefd

Browse files
committed
Refactor notifications to use UnitOfWork and Repository pattern
1 parent fda6755 commit bf3cefd

5 files changed

Lines changed: 46 additions & 93 deletions

File tree

AskFm/AskFm.BLL/Services/NotificationService.cs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@
99
public class NotificationService : INotificationService
1010
{
1111
private readonly INotificationRepository _notificationRepository;
12+
private readonly IUnitOfWork _unitOfWork;
1213
private readonly IHubContext<NotificationHub> _hubContext;
1314

1415

15-
public NotificationService(INotificationRepository notificationRepository, IHubContext<NotificationHub> hubContext)
16+
public NotificationService(INotificationRepository notificationRepository, IUnitOfWork unitOfWork, IHubContext<NotificationHub> hubContext)
1617
{
1718
_notificationRepository = notificationRepository;
19+
_unitOfWork = unitOfWork;
1820
_hubContext = hubContext;
1921
}
2022

@@ -102,13 +104,28 @@ public async Task<List<NotificationDto>> GetNotificationsByType(int userId, stri
102104
}
103105
public async Task<string> MarkNotificationAsRead(int notificationId)
104106
{
105-
await _notificationRepository.MarkNotificationAsRead(notificationId);
107+
var notification = await _unitOfWork.Notifications.GetByIdAsync(notificationId);
108+
if (notification == null)
109+
throw new InvalidOperationException($"Notification with ID {notificationId} not found.");
110+
111+
notification.isRead = true;
112+
_unitOfWork.Notifications.Update(notification);
113+
await _unitOfWork.SaveAsync();
114+
106115
return "notification has been read";
107116
}
108117

109118
public async Task<string> MarkAllNotificationsAsRead(int userId)
110119
{
111-
await _notificationRepository.MarkAllNotificationsAsRead(userId);
120+
var unreadNotifications = await _unitOfWork.Notifications.FindAllAsync(n => n.UserId == userId && !n.isRead);
121+
122+
foreach (var notification in unreadNotifications)
123+
{
124+
notification.isRead = true;
125+
_unitOfWork.Notifications.Update(notification);
126+
}
127+
128+
await _unitOfWork.SaveAsync();
112129
return "All notifications marked as read";
113130
}
114131

@@ -125,7 +142,8 @@ public async Task CreateNotification(int userId, NotificationStatus type, int re
125142
UpdatedAt = DateTime.UtcNow
126143
};
127144

128-
await _notificationRepository.AddNotification(notification);
145+
await _unitOfWork.Notifications.AddAsync(notification);
146+
await _unitOfWork.SaveAsync();
129147

130148
var notificationDto = new NotificationDto
131149
{

AskFm/AskFm.DAL/Interfaces/INotificationRepository.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,6 @@ namespace AskFm.DAL.Interfaces;
66
public interface INotificationRepository
77
{
88
Task<(IEnumerable<Notification> notifications, int totalCount)> GetAllNotifications(int userId, int pageNumber, int pageSize);
9-
Task<Notification> GetNotificationById(int notificationId);
10-
Task UpdateNotification(Notification notification);
11-
Task AddNotification(Notification notification);
129
Task<(IEnumerable<Notification> notifications, int totalCount)> GetNotificationsByType(int userId, NotificationStatus status, int pageNumber, int pageSize);
13-
Task MarkNotificationAsRead(int notificationId);
14-
Task MarkAllNotificationsAsRead(int userId);
1510
Task<ApplicationUser?> GetActorUserByResourceId(int resourceId, NotificationStatus type);
1611
}

AskFm/AskFm.DAL/Interfaces/IUnitOfWork.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
using AskFm.DAL.Models;
2-
using Thread = System.Threading.Thread;
32

43
namespace AskFm.DAL.Interfaces;
54

65
public interface IUnitOfWork : IDisposable
76
{
87
IRepository<ApplicationUser> Users { get; }
9-
IRepository<Thread> Threads { get; }
8+
IRepository<Models.Thread> Threads { get; }
109
IRepository<SavedThreads> SavedThreads { get; }
1110
IRepository<ThreadLike> ThreadLikes { get; }
1211
IRepository<Comment> Comments { get; }

AskFm/AskFm.DAL/Repositories/NotificationRepository.cs

Lines changed: 19 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@ namespace AskFm.DAL.Repositories;
77

88
public 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
}

AskFm/AskFm.DAL/UnitOfWork.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using AskFm.DAL.Interfaces;
22
using AskFm.DAL.Models;
33
using AskFm.DAL.Repositories;
4-
using Thread = System.Threading.Thread;
54

65
namespace AskFm.DAL;
76

@@ -10,7 +9,7 @@ public class UnitOfWork : IUnitOfWork
109
private readonly AppDbContext _context;
1110

1211
private IRepository<ApplicationUser> _users;
13-
private IRepository<Thread> _threads;
12+
private IRepository<Models.Thread> _threads;
1413
private IRepository<SavedThreads> _savedThreads;
1514
private IRepository<ThreadLike> _threadLikes;
1615
private IRepository<Comment> _comments;
@@ -36,12 +35,12 @@ public IRepository<ApplicationUser> Users
3635
}
3736
}
3837

39-
public IRepository<Thread> Threads {
38+
public IRepository<Models.Thread> Threads {
4039
get
4140
{
4241
if (_threads == null)
4342
{
44-
_threads = new Repository<Thread>(_context);
43+
_threads = new Repository<Models.Thread>(_context);
4544
}
4645
return _threads;
4746
}
@@ -119,6 +118,7 @@ public IRepository<Notification> Notifications
119118
}
120119
}
121120

121+
IRepository<Models.Thread> IUnitOfWork.Threads => throw new NotImplementedException();
122122

123123
public void Dispose()
124124
{

0 commit comments

Comments
 (0)