コード例 #1
0
        private static async Task _createBooking(Guid requestId, Reservation reservationDetails, HttpRequest originRequest)
        {
            LogUtility.LogWithContext(requestId, "Creating a booking");
            reservationDetails.EndTime = string.Empty;

            var createBookingResponse = await BikesHelper.ReserveBike(requestId, reservationDetails.BikeId, originRequest);

            if (!createBookingResponse.IsSuccessStatusCode)
            {
                LogUtility.LogErrorWithContext(requestId, "Error response from Bikes! ResponseCode: {0}, Content: {1}", createBookingResponse.StatusCode.ToString(), await createBookingResponse.Content.ReadAsStringAsync());
                await _failBooking(requestId, reservationDetails);

                return;
            }

            reservationDetails.State = ReservationStatus.Booked.ToString();
            var result = await MongoHelper.UpdateReservationStateAndEndTime(requestId, reservationDetails);

            if (result.ModifiedCount == 0)
            {
                LogUtility.LogErrorWithContext(requestId, "Reservation not updated! MatchedCount: {0}, ModifiedCount: {1}", result.MatchedCount.ToString(), result.ModifiedCount.ToString());
                await _failBooking(requestId, reservationDetails);

                return;
            }

            LogUtility.LogWithContext(requestId, "Create booking succeeded");
        }
コード例 #2
0
        private static async Task _completeBooking(Guid requestId, Reservation reservationDetails, HttpRequest originRequest)
        {
            LogUtility.LogWithContext(requestId, "Completing a booking");
            reservationDetails.State   = ReservationStatus.Completing.ToString();
            reservationDetails.EndTime = string.Empty;

            var reservationCompletingResult = await MongoHelper.UpdateReservationStateAndEndTime(requestId, reservationDetails);

            if (reservationCompletingResult.ModifiedCount == 0)
            {
                LogUtility.LogErrorWithContext(requestId, "Reservation not updated to 'Completing'! MatchedCount: {0}, ModifiedCount: {1}", reservationCompletingResult.MatchedCount.ToString(), reservationCompletingResult.ModifiedCount.ToString());
                await _failBooking(requestId, reservationDetails);

                return;
            }

            var freeBikeResponse = await BikesHelper.FreeBike(requestId, reservationDetails.BikeId, originRequest);

            if (!freeBikeResponse.IsSuccessStatusCode)
            {
                LogUtility.LogErrorWithContext(requestId, "Error response from Bikes! ResponseCode: {0}, Content: {1}", freeBikeResponse.StatusCode.ToString(), await freeBikeResponse.Content.ReadAsStringAsync());
                await _failBooking(requestId, reservationDetails);

                return;
            }

            reservationDetails.State   = ReservationStatus.Completed.ToString();
            reservationDetails.EndTime = DateTime.UtcNow.ToString(DateTimeFormat);
            var createInvoiceResponse = await BillingHelper.CreateInvoice(requestId, reservationDetails, originRequest);

            if (!createInvoiceResponse.HttpResponse.IsSuccessStatusCode)
            {
                LogUtility.LogErrorWithContext(requestId, "Couldn't create invoice, rolling back!");
                // Rollback
                reservationDetails.EndTime = string.Empty;
                await BikesHelper.ReserveBike(requestId, reservationDetails.BikeId, originRequest);
                await _failBooking(requestId, reservationDetails);

                return;
            }
            reservationDetails.InvoiceId = createInvoiceResponse.InvoiceId;

            var reservationCompletedResult = await MongoHelper.UpdateReservationStateAndEndTime(requestId, reservationDetails);

            if (reservationCompletedResult.ModifiedCount == 0)
            {
                LogUtility.LogErrorWithContext(requestId, "Reservation not updated to 'Completed'! MatchedCount: {0}, ModifiedCount: {1}", reservationCompletedResult.MatchedCount.ToString(), reservationCompletedResult.ModifiedCount.ToString());
                await _failBooking(requestId, reservationDetails);

                return;
            }

            LogUtility.LogWithContext(requestId, "Complete booking succeeded");
        }