|
| 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 AttachmentRepository : IAttachmentRepository |
| 9 | + { |
| 10 | + private readonly ApplicationDbContext _context; |
| 11 | + |
| 12 | + public AttachmentRepository(ApplicationDbContext context) |
| 13 | + { |
| 14 | + _context = context; |
| 15 | + } |
| 16 | + |
| 17 | + public AttachmentEntity GetAttachmentById(int id) |
| 18 | + { |
| 19 | + return _context.Attachments.Find(id); |
| 20 | + } |
| 21 | + |
| 22 | + public IEnumerable<AttachmentEntity> GetAllAttachments() |
| 23 | + { |
| 24 | + return _context.Attachments.ToList(); |
| 25 | + } |
| 26 | + |
| 27 | + public AttachmentEntity CreateAttachment(AttachmentEntity attachment) |
| 28 | + { |
| 29 | + _context.Attachments.Add(attachment); |
| 30 | + _context.SaveChanges(); |
| 31 | + return attachment; |
| 32 | + } |
| 33 | + |
| 34 | + public AttachmentEntity UpdateAttachment(int id, AttachmentEntity attachment) |
| 35 | + { |
| 36 | + if (_context.Attachments.Find(id) is AttachmentEntity oldAttachment) |
| 37 | + { |
| 38 | + attachment.Id = id; |
| 39 | + _context.Attachments.Entry(oldAttachment).State = EntityState.Detached; |
| 40 | + _context.Attachments.Entry(attachment).State = EntityState.Modified; |
| 41 | + _context.SaveChanges(); |
| 42 | + } |
| 43 | + return attachment; |
| 44 | + } |
| 45 | + |
| 46 | + public int DeleteAttachment(int id) |
| 47 | + { |
| 48 | + var attachmentToDelete = _context.Attachments.Find(id); |
| 49 | + if (attachmentToDelete != null) |
| 50 | + { |
| 51 | + _context.Attachments.Remove(attachmentToDelete); |
| 52 | + _context.SaveChanges(); |
| 53 | + return id; |
| 54 | + } |
| 55 | + return -1; |
| 56 | + } |
| 57 | + } |
| 58 | +} |
0 commit comments