From 0c741d2a573d14b9c09d352642e264346ba77f92 Mon Sep 17 00:00:00 2001
From: Mouaz Abdelsamad <ma03081@surrey.ac.uk>
Date: Sat, 13 Apr 2024 16:34:56 +0100
Subject: [PATCH] add filter by airline id

---
 FlightMicroservice/Controllers/FlightController.cs       | 4 ++--
 FlightMicroservice/Services/FlightService.cs             | 5 ++++-
 FlightMicroservice/Services/IFlightService.cs            | 2 +-
 GatewayAPI/Clients/FlightService/FlightServiceClient.cs  | 9 ++++++---
 GatewayAPI/Clients/FlightService/IFlightServiceClient.cs | 2 +-
 GatewayAPI/Controllers/FlightController.cs               | 4 ++--
 6 files changed, 16 insertions(+), 10 deletions(-)

diff --git a/FlightMicroservice/Controllers/FlightController.cs b/FlightMicroservice/Controllers/FlightController.cs
index 649e104..2bd55a2 100644
--- a/FlightMicroservice/Controllers/FlightController.cs
+++ b/FlightMicroservice/Controllers/FlightController.cs
@@ -18,9 +18,9 @@ namespace FlightMicroservice.Controllers
         }
 
         [HttpGet()]
-        public ActionResult GetFlights(string? origin = null, string? destination = null, DateTime? departureTime = null, DateTime? arrivalTime = null)
+        public ActionResult GetFlights(int? airlineId = null, string? origin = null, string? destination = null, DateTime? departureTime = null, DateTime? arrivalTime = null)
         {
-            List<Flight> flights = flightService.GetFlights(origin, destination, departureTime, arrivalTime);
+            List<Flight> flights = flightService.GetFlights(airlineId, origin, destination, departureTime, arrivalTime);
             if (flights == null)
                 return BadRequest();
 
diff --git a/FlightMicroservice/Services/FlightService.cs b/FlightMicroservice/Services/FlightService.cs
index 91e30b8..c849e71 100644
--- a/FlightMicroservice/Services/FlightService.cs
+++ b/FlightMicroservice/Services/FlightService.cs
@@ -18,10 +18,13 @@ namespace FlightMicroservice.Services
             return flight;
         }
 
-        public List<Flight> GetFlights(string? origin, string? destination, DateTime? departureTime, DateTime? arrivalTime)
+        public List<Flight> GetFlights(int? airlineId, string? origin, string? destination, DateTime? departureTime, DateTime? arrivalTime)
         {
             IQueryable<Flight> query = dbContext.Flights.AsQueryable();
 
+            if(airlineId != null)
+                query = query.Where(flight => flight.AirlineId == airlineId);
+
             if (!string.IsNullOrWhiteSpace(origin))
                 query = query.Where(flight => flight.Origin == origin);
 
diff --git a/FlightMicroservice/Services/IFlightService.cs b/FlightMicroservice/Services/IFlightService.cs
index f3bed26..4dad71b 100644
--- a/FlightMicroservice/Services/IFlightService.cs
+++ b/FlightMicroservice/Services/IFlightService.cs
@@ -5,7 +5,7 @@ namespace FlightMicroservice.Services
     public interface IFlightService
     {
         Flight? GetFlight(int flightId);
-        List<Flight> GetFlights(string? origin, string? destination, DateTime? departureTime, DateTime? arrivalTime);
+        List<Flight> GetFlights(int? airlineId, string? origin, string? destination, DateTime? departureTime, DateTime? arrivalTime);
         Flight CreateFlight(int airlineId, string origin, string destination, DateTime departureTime, DateTime arrivalTime, int economyCapacity, int businessCapacity, decimal economyPrice, decimal businessPrice);
         bool RemoveFlight(int flightId);
         List<Seat> GetSeatsByFlightId(int flightId);
diff --git a/GatewayAPI/Clients/FlightService/FlightServiceClient.cs b/GatewayAPI/Clients/FlightService/FlightServiceClient.cs
index 82e4ca7..aff15a4 100644
--- a/GatewayAPI/Clients/FlightService/FlightServiceClient.cs
+++ b/GatewayAPI/Clients/FlightService/FlightServiceClient.cs
@@ -18,14 +18,17 @@ namespace GatewayAPI.Clients.FlightService
             return await httpClient.GetAsync($"{FLIGHT_API_PATH}/{flightId}");
         }
 
-        public async Task<HttpResponseMessage> GetFlightsAsync(string? origin = null, string? destination = null, DateTime? departureTime = null, DateTime? arrivalTime = null)
+        public async Task<HttpResponseMessage> GetFlightsAsync(int? airlineId = null, string? origin = null, string? destination = null, DateTime? departureTime = null, DateTime? arrivalTime = null)
         {
             var queryParams = new List<string>();
 
-            if (origin != null)
+            if(airlineId != null)
+                queryParams.Add($"airlineId={airlineId}");
+
+            if (!string.IsNullOrEmpty(origin))
                 queryParams.Add($"origin={Uri.EscapeDataString(origin)}");
 
-            if (destination != null)
+            if (!string.IsNullOrEmpty(destination))
                 queryParams.Add($"destination={Uri.EscapeDataString(destination)}");
 
             if (departureTime.HasValue)
diff --git a/GatewayAPI/Clients/FlightService/IFlightServiceClient.cs b/GatewayAPI/Clients/FlightService/IFlightServiceClient.cs
index ac86b80..70b9e43 100644
--- a/GatewayAPI/Clients/FlightService/IFlightServiceClient.cs
+++ b/GatewayAPI/Clients/FlightService/IFlightServiceClient.cs
@@ -5,7 +5,7 @@ namespace GatewayAPI.Clients.FlightService
     public interface IFlightServiceClient
     {
         Task<HttpResponseMessage> GetFlightAsync(int flightId);
-        Task<HttpResponseMessage> GetFlightsAsync(string? origin = null, string? destination = null, DateTime? departureTime = null, DateTime? arrivalTime = null);
+        Task<HttpResponseMessage> GetFlightsAsync(int? airlineId = null, string? origin = null, string? destination = null, DateTime? departureTime = null, DateTime? arrivalTime = null);
         Task<HttpResponseMessage> AddFlightAsync(FlightCreation flight);
         Task<HttpResponseMessage> GetFlightCapacityAsync(int flightId, int classType);
         Task<HttpResponseMessage> GetFlightSeatsAsync(int flightId);
diff --git a/GatewayAPI/Controllers/FlightController.cs b/GatewayAPI/Controllers/FlightController.cs
index c567683..2166bfa 100644
--- a/GatewayAPI/Controllers/FlightController.cs
+++ b/GatewayAPI/Controllers/FlightController.cs
@@ -16,9 +16,9 @@ namespace GatewayAPI.Controllers
         }
 
         [HttpGet()]
-        public async Task<IActionResult> GetFlights(string? origin = null, string? destination = null, DateTime? departureTime = null, DateTime? arrivalTime = null)
+        public async Task<IActionResult> GetFlights(int? airlineId = null, string? origin = null, string? destination = null, DateTime? departureTime = null, DateTime? arrivalTime = null)
         {
-            HttpResponseMessage response = await flightServiceClient.GetFlightsAsync(origin, destination, departureTime, arrivalTime);
+            HttpResponseMessage response = await flightServiceClient.GetFlightsAsync(airlineId, origin, destination, departureTime, arrivalTime);
             return new HttpResponseMessageResult(response);
         }
 
-- 
GitLab