Introduice exercices entity and correspondant routes

pull/1/head
Leo TUAILLON 5 months ago
parent 7130a38f75
commit f574a6b4cb

@ -0,0 +1,22 @@
using System.ComponentModel.DataAnnotations;
using Infrastructure.Base;
namespace Infrastructure.Entities;
public class Exercice : EntityBase
{
[Required]
public string Name { get; set; }
public string Description { get; set; }
public float Duration { get; set; }
public string Image { get; set; }
public string Video { get; set; }
public int NbSeries { get; set; }
public int NbRepetitions { get; set; }
}

@ -0,0 +1,19 @@
using System.Collections.ObjectModel;
using System.ComponentModel.DataAnnotations;
using Infrastructure.Base;
namespace Infrastructure.Entities;
public class Program : EntityBase
{
[Required]
public string Name { get; set; }
public int WeekDuration { get; set; }
public string Description { get; set; }
public string Difficulty { get; set; }
public virtual ICollection<Session> Sessions { get; set; } = new Collection<Session>();
}

@ -0,0 +1,17 @@
using System.Collections.ObjectModel;
using System.ComponentModel.DataAnnotations;
using Infrastructure.Base;
namespace Infrastructure.Entities;
public class Session : EntityBase
{
[Required]
public string Name { get; set; }
public string Description { get; set; }
public float Duration { get; set; }
public virtual ICollection<Exercice> Exercices { get; set; } = new Collection<Exercice>();
}

@ -0,0 +1,10 @@
using Infrastructure.Entities;
namespace Infrastructure.Repositories;
public class ExerciceRepository : GenericRepository<Exercice>, IExerciceRepository
{
public ExerciceRepository(OptifitDbContext context) : base(context)
{
}
}

@ -2,8 +2,6 @@ using System.Linq.Expressions;
using Infrastructure.Base;
using Microsoft.EntityFrameworkCore;
using Shared;
using System.Linq.Expressions;
using System.Threading.Tasks;
using System.Linq.Dynamic.Core;
namespace Infrastructure.Repositories;

@ -0,0 +1,7 @@
using Infrastructure.Entities;
namespace Infrastructure.Repositories;
public interface IExerciceRepository : IRepository<Exercice>
{
}

@ -0,0 +1,82 @@
using Asp.Versioning;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Server.Dto.Request;
using Server.Dto.Response;
using Server.IServices;
namespace Server.Controller.v1;
[ApiController]
[ApiVersion("1.0")]
[Route("api/v{version:apiVersion}/[controller]")]
public class ExercicesController : ControllerBase
{
private readonly ILogger<ExercicesController> _logger;
private readonly IExerciceService _dataServices;
public ExercicesController(ILogger<ExercicesController> logger, IExerciceService dataServices)
{
_logger = logger;
_dataServices = dataServices;
}
[HttpGet]
[ProducesResponseType(typeof(IEnumerable<ResponseExerciceDto>), StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[RequireHttps]
[AllowAnonymous]
public async Task<IActionResult> GetExercices([FromQuery] int pageIndex = 1, [FromQuery] int pageSize = 5, [FromQuery] bool ascending = true)
{
var exercices = await _dataServices.GetExercices(pageIndex, pageSize, ascending);
return exercices.TotalCount == 0 ? NoContent() : Ok(exercices);
}
[HttpGet("{id}")]
[ProducesResponseType(typeof(ResponseExerciceDto), StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[RequireHttps]
[AllowAnonymous]
public async Task<IActionResult> GetExerciceById(string id)
{
var exercice = await _dataServices.GetExerciceById(id);
return exercice == null ? NotFound() : Ok(exercice);
}
[HttpPost]
[ProducesResponseType(typeof(ResponseExerciceDto), StatusCodes.Status201Created)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[AllowAnonymous]
public async Task<IActionResult> CreateExercice([FromBody] RequestExerciceDto request)
{
if (!ModelState.IsValid) return BadRequest(ModelState);
var createdExercice = await _dataServices.CreateExercice(request);
return CreatedAtAction(nameof(GetExerciceById), new { id = createdExercice.Id }, createdExercice);
}
[HttpPut("{id}")]
[ProducesResponseType(typeof(ResponseExerciceDto), StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[RequireHttps]
[AllowAnonymous]
public async Task<IActionResult> UpdateExercice(string id, [FromBody] RequestExerciceDto request)
{
if (!ModelState.IsValid) return BadRequest(ModelState);
var updatedExercice = await _dataServices.UpdateExercice(id, request);
return updatedExercice == null ? NotFound() : Ok(updatedExercice);
}
[HttpDelete("{id}")]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[RequireHttps]
[AllowAnonymous]
public async Task<IActionResult> DeleteExercice(string id)
{
var deleted = await _dataServices.DeleteExercice(id);
return deleted ? NoContent() : NotFound();
}
}

@ -0,0 +1,22 @@
using System.ComponentModel.DataAnnotations;
namespace Server.Dto.Request
{
public class RequestExerciceDto
{
[Required]
public string Name { get; set; }
public string Description { get; set; }
public float Duration { get; set; }
public string Image { get; set; }
public string Video { get; set; }
public int NbSeries { get; set; }
public int NbRepetitions { get; set; }
}
}

@ -0,0 +1,20 @@
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
namespace Server.Dto.Request
{
public class RequestProgramDto
{
[Required]
public string Name { get; set; }
[Required]
public int WeekDuration { get; set; }
public string Description { get; set; }
public string Difficulty { get; set; }
public List<RequestSessionDto> Sessions { get; set; } = new List<RequestSessionDto>();
}
}

@ -0,0 +1,17 @@
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
namespace Server.Dto.Request
{
public class RequestSessionDto
{
[Required]
public string Name { get; set; }
public string Description { get; set; }
public float Duration { get; set; }
public List<RequestExerciceDto> Exercices { get; set; } = new List<RequestExerciceDto>();
}
}

@ -0,0 +1,21 @@
namespace Server.Dto.Response
{
public class ResponseExerciceDto
{
public string Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public float Duration { get; set; }
public string Image { get; set; }
public string Video { get; set; }
public int NbSeries { get; set; }
public int NbRepetitions { get; set; }
}
}

@ -0,0 +1,19 @@
using System.Collections.Generic;
namespace Server.Dto.Response
{
public class ResponseProgramDto
{
public string Id { get; set; }
public string Name { get; set; }
public int WeekDuration { get; set; }
public string Description { get; set; }
public string Difficulty { get; set; }
public List<ResponseSessionDto> Sessions { get; set; } = new List<ResponseSessionDto>();
}
}

@ -0,0 +1,17 @@
using System.Collections.Generic;
namespace Server.Dto.Response
{
public class ResponseSessionDto
{
public string Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public float Duration { get; set; }
public List<ResponseExerciceDto> Exercices { get; set; } = new List<ResponseExerciceDto>();
}
}

@ -0,0 +1,14 @@
using Server.Dto.Request;
using Server.Dto.Response;
using Shared;
namespace Server.IServices;
public interface IExerciceService
{
Task<PaginatedResult<ResponseExerciceDto>> GetExercices(int page, int size, bool ascending = true);
Task<ResponseExerciceDto?> GetExerciceById(string id);
Task<ResponseExerciceDto> CreateExercice(RequestExerciceDto request);
Task<ResponseExerciceDto?> UpdateExercice(string id, RequestExerciceDto request);
Task<bool> DeleteExercice(string id);
}

@ -0,0 +1,15 @@
using AutoMapper;
using Infrastructure.Entities;
using Server.Dto.Request;
using Server.Dto.Response;
namespace Server.Mappers;
public class ExerciceProfile : Profile
{
public ExerciceProfile()
{
CreateMap<Exercice, ResponseExerciceDto>();
CreateMap<RequestExerciceDto, Exercice>();
}
}

@ -5,9 +5,9 @@ using AutoMapper;
namespace Server.Mappers;
public class UsersMappers : Profile
public class UserProfile : Profile
{
public UsersMappers()
public UserProfile()
{
_ = CreateMap<User, ResponseUserDto>()
.ForMember(dest => dest.Id, opt => opt.MapFrom(src => src.Id))

@ -12,13 +12,19 @@ var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddScoped<IUsersService, UsersService>();
builder.Services.AddScoped<UserRepository>(); // Register UserRepository
builder.Services.AddScoped<IExerciceService, ExerciceService>();
builder.Services.AddScoped<IUserRepository, UserRepository>();
builder.Services.AddScoped<IExerciceRepository, ExerciceRepository>();
builder.Services.AddControllers();
builder.Services.AddDbContext<OptifitDbContext>(options =>
options.UseSqlite("Data Source=FirstTest.db"));
// Register AutoMapper
builder.Services.AddAutoMapper(typeof(UsersMappers));
builder.Services.AddAutoMapper(typeof(UserProfile));
builder.Services.AddAutoMapper(typeof(ExerciceProfile));
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();

@ -0,0 +1,66 @@
using AutoMapper;
using Infrastructure;
using Infrastructure.Entities;
using Infrastructure.Repositories;
using Server.Dto.Request;
using Server.Dto.Response;
using Server.IServices;
using Shared;
namespace Server.Services;
public class ExerciceService : IExerciceService
{
private readonly OptifitDbContext _context;
private readonly IExerciceRepository _exerciceRepository;
private readonly IMapper _mapper;
public ExerciceService(OptifitDbContext context, IExerciceRepository exerciceRepository, IMapper mapper)
{
_context = context;
_exerciceRepository = exerciceRepository;
_mapper = mapper;
}
public async Task<PaginatedResult<ResponseExerciceDto>> GetExercices(int page, int size, bool ascending = true)
{
var exercices = await _exerciceRepository.GetPaginatedListAsync(page - 1, size, null, null);
var result = _mapper.Map<PaginatedResult<ResponseExerciceDto>>(exercices);
return result;
}
public async Task<ResponseExerciceDto?> GetExerciceById(string id)
{
var exercice = await _exerciceRepository.GetByIdAsync(id);
return exercice == null ? null : _mapper.Map<ResponseExerciceDto>(exercice);
}
public async Task<ResponseExerciceDto> CreateExercice(RequestExerciceDto request)
{
var exercice = _mapper.Map<Exercice>(request);
await _exerciceRepository.InsertAsync(exercice);
await _context.SaveChangesAsync();
return _mapper.Map<ResponseExerciceDto>(exercice);
}
public async Task<ResponseExerciceDto?> UpdateExercice(string id, RequestExerciceDto request)
{
var exercice = await _exerciceRepository.GetByIdAsync(id);
if (exercice == null) return null;
_mapper.Map(request, exercice);
_exerciceRepository.Update(exercice);
await _context.SaveChangesAsync();
return _mapper.Map<ResponseExerciceDto>(exercice);
}
public async Task<bool> DeleteExercice(string id)
{
var exercice = await _exerciceRepository.GetByIdAsync(id);
if (exercice == null) return false;
_exerciceRepository.Delete(id);
await _context.SaveChangesAsync();
return true;
}
}

@ -10,10 +10,10 @@ namespace Server.Services;
public class UsersService : IUsersService
{
private readonly OptifitDbContext _context;
private readonly UserRepository _userRepository;
private readonly IUserRepository _userRepository;
private readonly IMapper _mapper;
public UsersService(OptifitDbContext context, UserRepository userRepository, IMapper mapper)
public UsersService(OptifitDbContext context, IUserRepository userRepository, IMapper mapper)
{
_context = context;
_userRepository = userRepository;

Loading…
Cancel
Save