parent
10a26eb93c
commit
fc3a69ea3a
@ -1,6 +1,79 @@
|
|||||||
namespace Server.Controller.v1;
|
using Asp.Versioning;
|
||||||
|
using Microsoft.AspNetCore.Authorization;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Server.Dto.Request;
|
||||||
|
using Server.Dto.Response;
|
||||||
|
using Server.IServices;
|
||||||
|
|
||||||
public class SessionsController
|
namespace Server.Controller.v1
|
||||||
{
|
{
|
||||||
|
[ApiController]
|
||||||
|
[ApiVersion("1.0")]
|
||||||
|
[Route("api/v{version:apiVersion}/[controller]")]
|
||||||
|
public class SessionsController : ControllerBase
|
||||||
|
{
|
||||||
|
private readonly ILogger<SessionsController> _logger;
|
||||||
|
private readonly ISessionService _dataServices;
|
||||||
|
|
||||||
|
public SessionsController(ILogger<SessionsController> logger, ISessionService dataServices)
|
||||||
|
{
|
||||||
|
_logger = logger;
|
||||||
|
_dataServices = dataServices;
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpGet]
|
||||||
|
[ProducesResponseType(typeof(IEnumerable<ResponseSessionDto>), StatusCodes.Status200OK)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
||||||
|
[AllowAnonymous]
|
||||||
|
public async Task<IActionResult> GetSessions([FromQuery] int pageIndex = 1, [FromQuery] int pageSize = 5, [FromQuery] bool ascending = true)
|
||||||
|
{
|
||||||
|
var sessions = await _dataServices.GetSessions(pageIndex, pageSize, ascending);
|
||||||
|
return sessions.TotalCount == 0 ? NoContent() : Ok(sessions);
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpGet("{id}")]
|
||||||
|
[ProducesResponseType(typeof(ResponseSessionDto), StatusCodes.Status200OK)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||||
|
[AllowAnonymous]
|
||||||
|
public async Task<IActionResult> GetSessionById(string id)
|
||||||
|
{
|
||||||
|
var session = await _dataServices.GetSessionById(id);
|
||||||
|
return session == null ? NotFound() : Ok(session);
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpPost]
|
||||||
|
[ProducesResponseType(typeof(ResponseSessionDto), StatusCodes.Status201Created)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||||
|
[AllowAnonymous]
|
||||||
|
public async Task<IActionResult> CreateSession([FromBody] RequestSessionDto request)
|
||||||
|
{
|
||||||
|
if (!ModelState.IsValid) return BadRequest(ModelState);
|
||||||
|
|
||||||
|
var createdSession = await _dataServices.CreateSession(request);
|
||||||
|
return CreatedAtAction(nameof(GetSessionById), new { id = createdSession.Id }, createdSession);
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpPut("{id}")]
|
||||||
|
[ProducesResponseType(typeof(ResponseSessionDto), StatusCodes.Status200OK)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||||
|
[AllowAnonymous]
|
||||||
|
public async Task<IActionResult> UpdateSession(string id, [FromBody] RequestSessionDto request)
|
||||||
|
{
|
||||||
|
if (!ModelState.IsValid) return BadRequest(ModelState);
|
||||||
|
|
||||||
|
var updatedSession = await _dataServices.UpdateSession(id, request);
|
||||||
|
return updatedSession == null ? NotFound() : Ok(updatedSession);
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpDelete("{id}")]
|
||||||
|
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||||
|
[AllowAnonymous]
|
||||||
|
public async Task<IActionResult> DeleteSession(string id)
|
||||||
|
{
|
||||||
|
var deleted = await _dataServices.DeleteSession(id);
|
||||||
|
return deleted ? NoContent() : NotFound();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
@ -1,6 +1,79 @@
|
|||||||
namespace Server.Controller.v1;
|
using Asp.Versioning;
|
||||||
|
using Microsoft.AspNetCore.Authorization;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Server.Dto.Request;
|
||||||
|
using Server.Dto.Response;
|
||||||
|
using Server.IServices;
|
||||||
|
|
||||||
public class TrainingProgramController
|
namespace Server.Controller.v1
|
||||||
{
|
{
|
||||||
|
[ApiController]
|
||||||
|
[ApiVersion("1.0")]
|
||||||
|
[Route("api/v{version:apiVersion}/[controller]")]
|
||||||
|
public class TrainingProgramsController : ControllerBase
|
||||||
|
{
|
||||||
|
private readonly ILogger<TrainingProgramsController> _logger;
|
||||||
|
private readonly ITrainingProgramService _dataServices;
|
||||||
|
|
||||||
|
public TrainingProgramsController(ILogger<TrainingProgramsController> logger, ITrainingProgramService dataServices)
|
||||||
|
{
|
||||||
|
_logger = logger;
|
||||||
|
_dataServices = dataServices;
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpGet]
|
||||||
|
[ProducesResponseType(typeof(IEnumerable<ResponseTrainingProgramDto>), StatusCodes.Status200OK)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
||||||
|
[AllowAnonymous]
|
||||||
|
public async Task<IActionResult> GetTrainingPrograms([FromQuery] int pageIndex = 1, [FromQuery] int pageSize = 5, [FromQuery] bool ascending = true)
|
||||||
|
{
|
||||||
|
var programs = await _dataServices.GetTrainingPrograms(pageIndex, pageSize, ascending);
|
||||||
|
return programs.TotalCount == 0 ? NoContent() : Ok(programs);
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpGet("{id}")]
|
||||||
|
[ProducesResponseType(typeof(ResponseTrainingProgramDto), StatusCodes.Status200OK)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||||
|
[AllowAnonymous]
|
||||||
|
public async Task<IActionResult> GetTrainingProgramById(string id)
|
||||||
|
{
|
||||||
|
var program = await _dataServices.GetTrainingProgramById(id);
|
||||||
|
return program == null ? NotFound() : Ok(program);
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpPost]
|
||||||
|
[ProducesResponseType(typeof(ResponseTrainingProgramDto), StatusCodes.Status201Created)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||||
|
[AllowAnonymous]
|
||||||
|
public async Task<IActionResult> CreateTrainingProgram([FromBody] RequestTrainingProgramDto request)
|
||||||
|
{
|
||||||
|
if (!ModelState.IsValid) return BadRequest(ModelState);
|
||||||
|
|
||||||
|
var createdProgram = await _dataServices.CreateTrainingProgram(request);
|
||||||
|
return CreatedAtAction(nameof(GetTrainingProgramById), new { id = createdProgram.Id }, createdProgram);
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpPut("{id}")]
|
||||||
|
[ProducesResponseType(typeof(ResponseTrainingProgramDto), StatusCodes.Status200OK)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||||
|
[AllowAnonymous]
|
||||||
|
public async Task<IActionResult> UpdateTrainingProgram(string id, [FromBody] RequestTrainingProgramDto request)
|
||||||
|
{
|
||||||
|
if (!ModelState.IsValid) return BadRequest(ModelState);
|
||||||
|
|
||||||
|
var updatedProgram = await _dataServices.UpdateTrainingProgram(id, request);
|
||||||
|
return updatedProgram == null ? NotFound() : Ok(updatedProgram);
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpDelete("{id}")]
|
||||||
|
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||||
|
[AllowAnonymous]
|
||||||
|
public async Task<IActionResult> DeleteTrainingProgram(string id)
|
||||||
|
{
|
||||||
|
var deleted = await _dataServices.DeleteTrainingProgram(id);
|
||||||
|
return deleted ? NoContent() : NotFound();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
Binary file not shown.
@ -1,6 +1,15 @@
|
|||||||
namespace Server.IServices;
|
using Server.Dto.Request;
|
||||||
|
using Server.Dto.Response;
|
||||||
|
using Shared;
|
||||||
|
|
||||||
public class ISessionService
|
namespace Server.IServices
|
||||||
{
|
{
|
||||||
|
public interface ISessionService
|
||||||
|
{
|
||||||
|
Task<PaginatedResult<ResponseSessionDto>> GetSessions(int page, int size, bool ascending = true);
|
||||||
|
Task<ResponseSessionDto?> GetSessionById(string id);
|
||||||
|
Task<ResponseSessionDto> CreateSession(RequestSessionDto request);
|
||||||
|
Task<ResponseSessionDto?> UpdateSession(string id, RequestSessionDto request);
|
||||||
|
Task<bool> DeleteSession(string id);
|
||||||
|
}
|
||||||
}
|
}
|
@ -1,6 +1,15 @@
|
|||||||
namespace Server.IServices;
|
using Server.Dto.Request;
|
||||||
|
using Server.Dto.Response;
|
||||||
|
using Shared;
|
||||||
|
|
||||||
public class ITrainingProgramService
|
namespace Server.IServices
|
||||||
{
|
{
|
||||||
|
public interface ITrainingProgramService
|
||||||
|
{
|
||||||
|
Task<PaginatedResult<ResponseTrainingProgramDto>> GetTrainingPrograms(int page, int size, bool ascending = true);
|
||||||
|
Task<ResponseTrainingProgramDto?> GetTrainingProgramById(string id);
|
||||||
|
Task<ResponseTrainingProgramDto> CreateTrainingProgram(RequestTrainingProgramDto request);
|
||||||
|
Task<ResponseTrainingProgramDto?> UpdateTrainingProgram(string id, RequestTrainingProgramDto request);
|
||||||
|
Task<bool> DeleteTrainingProgram(string id);
|
||||||
|
}
|
||||||
}
|
}
|
@ -1,6 +1,16 @@
|
|||||||
namespace Server.Mappers;
|
using AutoMapper;
|
||||||
|
using Shared;
|
||||||
|
using Infrastructure.Entities;
|
||||||
|
using Server.Dto.Response;
|
||||||
|
|
||||||
public class PaginatedResultProfile
|
namespace Server.Mappers
|
||||||
{
|
{
|
||||||
|
public class PaginatedResultProfile : Profile
|
||||||
|
{
|
||||||
|
public PaginatedResultProfile()
|
||||||
|
{
|
||||||
|
CreateMap<PaginatedResult<Exercice>, PaginatedResult<ResponseExerciceDto>>()
|
||||||
|
.ForMember(dest => dest.Data, opt => opt.MapFrom(src => src.Data));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
@ -1,6 +1,19 @@
|
|||||||
namespace Server.Mappers;
|
using AutoMapper;
|
||||||
|
using Infrastructure.Entities;
|
||||||
|
using Server.Dto.Request;
|
||||||
|
using Server.Dto.Response;
|
||||||
|
|
||||||
public class SessionProfile
|
namespace Server.Mappers
|
||||||
{
|
{
|
||||||
|
public class SessionProfile : Profile
|
||||||
|
{
|
||||||
|
public SessionProfile()
|
||||||
|
{
|
||||||
|
CreateMap<Session, ResponseSessionDto>()
|
||||||
|
.ForMember(dest => dest.Exercices, opt => opt.MapFrom(src => src.Exercices));
|
||||||
|
|
||||||
|
CreateMap<RequestSessionDto, Session>()
|
||||||
|
.ForMember(dest => dest.Exercices, opt => opt.Ignore());
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
@ -1,6 +1,19 @@
|
|||||||
namespace Server.Mappers;
|
using AutoMapper;
|
||||||
|
using Infrastructure.Entities;
|
||||||
|
using Server.Dto.Request;
|
||||||
|
using Server.Dto.Response;
|
||||||
|
|
||||||
public class TrainingProgramProfile
|
namespace Server.Mappers
|
||||||
{
|
{
|
||||||
|
public class TrainingProgramProfile : Profile
|
||||||
|
{
|
||||||
|
public TrainingProgramProfile()
|
||||||
|
{
|
||||||
|
CreateMap<TrainingProgram, ResponseTrainingProgramDto>()
|
||||||
|
.ForMember(dest => dest.Sessions, opt => opt.MapFrom(src => src.Sessions));
|
||||||
|
|
||||||
|
CreateMap<RequestTrainingProgramDto, TrainingProgram>()
|
||||||
|
.ForMember(dest => dest, opt => opt.Ignore());
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
@ -1,6 +1,71 @@
|
|||||||
namespace Server.Services;
|
using AutoMapper;
|
||||||
|
using Infrastructure;
|
||||||
|
using Infrastructure.Entities;
|
||||||
|
using Infrastructure.Repositories;
|
||||||
|
using Server.Dto.Request;
|
||||||
|
using Server.Dto.Response;
|
||||||
|
using Server.IServices;
|
||||||
|
using Shared;
|
||||||
|
|
||||||
public class SessionService
|
namespace Server.Services
|
||||||
{
|
{
|
||||||
|
public class SessionService : ISessionService
|
||||||
|
{
|
||||||
|
private readonly OptifitDbContext _context;
|
||||||
|
private readonly ISessionRepository _sessionRepository;
|
||||||
|
private readonly IExerciceRepository _exerciceRepository;
|
||||||
|
private readonly IMapper _mapper;
|
||||||
|
|
||||||
|
public SessionService(OptifitDbContext context, ISessionRepository sessionRepository, IExerciceRepository exerciceRepository, IMapper mapper)
|
||||||
|
{
|
||||||
|
_context = context;
|
||||||
|
_sessionRepository = sessionRepository;
|
||||||
|
_exerciceRepository = exerciceRepository;
|
||||||
|
_mapper = mapper;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<PaginatedResult<ResponseSessionDto>> GetSessions(int page, int size, bool ascending = true)
|
||||||
|
{
|
||||||
|
var sessions = await _sessionRepository.GetPaginatedListAsync(page - 1, size, null, null);
|
||||||
|
var result = _mapper.Map<PaginatedResult<ResponseSessionDto>>(sessions);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<ResponseSessionDto?> GetSessionById(string id)
|
||||||
|
{
|
||||||
|
var session = await _sessionRepository.GetByIdAsync(id, s => s.Exercices);
|
||||||
|
return session == null ? null : _mapper.Map<ResponseSessionDto>(session);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<ResponseSessionDto> CreateSession(RequestSessionDto request)
|
||||||
|
{
|
||||||
|
var session = _mapper.Map<Session>(request);
|
||||||
|
session.Exercices = (ICollection<Exercice>)await _exerciceRepository.GetAllAsync(e => request.ExerciceIds.Contains(e.Id));
|
||||||
|
await _sessionRepository.InsertAsync(session);
|
||||||
|
await _context.SaveChangesAsync();
|
||||||
|
return _mapper.Map<ResponseSessionDto>(session);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<ResponseSessionDto?> UpdateSession(string id, RequestSessionDto request)
|
||||||
|
{
|
||||||
|
var session = await _sessionRepository.GetByIdAsync(id, s => s.Exercices);
|
||||||
|
if (session == null) return null;
|
||||||
|
|
||||||
|
_mapper.Map(request, session);
|
||||||
|
session.Exercices = (ICollection<Exercice>)await _exerciceRepository.GetAllAsync(e => request.ExerciceIds.Contains(e.Id));
|
||||||
|
_sessionRepository.Update(session);
|
||||||
|
await _context.SaveChangesAsync();
|
||||||
|
return _mapper.Map<ResponseSessionDto>(session);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<bool> DeleteSession(string id)
|
||||||
|
{
|
||||||
|
var session = await _sessionRepository.GetByIdAsync(id);
|
||||||
|
if (session == null) return false;
|
||||||
|
|
||||||
|
_sessionRepository.Delete(id);
|
||||||
|
await _context.SaveChangesAsync();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
@ -1,6 +1,67 @@
|
|||||||
namespace Server.Services;
|
using AutoMapper;
|
||||||
|
using Infrastructure;
|
||||||
|
using Infrastructure.Entities;
|
||||||
|
using Infrastructure.Repositories;
|
||||||
|
using Server.Dto.Request;
|
||||||
|
using Server.Dto.Response;
|
||||||
|
using Server.IServices;
|
||||||
|
using Shared;
|
||||||
|
|
||||||
public class TrainingProgramService
|
namespace Server.Services
|
||||||
{
|
{
|
||||||
|
public class TrainingProgramService : ITrainingProgramService
|
||||||
|
{
|
||||||
|
private readonly OptifitDbContext _context;
|
||||||
|
private readonly ITrainingProgramRepository _trainingProgramRepository;
|
||||||
|
private readonly IMapper _mapper;
|
||||||
|
|
||||||
|
public TrainingProgramService(OptifitDbContext context, ITrainingProgramRepository trainingProgramRepository, IMapper mapper)
|
||||||
|
{
|
||||||
|
_context = context;
|
||||||
|
_trainingProgramRepository = trainingProgramRepository;
|
||||||
|
_mapper = mapper;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<PaginatedResult<ResponseTrainingProgramDto>> GetTrainingPrograms(int page, int size, bool ascending = true)
|
||||||
|
{
|
||||||
|
var programs = await _trainingProgramRepository.GetPaginatedListAsync(page - 1, size, null, null);
|
||||||
|
var result = _mapper.Map<PaginatedResult<ResponseTrainingProgramDto>>(programs);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<ResponseTrainingProgramDto?> GetTrainingProgramById(string id)
|
||||||
|
{
|
||||||
|
var program = await _trainingProgramRepository.GetByIdAsync(id, p => p.Sessions);
|
||||||
|
return program == null ? null : _mapper.Map<ResponseTrainingProgramDto>(program);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<ResponseTrainingProgramDto> CreateTrainingProgram(RequestTrainingProgramDto request)
|
||||||
|
{
|
||||||
|
var program = _mapper.Map<TrainingProgram>(request);
|
||||||
|
await _trainingProgramRepository.InsertAsync(program);
|
||||||
|
await _context.SaveChangesAsync();
|
||||||
|
return _mapper.Map<ResponseTrainingProgramDto>(program);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<ResponseTrainingProgramDto?> UpdateTrainingProgram(string id, RequestTrainingProgramDto request)
|
||||||
|
{
|
||||||
|
var program = await _trainingProgramRepository.GetByIdAsync(id, p => p.Sessions);
|
||||||
|
if (program == null) return null;
|
||||||
|
|
||||||
|
_mapper.Map(request, program);
|
||||||
|
_trainingProgramRepository.Update(program);
|
||||||
|
await _context.SaveChangesAsync();
|
||||||
|
return _mapper.Map<ResponseTrainingProgramDto>(program);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<bool> DeleteTrainingProgram(string id)
|
||||||
|
{
|
||||||
|
var program = await _trainingProgramRepository.GetByIdAsync(id);
|
||||||
|
if (program == null) return false;
|
||||||
|
|
||||||
|
_trainingProgramRepository.Delete(id);
|
||||||
|
await _context.SaveChangesAsync();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in new issue