Temporarly Save Training Svc progression

features/training-svc
Leo TUAILLON 5 days ago
parent 25a830f516
commit 969a9c8cd6

@ -9,12 +9,12 @@ namespace TrainingSvc.Controllers;
[ApiController]
[Route("api/training/[controller]")]
public class ExerciceController : ControllerBase
public class ExercicesController : ControllerBase
{
private readonly TrainingDbContext _context;
private readonly IMapper _mapper;
public ExerciceController(TrainingDbContext context, IMapper mapper)
public ExercicesController(TrainingDbContext context, IMapper mapper)
{
_context = context;
_mapper = mapper;
@ -50,13 +50,26 @@ public class ExerciceController : ControllerBase
}
[HttpPut("{id}")]
public async Task<IActionResult> Update(string id, [FromBody] UpdateExerciceInstanceDto dto)
public async Task<IActionResult> Update(string id, [FromBody] UpdateSessionDto dto)
{
var entity = await _context.ExerciceInstances
.Include(e => e.ExerciceTemplate)
.FirstOrDefaultAsync(e => e.Id == id);
if (entity == null) return NotFound();
_mapper.Map(dto, entity);
var session = await _context.Sessions
.Include(s => s.Exercices)
.FirstOrDefaultAsync(s => s.Id == id);
if (session == null) return NotFound();
_mapper.Map(dto, session);
// Supprime tous 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();
}

@ -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();
}
}

@ -9,5 +9,5 @@ public class CreateExerciceInstanceDto
public int NbReps { get; set; }
public float RestingTime { get; set; }
public float? Weight { get; set; }
public string SessionId { get; set; }
public string? SessionId { get; set; }
}

@ -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();
}

@ -7,6 +7,7 @@ namespace TrainingSvc.Entities;
public class Session : EntityBase
{
[Required]
public required string Name { get; set; }
public string? Description { get; set; }

@ -4,7 +4,12 @@ using Microsoft.EntityFrameworkCore;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
builder.Services.AddControllers()
.AddJsonOptions(options =>
{
options.JsonSerializerOptions.Converters.Add(new System.Text.Json.Serialization.JsonStringEnumConverter(System.Text.Json.JsonNamingPolicy.CamelCase, allowIntegerValues: false));
});
builder.Services.AddDbContext<TrainingDbContext>(opt =>
{
opt.UseNpgsql(builder.Configuration.GetConnectionString("TrainingDb"));

@ -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…
Cancel
Save