79 lines
2.4 KiB
C#
Raw Normal View History

2016-10-17 20:10:18 -07:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.eShopOnContainers.Services.Basket.API.Model;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions;
using Basket.API.IntegrationEvents.Events;
using Microsoft.eShopOnContainers.Services.Basket.API.Services;
2016-10-17 20:10:18 -07:00
namespace Microsoft.eShopOnContainers.Services.Basket.API.Controllers
{
//TODO NOTE: Right now this is a very chunky API, as the app evolves it is possible we would
//want to make the actions more fine grained, add basket item as an action for example.
2016-10-17 20:10:18 -07:00
//If this is the case we should also investigate changing the serialization format used for Redis,
//using a HashSet instead of a simple string.
[Route("/")]
[Authorize]
2016-10-17 20:10:18 -07:00
public class BasketController : Controller
{
private readonly IBasketRepository _repository;
private readonly IIdentityService _identitySvc;
private readonly IEventBus _eventBus;
2016-10-17 20:10:18 -07:00
public BasketController(IBasketRepository repository,
IIdentityService identityService,
IEventBus eventBus)
2016-10-17 20:10:18 -07:00
{
_repository = repository;
_identitySvc = identityService;
_eventBus = eventBus;
2016-10-17 20:10:18 -07:00
}
// GET api/values/5
[HttpGet("{id}")]
public async Task<IActionResult> Get(string id)
2016-10-17 20:10:18 -07:00
{
var basket = await _repository.GetBasketAsync(id);
return Ok(basket);
2016-10-17 20:10:18 -07:00
}
// POST api/values
[HttpPost]
2016-11-24 15:31:33 +01:00
public async Task<IActionResult> Post([FromBody]CustomerBasket value)
2016-10-17 20:10:18 -07:00
{
var basket = await _repository.UpdateBasketAsync(value);
2016-11-24 15:31:33 +01:00
return Ok(basket);
2016-10-17 20:10:18 -07:00
}
2017-05-08 13:36:31 +02:00
[Route("checkouts")]
[HttpPost]
public async Task<IActionResult> Checkout()
{
var userId = _identitySvc.GetUserIdentity();
var basket = await _repository.GetBasketAsync(userId);
_eventBus.Publish(new UserCheckoutAccepted(userId, basket));
if (basket == null)
{
return BadRequest();
}
return Accepted();
}
2016-10-17 20:10:18 -07:00
// DELETE api/values/5
[HttpDelete("{id}")]
public void Delete(string id)
2016-10-17 20:10:18 -07:00
{
_repository.DeleteBasketAsync(id);
2016-10-17 20:10:18 -07:00
}
2016-10-17 20:10:18 -07:00
}
}