forked from leaderboardsgg/leaderboard-backend-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Run Controller and Service (leaderboardsgg#71)
Also moved some stuff around
- Loading branch information
1 parent
fd1e43d
commit 6089632
Showing
14 changed files
with
175 additions
and
35 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
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 |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using LeaderboardBackend.Controllers; | ||
using LeaderboardBackend.Models.Entities; | ||
using LeaderboardBackend.Services; | ||
using LeaderboardBackend.Services.Impl; | ||
using LeaderboardBackend.Test.Helpers; | ||
using Microsoft.AspNetCore.Mvc; | ||
using NUnit.Framework; | ||
using System; | ||
using System.Threading.Tasks; | ||
|
||
namespace LeaderboardBackend.Test.Controllers; | ||
|
||
internal class RunsControllerTest | ||
{ | ||
private RunsController _controller = null!; | ||
|
||
[SetUp] | ||
public void Setup() | ||
{ | ||
RunService service = new RunService(ApplicationContextFactory.CreateNewContext()); | ||
_controller = new RunsController(service); | ||
} | ||
|
||
[Test] | ||
public async Task GetRun_NotFound_WhenNotExist() | ||
{ | ||
ActionResult<Run> response = await _controller.GetRun(new Guid()); | ||
ObjectResultHelpers.AssertResponseNotFound(response); | ||
} | ||
} |
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
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 |
---|---|---|
@@ -0,0 +1,61 @@ | ||
using Microsoft.AspNetCore.Mvc; | ||
using LeaderboardBackend.Models.Entities; | ||
using LeaderboardBackend.Services; | ||
using Microsoft.AspNetCore.Http; | ||
using LeaderboardBackend.Controllers.Annotations; | ||
using LeaderboardBackend.Models.Requests.Run; | ||
|
||
namespace LeaderboardBackend.Controllers | ||
{ | ||
[Route("api/[controller]")] | ||
[ApiController] | ||
public class RunsController : ControllerBase | ||
{ | ||
private readonly IRunService _runService; | ||
|
||
public RunsController(IRunService runService) | ||
{ | ||
_runService = runService; | ||
} | ||
|
||
/// <summary>Gets a Run.</summary> | ||
/// <param name="id">The Run ID. Must parse to a long for this route to be hit.</param> | ||
/// <response code="200">The Run with the provided ID.</response> | ||
/// <response code="404">If no Run is found with the provided ID.</response> | ||
[ApiConventionMethod(typeof(Conventions), | ||
nameof(Conventions.Get))] | ||
[ProducesResponseType(StatusCodes.Status200OK)] | ||
[ProducesResponseType(StatusCodes.Status404NotFound)] | ||
[HttpGet("{id}")] | ||
public async Task<ActionResult<Run>> GetRun(Guid id) | ||
{ | ||
Run? run = await _runService.GetRun(id); | ||
if (run == null) | ||
{ | ||
return NotFound(); | ||
} | ||
|
||
return Ok(run); | ||
} | ||
|
||
/// <summary>Creates a Run.</summary> | ||
[ApiConventionMethod(typeof(Conventions), | ||
nameof(Conventions.Post))] | ||
[ProducesResponseType(StatusCodes.Status201Created)] | ||
[ProducesResponseType(StatusCodes.Status400BadRequest)] | ||
[ProducesDefaultResponseType] | ||
[HttpPost] | ||
public async Task<ActionResult> CreateRun([FromBody] CreateRunRequest request) | ||
{ | ||
Run run = new() | ||
{ | ||
Played = request.Played, | ||
Submitted = request.Submitted, | ||
Status = request.Status, | ||
}; | ||
|
||
await _runService.CreateRun(run); | ||
return CreatedAtAction(nameof(GetRun), new { id = run.Id }, run); | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using LeaderboardBackend.Models.Entities; | ||
using System.ComponentModel.DataAnnotations; | ||
|
||
namespace LeaderboardBackend.Models.Requests.Run; | ||
|
||
public class CreateRunRequest | ||
{ | ||
[Required] | ||
public DateTime Played { get; set; } | ||
|
||
[Required] | ||
public DateTime Submitted { get; set; } | ||
|
||
[Required] | ||
public RunStatus Status { get; set; } | ||
} |
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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using LeaderboardBackend.Models.Entities; | ||
|
||
namespace LeaderboardBackend.Services; | ||
|
||
public interface IRunService | ||
{ | ||
Task<Run?> GetRun(Guid id); | ||
|
||
Task CreateRun(Run run); | ||
} |
File renamed without changes.
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
File renamed without changes.
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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
using LeaderboardBackend.Models.Entities; | ||
|
||
namespace LeaderboardBackend.Services.Impl; | ||
|
||
public class RunService : IRunService | ||
{ | ||
private readonly ApplicationContext _applicationContext; | ||
|
||
public RunService(ApplicationContext applicationContext) | ||
{ | ||
_applicationContext = applicationContext; | ||
} | ||
|
||
public async Task<Run?> GetRun(Guid id) | ||
{ | ||
return await _applicationContext.Runs.FindAsync(id); | ||
} | ||
|
||
public async Task CreateRun(Run run) | ||
{ | ||
_applicationContext.Runs.Add(run); | ||
await _applicationContext.SaveChangesAsync(); | ||
} | ||
} |
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