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.
107 lines
2.8 KiB
107 lines
2.8 KiB
using DbContextLib;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Model.Business;
|
|
using Model.DTO;
|
|
using Services;
|
|
|
|
namespace API.Controllers
|
|
{
|
|
[Route("api/[controller]")]
|
|
[Authorize]
|
|
[ApiController]
|
|
public class UserController : Controller
|
|
{
|
|
private IDataService _userDataService;
|
|
|
|
public UserController(IDataService userDataService)
|
|
{
|
|
_userDataService = userDataService;
|
|
}
|
|
|
|
[HttpGet("users/{page}/{number}")]
|
|
[Authorize]
|
|
public IActionResult GetUsers(int page, int number)
|
|
{
|
|
var nbUser = _userDataService.GetUsers(page, number).Count();
|
|
if(nbUser == 0)
|
|
{
|
|
return StatusCode(204);
|
|
}
|
|
return Ok(_userDataService.GetUsers(page, number));
|
|
}
|
|
|
|
[HttpGet("user/id/{id}")]
|
|
[Authorize]
|
|
public IActionResult GetUserById(int id)
|
|
{
|
|
try
|
|
{
|
|
return Ok(_userDataService.GetUserById(id));
|
|
} catch (ArgumentException ex)
|
|
{
|
|
return NotFound();
|
|
}
|
|
|
|
}
|
|
|
|
[HttpGet("user/username/{username}")]
|
|
[Authorize]
|
|
public IActionResult GetUserByUsername(string username)
|
|
{
|
|
try
|
|
{
|
|
return Ok(_userDataService.GetUserByUsername(username));
|
|
}catch (ArgumentException ex)
|
|
{
|
|
return NotFound();
|
|
}
|
|
|
|
}
|
|
|
|
[HttpDelete]
|
|
[Authorize]
|
|
public IActionResult DeleteUser(int id)
|
|
{
|
|
var sucess = _userDataService.DeleteUser(id);
|
|
if(sucess)
|
|
{
|
|
return Ok(_userDataService.DeleteUser(id));
|
|
} else
|
|
{
|
|
return NotFound();
|
|
}
|
|
|
|
}
|
|
|
|
[HttpPost]
|
|
[Authorize]
|
|
public IActionResult UpdateUser(string username, string password, string email, bool isAdmin)
|
|
{
|
|
|
|
// return Ok(_userDataService.CreateUser(username, password, email, isAdmin));
|
|
return CreatedAtAction(nameof(GetUsers), _userDataService.CreateUser(username, password, email, isAdmin));
|
|
}
|
|
|
|
[HttpPut]
|
|
[Authorize]
|
|
public IActionResult CreateUser(int id, [FromBody] UserDTO userDTO)
|
|
{
|
|
if(id != userDTO.Id)
|
|
{
|
|
return BadRequest();
|
|
}
|
|
if(!ModelState.IsValid)
|
|
{
|
|
return BadRequest();
|
|
}
|
|
if(userDTO != null)
|
|
{
|
|
return Ok(_userDataService.UpdateUser(id, userDTO));
|
|
}
|
|
return NotFound();
|
|
}
|
|
}
|
|
}
|