Example #1
0
        public async Task CreateBooking(int productId, string productName, decimal unitPrice, int quantity)
        {
            var bookingCheckout = new BookingCheckout
            {
                RequestId   = Guid.NewGuid().ToString(),
                ProductId   = productId,
                ProductName = productName,
                UnitPrice   = unitPrice,
                Quantity    = quantity
            };

            var content = new StringContent(JsonConvert.SerializeObject(bookingCheckout), System.Text.Encoding.UTF8, "application/json");

            await _httpClient.PostAsync(_urls.BookingUrl + "/api/v1/bookings/checkout", content);
        }
        public async Task <IActionResult> Checkout([FromBody] BookingCheckout bookingCheckout)
        {
            var userId = _identityService.GetUserIdentity();

            //to do: check unique request id

            var booking = new Booking(bookingCheckout.ProductId, bookingCheckout.ProductName, bookingCheckout.UnitPrice, bookingCheckout.Quantity, userId);

            _bookingRepository.Add(booking);

            await _bookingRepository.SaveChangesAsync();

            var eventMessage = new BookingStartedIntegrationEvent(userId, booking.Id, bookingCheckout.ProductId, bookingCheckout.Quantity);
            await _endpoint.Publish(eventMessage);

            return(Accepted());
        }
        public async Task Checkout(BookingCheckout bookingCheckout, [FromServices] DaprClient dapr, [FromServices] ILogger <BookingsController> logger)
        {
            var bookingId = Guid.NewGuid().ToString();

            logger.LogInformation("Booking {BookingId} created for request {RequestId} with product {ProductId}", bookingId, bookingCheckout.RequestId, bookingCheckout.ProductId);

            var eventMessage = new BookingToCheck
            {
                BookingId = bookingId,
                ProductId = bookingCheckout.ProductId,
                Quantity  = bookingCheckout.Quantity
            };

            await dapr.PublishEventAsync("pubsub", "bookingToCheck", eventMessage);

            logger.LogInformation("Sent bookingToCheck message for booking {BookingId}", bookingId);
        }