Skip to content
Snippets Groups Projects
Commit e6fa7b5e authored by Abdelsamad, Mouaz R (UG - SISC)'s avatar Abdelsamad, Mouaz R (UG - SISC)
Browse files

Connect Booking Service with Gateway

parent 0424df9b
No related branches found
No related tags found
No related merge requests found
...@@ -10,7 +10,7 @@ DB_USER=user ...@@ -10,7 +10,7 @@ DB_USER=user
DB_PASSWORD=user DB_PASSWORD=user
DB_CHARSET=utf8mb4 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_PORT=3308
#DB_NAME=AspNetCoreDb #DB_NAME=AspNetCoreDb
#DB_USER=root #DB_USER=root
......
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);
}
}
}
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); }
}
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);
}
}
}
namespace GatewayAPI.Models
{
public class BookingCreation
{
public required int FlightId { get; set; }
public required int BookingClass { get; set; }
public int? SeatId { get; set; }
}
}
namespace GatewayAPI.Models
{
public class BookingUpdate
{
public required int SeatId { get; set; }
}
}
using GatewayAPI.Clients.FlightService; using GatewayAPI.Clients.BookingService;
using GatewayAPI.Clients.FlightService;
using GatewayAPI.Clients.UserService; using GatewayAPI.Clients.UserService;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
...@@ -20,6 +21,12 @@ namespace GatewayAPI ...@@ -20,6 +21,12 @@ namespace GatewayAPI
client.BaseAddress = new Uri(baseUrl.EndsWith("/") ? baseUrl : baseUrl + "/"); client.BaseAddress = new Uri(baseUrl.EndsWith("/") ? baseUrl : baseUrl + "/");
}).AddHttpMessageHandler<RequestCookieHandler>(); }).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; return services;
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment