64 lines
2.2 KiB
C#
Raw Normal View History

using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.eShopOnContainers.Services.Locations.API.Infrastructure.Services;
using Microsoft.eShopOnContainers.Services.Locations.API.ViewModel;
using System;
using System.Threading.Tasks;
namespace Locations.API.Controllers
{
[Route("api/v1/[controller]")]
[Authorize]
public class LocationsController : ControllerBase
{
private readonly ILocationsService _locationsService;
private readonly IIdentityService _identityService;
public LocationsController(ILocationsService locationsService, IIdentityService identityService)
{
_locationsService = locationsService ?? throw new ArgumentNullException(nameof(locationsService));
_identityService = identityService ?? throw new ArgumentNullException(nameof(identityService));
}
2017-06-09 12:16:57 +02:00
//GET api/v1/[controller]/user/1
2017-06-13 17:31:37 +02:00
[Route("user/{userId:guid}")]
2017-06-08 17:33:55 +02:00
[HttpGet]
2017-06-13 17:31:37 +02:00
public async Task<IActionResult> GetUserLocation(Guid userId)
2017-06-08 17:33:55 +02:00
{
2017-06-13 17:31:37 +02:00
var userLocation = await _locationsService.GetUserLocation(userId.ToString());
2017-06-08 17:33:55 +02:00
return Ok(userLocation);
}
2017-06-09 12:16:57 +02:00
//GET api/v1/[controller]/
[Route("")]
2017-06-08 17:33:55 +02:00
[HttpGet]
public async Task<IActionResult> GetAllLocations()
{
2017-06-09 12:16:57 +02:00
var locations = await _locationsService.GetAllLocation();
return Ok(locations);
}
//GET api/v1/[controller]/1
[Route("{locationId}")]
[HttpGet]
public async Task<IActionResult> GetLocation(int locationId)
2017-06-09 12:16:57 +02:00
{
var location = await _locationsService.GetLocation(locationId);
return Ok(location);
2017-06-08 17:33:55 +02:00
}
//POST api/v1/[controller]/
[Route("")]
[HttpPost]
2017-06-09 12:16:57 +02:00
public async Task<IActionResult> CreateOrUpdateUserLocation([FromBody]LocationRequest newLocReq)
{
var userId = _identityService.GetUserIdentity();
var result = await _locationsService.AddOrUpdateUserLocation(userId, newLocReq);
2017-06-13 17:31:37 +02:00
return result ?
(IActionResult)Ok() :
(IActionResult)BadRequest();
}
}
}