Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
using BookingMicroservice.Clients;
using BookingMicroservice.Exceptions;
using BookingMicroservice.Models;
namespace BookingMicroservice.Services
{
public class ReservationComplianceService : IReservationComplianceService
{
private readonly IBookingService bookingService;
private readonly IFlightServiceClient flightServiceClient;
private const string NO_AVAILABLE_SEAT_MSG = "Unfortunately, there are no {0} seats available for this flight. Please try selecting a different class, flight, or date.";
private const string BOOKED_SEAT_MSG = "The selected seat is already booked. Please choose a different seat";
public ReservationComplianceService(IBookingService bookingService, IFlightServiceClient flightServiceClient)
{
this.bookingService = bookingService;
this.flightServiceClient = flightServiceClient;
}
public async Task<Booking> TryCreateBookingAsync(int flightId, int userId, BookingClass bookingClass, int? seatId)
{
HttpResponseMessage capacityResponse = await flightServiceClient.GetFlightCapacityAsync(flightId, (int)bookingClass);
if (!capacityResponse.IsSuccessStatusCode)
throw new InvalidOperationException("Could not retrieve flight capacity.");
string capacityString = await capacityResponse.Content.ReadAsStringAsync();
if (!int.TryParse(capacityString, out int capacity))
throw new InvalidOperationException("Invalid capacity value received from flight service.");
// Get current bookings for the flight and class
//int currentBookings = bookingService.GetBookingForFlight(flightId).Count(booking => booking.BookingClass == bookingClass);
int currentBookings = bookingService.GetBookings(flightId: flightId, bookingClass: bookingClass).Count();
if (currentBookings >= capacity)
throw new BookingException(string.Format(NO_AVAILABLE_SEAT_MSG, bookingClass.ToString().ToLower()));
return bookingService.CreateBooking(flightId, userId, bookingClass, seatId);
}
public async Task TryBookSeatAsync(int bookingId, int seatId)
{
HttpResponseMessage seatAvailabilityResponse = await flightServiceClient.IsSeatAvailableAsync(seatId);
if (!seatAvailabilityResponse.IsSuccessStatusCode)
throw new InvalidOperationException("Could not retrieve seat availability.");
string seatAvailabilityString = await seatAvailabilityResponse.Content.ReadAsStringAsync();
if (!bool.TryParse(seatAvailabilityString, out bool isSeatAvailable))
throw new InvalidOperationException("Invalid seat availability value received from flight service.");
if(!isSeatAvailable)
throw new BookingException(BOOKED_SEAT_MSG);
HttpResponseMessage seatBookingRespone = await flightServiceClient.BookSeatAsync(seatId);
string capacityString = await seatBookingRespone.Content.ReadAsStringAsync(); // remove this later
if (!seatAvailabilityResponse.IsSuccessStatusCode)
throw new InvalidOperationException("Error booking seat.");
bookingService.UpdateBooking(bookingId, seatId);
}
}
}