72 lines
2.6 KiB
C#
72 lines
2.6 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 ValidationPointController : ControllerBase
|
|
{
|
|
private IValidationPoint _validationPoint;
|
|
|
|
public ValidationPointController(IValidationPoint validationPoint)
|
|
{
|
|
_validationPoint = validationPoint;
|
|
}
|
|
|
|
|
|
[HttpPost]
|
|
public async Task<IActionResult> ValidationPoint([FromBody] ValidationPointDto model)
|
|
{
|
|
// Look for the cookie by its string key
|
|
if (!Request.Cookies.TryGetValue("token", out string token))
|
|
{
|
|
return Unauthorized(new { message = "Not authenticated" });
|
|
}
|
|
|
|
var currentUser = (User)HttpContext.Items["User"];
|
|
if (null == currentUser)
|
|
return Unauthorized(new { message = "Unauthorized" });
|
|
_validationPoint.SetCurrentUser(currentUser.Username);
|
|
var response = await _validationPoint.SaveValidationPointAsync(model);
|
|
return Ok(response);
|
|
}
|
|
|
|
[HttpGet]
|
|
public async Task<IActionResult> ValidationPoint(bool onlyActive=true)
|
|
{
|
|
var retval = await _validationPoint.LoadValidationPointAsync(onlyActive);
|
|
return Ok(retval);
|
|
}
|
|
|
|
[HttpGet("{id:int}")]
|
|
public async Task<IActionResult> ValidationPoint(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 _validationPoint.LoadValidationPointByIdAsync(id);
|
|
return Ok(retval);
|
|
}
|
|
[HttpDelete("{id:int}")]
|
|
public async Task<IActionResult> DeleteConcessionType(int id)
|
|
{
|
|
// only admins can access other user records
|
|
var currentUser = (User)HttpContext.Items["User"];
|
|
if (null == currentUser)
|
|
return Unauthorized(new { message = "Unauthorized" });
|
|
_validationPoint.SetCurrentUser(currentUser.Username);
|
|
var retval = await _validationPoint.DeleteValidationPointAsync(id);
|
|
return Ok(retval);
|
|
}
|
|
}
|
|
}
|