parent
1f0537d83e
commit
b19e762d78
@ -0,0 +1,32 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using TrainingSvc.DTOs;
|
||||
using TrainingSvc.IServices;
|
||||
|
||||
namespace TrainingSvc.Controllers;
|
||||
|
||||
[ApiController]
|
||||
[Route("api/training/[controller]")]
|
||||
public class TrainingProgramsController : ControllerBase
|
||||
{
|
||||
private readonly ITrainingProgramService _service;
|
||||
|
||||
public TrainingProgramsController(ITrainingProgramService service)
|
||||
{
|
||||
_service = service;
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public async Task<ActionResult<IEnumerable<TrainingProgramDto>>> GetAll()
|
||||
{
|
||||
var list = await _service.GetAllAsync();
|
||||
return Ok(list);
|
||||
}
|
||||
|
||||
[HttpGet("{id}")]
|
||||
public async Task<ActionResult<TrainingProgramDto>> GetById(string id)
|
||||
{
|
||||
var dto = await _service.GetByIdAsync(id);
|
||||
if (dto == null) return NotFound();
|
||||
return Ok(dto);
|
||||
}
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
using Shared.Enum;
|
||||
|
||||
namespace TrainingSvc.DTOs;
|
||||
|
||||
public class TrainingProgramDto
|
||||
{
|
||||
public string Id { get; set; }
|
||||
public string Lang { get; set; }
|
||||
public string Name { get; set; }
|
||||
public string? Description { get; set; }
|
||||
public int WeekDuration { get; set; }
|
||||
public int NbDays { get; set; }
|
||||
public string OwnerId { get; set; }
|
||||
public EGoal Goal { get; set; }
|
||||
public EDifficulty Difficulty { get; set; }
|
||||
public List<SessionDto> Sessions { get; set; } = new();
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
using TrainingSvc.DTOs;
|
||||
|
||||
namespace TrainingSvc.IServices;
|
||||
|
||||
public interface ISessionService
|
||||
{
|
||||
Task<IEnumerable<SessionDto>> GetAllAsync();
|
||||
Task<SessionDto?> GetByIdAsync(string id);
|
||||
Task<SessionDto> CreateAsync(CreateSessionDto dto);
|
||||
Task<bool> UpdateAsync(string id, UpdateSessionDto dto);
|
||||
Task<bool> DeleteAsync(string id);
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
using TrainingSvc.DTOs;
|
||||
|
||||
namespace TrainingSvc.IServices;
|
||||
|
||||
public interface ITrainingProgramService
|
||||
{
|
||||
Task<IEnumerable<TrainingProgramDto>> GetAllAsync();
|
||||
Task<TrainingProgramDto?> GetByIdAsync(string id);
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
using AutoMapper;
|
||||
using TrainingSvc.DTOs;
|
||||
using TrainingSvc.Entities;
|
||||
|
||||
namespace TrainingSvc.RequestHelpers;
|
||||
|
||||
public class TrainingProgramProfile : Profile
|
||||
{
|
||||
public TrainingProgramProfile()
|
||||
{
|
||||
CreateMap<TrainingProgram, TrainingProgramDto>();
|
||||
}
|
||||
}
|
@ -0,0 +1,95 @@
|
||||
using AutoMapper;
|
||||
using TrainingSvc.DTOs;
|
||||
using TrainingSvc.Entities;
|
||||
using TrainingSvc.Repositories;
|
||||
using TrainingSvc.IServices;
|
||||
|
||||
namespace TrainingSvc.Services;
|
||||
|
||||
public class SessionService : ISessionService
|
||||
{
|
||||
private readonly ISessionRepository _sessionRepo;
|
||||
private readonly IExerciceInstanceRepository _exerciceRepo;
|
||||
private readonly IExerciceTemplateRepository _exerciceTemplateRepo;
|
||||
private readonly IMapper _mapper;
|
||||
|
||||
public SessionService(
|
||||
ISessionRepository sessionRepo,
|
||||
IExerciceInstanceRepository exerciceRepo,
|
||||
IExerciceTemplateRepository exerciceTemplateRepo,
|
||||
IMapper mapper)
|
||||
{
|
||||
_sessionRepo = sessionRepo;
|
||||
_exerciceRepo = exerciceRepo;
|
||||
_exerciceTemplateRepo = exerciceTemplateRepo;
|
||||
_mapper = mapper;
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<SessionDto>> GetAllAsync()
|
||||
{
|
||||
var sessions = await ((SessionRepository)_sessionRepo).GetAllWithExercicesAndTemplatesAsync();
|
||||
return _mapper.Map<IEnumerable<SessionDto>>(sessions);
|
||||
}
|
||||
|
||||
public async Task<SessionDto?> GetByIdAsync(string id)
|
||||
{
|
||||
var entity = await ((SessionRepository)_sessionRepo).GetByIdWithExercicesAndTemplatesAsync(id);
|
||||
return entity == null ? null : _mapper.Map<SessionDto>(entity);
|
||||
}
|
||||
|
||||
public async Task<SessionDto> CreateAsync(CreateSessionDto dto)
|
||||
{
|
||||
// Ne mappe pas les exercices ici
|
||||
var session = _mapper.Map<Session>(dto);
|
||||
session.Exercices.Clear(); // S'assure qu'il n'y a pas d'exercices liés
|
||||
|
||||
await _sessionRepo.InsertAsync(session);
|
||||
await _sessionRepo.SaveChangesAsync();
|
||||
|
||||
foreach (var exoDto in dto.Exercices)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(exoDto.ExerciceTemplateId))
|
||||
{
|
||||
var exists = await _exerciceTemplateRepo.ExistsAsync(t => t.Id == exoDto.ExerciceTemplateId);
|
||||
if (!exists)
|
||||
throw new ArgumentException("ExerciceTemplateId invalide.");
|
||||
}
|
||||
var exo = _mapper.Map<ExerciceInstance>(exoDto);
|
||||
exo.SessionId = session.Id;
|
||||
await _exerciceRepo.InsertAsync(exo);
|
||||
}
|
||||
await _exerciceRepo.SaveChangesAsync();
|
||||
|
||||
var entity = await ((SessionRepository)_sessionRepo).GetByIdWithExercicesAndTemplatesAsync(session.Id);
|
||||
return _mapper.Map<SessionDto>(entity);
|
||||
}
|
||||
|
||||
public async Task<bool> UpdateAsync(string id, UpdateSessionDto dto)
|
||||
{
|
||||
var session = await ((SessionRepository)_sessionRepo).GetByIdWithExercicesAndTemplatesAsync(id);
|
||||
if (session == null) return false;
|
||||
|
||||
_mapper.Map(dto, session);
|
||||
_sessionRepo.Update(session);
|
||||
await _sessionRepo.SaveChangesAsync();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public async Task<bool> DeleteAsync(string id)
|
||||
{
|
||||
var session = await _sessionRepo.GetByIdAsync(id, s => s.Exercices);
|
||||
if (session == null) return false;
|
||||
|
||||
foreach (var ex in session.Exercices.ToList())
|
||||
{
|
||||
_exerciceRepo.Delete(ex.Id);
|
||||
}
|
||||
await _exerciceRepo.SaveChangesAsync();
|
||||
|
||||
_sessionRepo.Delete(id);
|
||||
await _sessionRepo.SaveChangesAsync();
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
using AutoMapper;
|
||||
using TrainingSvc.DTOs;
|
||||
using TrainingSvc.IServices;
|
||||
using TrainingSvc.Repositories;
|
||||
|
||||
namespace TrainingSvc.Services;
|
||||
|
||||
public class TrainingProgramService : ITrainingProgramService
|
||||
{
|
||||
private readonly ITrainingProgramRepository _repo;
|
||||
private readonly IMapper _mapper;
|
||||
|
||||
public TrainingProgramService(ITrainingProgramRepository repo, IMapper mapper)
|
||||
{
|
||||
_repo = repo;
|
||||
_mapper = mapper;
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<TrainingProgramDto>> GetAllAsync()
|
||||
{
|
||||
var list = await _repo.GetAllWithSessionsAsync();
|
||||
return _mapper.Map<IEnumerable<TrainingProgramDto>>(list);
|
||||
}
|
||||
|
||||
public async Task<TrainingProgramDto?> GetByIdAsync(string id)
|
||||
{
|
||||
var entity = await _repo.GetByIdWithSessionsAsync(id);
|
||||
return entity == null ? null : _mapper.Map<TrainingProgramDto>(entity);
|
||||
}
|
||||
}
|
Loading…
Reference in new issue