63 lines
2.1 KiB
C#
63 lines
2.1 KiB
C#
using Microsoft.AspNetCore.Mvc;
|
|
using CarParkValidationAPI.Authorization;
|
|
using CarParkValidationAPI.Entities;
|
|
using CarParkValidationAPI.Models;
|
|
using CarParkValidationAPI.Interface;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace CarParkValidationAPI.Controllers
|
|
{
|
|
// [Authorize]
|
|
[ApiController]
|
|
[Route("api/v1/[controller]")]
|
|
public class AdminUserController : ControllerBase
|
|
{
|
|
private IAdminUser _adminUser;
|
|
public AdminUserController(IAdminUser adminUser)
|
|
{
|
|
_adminUser = adminUser;
|
|
}
|
|
|
|
[HttpPost]
|
|
public async Task<IActionResult> AdminUser([FromBody] AdminUserDto model)
|
|
{
|
|
var currentUser = (User)HttpContext.Items["User"];
|
|
if (null == currentUser)
|
|
return Unauthorized(new { message = "Unauthorized" });
|
|
_adminUser.SetCurrentUser(currentUser.Username);
|
|
var response = await _adminUser.SaveAdminUserAsync(model);
|
|
return Ok(response);
|
|
}
|
|
|
|
[HttpGet]
|
|
public async Task<IActionResult> AdminUser(bool onlyActive=true)
|
|
{
|
|
var retval = await _adminUser.LoadAdminUserAsync(onlyActive);
|
|
return Ok(retval);
|
|
}
|
|
|
|
[HttpGet("{id:int}")]
|
|
public async Task<IActionResult> AdminUser(int id)
|
|
{
|
|
/*
|
|
// only admins can access other user records
|
|
var currentUser = (User)HttpContext.Items["User"];
|
|
if (id != currentUser.Id && currentUser.Role != Role.Admin)
|
|
return Unauthorized(new { message = "Unauthorized" });
|
|
*/
|
|
var retval = await _adminUser.LoadAdminUserByIdAsync(id);
|
|
return Ok(retval);
|
|
}
|
|
[HttpDelete("{id:int}")]
|
|
public async Task<IActionResult> DeleteAdminUser(int id)
|
|
{
|
|
var currentUser = (User)HttpContext.Items["User"];
|
|
if (null == currentUser)
|
|
return Unauthorized(new { message = "Unauthorized" });
|
|
_adminUser.SetCurrentUser(currentUser.Username);
|
|
var retval = await _adminUser.DeleteAdminUserAsync(id);
|
|
return Ok(retval);
|
|
}
|
|
}
|
|
}
|