-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFootballController.cs
More file actions
166 lines (146 loc) · 6.68 KB
/
Copy pathFootballController.cs
File metadata and controls
166 lines (146 loc) · 6.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
using Microsoft.AspNetCore.Mvc;
using interview_integrationstask.Services;
namespace interview_integrationstask.Controllers
{
[ApiController]
[Route("api/[controller]")]
public class FootballController: ControllerBase
{
private readonly IFootballApiService _footballApiService;
private readonly ILogger<FootballController> _logger;
public FootballController(IFootballApiService footballApiService, ILogger<FootballController> logger)
{
_footballApiService = footballApiService;
_logger = logger;
}
/// <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)
{
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");
}
}
/// <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)
{
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");
}
}
/// <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)
{
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.");
}
}
/// <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)
{
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.");
}
}
}
}