-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from Redacted-Team/shahan-hensley-sprint-4
Scrum 217 & Possible Others Defining and routing the endpoints from the microservice through the API gateway to the main site on the gateway.
- Loading branch information
Showing
4 changed files
with
69 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,70 +1,85 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Net.Http; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.Extensions.Logging; | ||
|
||
using Microsoft.Extensions.Caching.Distributed; | ||
using System.Text.Json; | ||
using System.Net.Http.Json; | ||
|
||
namespace Gateway | ||
{ | ||
/// <summary> | ||
/// Controller responsible for handling requests and responses at the gateway. | ||
/// </summary> | ||
[ApiController] | ||
[Route("[controller]")] | ||
public class GatewayController : ControllerBase | ||
{ | ||
private static readonly List<GameInfo> TheInfo = new List<GameInfo> | ||
{ | ||
new GameInfo { | ||
//Id = 1, | ||
Title = "Snake", | ||
//Content = "~/js/snake.js", | ||
Author = "Fall 2023 Semester", | ||
DateAdded = "", | ||
Description = "Snake is a classic arcade game that challenges the player to control a snake-like creature that grows longer as it eats apples. The player must avoid hitting the walls or the snake's own body, which can end the game.\r\n", | ||
HowTo = "Control with arrow keys.", | ||
//Thumbnail = "/images/snake.jpg" //640x360 resolution | ||
LeaderBoardStack = new Stack<KeyValuePair<string, int>>(), | ||
|
||
}, | ||
new GameInfo { | ||
//Id = 2, | ||
Title = "Tetris", | ||
//Content = "~/js/tetris.js", | ||
Author = "Fall 2023 Semester", | ||
DateAdded = "", | ||
Description = "Tetris is a classic arcade puzzle game where the player has to arrange falling blocks, also known as Tetronimos, of different shapes and colors to form complete rows on the bottom of the screen. The game gets faster and harder as the player progresses, and ends when the Tetronimos reach the top of the screen.", | ||
HowTo = "Control with arrow keys: Up arrow to spin, down to speed up fall, space to insta-drop.", | ||
//Thumbnail = "/images/tetris.jpg" | ||
LeaderBoardStack = new Stack<KeyValuePair<string, int>>(), | ||
|
||
}, | ||
new GameInfo { | ||
//Id = 3, | ||
Title = "Pong", | ||
//Content = "~/js/pong.js", | ||
Author = "Fall 2023 Semester", | ||
DateAdded = "", | ||
Description = "Pong is a classic arcade game where the player uses a paddle to hit a ball against a computer's paddle. Either party scores when the ball makes it past the opponent's paddle.", | ||
HowTo = "Control with arrow keys.", | ||
//Thumbnail = "/images/pong.jpg" | ||
LeaderBoardStack = new Stack<KeyValuePair<string, int>>(), | ||
}, | ||
|
||
}; | ||
|
||
private readonly ILogger<GatewayController> _logger; | ||
private readonly HttpClient _httpClient; // Making readonly ensures thread safety | ||
private readonly ILogger<GatewayController> _logger; // Making readonly ensures thread safety | ||
|
||
public GatewayController(ILogger<GatewayController> logger) | ||
public GatewayController(HttpClient httpClient, ILogger<GatewayController> logger) | ||
{ | ||
_httpClient = httpClient; | ||
_logger = logger; | ||
} | ||
|
||
/// <summary> | ||
/// Handles GET requests to retrieve game information from the microservice. | ||
/// </summary> | ||
/// <returns>A collection of GameInfo objects.</returns> | ||
[HttpGet] | ||
public IEnumerable<GameInfo> Get() | ||
public async Task<IEnumerable<GameInfo>> Get() | ||
{ | ||
return TheInfo; | ||
try | ||
{ | ||
// Make a GET request to the microservice's endpoint | ||
HttpResponseMessage response = await _httpClient.GetAsync("https://localhost:7223/Micro"); // URL might need to change for deployment | ||
|
||
// Check if the request was successful | ||
if (response.IsSuccessStatusCode) | ||
{ | ||
// Deserialize the response content to a list of GameInfo objects | ||
var gameInfoList = await response.Content.ReadAsAsync<List<GameInfo>>(); | ||
return gameInfoList; | ||
} | ||
else | ||
{ | ||
_logger.LogError($"Failed to retrieve data from microservice. Status code: {response.StatusCode}"); | ||
// Return a placeholder list of GameInfo objects indicating failure | ||
return GenerateFailureResponse(); | ||
} | ||
} | ||
catch (Exception ex) | ||
{ | ||
_logger.LogError($"An error occurred while fetching data from microservice: {ex.Message}"); | ||
// Return a placeholder list of GameInfo objects indicating failure | ||
return GenerateFailureResponse(); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Generates a placeholder list of GameInfo objects indicating failure to retrieve data. | ||
/// </summary> | ||
/// <returns>A collection of GameInfo objects indicating failure.</returns> | ||
private IEnumerable<GameInfo> GenerateFailureResponse() | ||
{ | ||
// Using IEnumerable allows flexibility in returning a placeholder response. | ||
// It allows the method to return different types of collections (e.g., List, Array) if needed in the future. | ||
// In this case, IEnumerable provides a simple way to return a list of failed responses. | ||
|
||
// Generate a placeholder list of GameInfo objects indicating failure to retrieve data | ||
return new List<GameInfo> | ||
{ | ||
new GameInfo | ||
{ | ||
Title = "Failed to retrieve from Microservice", | ||
Author = "Failed to retrieve from Microservice", | ||
Description = "Failed to retrieve from Microservice", | ||
HowTo = "Failed to retrieve from Microservice", | ||
LeaderBoardStack = new Stack<KeyValuePair<string, int>>() // Initializing an empty stack | ||
} | ||
}; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters