You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
160 lines
5.4 KiB
160 lines
5.4 KiB
using ApiMappeur;
|
|
using Dto;
|
|
using HeartTrackAPI.Request;
|
|
using HeartTrackAPI.Responce;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Model;
|
|
using Model.Manager;
|
|
using Model.Repository;
|
|
using Shared;
|
|
|
|
namespace HeartTrackAPI.Controllers;
|
|
|
|
[ApiController]
|
|
[Route("api/users")]
|
|
public class UsersController : Controller
|
|
{
|
|
private readonly ILogger<UsersController> _logger;
|
|
private readonly IUserRepository _userService;
|
|
public UsersController(ILogger<UsersController> logger, IDataManager dataManager)
|
|
{
|
|
_logger = logger;
|
|
_userService = dataManager.UserRepository;
|
|
}
|
|
|
|
[HttpGet]
|
|
[ProducesResponseType(typeof(PageResponse<UserDto>), 200)]
|
|
[ProducesResponseType(400)]
|
|
[ProducesResponseType(500)]
|
|
public async Task<ActionResult<PageResponse<UserDto>>> Get([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(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()));
|
|
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>> GetById(int id)
|
|
{
|
|
try
|
|
{
|
|
_logger.LogInformation("Executing {Action} with parameters: {Parameters}", nameof(GetById), 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");
|
|
}
|
|
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>> Count()
|
|
{
|
|
try
|
|
{
|
|
_logger.LogInformation("Executing {Action} with parameters: {Parameters}", nameof(Count), 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)]
|
|
public async Task<ActionResult<UserDto>> Update(int id, [FromBody] UserDto user)
|
|
{
|
|
try
|
|
{
|
|
_logger.LogInformation("Executing {Action} with parameters: {Parameters} for {Id}", nameof(Update), user,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 updatedAthlete = await _userService.UpdateItem(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> Delete(int id)
|
|
{
|
|
try
|
|
{
|
|
_logger.LogInformation("Executing {Action} with parameters: {Parameters} for {Id}", nameof(Delete), 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 isDeleted = await _userService.DeleteItem(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);
|
|
}
|
|
}
|
|
|
|
|
|
} |