|
|
|
@ -1,11 +1,9 @@
|
|
|
|
|
using APIMappers;
|
|
|
|
|
using Dto;
|
|
|
|
|
using Entities;
|
|
|
|
|
using HeartTrackAPI.Request;
|
|
|
|
|
using HeartTrackAPI.Responce;
|
|
|
|
|
using Microsoft.AspNetCore.Identity;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using Model;
|
|
|
|
|
using Model.Manager;
|
|
|
|
|
using Model.Repository;
|
|
|
|
|
using Shared;
|
|
|
|
@ -18,11 +16,13 @@ namespace HeartTrackAPI.Controllers;
|
|
|
|
|
public class UsersController : Controller
|
|
|
|
|
{
|
|
|
|
|
private readonly ILogger<UsersController> _logger;
|
|
|
|
|
private readonly IActivityRepository _activityService;
|
|
|
|
|
private readonly IUserRepository _userService;
|
|
|
|
|
public UsersController(ILogger<UsersController> logger, IDataManager dataManager)
|
|
|
|
|
{
|
|
|
|
|
_logger = logger;
|
|
|
|
|
_userService = dataManager.UserRepo;
|
|
|
|
|
_activityService = dataManager.ActivityRepo;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpGet]
|
|
|
|
@ -43,7 +43,7 @@ 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 pageResponse = new PageResponse<UserDto>(request.Index, request.Count, totalCount, athletes!.Select(a => a.ToDto()));
|
|
|
|
|
return Ok(pageResponse);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
@ -118,7 +118,6 @@ public class UsersController : Controller
|
|
|
|
|
return StatusCode(500);
|
|
|
|
|
}
|
|
|
|
|
return Ok(updatedAthlete.ToDto());
|
|
|
|
|
return Ok();
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
@ -160,6 +159,39 @@ public class UsersController : Controller
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpGet("{id}/friends")]
|
|
|
|
|
[ProducesResponseType(typeof(PageResponse<UserDto>), 200)]
|
|
|
|
|
[ProducesResponseType(404)]
|
|
|
|
|
[ProducesResponseType(500)]
|
|
|
|
|
public async Task<ActionResult<PageResponse<UserDto>>> 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);
|
|
|
|
|
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);
|
|
|
|
|
if (friends == null) return NotFound();
|
|
|
|
|
var pageResponse = new PageResponse<UserDto>(request.Index, request.Count, totalCount, friends.Select(a => a.ToDto()));
|
|
|
|
|
return Ok(pageResponse);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(e, "Error while getting the number of users");
|
|
|
|
|
return StatusCode(500);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[HttpPost("{id}/friend/{friendId}")]
|
|
|
|
|
[ProducesResponseType(200)]
|
|
|
|
@ -197,6 +229,115 @@ public class UsersController : Controller
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpDelete("{id}/friend/{friendId}")]
|
|
|
|
|
[ProducesResponseType(200)]
|
|
|
|
|
[ProducesResponseType(404)]
|
|
|
|
|
[ProducesResponseType(500)]
|
|
|
|
|
public async Task<IActionResult> RemoveFriend(int id, int friendId)
|
|
|
|
|
{
|
|
|
|
|
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);
|
|
|
|
|
if(!isRemoved)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError("Error while removing friend with id {friendId} to athlete with id {id}", friendId, id);
|
|
|
|
|
return StatusCode(500);
|
|
|
|
|
}
|
|
|
|
|
return Ok();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(e, "Error while getting the number of users");
|
|
|
|
|
return StatusCode(500);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ou faire un get qui si le role est coach resend les athletes et si le role est athlete resend les coach
|
|
|
|
|
[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
|
|
|
|
|
{
|
|
|
|
|
_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);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(e, "Error while getting the number of users");
|
|
|
|
|
return StatusCode(500);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[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)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var totalCount = await _activityService.GetNbActivitiesByUser(userId);
|
|
|
|
|
if (pageRequest.Count * pageRequest.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(GetActivitiesByUser), pageRequest);
|
|
|
|
|
var activities = await _activityService.GetActivitiesByUser(userId, pageRequest.Index, pageRequest.Count, ActivityOrderCriteria.None, pageRequest.Descending ?? false);
|
|
|
|
|
if(activities == null)
|
|
|
|
|
{
|
|
|
|
|
return NotFound("No activities found");
|
|
|
|
|
}
|
|
|
|
|
var pageResponse = new PageResponse<ActivityDto>(pageRequest.Index, pageRequest.Count, totalCount, activities.Select(a => a.ToDto()));
|
|
|
|
|
return Ok(pageResponse);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(e, "Error while getting all activities");
|
|
|
|
|
return StatusCode(500);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
|
|
[HttpGet("{userId}/trainings")]
|
|
|
|
|
[ProducesResponseType(typeof(PageResponse<TrainingDto>), 200)]
|
|
|
|
|
[ProducesResponseType(404)]
|
|
|
|
|
[ProducesResponseType(500)]
|
|
|
|
|
public async Task<ActionResult<PageResponse<TrainingDto>> GetTrainings(int userId, [FromQuery] PageRequest request)
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[HttpPost("logout")]
|
|
|
|
|
[ProducesResponseType(200)]
|
|
|
|
|
[ProducesResponseType(401)]
|
|
|
|
|