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

Update Endpoints #241

Merged
merged 3 commits into from
Sep 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
7 changes: 3 additions & 4 deletions LeaderboardBackend.Test/Categories.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using LeaderboardBackend.Models.Entities;
using LeaderboardBackend.Models.Requests;
using LeaderboardBackend.Models.ViewModels;
using LeaderboardBackend.Test.Lib;
using LeaderboardBackend.Test.TestApi;
using LeaderboardBackend.Test.TestApi.Extensions;
using NUnit.Framework;
Expand Down Expand Up @@ -52,7 +51,7 @@ await _apiClient.Get<CategoryViewModel>(
public static async Task CreateCategory_GetCategory_OK()
{
LeaderboardViewModel createdLeaderboard = await _apiClient.Post<LeaderboardViewModel>(
"/api/leaderboards",
"/leaderboards/create",
new()
{
Body = new CreateLeaderboardRequest()
Expand All @@ -66,7 +65,7 @@ public static async Task CreateCategory_GetCategory_OK()
);

CategoryViewModel createdCategory = await _apiClient.Post<CategoryViewModel>(
"/api/categories",
"/categories/create",
new()
{
Body = new CreateCategoryRequest()
Expand All @@ -83,7 +82,7 @@ public static async Task CreateCategory_GetCategory_OK()
);

CategoryViewModel retrievedCategory = await _apiClient.Get<CategoryViewModel>(
$"/api/categories/{createdCategory?.Id}", new() { }
$"/api/category/{createdCategory?.Id}", new() { }
);

Assert.AreEqual(createdCategory, retrievedCategory);
Expand Down
18 changes: 9 additions & 9 deletions LeaderboardBackend.Test/Leaderboards.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void GetLeaderboard_NotFound()
RequestFailureException e = Assert.ThrowsAsync<RequestFailureException>(
async () =>
await _apiClient.Get<LeaderboardViewModel>(
$"/api/leaderboards/{long.MaxValue}",
$"/api/leaderboard/{long.MaxValue}",
new()
)
)!;
Expand All @@ -59,12 +59,12 @@ public async Task CreateLeaderboard_GetLeaderboard_OK()
{
CreateLeaderboardRequest req = _createBoardReqFaker.Generate();
LeaderboardViewModel createdLeaderboard = await _apiClient.Post<LeaderboardViewModel>(
"/api/leaderboards",
"/leaderboards/create",
new() { Body = req, Jwt = _jwt }
);

LeaderboardViewModel retrievedLeaderboard = await _apiClient.Get<LeaderboardViewModel>(
$"/api/leaderboards/{createdLeaderboard?.Id}",
$"/api/leaderboard/{createdLeaderboard?.Id}",
new()
);

Expand All @@ -87,7 +87,7 @@ public async Task CreateLeaderboards_GetLeaderboards()
.Select(
req =>
_apiClient.Post<LeaderboardViewModel>(
"/api/leaderboards",
"/leaderboards/create",
new() { Body = req, Jwt = _jwt }
)
);
Expand All @@ -109,21 +109,21 @@ public async Task GetLeaderboards_BySlug_OK()
{
CreateLeaderboardRequest createReqBody = _createBoardReqFaker.Generate();
LeaderboardViewModel createdLeaderboard = await _apiClient.Post<LeaderboardViewModel>(
"/api/leaderboards",
"/leaderboards/create",
new() { Body = createReqBody, Jwt = _jwt }
);

// create random unrelated boards
foreach (CreateLeaderboardRequest req in _createBoardReqFaker.Generate(2))
{
await _apiClient.Post<LeaderboardViewModel>(
"/api/leaderboards",
"/leaderboards/create",
new() { Body = req, Jwt = _jwt }
);
}

LeaderboardViewModel leaderboard = await _apiClient.Get<LeaderboardViewModel>(
$"api/leaderboards/{createReqBody.Slug}",
$"api/leaderboard?slug={createReqBody.Slug}",
new()
);

Expand All @@ -137,13 +137,13 @@ public async Task GetLeaderboards_BySlug_NotFound()
foreach (CreateLeaderboardRequest req in _createBoardReqFaker.Generate(2))
{
await _apiClient.Post<LeaderboardViewModel>(
"/api/leaderboards",
"/leaderboards/create",
new() { Body = req, Jwt = _jwt }
);
}

CreateLeaderboardRequest reqForInexistentBoard = _createBoardReqFaker.Generate();
Func<Task<string>> act = async () => await _apiClient.Get<string>($"/api/leaderboards/{reqForInexistentBoard.Slug}", new());
Func<Task<string>> act = async () => await _apiClient.Get<string>($"/api/leaderboard?slug={reqForInexistentBoard.Slug}", new());
await act.Should().ThrowAsync<RequestFailureException>().Where(e => e.Response.StatusCode == HttpStatusCode.NotFound);
}

Expand Down
11 changes: 5 additions & 6 deletions LeaderboardBackend.Test/Runs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using LeaderboardBackend.Models.Entities;
using LeaderboardBackend.Models.Requests;
using LeaderboardBackend.Models.ViewModels;
using LeaderboardBackend.Test.Lib;
using LeaderboardBackend.Test.TestApi;
using LeaderboardBackend.Test.TestApi.Extensions;
using NodaTime;
Expand Down Expand Up @@ -35,7 +34,7 @@ public async Task SetUp()
_jwt = (await _apiClient.LoginAdminUser()).Token;

LeaderboardViewModel createdLeaderboard = await _apiClient.Post<LeaderboardViewModel>(
"/api/leaderboards",
"/leaderboards/create",
new()
{
Body = new CreateLeaderboardRequest()
Expand All @@ -49,7 +48,7 @@ public async Task SetUp()
);

CategoryViewModel createdCategory = await _apiClient.Post<CategoryViewModel>(
"/api/categories",
"/categories/create",
new()
{
Body = new CreateCategoryRequest()
Expand Down Expand Up @@ -85,7 +84,7 @@ public static async Task GetCategory_OK()
RunViewModel createdRun = await CreateRun();

CategoryViewModel category = await _apiClient.Get<CategoryViewModel>(
$"api/runs/{createdRun.Id.ToUrlSafeBase64String()}/category",
$"api/run/{createdRun.Id.ToUrlSafeBase64String()}/category",
new() { Jwt = _jwt }
);

Expand All @@ -96,7 +95,7 @@ public static async Task GetCategory_OK()
private static async Task<RunViewModel> CreateRun()
{
return await _apiClient.Post<RunViewModel>(
"/api/runs",
"/runs/create",
new()
{
Body = new CreateRunRequest
Expand All @@ -113,7 +112,7 @@ private static async Task<RunViewModel> CreateRun()

private static async Task<RunViewModel> GetRun(Guid id)
{
return await _apiClient.Get<RunViewModel>($"/api/runs/{id.ToUrlSafeBase64String()}", new() { Jwt = _jwt });
return await _apiClient.Get<RunViewModel>($"/api/run/{id.ToUrlSafeBase64String()}", new() { Jwt = _jwt });
}
}
}
1 change: 0 additions & 1 deletion LeaderboardBackend/Controllers/ApiController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ namespace LeaderboardBackend.Controllers;
[ApiController]
[Consumes("application/json")]
[Produces("application/json")]
[Route("api/[controller]")]
[SwaggerResponse(400, Type = typeof(ProblemDetails))]
[SwaggerResponse(500)]
public abstract class ApiController : ControllerBase { }
4 changes: 2 additions & 2 deletions LeaderboardBackend/Controllers/CategoriesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public CategoriesController(ICategoryService categoryService)
}

[AllowAnonymous]
[HttpGet("{id}")]
[HttpGet("api/category/{id}")]
[SwaggerOperation("Gets a Category by its ID.")]
[SwaggerResponse(200)]
[SwaggerResponse(404)]
Expand All @@ -36,7 +36,7 @@ public async Task<ActionResult<CategoryViewModel>> GetCategory(long id)
}

[Authorize(Policy = UserTypes.ADMINISTRATOR)]
[HttpPost]
[HttpPost("categories/create")]
[SwaggerOperation("Creates a new Category. This request is restricted to Moderators.")]
[SwaggerResponse(201)]
[SwaggerResponse(403)]
Expand Down
10 changes: 5 additions & 5 deletions LeaderboardBackend/Controllers/LeaderboardsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public LeaderboardsController(ILeaderboardService leaderboardService)
}

[AllowAnonymous]
[HttpGet("{id:long}")]
[HttpGet("api/leaderboard/{id:long}")]
[SwaggerOperation("Gets a leaderboard by its ID.")]
[SwaggerResponse(200)]
[SwaggerResponse(404)]
Expand All @@ -36,11 +36,11 @@ public async Task<ActionResult<LeaderboardViewModel>> GetLeaderboard(long id)
}

[AllowAnonymous]
[HttpGet("{slug}")]
[HttpGet("api/leaderboard")]
[SwaggerOperation("Gets a Leaderboard by its slug.")]
[SwaggerResponse(200)]
[SwaggerResponse(404)]
public async Task<ActionResult<LeaderboardViewModel>> GetLeaderboardBySlug(string slug)
public async Task<ActionResult<LeaderboardViewModel>> GetLeaderboardBySlug([FromQuery, SwaggerParameter(Required = true)] string slug)
{
Leaderboard? leaderboard = await _leaderboardService.GetLeaderboardBySlug(slug);

Expand All @@ -53,7 +53,7 @@ public async Task<ActionResult<LeaderboardViewModel>> GetLeaderboardBySlug(strin
}

[AllowAnonymous]
[HttpGet]
[HttpGet("api/leaderboards")]
[SwaggerOperation("Gets leaderboards by their IDs.")]
[SwaggerResponse(200)]
public async Task<ActionResult<List<LeaderboardViewModel>>> GetLeaderboards(
Expand All @@ -65,7 +65,7 @@ [FromQuery] long[] ids
}

[Authorize(Policy = UserTypes.ADMINISTRATOR)]
[HttpPost]
[HttpPost("leaderboards/create")]
[SwaggerOperation("Creates a new leaderboard. This request is restricted to Administrators.")]
[SwaggerResponse(201)]
[SwaggerResponse(401)]
Expand Down
6 changes: 3 additions & 3 deletions LeaderboardBackend/Controllers/RunsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ IUserService userService
) : ApiController
{
[AllowAnonymous]
[HttpGet("{id}")]
[HttpGet("api/run/{id}")]
[SwaggerOperation("Gets a Run by its ID.")]
[SwaggerResponse(200)]
[SwaggerResponse(404)]
Expand All @@ -34,7 +34,7 @@ public async Task<ActionResult<RunViewModel>> GetRun(Guid id)
}

[Authorize]
[HttpPost]
[HttpPost("runs/create")]
[SwaggerOperation("Creates a new Run.")]
[SwaggerResponse(201)]
[SwaggerResponse(401)]
Expand Down Expand Up @@ -63,7 +63,7 @@ public async Task<ActionResult> CreateRun([FromBody] CreateRunRequest request)
return Unauthorized();
}

[HttpGet("{id}/category")]
[HttpGet("/api/run/{id}/category")]
[SwaggerResponse(200)]
[SwaggerResponse(404)]
public async Task<ActionResult<CategoryViewModel>> GetCategoryForRun(Guid id)
Expand Down
4 changes: 2 additions & 2 deletions LeaderboardBackend/Controllers/UsersController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public UsersController(IUserService userService)
}

[AllowAnonymous]
[HttpGet("{id:guid}")]
[HttpGet("api/user/{id:guid}")]
[SwaggerOperation("Gets a User by their ID.")]
[SwaggerResponse(200, "The `User` was found and returned successfully.")]
[SwaggerResponse(404, "No `User` with the requested ID could be found.")]
Expand All @@ -38,7 +38,7 @@ public async Task<ActionResult<UserViewModel>> GetUserById(
}

[Authorize]
[HttpGet("me")]
[HttpGet("user/me")]
[SwaggerOperation(
"Gets the currently logged-in User.",
"""
Expand Down
4 changes: 2 additions & 2 deletions LeaderboardBackend/Services/Impl/LeaderboardService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ public LeaderboardService(ApplicationContext applicationContext)
return await _applicationContext.Leaderboards.FindAsync(id);
}

public Task<Leaderboard?> GetLeaderboardBySlug(string slug)
public async Task<Leaderboard?> GetLeaderboardBySlug(string slug)
{
return _applicationContext.Leaderboards
return await _applicationContext.Leaderboards
.AsNoTracking()
.FirstOrDefaultAsync(x => x.Slug == slug);
}
Expand Down
26 changes: 14 additions & 12 deletions LeaderboardBackend/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@
}
}
},
"/api/Categories/{id}": {
"/api/category/{id}": {
"get": {
"tags": [
"Categories"
Expand Down Expand Up @@ -398,7 +398,7 @@
}
}
},
"/api/Categories": {
"/categories/create": {
"post": {
"tags": [
"Categories"
Expand Down Expand Up @@ -453,7 +453,7 @@
}
}
},
"/api/Leaderboards/{id}": {
"/api/leaderboard/{id}": {
"get": {
"tags": [
"Leaderboards"
Expand Down Expand Up @@ -500,7 +500,7 @@
}
}
},
"/api/Leaderboards/{slug}": {
"/api/leaderboard": {
"get": {
"tags": [
"Leaderboards"
Expand All @@ -509,7 +509,7 @@
"parameters": [
{
"name": "slug",
"in": "path",
"in": "query",
"required": true,
"schema": {
"type": "string"
Expand Down Expand Up @@ -546,7 +546,7 @@
}
}
},
"/api/Leaderboards": {
"/api/leaderboards": {
"get": {
"tags": [
"Leaderboards"
Expand Down Expand Up @@ -593,7 +593,9 @@
}
}
}
},
}
},
"/leaderboards/create": {
"post": {
"tags": [
"Leaderboards"
Expand Down Expand Up @@ -651,7 +653,7 @@
}
}
},
"/api/Runs/{id}": {
"/api/run/{id}": {
"get": {
"tags": [
"Runs"
Expand Down Expand Up @@ -708,7 +710,7 @@
}
}
},
"/api/Runs": {
"/runs/create": {
"post": {
"tags": [
"Runs"
Expand Down Expand Up @@ -759,7 +761,7 @@
}
}
},
"/api/Runs/{id}/category": {
"/api/run/{id}/category": {
"get": {
"tags": [
"Runs"
Expand Down Expand Up @@ -805,7 +807,7 @@
}
}
},
"/api/Users/{id}": {
"/api/user/{id}": {
"get": {
"tags": [
"Users"
Expand Down Expand Up @@ -853,7 +855,7 @@
}
}
},
"/api/Users/me": {
"/user/me": {
"get": {
"tags": [
"Users"
Expand Down