diff --git a/.env b/.env index f3c6ff9e302190a65a627bbe186d24880f7601bb..28e4f237c3b58cb9062b03189e4c31dbb7d58033 100644 --- a/.env +++ b/.env @@ -10,7 +10,7 @@ DB_USER=user DB_PASSWORD=user DB_CHARSET=utf8mb4 -# Keeping these for dev perposes, will remove at the end +# Keeping these for dev purposes, will remove at the end #DB_PORT=3308 #DB_NAME=AspNetCoreDb #DB_USER=root diff --git a/GatewayAPI/Clients/BookingService/BookingServiceClient.cs b/GatewayAPI/Clients/BookingService/BookingServiceClient.cs new file mode 100644 index 0000000000000000000000000000000000000000..dab63de5d0387d0608ea5710ff98cd180b622299 --- /dev/null +++ b/GatewayAPI/Clients/BookingService/BookingServiceClient.cs @@ -0,0 +1,51 @@ +using GatewayAPI.Models; +using Microsoft.AspNetCore.WebUtilities; + +namespace GatewayAPI.Clients.BookingService +{ + public class BookingServiceClient : IBookingServiceClient + { + private readonly HttpClient httpClient; + private const string API_PATH = "api/Booking"; + + public BookingServiceClient(HttpClient httpClient) + { + this.httpClient = httpClient; + } + + public Task<HttpResponseMessage> GetBookingAsync(int id) + { + return httpClient.GetAsync($"{API_PATH}/{id}"); + } + + public Task<HttpResponseMessage> GetBookingsAsync(int? flightId = null, int? userId = null, int? bookingClass = null) + { + Dictionary<string, string?> queryParams = new Dictionary<string, string?>(); + + if (flightId.HasValue) + queryParams.Add("flightId", flightId.Value.ToString()); + + if (userId.HasValue) + queryParams.Add("userId", userId.Value.ToString()); + + if (bookingClass.HasValue) + queryParams.Add("bookingClass", bookingClass.Value.ToString()); + + string url = QueryHelpers.AddQueryString(API_PATH, queryParams); + + return httpClient.GetAsync(url); + } + + public Task<HttpResponseMessage> MakeBookingAsync(BookingCreation bookingModel) + { + return httpClient.PostAsJsonAsync($"{API_PATH}", bookingModel); + } + + public Task<HttpResponseMessage> UpdateBookingAsync(int bookindId, BookingUpdate bookingModel) + { + return httpClient.PutAsJsonAsync($"{API_PATH}/{bookindId}", bookingModel); + } + + + } +} diff --git a/GatewayAPI/Clients/BookingService/IBookingServiceClient.cs b/GatewayAPI/Clients/BookingService/IBookingServiceClient.cs new file mode 100644 index 0000000000000000000000000000000000000000..9b4d6b244c135dc5e08033893f571618d105ae0f --- /dev/null +++ b/GatewayAPI/Clients/BookingService/IBookingServiceClient.cs @@ -0,0 +1,11 @@ +using GatewayAPI.Models; + +namespace GatewayAPI.Clients.BookingService +{ + public interface IBookingServiceClient + { + Task<HttpResponseMessage> GetBookingAsync(int id); + Task<HttpResponseMessage> GetBookingsAsync(int? flightId = null, int? userId = null, int? bookingClass = null); + Task<HttpResponseMessage> MakeBookingAsync(BookingCreation bookingModel); + Task<HttpResponseMessage> UpdateBookingAsync(int bookindId, BookingUpdate bookingModel); } +} diff --git a/GatewayAPI/Controllers/BookingController.cs b/GatewayAPI/Controllers/BookingController.cs new file mode 100644 index 0000000000000000000000000000000000000000..3828149606d8c81453d89723f06c10c1992bd4e4 --- /dev/null +++ b/GatewayAPI/Controllers/BookingController.cs @@ -0,0 +1,47 @@ +using GatewayAPI.Clients.BookingService; +using GatewayAPI.Models; +using Microsoft.AspNetCore.Mvc; + +namespace GatewayAPI.Controllers +{ + + [ApiController] + [Route("api/[Controller]")] + public class BookingController : ControllerBase + { + private readonly IBookingServiceClient bookingServiceClient; + + public BookingController(IBookingServiceClient bookingServiceClient) + { + this.bookingServiceClient = bookingServiceClient; + } + + [HttpGet("{id}")] + public async Task<IActionResult> GetBooking(int id) + { + HttpResponseMessage response = await bookingServiceClient.GetBookingAsync(id); + return new HttpResponseMessageResult(response); + } + + [HttpGet()] + public async Task<IActionResult> GetBookings(int? flightId = null, int? userId = null, int? bookingClass = null) + { + HttpResponseMessage response = await bookingServiceClient.GetBookingsAsync(flightId, userId, bookingClass); + return new HttpResponseMessageResult(response); + } + + [HttpPost()] + public async Task<IActionResult> MakeBooking([FromBody] BookingCreation bookingCreationModel) + { + HttpResponseMessage response = await bookingServiceClient.MakeBookingAsync(bookingCreationModel); + return new HttpResponseMessageResult(response); + } + + [HttpPut("{id}")] + public async Task<IActionResult> UpdateBooking([FromRoute] int id, [FromBody] BookingUpdate bookingUpdateModel) + { + HttpResponseMessage response = await bookingServiceClient.UpdateBookingAsync(id, bookingUpdateModel); + return new HttpResponseMessageResult(response); + } + } +} diff --git a/GatewayAPI/Models/BookingCreation.cs b/GatewayAPI/Models/BookingCreation.cs new file mode 100644 index 0000000000000000000000000000000000000000..c3134926859c658ace68b0dc5806c7c398270dc8 --- /dev/null +++ b/GatewayAPI/Models/BookingCreation.cs @@ -0,0 +1,10 @@ +namespace GatewayAPI.Models +{ + public class BookingCreation + { + public required int FlightId { get; set; } + public required int BookingClass { get; set; } + public int? SeatId { get; set; } + + } +} diff --git a/GatewayAPI/Models/BookingUpdate.cs b/GatewayAPI/Models/BookingUpdate.cs new file mode 100644 index 0000000000000000000000000000000000000000..e723879bb78c69547b21bb9412ca444603f632b6 --- /dev/null +++ b/GatewayAPI/Models/BookingUpdate.cs @@ -0,0 +1,7 @@ +namespace GatewayAPI.Models +{ + public class BookingUpdate + { + public required int SeatId { get; set; } + } +} diff --git a/GatewayAPI/ServiceCollectionExtensions.cs b/GatewayAPI/ServiceCollectionExtensions.cs index 7235c816dcbe5767cfb5c3ef2ccdce5d583e42c4..25d509a6376142baed07f611fd0fa05134283fee 100644 --- a/GatewayAPI/ServiceCollectionExtensions.cs +++ b/GatewayAPI/ServiceCollectionExtensions.cs @@ -1,4 +1,5 @@ -using GatewayAPI.Clients.FlightService; +using GatewayAPI.Clients.BookingService; +using GatewayAPI.Clients.FlightService; using GatewayAPI.Clients.UserService; using System.Runtime.CompilerServices; @@ -20,6 +21,12 @@ namespace GatewayAPI client.BaseAddress = new Uri(baseUrl.EndsWith("/") ? baseUrl : baseUrl + "/"); }).AddHttpMessageHandler<RequestCookieHandler>(); + services.AddHttpClient<IBookingServiceClient, BookingServiceClient>(client => + { + string baseUrl = configurationManager["BookingMicroservice:BaseUrl"] ?? throw new InvalidOperationException("BookingMicroservice BaseUrl is not configured."); + client.BaseAddress = new Uri(baseUrl.EndsWith("/") ? baseUrl : baseUrl + "/"); + }).AddHttpMessageHandler<RequestCookieHandler>(); + return services; } }