using Microsoft.EntityFrameworkCore; using vendorApproverBackend.Data; using vendorApproverBackend.Entities; using vendorApproverBackend.Requests; using vendorApproverBackend.Exceptions; namespace vendorApproverBackend.Services { public class CertificationPackService { private readonly DataContext _context; public CertificationPackService(DataContext context) { _context = context; } public async Task AddPackAsync(AddCertificationPackRequest request) { var pack = new CertificationPack { Name = request.Name, Description = request.Description, Timestamp = DateTime.UtcNow }; if (request.Price.HasValue) { pack.Price = request.Price.Value; } _context.CertificationPacks.Add(pack); await _context.SaveChangesAsync(); return pack; } public async Task GetPackByIdAsync(long id) { var pack = await _context.CertificationPacks.FindAsync(id); if (pack == null) { throw new NotFoundException($"Certification Pack with ID {id} not found."); } return pack; } public async Task> GetAllPacksAsync() { return await _context.CertificationPacks .OrderByDescending(p => p.Timestamp) .ToListAsync(); } public async Task UpdatePackAsync(long id, UpdateCertificationPackRequest request) { var pack = await GetPackByIdAsync(id); if (request.Name != null) { pack.Name = request.Name; } if (request.Description != null) { pack.Description = request.Description; } if (request.Price.HasValue) { pack.Price = request.Price.Value; } _context.CertificationPacks.Update(pack); await _context.SaveChangesAsync(); return pack; } public async Task DeletePackAsync(long id) { var pack = await GetPackByIdAsync(id); _context.CertificationPacks.Remove(pack); await _context.SaveChangesAsync(); } } }