From 969a9c8cd67795350c19c9534643e3dc83c2c15e Mon Sep 17 00:00:00 2001 From: tleodev Date: Sat, 14 Jun 2025 21:37:19 +0200 Subject: [PATCH] Temporarly Save Training Svc progression --- ...ceController.cs => ExercicesController.cs} | 29 +++-- .../Controllers/SessionsController.cs | 110 ++++++++++++++++++ src/TrainingSvc/DTOs/CreateExerciceDto.cs | 2 +- src/TrainingSvc/DTOs/CreateSessionDto.cs | 13 +++ src/TrainingSvc/DTOs/SessionDto.cs | 14 +++ src/TrainingSvc/DTOs/UpdateSessionDto.cs | 13 +++ src/TrainingSvc/Entities/Session.cs | 1 + src/TrainingSvc/Program.cs | 7 +- .../RequestHelpers/SessionProfile.cs | 17 +++ 9 files changed, 196 insertions(+), 10 deletions(-) rename src/TrainingSvc/Controllers/{ExerciceController.cs => ExercicesController.cs} (73%) create mode 100644 src/TrainingSvc/Controllers/SessionsController.cs create mode 100644 src/TrainingSvc/DTOs/CreateSessionDto.cs create mode 100644 src/TrainingSvc/DTOs/SessionDto.cs create mode 100644 src/TrainingSvc/DTOs/UpdateSessionDto.cs create mode 100644 src/TrainingSvc/RequestHelpers/SessionProfile.cs diff --git a/src/TrainingSvc/Controllers/ExerciceController.cs b/src/TrainingSvc/Controllers/ExercicesController.cs similarity index 73% rename from src/TrainingSvc/Controllers/ExerciceController.cs rename to src/TrainingSvc/Controllers/ExercicesController.cs index 4abca70..7f8dada 100644 --- a/src/TrainingSvc/Controllers/ExerciceController.cs +++ b/src/TrainingSvc/Controllers/ExercicesController.cs @@ -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 Update(string id, [FromBody] UpdateExerciceInstanceDto dto) + public async Task 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(exoDto); + exo.SessionId = session.Id; + _context.ExerciceInstances.Add(exo); + } + await _context.SaveChangesAsync(); return NoContent(); } diff --git a/src/TrainingSvc/Controllers/SessionsController.cs b/src/TrainingSvc/Controllers/SessionsController.cs new file mode 100644 index 0000000..f91c238 --- /dev/null +++ b/src/TrainingSvc/Controllers/SessionsController.cs @@ -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>> GetAll() + { + var list = await _context.Sessions + .Include(s => s.Exercices) + .ThenInclude(e => e.ExerciceTemplate) + .ToListAsync(); + return Ok(_mapper.Map>(list)); + } + + [HttpGet("{id}")] + public async Task> 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(entity); + } + + [HttpPost] + public async Task> Create([FromBody] CreateSessionDto dto) + { + var session = _mapper.Map(dto); + _context.Sessions.Add(session); + await _context.SaveChangesAsync(); + + // Crée les ExerciceInstance associées + foreach (var exoDto in dto.Exercices) + { + var exo = _mapper.Map(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(entity)); + } + + [HttpPut("{id}")] + public async Task 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(exoDto); + exo.SessionId = session.Id; + _context.ExerciceInstances.Add(exo); + } + + await _context.SaveChangesAsync(); + return NoContent(); + } + + [HttpDelete("{id}")] + public async Task 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(); + } +} \ No newline at end of file diff --git a/src/TrainingSvc/DTOs/CreateExerciceDto.cs b/src/TrainingSvc/DTOs/CreateExerciceDto.cs index aacc2e6..d61c534 100644 --- a/src/TrainingSvc/DTOs/CreateExerciceDto.cs +++ b/src/TrainingSvc/DTOs/CreateExerciceDto.cs @@ -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; } } \ No newline at end of file diff --git a/src/TrainingSvc/DTOs/CreateSessionDto.cs b/src/TrainingSvc/DTOs/CreateSessionDto.cs new file mode 100644 index 0000000..30c155d --- /dev/null +++ b/src/TrainingSvc/DTOs/CreateSessionDto.cs @@ -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 Exercices { get; set; } = new(); +} \ No newline at end of file diff --git a/src/TrainingSvc/DTOs/SessionDto.cs b/src/TrainingSvc/DTOs/SessionDto.cs new file mode 100644 index 0000000..10100b0 --- /dev/null +++ b/src/TrainingSvc/DTOs/SessionDto.cs @@ -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 Exercices { get; set; } = new(); +} \ No newline at end of file diff --git a/src/TrainingSvc/DTOs/UpdateSessionDto.cs b/src/TrainingSvc/DTOs/UpdateSessionDto.cs new file mode 100644 index 0000000..eecb871 --- /dev/null +++ b/src/TrainingSvc/DTOs/UpdateSessionDto.cs @@ -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 Exercices { get; set; } = new(); +} \ No newline at end of file diff --git a/src/TrainingSvc/Entities/Session.cs b/src/TrainingSvc/Entities/Session.cs index 5c6806a..00b2513 100644 --- a/src/TrainingSvc/Entities/Session.cs +++ b/src/TrainingSvc/Entities/Session.cs @@ -7,6 +7,7 @@ namespace TrainingSvc.Entities; public class Session : EntityBase { + [Required] public required string Name { get; set; } public string? Description { get; set; } diff --git a/src/TrainingSvc/Program.cs b/src/TrainingSvc/Program.cs index 1a102aa..cbabc15 100644 --- a/src/TrainingSvc/Program.cs +++ b/src/TrainingSvc/Program.cs @@ -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(opt => { opt.UseNpgsql(builder.Configuration.GetConnectionString("TrainingDb")); diff --git a/src/TrainingSvc/RequestHelpers/SessionProfile.cs b/src/TrainingSvc/RequestHelpers/SessionProfile.cs new file mode 100644 index 0000000..752e8dc --- /dev/null +++ b/src/TrainingSvc/RequestHelpers/SessionProfile.cs @@ -0,0 +1,17 @@ +using AutoMapper; +using TrainingSvc.DTOs; +using TrainingSvc.Entities; + +namespace TrainingSvc.RequestHelpers; + +public class SessionProfile : Profile +{ + public SessionProfile() + { + CreateMap() + .ForMember(dest => dest.Exercices, opt + => opt.MapFrom(src => src.Exercices)); + CreateMap(); + CreateMap(); + } +} \ No newline at end of file