63 lines
2.0 KiB
C#
63 lines
2.0 KiB
C#
using LocationsApi.Models;
|
|
using LocationsApi.Services;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using System.Security.Claims;
|
|
|
|
namespace LocationsApi.Controllers;
|
|
|
|
[ApiController]
|
|
[Route("api/[controller]")]
|
|
public class LocationsController : ControllerBase
|
|
{
|
|
private readonly LocationService _locations;
|
|
|
|
public LocationsController(LocationService locations)
|
|
{
|
|
_locations = locations;
|
|
}
|
|
|
|
[HttpGet]
|
|
public async Task<ActionResult<List<LocationResponse>>> GetAll(
|
|
[FromQuery] int? minQ,
|
|
[FromQuery] int? maxQ,
|
|
[FromQuery] int? minR,
|
|
[FromQuery] int? maxR)
|
|
{
|
|
var locations = minQ.HasValue && maxQ.HasValue && minR.HasValue && maxR.HasValue
|
|
? await _locations.GetRangeAsync(minQ.Value, maxQ.Value, minR.Value, maxR.Value)
|
|
: await _locations.GetAllAsync();
|
|
|
|
return Ok(locations.Select(LocationResponse.FromLocation).ToList());
|
|
}
|
|
|
|
[HttpGet("{q:int}/{r:int}")]
|
|
public async Task<ActionResult<LocationResponse>> Get(int q, int r)
|
|
{
|
|
var location = await _locations.GetAsync(q, r);
|
|
return location is null ? NotFound() : Ok(LocationResponse.FromLocation(location));
|
|
}
|
|
|
|
[Authorize(Roles = "USER,SUPER")]
|
|
[HttpPost("claim")]
|
|
public async Task<ActionResult<LocationResponse>> Claim([FromBody] ClaimLocationRequest request)
|
|
{
|
|
var username = User.FindFirstValue(ClaimTypes.Name);
|
|
if (string.IsNullOrWhiteSpace(username))
|
|
{
|
|
return Unauthorized();
|
|
}
|
|
|
|
var location = await _locations.ClaimAsync(request.Q, request.R, username);
|
|
return Ok(LocationResponse.FromLocation(location));
|
|
}
|
|
|
|
[Authorize(Roles = "SUPER")]
|
|
[HttpPut]
|
|
public async Task<ActionResult<LocationResponse>> Upsert([FromBody] UpsertLocationRequest request)
|
|
{
|
|
var location = await _locations.UpsertAsync(request);
|
|
return Ok(LocationResponse.FromLocation(location));
|
|
}
|
|
}
|