@ -7,7 +7,9 @@ using Microsoft.eShopOnContainers.Services.Basket.API.Model;
using Microsoft.eShopOnContainers.Services.Basket.API.Services ;
using Microsoft.Extensions.Logging ;
using System ;
using System.Linq ;
using System.Net ;
using System.Security.Claims ;
using System.Threading.Tasks ;
namespace Microsoft.eShopOnContainers.Services.Basket.API.Controllers
@ -35,7 +37,7 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API.Controllers
}
[HttpGet("{id}")]
[ProducesResponseType(typeof(CustomerBasket), (int)HttpStatusCode.OK)]
[ProducesResponseType(typeof(CustomerBasket), (int) HttpStatusCode.OK)]
public async Task < ActionResult < CustomerBasket > > GetBasketByIdAsync ( string id )
{
var basket = await _repository . GetBasketAsync ( id ) ;
@ -44,22 +46,24 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API.Controllers
}
[HttpPost]
[ProducesResponseType(typeof(CustomerBasket), (int)HttpStatusCode.OK)]
public async Task < ActionResult < CustomerBasket > > UpdateBasketAsync ( [ FromBody ] CustomerBasket value )
[ProducesResponseType(typeof(CustomerBasket), (int) HttpStatusCode.OK)]
public async Task < ActionResult < CustomerBasket > > UpdateBasketAsync ( [ FromBody ] CustomerBasket value )
{
return Ok ( await _repository . UpdateBasketAsync ( value ) ) ;
}
[Route("checkout")]
[HttpPost]
[ProducesResponseType((int)HttpStatusCode.Accepted)]
[ProducesResponseType((int)HttpStatusCode.BadRequest)]
public async Task < ActionResult > CheckoutAsync ( [ FromBody ] BasketCheckout basketCheckout , [ FromHeader ( Name = "x-requestid" ) ] string requestId )
[ProducesResponseType((int) HttpStatusCode.Accepted)]
[ProducesResponseType((int) HttpStatusCode.BadRequest)]
public async Task < ActionResult > CheckoutAsync ( [ FromBody ] BasketCheckout basketCheckout ,
[FromHeader(Name = "x-requestid")] string requestId )
{
var userId = _identityService . GetUserIdentity ( ) ;
basketCheckout . RequestId = ( Guid . TryParse ( requestId , out Guid guid ) & & guid ! = Guid . Empty ) ?
guid : basketCheckout . RequestId ;
basketCheckout . RequestId = ( Guid . TryParse ( requestId , out Guid guid ) & & guid ! = Guid . Empty )
? guid
: basketCheckout . RequestId ;
var basket = await _repository . GetBasketAsync ( userId ) ;
@ -69,23 +73,32 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API.Controllers
}
var userName = User . FindFirst ( x = > x . Type = = "unique_name" ) . Value ;
var user = HttpContext . User ;
var eventMessage = new UserCheckoutAcceptedIntegrationEvent ( userId , userName , 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 ) ;
var eventMessage = new UserCheckoutAcceptedIntegrationEvent ( userId , userName , 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 ) ;
int tenantId = GetTenantId ( ) ;
eventMessage . TenantId = tenantId ;
// Once basket is checkout, sends an integration event to
// ordering.api to convert basket to order and proceeds with
// order creation process
try
{
_logger . LogInformation ( "----- Publishing integration event: {IntegrationEventId} from {AppName} - ({@IntegrationEvent})" , eventMessage . Id , Program . AppName , eventMessage ) ;
_logger . LogInformation (
"----- Publishing integration event: {IntegrationEventId} from {AppName} - ({@IntegrationEvent})" ,
eventMessage . Id , Program . AppName , eventMessage ) ;
_eventBus . Publish ( eventMessage ) ;
}
catch ( Exception ex )
{
_logger . LogError ( ex , "ERROR Publishing integration event: {IntegrationEventId} from {AppName}" , eventMessage . Id , Program . AppName ) ;
_logger . LogError ( ex , "ERROR Publishing integration event: {IntegrationEventId} from {AppName}" ,
eventMessage . Id , Program . AppName ) ;
throw ;
}
@ -95,10 +108,21 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API.Controllers
// DELETE api/values/5
[HttpDelete("{id}")]
[ProducesResponseType(typeof(void), (int)HttpStatusCode.OK)]
[ProducesResponseType(typeof(void), (int) HttpStatusCode.OK)]
public async Task DeleteBasketByIdAsync ( string id )
{
await _repository . DeleteBasketAsync ( id ) ;
}
private int GetTenantId ( )
{
if ( HttpContext . User is ClaimsPrincipal claims )
{
int tenantId = int . Parse ( claims . Claims . FirstOrDefault ( x = > x . Type = = "tenant_id" ) ? . Value ? ? "0" ) ;
return tenantId ;
}
return 0 ;
}
}
}
}