parent
7d1a10a281
commit
224f16110f
@ -0,0 +1,22 @@
|
|||||||
|
namespace ApiMappeur;
|
||||||
|
|
||||||
|
public static class EnumMappeur
|
||||||
|
{
|
||||||
|
public static Shared.AthleteOrderCriteria ToEnum(this string value)
|
||||||
|
{
|
||||||
|
return value switch
|
||||||
|
{
|
||||||
|
"None" => Shared.AthleteOrderCriteria.None,
|
||||||
|
"ByUsername" => Shared.AthleteOrderCriteria.ByUsername,
|
||||||
|
"ByFirstName" => Shared.AthleteOrderCriteria.ByFirstName,
|
||||||
|
"ByLastName" => Shared.AthleteOrderCriteria.ByLastName,
|
||||||
|
"BySexe" => Shared.AthleteOrderCriteria.BySexe,
|
||||||
|
"ByLenght" => Shared.AthleteOrderCriteria.ByLenght,
|
||||||
|
"ByWeight" => Shared.AthleteOrderCriteria.ByWeight,
|
||||||
|
"ByDateOfBirth" => Shared.AthleteOrderCriteria.ByDateOfBirth,
|
||||||
|
"ByEmail" => Shared.AthleteOrderCriteria.ByEmail,
|
||||||
|
"ByIsCoach" => Shared.AthleteOrderCriteria.ByIsCoach,
|
||||||
|
_ => Shared.AthleteOrderCriteria.None
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
@ -1,26 +0,0 @@
|
|||||||
using Dto;
|
|
||||||
using Microsoft.AspNetCore.Mvc;
|
|
||||||
using Shared;
|
|
||||||
|
|
||||||
[ApiController]
|
|
||||||
[Route("api/athletes")]
|
|
||||||
public class AthletesController : ControllerBase
|
|
||||||
{
|
|
||||||
private readonly ILogger<AthletesController> _logger;
|
|
||||||
IAthleteService _stubbedDto;
|
|
||||||
private const int DEFAULT_INDEX = 0, DEFAULT_COUNT = 5;
|
|
||||||
|
|
||||||
public AthletesController(ILogger<AthletesController> logger, IAthleteService athletesService)
|
|
||||||
{
|
|
||||||
_logger = logger;
|
|
||||||
_stubbedDto = athletesService;
|
|
||||||
}
|
|
||||||
|
|
||||||
[HttpGet("all")]
|
|
||||||
[ProducesResponseType(typeof(IEnumerable<AthleteDto>), 200)]
|
|
||||||
public async Task<IActionResult> GetAllAthletesAsync()
|
|
||||||
{
|
|
||||||
var athletes = await _stubbedDto.GetAllAthletesAsync();
|
|
||||||
return Ok(athletes);
|
|
||||||
}
|
|
||||||
}
|
|
@ -0,0 +1,159 @@
|
|||||||
|
using ApiMappeur;
|
||||||
|
using Dto;
|
||||||
|
using HeartTrackAPI.Request;
|
||||||
|
using HeartTrackAPI.Responce;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Model;
|
||||||
|
using Shared;
|
||||||
|
|
||||||
|
namespace HeartTrackAPI.Controllers;
|
||||||
|
|
||||||
|
[ApiController]
|
||||||
|
[Route("api/users")]
|
||||||
|
public class UsersController : Controller
|
||||||
|
{
|
||||||
|
// For the moment only support user who are athletes next handle user that are coach or athlete
|
||||||
|
private readonly ILogger<UsersController> _logger;
|
||||||
|
private readonly IUserService _userService;
|
||||||
|
public UsersController(ILogger<UsersController> logger, IUserService usersService)
|
||||||
|
{
|
||||||
|
_logger = logger;
|
||||||
|
_userService = usersService;
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpGet]
|
||||||
|
[ProducesResponseType(typeof(IEnumerable<UserDto>), 200)]
|
||||||
|
[ProducesResponseType(400)]
|
||||||
|
[ProducesResponseType(500)]
|
||||||
|
public async Task<ActionResult<PageResponse<UserDto>>> GetAllAthletes([FromQuery] PageRequest request)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var totalCount = await _userService.GetNbItems();
|
||||||
|
if (request.Count * request.Index >= totalCount)
|
||||||
|
{
|
||||||
|
_logger.LogError("To many object is asked the max is {totalCount} but the request is superior of ", totalCount);
|
||||||
|
return BadRequest("To many object is asked the max is : " + totalCount);
|
||||||
|
}
|
||||||
|
_logger.LogInformation("Executing {Action} with parameters: {Parameters}", nameof(GetAllAthletes), null);
|
||||||
|
// request.OrderingPropertyName
|
||||||
|
var athletes = await _userService.GetUsers(request.Index, request.Count, AthleteOrderCriteria.None, request.Descending ?? false);
|
||||||
|
var pageResponse = new PageResponse<UserDto>(request.Index, request.Count, totalCount, athletes.Select(a => a.ToDto()));
|
||||||
|
return Ok(pageResponse);
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
_logger.LogError(e, "Error while getting all athletes");
|
||||||
|
return StatusCode(500);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpGet("{id}")]
|
||||||
|
[ProducesResponseType(typeof(UserDto), 200)]
|
||||||
|
[ProducesResponseType(404)]
|
||||||
|
[ProducesResponseType(500)]
|
||||||
|
public async Task<ActionResult<UserDto>> GetAthleteById(int id)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
_logger.LogInformation("Executing {Action} with parameters: {Parameters}", nameof(GetAthleteById), id);
|
||||||
|
var athlete = await _userService.GetUserByIdAsync(id);
|
||||||
|
if (athlete == null)
|
||||||
|
{
|
||||||
|
_logger.LogError("Athlete with id {id} not found", id);
|
||||||
|
return NotFound($"Athlete with id {id} not found");
|
||||||
|
}
|
||||||
|
return Ok(athlete.ToDto());
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
_logger.LogError(e, "Error while getting athlete by id {id}", id);
|
||||||
|
return StatusCode(500);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
[HttpGet("count")]
|
||||||
|
[ProducesResponseType(typeof(int), 200)]
|
||||||
|
[ProducesResponseType(500)]
|
||||||
|
public async Task<ActionResult<int>> GetNbUsers()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
_logger.LogInformation("Executing {Action} with parameters: {Parameters}", nameof(GetNbUsers), null);
|
||||||
|
var nbUsers = await _userService.GetNbItems();
|
||||||
|
return Ok(nbUsers);
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
_logger.LogError(e, "Error while getting the number of users");
|
||||||
|
return StatusCode(500);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpPut("{id}")]
|
||||||
|
[ProducesResponseType(typeof(UserDto), 200)]
|
||||||
|
[ProducesResponseType(404)]
|
||||||
|
[ProducesResponseType(500)]
|
||||||
|
// need to adapt with coach
|
||||||
|
public async Task<ActionResult<UserDto>> UpdateUser(int id, [FromBody] UserDto user)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
_logger.LogInformation("Executing {Action} with parameters: {Parameters} for {Id}", nameof(UpdateUser), user,id);
|
||||||
|
var athlete = await _userService.GetUserByIdAsync(id);
|
||||||
|
if (athlete == null)
|
||||||
|
{
|
||||||
|
_logger.LogError("Athlete with id {id} not found", id);
|
||||||
|
return NotFound($"Athlete with id {id} not found");
|
||||||
|
}
|
||||||
|
var updatedAthlete = await _userService.UpdateUser(id, user.ToModel());
|
||||||
|
if(updatedAthlete == null)
|
||||||
|
{
|
||||||
|
_logger.LogError("Error while updating athlete with id {id}", id);
|
||||||
|
return StatusCode(500);
|
||||||
|
}
|
||||||
|
return Ok(updatedAthlete.ToDto());
|
||||||
|
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
_logger.LogError(e, "Error while getting the number of users");
|
||||||
|
return StatusCode(500);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpDelete("{id}")]
|
||||||
|
[ProducesResponseType(200)]
|
||||||
|
[ProducesResponseType(404)]
|
||||||
|
[ProducesResponseType(500)]
|
||||||
|
public async Task<IActionResult> DeleteUser(int id)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
_logger.LogInformation("Executing {Action} with parameters: {Parameters} for {Id}", nameof(DeleteUser), null,id);
|
||||||
|
|
||||||
|
|
||||||
|
var athlete = await _userService.GetUserByIdAsync(id);
|
||||||
|
if (athlete == null)
|
||||||
|
{
|
||||||
|
_logger.LogError("Athlete with id {id} not found", id);
|
||||||
|
return NotFound($"Athlete with id {id} not found");
|
||||||
|
}
|
||||||
|
var isDeleted = await _userService.DeleteUser(id);
|
||||||
|
if(!isDeleted)
|
||||||
|
{
|
||||||
|
_logger.LogError("Error while deleting athlete with id {id}", id);
|
||||||
|
return StatusCode(500);
|
||||||
|
}
|
||||||
|
return Ok();
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
_logger.LogError(e, "Error while getting the number of users");
|
||||||
|
return StatusCode(500);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,9 @@
|
|||||||
|
namespace HeartTrackAPI.Request;
|
||||||
|
|
||||||
|
public class PageRequest
|
||||||
|
{
|
||||||
|
public string? OrderingPropertyName { get; set; } = null;// need to be map on the dto OrderCriteria
|
||||||
|
public bool? Descending { get; set; } = false;
|
||||||
|
public int Index { get; set; } = 0;
|
||||||
|
public int Count { get; set; } = 5;
|
||||||
|
}
|
@ -0,0 +1,22 @@
|
|||||||
|
namespace HeartTrackAPI.Responce;
|
||||||
|
|
||||||
|
public class PageResponse<T>
|
||||||
|
{
|
||||||
|
// The index of the first item
|
||||||
|
public int Index { get; set; } = 1;
|
||||||
|
// The number of items
|
||||||
|
public int Count { get; set; } = 1;
|
||||||
|
// The total number of items
|
||||||
|
public int Total { get; set; } = 1;
|
||||||
|
// The items
|
||||||
|
public IEnumerable<T> Items { get; set; }
|
||||||
|
|
||||||
|
public PageResponse(int index, int count, int total, IEnumerable<T> items)
|
||||||
|
{
|
||||||
|
Index = index;
|
||||||
|
Count = count;
|
||||||
|
Total = total;
|
||||||
|
Items = items;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,10 @@
|
|||||||
|
namespace Model;
|
||||||
|
|
||||||
|
public class Coach : Role
|
||||||
|
{
|
||||||
|
public override bool CheckAdd(User user)
|
||||||
|
{
|
||||||
|
return user.Role is Athlete;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,6 @@
|
|||||||
|
namespace Model;
|
||||||
|
|
||||||
|
public class Notification
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,6 @@
|
|||||||
|
namespace Model;
|
||||||
|
|
||||||
|
public class RelationshipRequest
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,42 @@
|
|||||||
|
namespace Model;
|
||||||
|
|
||||||
|
public abstract class Role
|
||||||
|
{
|
||||||
|
protected List<User> UsersList { get; set; } = [];
|
||||||
|
protected List<RelationshipRequest> UsersRequests { get; set; } = [];
|
||||||
|
protected List<Training> TrainingList { get; set; } = [];
|
||||||
|
public abstract bool CheckAdd(User user);
|
||||||
|
|
||||||
|
public bool AddUser(User user)
|
||||||
|
{
|
||||||
|
if (!CheckAdd(user)) return false;
|
||||||
|
UsersList.Add(user);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool RemoveUser(User user)
|
||||||
|
{
|
||||||
|
return UsersList.Remove(user);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void AddTraining(Training training)
|
||||||
|
{
|
||||||
|
TrainingList.Add(training);
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool RemoveTraining(Training training)
|
||||||
|
{
|
||||||
|
return TrainingList.Remove(training);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void AddUserRequest(RelationshipRequest request)
|
||||||
|
{
|
||||||
|
UsersRequests.Add(request);
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool RemoveUserRequest(RelationshipRequest request)
|
||||||
|
{
|
||||||
|
return UsersRequests.Remove(request);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,6 @@
|
|||||||
|
namespace Model;
|
||||||
|
|
||||||
|
public class Training
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,41 @@
|
|||||||
|
namespace Model;
|
||||||
|
|
||||||
|
public class User
|
||||||
|
{
|
||||||
|
public int Id { get; set; }
|
||||||
|
public string Username { get; set; }
|
||||||
|
public string ProfilePicture { get; set; }
|
||||||
|
public string LastName { get; set; }
|
||||||
|
public string FirstName { get; set; }
|
||||||
|
public string Email { get; set; }
|
||||||
|
public string MotDePasse { get; set; }
|
||||||
|
public string Sexe { get; set; }
|
||||||
|
public float Lenght { get; set; }
|
||||||
|
public float Weight { get; set; }
|
||||||
|
public DateTime DateOfBirth { get; set; }
|
||||||
|
public Role Role { get; set; }
|
||||||
|
|
||||||
|
protected List<Notification> Notifications { get; set; } = new List<Notification>();
|
||||||
|
|
||||||
|
public User( string username, string profilePicture, string nom, string prenom, string email, string motDePasse, string sexe, float taille, float poids, DateTime dateNaissance, Role role)
|
||||||
|
{
|
||||||
|
Username = username;
|
||||||
|
ProfilePicture = profilePicture;
|
||||||
|
LastName = nom;
|
||||||
|
FirstName = prenom;
|
||||||
|
Email = email;
|
||||||
|
MotDePasse = motDePasse;
|
||||||
|
Sexe = sexe;
|
||||||
|
Lenght = taille;
|
||||||
|
Weight = poids;
|
||||||
|
DateOfBirth = dateNaissance;
|
||||||
|
Role = role;
|
||||||
|
}
|
||||||
|
public User(){}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,40 @@
|
|||||||
|
namespace StubAPI;
|
||||||
|
|
||||||
|
|
||||||
|
public static class Extensions
|
||||||
|
{
|
||||||
|
internal static Task<T?> AddItem<T>(this IList<T> collection, T? item)
|
||||||
|
{
|
||||||
|
if(item == null || collection.Contains(item))
|
||||||
|
{
|
||||||
|
return Task.FromResult<T?>(default(T));
|
||||||
|
}
|
||||||
|
collection.Add(item);
|
||||||
|
return Task.FromResult<T?>(item);
|
||||||
|
}
|
||||||
|
|
||||||
|
internal static Task<bool> DeleteItem<T>(this IList<T> collection, T? item)
|
||||||
|
{
|
||||||
|
if(item == null)
|
||||||
|
{
|
||||||
|
return Task.FromResult(false);
|
||||||
|
}
|
||||||
|
bool result = collection.Remove(item!);
|
||||||
|
return Task.FromResult(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
internal static Task<T?> UpdateItem<T>(this IList<T> collection, T? oldItem, T? newItem)
|
||||||
|
{
|
||||||
|
if(oldItem == null || newItem == null) return Task.FromResult<T?>(default(T));
|
||||||
|
|
||||||
|
if(!collection.Contains(oldItem))
|
||||||
|
{
|
||||||
|
return Task.FromResult<T?>(default(T));
|
||||||
|
}
|
||||||
|
|
||||||
|
collection.Remove(oldItem!);
|
||||||
|
collection.Add(newItem!);
|
||||||
|
return Task.FromResult<T?>(newItem);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in new issue