using AutoMapper; using Infrastructure; using Infrastructure.Entities; using Infrastructure.Repositories; using Server.Dto.Request; using Server.Dto.Response; using Server.IServices; using Shared; namespace Server.Services; public class ExerciceService : IExerciceService { private readonly OptifitDbContext _context; private readonly IExerciceRepository _exerciceRepository; private readonly IMapper _mapper; public ExerciceService(OptifitDbContext context, IExerciceRepository exerciceRepository, IMapper mapper) { _context = context; _exerciceRepository = exerciceRepository; _mapper = mapper; } public async Task> GetExercices(int page, int size, bool ascending = true) { var exercices = await _exerciceRepository.GetPaginatedListAsync(page - 1, size, null, null); var result = _mapper.Map>(exercices); return result; } public async Task GetExerciceById(string id) { var exercice = await _exerciceRepository.GetByIdAsync(id); return exercice == null ? null : _mapper.Map(exercice); } public async Task CreateExercice(RequestExerciceDto request) { var exercice = _mapper.Map(request); await _exerciceRepository.InsertAsync(exercice); await _context.SaveChangesAsync(); return _mapper.Map(exercice); } public async Task UpdateExercice(string id, RequestExerciceDto request) { var exercice = await _exerciceRepository.GetByIdAsync(id); if (exercice == null) return null; _mapper.Map(request, exercice); _exerciceRepository.Update(exercice); await _context.SaveChangesAsync(); return _mapper.Map(exercice); } public async Task DeleteExercice(string id) { var exercice = await _exerciceRepository.GetByIdAsync(id); if (exercice == null) return false; _exerciceRepository.Delete(id); await _context.SaveChangesAsync(); return true; } }