parent
25a830f516
commit
969a9c8cd6
@ -0,0 +1,110 @@
|
||||
using AutoMapper;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Shared.DTOs;
|
||||
using TrainingSvc.Data;
|
||||
using TrainingSvc.DTOs;
|
||||
using TrainingSvc.Entities;
|
||||
|
||||
namespace TrainingSvc.Controllers;
|
||||
|
||||
[ApiController]
|
||||
[Route("api/training/[controller]")]
|
||||
public class SessionsController : ControllerBase
|
||||
{
|
||||
private readonly TrainingDbContext _context;
|
||||
private readonly IMapper _mapper;
|
||||
|
||||
public SessionsController(TrainingDbContext context, IMapper mapper)
|
||||
{
|
||||
_context = context;
|
||||
_mapper = mapper;
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public async Task<ActionResult<IEnumerable<SessionDto>>> GetAll()
|
||||
{
|
||||
var list = await _context.Sessions
|
||||
.Include(s => s.Exercices)
|
||||
.ThenInclude(e => e.ExerciceTemplate)
|
||||
.ToListAsync();
|
||||
return Ok(_mapper.Map<IEnumerable<SessionDto>>(list));
|
||||
}
|
||||
|
||||
[HttpGet("{id}")]
|
||||
public async Task<ActionResult<SessionDto>> GetById(string id)
|
||||
{
|
||||
var entity = await _context.Sessions
|
||||
.Include(s => s.Exercices)
|
||||
.ThenInclude(e => e.ExerciceTemplate)
|
||||
.FirstOrDefaultAsync(s => s.Id == id);
|
||||
if (entity == null) return NotFound();
|
||||
return _mapper.Map<SessionDto>(entity);
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public async Task<ActionResult<SessionDto>> Create([FromBody] CreateSessionDto dto)
|
||||
{
|
||||
var session = _mapper.Map<Session>(dto);
|
||||
_context.Sessions.Add(session);
|
||||
await _context.SaveChangesAsync();
|
||||
|
||||
// Crée les ExerciceInstance associées
|
||||
foreach (var exoDto in dto.Exercices)
|
||||
{
|
||||
var exo = _mapper.Map<ExerciceInstance>(exoDto);
|
||||
exo.SessionId = session.Id;
|
||||
_context.ExerciceInstances.Add(exo);
|
||||
}
|
||||
await _context.SaveChangesAsync();
|
||||
|
||||
// Recharge la session avec les exercices et templates
|
||||
var entity = await _context.Sessions
|
||||
.Include(s => s.Exercices)
|
||||
.ThenInclude(e => e.ExerciceTemplate)
|
||||
.FirstOrDefaultAsync(s => s.Id == session.Id);
|
||||
|
||||
return CreatedAtAction(nameof(GetById), new { id = session.Id }, _mapper.Map<SessionDto>(entity));
|
||||
}
|
||||
|
||||
[HttpPut("{id}")]
|
||||
public async Task<IActionResult> Update(string id, [FromBody] UpdateSessionDto dto)
|
||||
{
|
||||
var session = await _context.Sessions
|
||||
.Include(s => s.Exercices)
|
||||
.FirstOrDefaultAsync(s => s.Id == id);
|
||||
if (session == null) return NotFound();
|
||||
|
||||
_mapper.Map(dto, session);
|
||||
|
||||
// Supprime les anciens exercices
|
||||
_context.ExerciceInstances.RemoveRange(session.Exercices);
|
||||
|
||||
// Ajoute les nouveaux exercices
|
||||
foreach (var exoDto in dto.Exercices)
|
||||
{
|
||||
var exo = _mapper.Map<ExerciceInstance>(exoDto);
|
||||
exo.SessionId = session.Id;
|
||||
_context.ExerciceInstances.Add(exo);
|
||||
}
|
||||
|
||||
await _context.SaveChangesAsync();
|
||||
return NoContent();
|
||||
}
|
||||
|
||||
[HttpDelete("{id}")]
|
||||
public async Task<IActionResult> Delete(string id)
|
||||
{
|
||||
var session = await _context.Sessions
|
||||
.Include(s => s.Exercices)
|
||||
.FirstOrDefaultAsync(s => s.Id == id);
|
||||
|
||||
if (session == null) return NotFound();
|
||||
|
||||
_context.ExerciceInstances.RemoveRange(session.Exercices);
|
||||
_context.Sessions.Remove(session);
|
||||
|
||||
await _context.SaveChangesAsync();
|
||||
return NoContent();
|
||||
}
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
using Shared.Enum;
|
||||
|
||||
namespace TrainingSvc.DTOs;
|
||||
|
||||
public class CreateSessionDto
|
||||
{
|
||||
public string Name { get; set; }
|
||||
public string? Description { get; set; }
|
||||
public int Day { get; set; }
|
||||
public ETarget? Target { get; set; }
|
||||
public string TrainingProgramId { get; set; }
|
||||
public List<CreateExerciceInstanceDto> Exercices { get; set; } = new();
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
using Shared.Enum;
|
||||
|
||||
namespace TrainingSvc.DTOs;
|
||||
|
||||
public class SessionDto
|
||||
{
|
||||
public string Id { get; set; }
|
||||
public string Name { get; set; }
|
||||
public string? Description { get; set; }
|
||||
public int Day { get; set; }
|
||||
public ETarget? Target { get; set; }
|
||||
public string TrainingProgramId { get; set; }
|
||||
public List<ExerciceDto> Exercices { get; set; } = new();
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
using Shared.Enum;
|
||||
|
||||
namespace TrainingSvc.DTOs;
|
||||
|
||||
public class UpdateSessionDto
|
||||
{
|
||||
public string Name { get; set; }
|
||||
public string? Description { get; set; }
|
||||
public int Day { get; set; }
|
||||
public ETarget? Target { get; set; }
|
||||
public string TrainingProgramId { get; set; }
|
||||
public List<CreateExerciceInstanceDto> Exercices { get; set; } = new();
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
using AutoMapper;
|
||||
using TrainingSvc.DTOs;
|
||||
using TrainingSvc.Entities;
|
||||
|
||||
namespace TrainingSvc.RequestHelpers;
|
||||
|
||||
public class SessionProfile : Profile
|
||||
{
|
||||
public SessionProfile()
|
||||
{
|
||||
CreateMap<Session, SessionDto>()
|
||||
.ForMember(dest => dest.Exercices, opt
|
||||
=> opt.MapFrom(src => src.Exercices));
|
||||
CreateMap<CreateSessionDto, Session>();
|
||||
CreateMap<UpdateSessionDto, Session>();
|
||||
}
|
||||
}
|
Loading…
Reference in new issue