|
|
|
@ -1,8 +1,11 @@
|
|
|
|
|
using System.ComponentModel.DataAnnotations;
|
|
|
|
|
using APIMappers;
|
|
|
|
|
using Dto;
|
|
|
|
|
using Dto.Tiny;
|
|
|
|
|
using HeartTrackAPI.Request;
|
|
|
|
|
using HeartTrackAPI.Responce;
|
|
|
|
|
using HeartTrackAPI.Utils;
|
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
|
using Microsoft.AspNetCore.Identity;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using Model.Manager;
|
|
|
|
@ -38,10 +41,10 @@ public class UsersController : Controller
|
|
|
|
|
/// <response code="400">La demande de pagination est invalide.</response>
|
|
|
|
|
/// <response code="500">Erreur interne du serveur.</response>
|
|
|
|
|
[HttpGet]
|
|
|
|
|
[ProducesResponseType(typeof(PageResponse<UserDto>), 200)]
|
|
|
|
|
[ProducesResponseType(typeof(PageResponse<UserTinyDto>), 200)]
|
|
|
|
|
[ProducesResponseType(400)]
|
|
|
|
|
[ProducesResponseType(500)]
|
|
|
|
|
public async Task<ActionResult<PageResponse<UserDto>>> Get([FromQuery] PageRequest request)
|
|
|
|
|
public async Task<ActionResult<PageResponse<UserTinyDto>>> Get([FromQuery] PageRequest request)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
@ -54,8 +57,8 @@ public class UsersController : Controller
|
|
|
|
|
|
|
|
|
|
_logger.LogInformation("Executing {Action} with parameters: {Parameters}", nameof(Get), null);
|
|
|
|
|
|
|
|
|
|
var athletes = await _userService.GetUsers(request.Index, request.Count, Enum.TryParse(request.OrderingPropertyName, out AthleteOrderCriteria result) ? result : AthleteOrderCriteria.None, request.Descending ?? false);
|
|
|
|
|
var pageResponse = new PageResponse<UserDto>(request.Index, request.Count, totalCount, athletes!.Select(a => a.ToDto()));
|
|
|
|
|
var athletes = await _userService.GetUsersTiny(request.Index, request.Count, Enum.TryParse(request.OrderingPropertyName, out AthleteOrderCriteria result) ? result : AthleteOrderCriteria.None, request.Descending ?? false);
|
|
|
|
|
var pageResponse = new PageResponse<UserTinyDto>(request.Index, request.Count, totalCount, athletes);
|
|
|
|
|
return Ok(pageResponse);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
@ -74,21 +77,21 @@ public class UsersController : Controller
|
|
|
|
|
/// <response code="404">Aucun utilisateur trouvé pour l'identifiant spécifié.</response>
|
|
|
|
|
/// <response code="500">Erreur interne du serveur.</response>
|
|
|
|
|
[HttpGet("{id}")]
|
|
|
|
|
[ProducesResponseType(typeof(UserDto), 200)]
|
|
|
|
|
[ProducesResponseType(typeof(ResponseUserDto), 200)]
|
|
|
|
|
[ProducesResponseType(404)]
|
|
|
|
|
[ProducesResponseType(500)]
|
|
|
|
|
public async Task<ActionResult<UserDto>> GetById([Range(0,int.MaxValue)]int id)
|
|
|
|
|
public async Task<ActionResult<ResponseUserDto>> GetById([Range(0,int.MaxValue)]int id)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
_logger.LogInformation("Executing {Action} with parameters: {Parameters}", nameof(GetById), id);
|
|
|
|
|
var athlete = await _userService.GetItemById(id);
|
|
|
|
|
var athlete = await _userService.GetUserById(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());
|
|
|
|
|
return Ok(athlete);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
@ -110,7 +113,7 @@ public class UsersController : Controller
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
_logger.LogInformation("Executing {Action} with parameters: {Parameters}", nameof(Count), null);
|
|
|
|
|
_logger.LogInformation("Executing {Action}", nameof(Count));
|
|
|
|
|
var nbUsers = await _userService.GetNbItems();
|
|
|
|
|
return Ok(nbUsers);
|
|
|
|
|
}
|
|
|
|
@ -131,27 +134,27 @@ public class UsersController : Controller
|
|
|
|
|
/// <response code="404">Utilisateur non trouvé.</response>
|
|
|
|
|
/// <response code="500">Erreur interne du serveur.</response>
|
|
|
|
|
[HttpPut("{id}")]
|
|
|
|
|
[ProducesResponseType(typeof(UserDto), 200)]
|
|
|
|
|
[ProducesResponseType(typeof(UserTinyDto), 200)]
|
|
|
|
|
[ProducesResponseType(404)]
|
|
|
|
|
[ProducesResponseType(500)]
|
|
|
|
|
public async Task<ActionResult<UserDto>> Update(int id, [FromBody] UserDto user)
|
|
|
|
|
public async Task<ActionResult<UserTinyDto>> Update(int id, [FromBody] UserTinyDto user)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
_logger.LogInformation("Executing {Action} with parameters: {Parameters} for {Id}", nameof(Update), user,id);
|
|
|
|
|
var athlete = await _userService.GetItemById(id);
|
|
|
|
|
var athlete = await _userService.GetUserById(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.UpdateItem(id, user.ToModel());
|
|
|
|
|
var updatedAthlete = await _userService.UpdateUser(id, user);
|
|
|
|
|
if(updatedAthlete == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError("Error while updating athlete with id {id}", id);
|
|
|
|
|
return Problem();
|
|
|
|
|
}
|
|
|
|
|
return Ok(updatedAthlete.ToDto());
|
|
|
|
|
return Ok(updatedAthlete);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
@ -178,9 +181,8 @@ public class UsersController : Controller
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
_logger.LogInformation("Executing {Action} with parameters: {Parameters} for {Id}", nameof(Delete), null,id);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var athlete = await _userService.GetItemById(id);
|
|
|
|
|
|
|
|
|
|
var athlete = await _userService.GetUserById(id);
|
|
|
|
|
if (athlete == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError("Athlete with id {id} not found", id);
|
|
|
|
@ -211,31 +213,30 @@ public class UsersController : Controller
|
|
|
|
|
/// <response code="404">Utilisateur non trouvé.</response>
|
|
|
|
|
/// <response code="500">Erreur interne du serveur.</response>
|
|
|
|
|
[HttpGet("{id}/friends")]
|
|
|
|
|
[ProducesResponseType(typeof(PageResponse<UserDto>), 200)]
|
|
|
|
|
[ProducesResponseType(typeof(PageResponse<UserTinyDto>), 200)]
|
|
|
|
|
[ProducesResponseType(404)]
|
|
|
|
|
[ProducesResponseType(500)]
|
|
|
|
|
public async Task<ActionResult<PageResponse<UserDto>>> GetFriends(int id, [FromQuery] PageRequest request)
|
|
|
|
|
public async Task<ActionResult<PageResponse<UserTinyDto>>> GetFriends(int id, [FromQuery] PageRequest request)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
_logger.LogInformation("Executing {Action} with parameters: {Parameters} for {Id}", nameof(GetFriends), null,id);
|
|
|
|
|
var athlete = await _userService.GetItemById(id);
|
|
|
|
|
if (athlete == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError("Athlete with id {id} not found", id);
|
|
|
|
|
return NotFound($"Athlete with id {id} not found");
|
|
|
|
|
}
|
|
|
|
|
var totalCount = await _userService.GetNbFriends(athlete);
|
|
|
|
|
var totalCount = await _userService.GetNbFriends(id);
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
var friends = await _userService.GetFriends(athlete, request.Index, request.Count, Enum.TryParse(request.OrderingPropertyName, out AthleteOrderCriteria result) ? result : AthleteOrderCriteria.None, request.Descending ?? false);
|
|
|
|
|
var friends = await _userService.GetFriends(id, request.Index, request.Count, Enum.TryParse(request.OrderingPropertyName, out AthleteOrderCriteria result) ? result : AthleteOrderCriteria.None, request.Descending ?? false);
|
|
|
|
|
if (friends == null) return NotFound();
|
|
|
|
|
var pageResponse = new PageResponse<UserDto>(request.Index, request.Count, totalCount, friends.Select(a => a.ToDto()));
|
|
|
|
|
var pageResponse = new PageResponse<UserTinyDto>(request.Index, request.Count, totalCount, friends);
|
|
|
|
|
return Ok(pageResponse);
|
|
|
|
|
}
|
|
|
|
|
catch(ModelNotFoundException e)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(e, "Error while adding a friend to an athlete");
|
|
|
|
|
return BadRequest(e.Message);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(e, "Error while getting the number of users");
|
|
|
|
@ -256,24 +257,13 @@ public class UsersController : Controller
|
|
|
|
|
[ProducesResponseType(200)]
|
|
|
|
|
[ProducesResponseType(404)]
|
|
|
|
|
[ProducesResponseType(500)]
|
|
|
|
|
public async Task<IActionResult> AddFriend(int id, int friendId)
|
|
|
|
|
public async Task<IActionResult> AddFollowing(int id, int friendId)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
_logger.LogInformation("Executing {Action} with parameters: {Parameters} for {Id}", nameof(AddFriend), friendId,id);
|
|
|
|
|
var athlete = await _userService.GetItemById(id);
|
|
|
|
|
if (athlete == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError("Athlete with id {id} not found", id);
|
|
|
|
|
return NotFound($"Athlete with id {id} not found");
|
|
|
|
|
}
|
|
|
|
|
var friend = await _userService.GetItemById(friendId);
|
|
|
|
|
if (friend == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError("Athlete with id {id} not found", friendId);
|
|
|
|
|
return NotFound($"Athlete with id {friendId} not found");
|
|
|
|
|
}
|
|
|
|
|
var isAdded = await _userService.AddFriend(athlete, friend);
|
|
|
|
|
_logger.LogInformation("Executing {Action} with parameters: {Parameters} for {Id}", nameof(AddFollowing), friendId,id);
|
|
|
|
|
|
|
|
|
|
var isAdded = await _userService.AddFollowing(id, friendId);
|
|
|
|
|
if(!isAdded)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError("Error while adding friend with id {friendId} to athlete with id {id}", friendId, id);
|
|
|
|
@ -281,9 +271,14 @@ public class UsersController : Controller
|
|
|
|
|
}
|
|
|
|
|
return Ok();
|
|
|
|
|
}
|
|
|
|
|
catch(FriendShipException e)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(e, "Error while adding a friend to an athlete");
|
|
|
|
|
return BadRequest(e.Message);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(e, "Error while getting the number of users");
|
|
|
|
|
_logger.LogError(e, "Error while attempting to follow a user");
|
|
|
|
|
return Problem();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -307,19 +302,8 @@ public class UsersController : Controller
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
_logger.LogInformation("Executing {Action} with parameters: {Parameters} for {Id}", nameof(RemoveFriend), friendId,id);
|
|
|
|
|
var athlete = await _userService.GetItemById(id);
|
|
|
|
|
if (athlete == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError("Athlete with id {id} not found", id);
|
|
|
|
|
return NotFound($"Athlete with id {id} not found");
|
|
|
|
|
}
|
|
|
|
|
var friend = await _userService.GetItemById(friendId);
|
|
|
|
|
if (friend == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError("Athlete with id {id} not found", friendId);
|
|
|
|
|
return NotFound($"Athlete with id {friendId} not found");
|
|
|
|
|
}
|
|
|
|
|
var isRemoved = await _userService.RemoveFriend(athlete, friend);
|
|
|
|
|
|
|
|
|
|
var isRemoved = await _userService.RemoveFollowing(id, friendId);
|
|
|
|
|
if(!isRemoved)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError("Error while removing friend with id {friendId} to athlete with id {id}", friendId, id);
|
|
|
|
@ -327,55 +311,19 @@ public class UsersController : Controller
|
|
|
|
|
}
|
|
|
|
|
return Ok();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(e, "Error while getting the number of users");
|
|
|
|
|
return Problem();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Obtient la liste des athlètes d'un coach spécifique.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="coachId">L'identifiant du coach.</param>
|
|
|
|
|
/// <param name="request">Les critères de pagination et de tri.</param>
|
|
|
|
|
/// <returns>La liste paginée des athlètes.</returns>
|
|
|
|
|
/// <response code="200">Retourne la liste paginée des athlètes du coach.</response>
|
|
|
|
|
/// <response code="404">Coach non trouvé.</response>
|
|
|
|
|
/// <response code="500">Erreur interne du serveur.</response>
|
|
|
|
|
[HttpGet("{coachId}/athletes")]
|
|
|
|
|
[ProducesResponseType(typeof(PageResponse<UserDto>), 200)]
|
|
|
|
|
[ProducesResponseType(404)]
|
|
|
|
|
[ProducesResponseType(500)]
|
|
|
|
|
public async Task<ActionResult<PageResponse<UserDto>>> GetAthletes(int coachId, [FromQuery] PageRequest request)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
catch(FriendShipException e)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogInformation("Executing {Action} with parameters: {Parameters} for {Id}", nameof(GetAthletes), null,coachId);
|
|
|
|
|
var coach = await _userService.GetItemById(coachId);
|
|
|
|
|
if (coach == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError("Athlete with id {id} not found", coachId);
|
|
|
|
|
return NotFound($"Athlete with id {coachId} not found");
|
|
|
|
|
}
|
|
|
|
|
var totalCount = await _userService.GetNbFriends(coach);
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
var athletes = await _userService.GetFriends(coach, request.Index, request.Count, Enum.TryParse(request.OrderingPropertyName, out AthleteOrderCriteria result) ? result : AthleteOrderCriteria.None, request.Descending ?? false);
|
|
|
|
|
if (athletes == null) return NotFound();
|
|
|
|
|
var pageResponse = new PageResponse<UserDto>(request.Index, request.Count, totalCount, athletes.Select(a => a.ToDto()));
|
|
|
|
|
return Ok(pageResponse);
|
|
|
|
|
_logger.LogError(e, "Error while removing a friend to an athlete");
|
|
|
|
|
return BadRequest(e.Message);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(e, "Error while getting the number of users");
|
|
|
|
|
_logger.LogError(e, "Error while attempting to unfollow a user");
|
|
|
|
|
return Problem();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Obtient la liste des activités d'un utilisateur spécifique.
|
|
|
|
|
/// </summary>
|
|
|
|
@ -387,7 +335,7 @@ public class UsersController : Controller
|
|
|
|
|
/// <response code="500">Erreur interne du serveur.</response>
|
|
|
|
|
[HttpGet("{userId}/activities")]
|
|
|
|
|
// should be tiny DTOActivity returned with only the necessary information (will be used in the list of activities of a user)
|
|
|
|
|
public async Task<ActionResult<PageResponse<ActivityDto>>> GetActivitiesByUser(int userId, [FromQuery] PageRequest pageRequest)
|
|
|
|
|
public async Task<ActionResult<PageResponse<ActivityTinyDto>>> GetActivitiesByUser(int userId, [FromQuery] PageRequest pageRequest)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
@ -403,7 +351,7 @@ public class UsersController : Controller
|
|
|
|
|
{
|
|
|
|
|
return NotFound("No activities found");
|
|
|
|
|
}
|
|
|
|
|
var pageResponse = new PageResponse<ActivityDto>(pageRequest.Index, pageRequest.Count, totalCount, activities.Select(a => a.ToDto()));
|
|
|
|
|
var pageResponse = new PageResponse<ActivityTinyDto>(pageRequest.Index, pageRequest.Count, totalCount, activities.Select(a => a.ToDto()));
|
|
|
|
|
return Ok(pageResponse);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
@ -411,7 +359,7 @@ public class UsersController : Controller
|
|
|
|
|
_logger.LogError(e, "Error while getting all activities");
|
|
|
|
|
return Problem();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}*/
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Déconnecte l'utilisateur actuel.
|
|
|
|
|