Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/football data integration #2

Open
wants to merge 23 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
b5e3088
added api integration
KiansGithub Jan 25, 2025
9e92cb2
fixed Policy error on polly library
KiansGithub Jan 25, 2025
0c0f75c
fixed spelling error on api
KiansGithub Jan 25, 2025
09cc182
fixed spelling error on api
KiansGithub Jan 25, 2025
61c4aac
added stronger typing for return values rather than using dynamic
KiansGithub Jan 25, 2025
91453c7
changed getByName to getById
KiansGithub Jan 25, 2025
c075a39
refined api and removed redundant endpoints
KiansGithub Jan 25, 2025
774ef66
created interfaces for models following SOLID principles
KiansGithub Jan 25, 2025
cf5970b
added IScore interface
KiansGithub Jan 25, 2025
dad3e61
updated models to use interface
KiansGithub Jan 25, 2025
383baa0
adjust interfaces to use ICompetition
KiansGithub Jan 25, 2025
e0e6d5b
adding unit tests
KiansGithub Jan 25, 2025
8838308
added unit tests for controller and service
KiansGithub Jan 25, 2025
c3bf33e
added unit tests #2
KiansGithub Jan 26, 2025
c677b55
refining documentation
KiansGithub Jan 27, 2025
0431ad3
added more fields to competition model
KiansGithub Jan 27, 2025
2580513
added GetCompetition to controller
KiansGithub Jan 27, 2025
3c4a076
removed nullable ? from GetCompetition Async
KiansGithub Jan 27, 2025
99eacfb
added tests for get competition
KiansGithub Jan 27, 2025
730a62c
fixed synstax issues in FootballApiServiceTests.cs
KiansGithub Jan 27, 2025
e6c223a
added getTopScorers integration
KiansGithub Jan 27, 2025
7cb6bec
added testing for getCompetition and getTopScorers controller
KiansGithub Jan 27, 2025
c10f4d8
final commit
KiansGithub Jan 27, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
157 changes: 140 additions & 17 deletions interview-integrationstask/Controllers/FootballController.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.AspNetCore.Mvc;
using interview_integrationstask.Services;

namespace interview_integrationstask.Controllers
{
Expand All @@ -7,37 +8,159 @@ namespace interview_integrationstask.Controllers
[Route("api/[controller]")]
public class FootballController: ControllerBase
{
private readonly IFootballApiService _footballApiService;
private readonly ILogger<FootballController> _logger;

[HttpGet("teams")]
public IActionResult GetTeams()
public FootballController(IFootballApiService footballApiService, ILogger<FootballController> logger)
{
return Ok("Teams:");
}
_footballApiService = footballApiService;
_logger = logger;
}

[HttpGet("teams/{name}")]
public IActionResult GetTeamByName()
/// <summary>
/// Retrieves information about a specific team by Id
/// </summary>
/// <param name="id">The id of the team to retrieve</param>
[HttpGet("teams/{id}")]
public async Task<IActionResult> GetTeamById([FromRoute] int id)
{
return Ok("Team:");
try
{
var team = await _footballApiService.GetTeamsByIdAsync(id);
return Ok(team);
}
catch (KeyNotFoundException)
{
return NotFound($"Team '{id}' not found");
}
catch (RateLimitRejectedException ex)
{
return StatusCode(StatusCodes.Status429TooManyRequests, ex.Message);
}
catch (Exception ex)
{
_logger.LogError(ex, "Error retrieving team {TeamName}", id);
return StatusCode(StatusCodes.Status500InternalServerError, "An error occurred while retrieving team information");
}
}

[HttpGet("league/{name}")]
public IActionResult GetLeagueByName()
/// <summary>
/// Retrieves information about the scores of a specific team by Id
/// </summary>
/// <param name="id">The id of the team to retrieve</param>
/// <param name="dateFrom">Start date (yyyy-MM-dd). Defaults to 30 days ago if not provided.</param>
/// <param name="dateTo"> End date (yyyy-MM-dd). Defaults to today if not provided.</param>
[HttpGet("scores")]
public async Task<IActionResult> GetScores([FromQuery] int? teamId = null, [FromQuery] string? dateFrom = null, [FromQuery] string? dateTo = null)
{
return Ok("League:");
try
{
// Default dateFrom to 30 days ago if not provided
dateFrom ??= DateTime.UtcNow.AddDays(-30).ToString("yyyy-MM-dd");

// Default dateTo to today if not provided
dateTo ??= DateTime.UtcNow.ToString("yyyy-MM-dd");


_logger.LogInformation($"Retrieving scores for teamId: {teamId ?? 0}, dateFrom: {dateFrom}, dateTo: {dateTo}");

// Validate that teamId is provided if required
if (!teamId.HasValue)
{
return BadRequest("Team ID must be provided.");
}

// Call the service with the provided parameters
var scores = await _footballApiService.GetScoresAsync(teamId.Value, dateFrom, dateTo);

return Ok(scores);
}
catch (RateLimitRejectedException ex)
{
return StatusCode(StatusCodes.Status429TooManyRequests, ex.Message);
}
catch (Exception ex)
{
_logger.LogError(ex, "Error retrieving scores for teamId {TeamId} from {DateFrom} to {DateTo}", teamId, dateFrom, dateTo);
return StatusCode(StatusCodes.Status500InternalServerError, "An error occurred while retrieving scores");
}
}

[HttpGet("fixtures")]
public IActionResult GetFixtures()
/// <summary>
/// Retrieves information about a specific competition by code
/// </summary>
/// <param name="code">The competition code (e.g., "PL" for Premier League)</param>
/// <returns>Detailed competition information</returns>
[HttpGet("competitions/{code}")]
public async Task<IActionResult> GetCompetition([FromRoute] string code)
{
return Ok("Fixtures:");
try
{
_logger.LogInformation("Retrieving competition information for {CompetitionCode}", code);

// Call the service to get competition details
var competition = await _footballApiService.GetCompetitionAsync(code);

if (competition == null)
{
return NotFound($"Competition with code '{code}' not found.");
}

return Ok(competition);
}
catch (KeyNotFoundException)
{
return NotFound($"Competition with code '{code}' not found.");
}
catch (RateLimitRejectedException ex)
{
return StatusCode(StatusCodes.Status429TooManyRequests, ex.Message);
}
catch (Exception ex)
{
_logger.LogError(ex, "Error retrieving competition {CompetitionCode}", code);
return StatusCode(StatusCodes.Status500InternalServerError, "An error occurred while retrieving competition information.");
}
}

[HttpGet("scores")]
public IActionResult GetScores()
/// <summary>
/// Retrieves the top scorers for a specific competition by code.
/// </summary>
/// <param name="competitionCode">The competition code (e.g., "PL" for Premier League)</param>
/// <returns>A list of the top scorers for the specific competition.</returns>
[HttpGet("competitions/{competitionCode}/scorers")]
public async Task<IActionResult> GetTopScorers([FromRoute] string competitionCode)
{
return Ok("Scores:");
}
try
{
_logger.LogInformation(
"Retrieving top scorers for competition: {CompetitionCode}",
competitionCode);

// Call the service to get the top scorers
var topScorers = await _footballApiService.GetTopScorersAsync(competitionCode);

if (topScorers == null || topScorers.Scorers == null || !topScorers.Scorers.Any())
{
return NotFound($"No top scorers found for competition '{competitionCode}'.");
}

return Ok(topScorers);
}
catch (KeyNotFoundException)
{
return NotFound($"Competition '{competitionCode}' not found." );
}
catch (RateLimitRejectedException ex)
{
return StatusCode(StatusCodes.Status429TooManyRequests, ex.Message);
}
catch (Exception ex)
{
_logger.LogError(ex, "Error retrieving top scorers for competition {CompetitionCode}", competitionCode);
return StatusCode(StatusCodes.Status500InternalServerError, "An error occurred while retrieving top scorers.");
}
}
}

}
Loading