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.
78 lines
2.7 KiB
78 lines
2.7 KiB
using Microsoft.AspNetCore.Mvc;
|
|
using Server.Dto.Request;
|
|
using Server.Dto.Response;
|
|
using Asp.Versioning;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Server.IServices;
|
|
|
|
namespace Server.Controller.v1;
|
|
|
|
[ApiController]
|
|
[ApiVersion("1.0")]
|
|
[Route("api/v{version:apiVersion}/[controller]")]
|
|
public class UsersController : ControllerBase
|
|
{
|
|
private readonly ILogger<UsersController> _logger;
|
|
private IUsersService _dataServices;
|
|
|
|
public UsersController(ILogger<UsersController> logger, IUsersService dataServices)
|
|
{
|
|
_logger = logger;
|
|
_dataServices = dataServices;
|
|
}
|
|
|
|
[HttpGet]
|
|
[ProducesResponseType(typeof(IEnumerable<ResponseUserDto>), StatusCodes.Status200OK)]
|
|
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
|
public IActionResult GetUsers([FromQuery] int pageIndex = 1, [FromQuery] int pageSize = 5, [FromQuery] bool ascending = true)
|
|
{
|
|
var users = _dataServices.GetUsers(pageIndex, pageSize, ascending);
|
|
return users.Result.TotalCount == 0 ? NoContent() : Ok(users);
|
|
}
|
|
|
|
|
|
|
|
[HttpGet("{id}")]
|
|
[ProducesResponseType(typeof(ResponseUserDto), StatusCodes.Status200OK)]
|
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
|
public IActionResult GetAlumniById(string id)
|
|
{
|
|
var alumni = _dataServices.GetUserById(id);
|
|
return alumni.Result == null ? NotFound() : Ok(alumni);
|
|
}
|
|
|
|
[HttpPost]
|
|
[ProducesResponseType(typeof(ResponseUserDto), StatusCodes.Status201Created)]
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
|
[AllowAnonymous]
|
|
public async Task<IActionResult> CreateUser([FromBody] RequestUserDto request)
|
|
{
|
|
if (!ModelState.IsValid) return BadRequest(ModelState);
|
|
|
|
var createdUser = await _dataServices.CreateUser(request);
|
|
return CreatedAtAction(nameof(GetAlumniById), new { id = createdUser.Id }, createdUser);
|
|
}
|
|
|
|
[HttpPut("{id}")]
|
|
[ProducesResponseType(typeof(ResponseUserDto), StatusCodes.Status200OK)]
|
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
|
[AllowAnonymous]
|
|
public async Task<IActionResult> UpdateUser(string id, [FromBody] RequestUserDto request)
|
|
{
|
|
if (!ModelState.IsValid) return BadRequest(ModelState);
|
|
|
|
var updatedProgram = await _dataServices.UpdateUser(id, request);
|
|
return updatedProgram == null ? NotFound() : Ok(updatedProgram);
|
|
}
|
|
|
|
[HttpDelete("{id}")]
|
|
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
|
[AllowAnonymous]
|
|
public async Task<IActionResult> DeleteUser(string id)
|
|
{
|
|
var deleted = await _dataServices.DeleteUser(id);
|
|
return deleted ? NoContent() : NotFound();
|
|
}
|
|
} |