You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
OptifitWebService/Server/Services/ExerciceService.cs

66 lines
2.2 KiB

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<PaginatedResult<ResponseExerciceDto>> GetExercices(int page, int size, bool ascending = true)
{
var exercices = await _exerciceRepository.GetPaginatedListAsync(page - 1, size, null, null);
var result = _mapper.Map<PaginatedResult<ResponseExerciceDto>>(exercices);
return result;
}
public async Task<ResponseExerciceDto?> GetExerciceById(string id)
{
var exercice = await _exerciceRepository.GetByIdAsync(id);
return exercice == null ? null : _mapper.Map<ResponseExerciceDto>(exercice);
}
public async Task<ResponseExerciceDto> CreateExercice(RequestExerciceDto request)
{
var exercice = _mapper.Map<Exercice>(request);
await _exerciceRepository.InsertAsync(exercice);
await _context.SaveChangesAsync();
return _mapper.Map<ResponseExerciceDto>(exercice);
}
public async Task<ResponseExerciceDto?> 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<ResponseExerciceDto>(exercice);
}
public async Task<bool> DeleteExercice(string id)
{
var exercice = await _exerciceRepository.GetByIdAsync(id);
if (exercice == null) return false;
_exerciceRepository.Delete(id);
await _context.SaveChangesAsync();
return true;
}
}