using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using RMuseum.Models.Auth.Memory;
using RMuseum.Models.Divan;
using RMuseum.Services;
using RSecurityBackend.Models.Auth.Memory;
using System.Net;
using System.Threading.Tasks;
namespace RMuseum.Controllers
{
[Produces("application/json")]
[Route("api/locations")]
public class GeoLocationController : Controller
{
///
/// get all locations
///
///
[HttpGet]
[AllowAnonymous]
[ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(DivanGeoLocation[]))]
[ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))]
public async Task GetLocationsAsync()
{
var res = await _locationService.GetLocationsAsync();
if (!string.IsNullOrEmpty(res.ExceptionString))
return BadRequest(res.ExceptionString);
return Ok(res.Result);
}
///
/// get location by id
///
///
///
[HttpGet("{id}")]
[AllowAnonymous]
[ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(DivanGeoLocation))]
[ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))]
public async Task GetLocationAsync(int id)
{
var res = await _locationService.GetLocationAsync(id);
if (!string.IsNullOrEmpty(res.ExceptionString))
return BadRequest(res.ExceptionString);
return Ok(res.Result);
}
///
/// add new location
///
///
///
[HttpPost]
[Authorize(Policy = RMuseumSecurableItem.DivanEntityShortName + ":" + SecurableItem.ModifyOperationShortName)]
[ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(DivanGeoLocation))]
[ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))]
[ProducesResponseType((int)HttpStatusCode.Unauthorized)]
public async Task AddLocationAsync([FromBody] DivanGeoLocation location)
{
if (ReadOnlyMode)
return BadRequest("سایت به دلایل فنی مثل انتقال سرور موقتاً در حالت فقط خواندنی قرار دارد. لطفاً ساعاتی دیگر مجدداً تلاش کنید.");
var res = await _locationService.AddLocationAsync(location.Name, location.Latitude, location.Longitude);
if (!string.IsNullOrEmpty(res.ExceptionString))
return BadRequest(res.ExceptionString);
return Ok(res.Result);
}
///
/// update an existing location
///
///
///
[HttpPut]
[Authorize(Policy = RMuseumSecurableItem.DivanEntityShortName + ":" + SecurableItem.ModifyOperationShortName)]
[ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(bool))]
[ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))]
[ProducesResponseType((int)HttpStatusCode.Unauthorized)]
public async Task UpdateLocationAsync([FromBody] DivanGeoLocation location)
{
if (ReadOnlyMode)
return BadRequest("سایت به دلایل فنی مثل انتقال سرور موقتاً در حالت فقط خواندنی قرار دارد. لطفاً ساعاتی دیگر مجدداً تلاش کنید.");
var res = await _locationService.UpdateLocationAsync(location);
if (!string.IsNullOrEmpty(res.ExceptionString))
return BadRequest(res.ExceptionString);
return Ok(res.Result);
}
[HttpDelete("{id}")]
[Authorize(Policy = RMuseumSecurableItem.DivanEntityShortName + ":" + SecurableItem.ModifyOperationShortName)]
[ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(bool))]
[ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))]
[ProducesResponseType((int)HttpStatusCode.Unauthorized)]
public async Task DeleteLocationAsync(int id)
{
if (ReadOnlyMode)
return BadRequest("سایت به دلایل فنی مثل انتقال سرور موقتاً در حالت فقط خواندنی قرار دارد. لطفاً ساعاتی دیگر مجدداً تلاش کنید.");
var res = await _locationService.DeleteLocationAsync(id);
if (!string.IsNullOrEmpty(res.ExceptionString))
return BadRequest(res.ExceptionString);
return Ok(res.Result);
}
///
/// readonly mode
///
public bool ReadOnlyMode
{
get
{
try
{
return bool.Parse(Configuration["ReadOnlyMode"]);
}
catch
{
return false;
}
}
}
///
/// Configuration
///
protected IConfiguration Configuration { get; }
///
/// location service
///
private readonly IGeoLocationService _locationService;
///
/// constructor
///
///
///
public GeoLocationController(IGeoLocationService locationService, IConfiguration configuration)
{
_locationService = locationService;
Configuration = configuration;
}
}
}