| | | 1 | | using LifeChart.Domain.Medications; |
| | | 2 | | using LifeChart.Infrastructure.Persistence; |
| | | 3 | | using Microsoft.EntityFrameworkCore; |
| | | 4 | | |
| | | 5 | | namespace LifeChart.Infrastructure.Repositories; |
| | | 6 | | |
| | | 7 | | public class MedicationRepository : IMedicationRepository |
| | | 8 | | { |
| | | 9 | | private readonly LifeChartDbContext _context; |
| | | 10 | | |
| | 0 | 11 | | public MedicationRepository(LifeChartDbContext context) |
| | 0 | 12 | | => _context = context; |
| | | 13 | | |
| | | 14 | | public async Task<IEnumerable<Medication>> GetAllActiveAsync() |
| | 0 | 15 | | => await _context.Medications |
| | 0 | 16 | | .Where(m => m.Active) |
| | 0 | 17 | | .OrderBy(m => m.Name) |
| | 0 | 18 | | .ToListAsync(); |
| | | 19 | | |
| | | 20 | | public async Task<Medication?> GetByIdAsync(int id) |
| | 0 | 21 | | => await _context.Medications.FirstOrDefaultAsync(m => m.Id == id); |
| | | 22 | | |
| | | 23 | | public async Task SaveAsync(Medication medication) |
| | | 24 | | { |
| | 0 | 25 | | if (medication.Id == 0) |
| | 0 | 26 | | _context.Medications.Add(medication); |
| | | 27 | | else |
| | 0 | 28 | | _context.Medications.Update(medication); |
| | | 29 | | |
| | 0 | 30 | | await _context.SaveChangesAsync(); |
| | 0 | 31 | | } |
| | | 32 | | |
| | | 33 | | public async Task DeleteAsync(int id) |
| | | 34 | | { |
| | 0 | 35 | | var medication = await GetByIdAsync(id); |
| | 0 | 36 | | if (medication is null) return; |
| | 0 | 37 | | medication.Deactivate(); |
| | 0 | 38 | | await _context.SaveChangesAsync(); |
| | 0 | 39 | | } |
| | | 40 | | } |