* Use global usings * Use file-scoped namespaces * Updates docker images to preview 7 * Created a new migration plan * Included global usings for identity project * Updated docker file to preview version to 7 * Updated dockerfiles * Merged conent from Startup.cs to Program.cs * Removed Starup.cs * Removed unnecessary files * Revert "Removed unnecessary files" This reverts commit 536bddcd96b54673401cedbe802520dce12b3472. * Revert "Removed Starup.cs" This reverts commit 46175d7aa97475d88ec46bce39ed498c7037d924. * Revert "Merged conent from Startup.cs to Program.cs" This reverts commit 2766ea86dfef9220fe3f0c27a37a9a6c18153078. * Removed extra spaces * Updated basket-api project file * Update src/Services/Basket/Basket.API/Grpc/BasketService.cs Co-authored-by: David Pine <david.pine@microsoft.com> * Apply suggestions from code review Co-authored-by: David Pine <david.pine@microsoft.com> * Moved the fully qualified namespace on top * Updated relevant packages in basket.api project * Updated relevant packages in identity.api project Co-authored-by: David Pine <david.pine@microsoft.com>
90 lines
3.1 KiB
C#
90 lines
3.1 KiB
C#
namespace Microsoft.eShopOnContainers.Services.Basket.API.Controllers;
|
|
|
|
[Route("api/v1/[controller]")]
|
|
[Authorize]
|
|
[ApiController]
|
|
public class BasketController : ControllerBase
|
|
{
|
|
private readonly IBasketRepository _repository;
|
|
private readonly IIdentityService _identityService;
|
|
private readonly IEventBus _eventBus;
|
|
private readonly ILogger<BasketController> _logger;
|
|
|
|
public BasketController(
|
|
ILogger<BasketController> logger,
|
|
IBasketRepository repository,
|
|
IIdentityService identityService,
|
|
IEventBus eventBus)
|
|
{
|
|
_logger = logger;
|
|
_repository = repository;
|
|
_identityService = identityService;
|
|
_eventBus = eventBus;
|
|
}
|
|
|
|
[HttpGet("{id}")]
|
|
[ProducesResponseType(typeof(CustomerBasket), (int)HttpStatusCode.OK)]
|
|
public async Task<ActionResult<CustomerBasket>> GetBasketByIdAsync(string id)
|
|
{
|
|
var basket = await _repository.GetBasketAsync(id);
|
|
|
|
return Ok(basket ?? new CustomerBasket(id));
|
|
}
|
|
|
|
[HttpPost]
|
|
[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)
|
|
{
|
|
var userId = _identityService.GetUserIdentity();
|
|
|
|
basketCheckout.RequestId = (Guid.TryParse(requestId, out Guid guid) && guid != Guid.Empty) ?
|
|
guid : basketCheckout.RequestId;
|
|
|
|
var basket = await _repository.GetBasketAsync(userId);
|
|
|
|
if (basket == null)
|
|
{
|
|
return BadRequest();
|
|
}
|
|
|
|
var userName = this.HttpContext.User.FindFirst(x => x.Type == ClaimTypes.Name).Value;
|
|
|
|
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);
|
|
|
|
// Once basket is checkout, sends an integration event to
|
|
// ordering.api to convert basket to order and proceeds with
|
|
// order creation process
|
|
try
|
|
{
|
|
_eventBus.Publish(eventMessage);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "ERROR Publishing integration event: {IntegrationEventId} from {AppName}", eventMessage.Id, Program.AppName);
|
|
|
|
throw;
|
|
}
|
|
|
|
return Accepted();
|
|
}
|
|
|
|
// DELETE api/values/5
|
|
[HttpDelete("{id}")]
|
|
[ProducesResponseType(typeof(void), (int)HttpStatusCode.OK)]
|
|
public async Task DeleteBasketByIdAsync(string id)
|
|
{
|
|
await _repository.DeleteBasketAsync(id);
|
|
}
|
|
}
|