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;
|
2016-11-29 15:10:16 +01:00
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
2017-05-03 16:34:41 +02:00
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions;
|
|
|
|
|
using Basket.API.IntegrationEvents.Events;
|
|
|
|
|
using Microsoft.eShopOnContainers.Services.Basket.API.Services;
|
2017-05-11 11:51:13 +02:00
|
|
|
|
using Basket.API.Model;
|
2016-10-17 20:10:18 -07:00
|
|
|
|
|
|
|
|
|
namespace Microsoft.eShopOnContainers.Services.Basket.API.Controllers
|
|
|
|
|
{
|
|
|
|
|
[Route("/")]
|
2016-11-29 15:10:16 +01:00
|
|
|
|
[Authorize]
|
2016-10-17 20:10:18 -07:00
|
|
|
|
public class BasketController : Controller
|
|
|
|
|
{
|
2017-05-03 16:34:41 +02:00
|
|
|
|
private readonly IBasketRepository _repository;
|
|
|
|
|
private readonly IIdentityService _identitySvc;
|
|
|
|
|
private readonly IEventBus _eventBus;
|
2016-10-17 20:10:18 -07:00
|
|
|
|
|
2017-05-03 16:34:41 +02:00
|
|
|
|
public BasketController(IBasketRepository repository,
|
|
|
|
|
IIdentityService identityService,
|
|
|
|
|
IEventBus eventBus)
|
2016-10-17 20:10:18 -07:00
|
|
|
|
{
|
|
|
|
|
_repository = repository;
|
2017-05-03 16:34:41 +02:00
|
|
|
|
_identitySvc = identityService;
|
|
|
|
|
_eventBus = eventBus;
|
2016-10-17 20:10:18 -07:00
|
|
|
|
}
|
2017-05-22 15:56:22 -07:00
|
|
|
|
// GET /id
|
2016-10-17 20:10:18 -07:00
|
|
|
|
[HttpGet("{id}")]
|
2016-11-16 10:19:00 +01:00
|
|
|
|
public async Task<IActionResult> Get(string id)
|
2016-10-17 20:10:18 -07:00
|
|
|
|
{
|
2017-03-20 16:12:11 +01:00
|
|
|
|
var basket = await _repository.GetBasketAsync(id);
|
2016-11-16 10:19:00 +01:00
|
|
|
|
|
|
|
|
|
return Ok(basket);
|
2016-10-17 20:10:18 -07:00
|
|
|
|
}
|
|
|
|
|
|
2017-05-22 15:56:22 -07:00
|
|
|
|
// POST /value
|
2016-10-17 20:10:18 -07:00
|
|
|
|
[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
|
|
|
|
{
|
2017-03-20 16:12:11 +01: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-11 11:51:13 +02:00
|
|
|
|
[Route("checkout")]
|
2017-05-16 09:23:35 +02:00
|
|
|
|
[HttpPost]
|
2017-05-18 09:43:37 +02:00
|
|
|
|
public async Task<IActionResult> Checkout([FromBody]BasketCheckout basketCheckout, [FromHeader(Name = "x-requestid")] string requestId)
|
2017-05-03 16:34:41 +02:00
|
|
|
|
{
|
|
|
|
|
var userId = _identitySvc.GetUserIdentity();
|
2017-05-18 09:43:37 +02:00
|
|
|
|
basketCheckout.RequestId = (Guid.TryParse(requestId, out Guid guid) && guid != Guid.Empty) ?
|
|
|
|
|
guid : basketCheckout.RequestId;
|
2017-05-16 09:23:35 +02:00
|
|
|
|
|
2017-05-03 16:34:41 +02:00
|
|
|
|
var basket = await _repository.GetBasketAsync(userId);
|
2017-05-18 09:43:37 +02:00
|
|
|
|
var eventMessage = new UserCheckoutAcceptedIntegrationEvent(userId, basketCheckout.City, basketCheckout.Street,
|
|
|
|
|
basketCheckout.State, basketCheckout.Country, basketCheckout.ZipCode, basketCheckout.CardNumber, basketCheckout.CardHolderName,
|
|
|
|
|
basketCheckout.CardExpiration, basketCheckout.CardSecurityNumber, basketCheckout.CardTypeId, basketCheckout.Buyer, basketCheckout.RequestId, basket);
|
2017-05-11 11:51:13 +02:00
|
|
|
|
|
2017-05-14 14:48:37 +02:00
|
|
|
|
// Once basket is checkout, sends an integration event to
|
|
|
|
|
// ordering.api to convert basket to order and proceeds with
|
|
|
|
|
// order creation process
|
2017-05-11 11:51:13 +02:00
|
|
|
|
_eventBus.Publish(eventMessage);
|
|
|
|
|
|
2017-05-03 16:34:41 +02:00
|
|
|
|
if (basket == null)
|
|
|
|
|
{
|
|
|
|
|
return BadRequest();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Accepted();
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-17 20:10:18 -07:00
|
|
|
|
// DELETE api/values/5
|
|
|
|
|
[HttpDelete("{id}")]
|
2016-11-16 10:19:00 +01:00
|
|
|
|
public void Delete(string id)
|
2016-10-17 20:10:18 -07:00
|
|
|
|
{
|
2017-03-20 16:12:11 +01:00
|
|
|
|
_repository.DeleteBasketAsync(id);
|
2016-10-17 20:10:18 -07:00
|
|
|
|
}
|
2017-05-03 16:34:41 +02:00
|
|
|
|
|
2016-10-17 20:10:18 -07:00
|
|
|
|
}
|
|
|
|
|
}
|