|
| 1 | +using Microsoft.EntityFrameworkCore; |
| 2 | +using taskmaster_api.Data.Contexts; |
| 3 | +using taskmaster_api.Data.Entities; |
| 4 | +using taskmaster_api.Data.Repositories.Interface; |
| 5 | + |
| 6 | +namespace taskmaster_api.Data.Repositories |
| 7 | +{ |
| 8 | + public class NotificationRepository : INotificationRepository |
| 9 | + { |
| 10 | + private readonly ApplicationDbContext _context; |
| 11 | + |
| 12 | + public NotificationRepository(ApplicationDbContext context) |
| 13 | + { |
| 14 | + _context = context; |
| 15 | + } |
| 16 | + |
| 17 | + public NotificationEntity GetNotificationById(int id) |
| 18 | + { |
| 19 | + return _context.Notifications.Find(id); |
| 20 | + } |
| 21 | + |
| 22 | + public IEnumerable<NotificationEntity> GetAllNotifications() |
| 23 | + { |
| 24 | + return _context.Notifications.ToList(); |
| 25 | + } |
| 26 | + |
| 27 | + public NotificationEntity CreateNotification(NotificationEntity notification) |
| 28 | + { |
| 29 | + _context.Notifications.Add(notification); |
| 30 | + _context.SaveChanges(); |
| 31 | + return notification; |
| 32 | + } |
| 33 | + |
| 34 | + public NotificationEntity UpdateNotification(int id, NotificationEntity notification) |
| 35 | + { |
| 36 | + if (_context.Notifications.Find(id) is NotificationEntity oldNotification) |
| 37 | + { |
| 38 | + notification.Id = id; |
| 39 | + _context.Notifications.Entry(oldNotification).State = EntityState.Detached; |
| 40 | + _context.Notifications.Entry(notification).State = EntityState.Modified; |
| 41 | + _context.SaveChanges(); |
| 42 | + } |
| 43 | + return notification; |
| 44 | + } |
| 45 | + |
| 46 | + public int DeleteNotification(int id) |
| 47 | + { |
| 48 | + var notificationToDelete = _context.Notifications.Find(id); |
| 49 | + if (notificationToDelete != null) |
| 50 | + { |
| 51 | + _context.Notifications.Remove(notificationToDelete); |
| 52 | + _context.SaveChanges(); |
| 53 | + return id; |
| 54 | + } |
| 55 | + return -1; |
| 56 | + } |
| 57 | + } |
| 58 | +} |
0 commit comments