-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Misc cleanup and add snippets (for fun) * Make site usage report sponsors sort by count descending * minor cleanup * Perf optimizations for site usage report. * Add server-side validation for game start/end, registration start/end, and team size. Resolves #250. * Don't show plaintext host API key in response (and only optionally update during upsert operation).
- Loading branch information
1 parent
6df79a6
commit 8e3c051
Showing
16 changed files
with
156 additions
and
71 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,50 @@ | ||
{ | ||
// see https://code.visualstudio.com/docs/editor/userdefinedsnippets | ||
"Create Gameboard Unit Test Suite": { | ||
"scope": "csharp", | ||
"description": "Create a Gameboard unit test suite", | ||
"prefix": "test-suite-unit", | ||
"body": [ | ||
"namespace Gameboard.Api.Tests.Unit;", | ||
"", | ||
"public class ${TM_FILENAME/\\.cs//g}", | ||
"{", | ||
"\t$0", | ||
"}" | ||
] | ||
}, | ||
"Create Gameboard Unit Test": { | ||
"scope": "csharp", | ||
"description": "Start a new Gameboard unit test", | ||
"prefix": "test-unit", | ||
"isFileTemplate": true, | ||
"body": [ | ||
"[${0:Theory}, ${1:GameboardAutoData}]", | ||
"public async Task ${TM_FILENAME/Tests\\.cs//g}_$2_$3(IFixture fixture)", | ||
"{", | ||
"\t\/\/ given", | ||
"\t$4", | ||
"\t\/\/ when", | ||
"\t\/\/ var sut = new ${TM_FILENAME/Tests\\.cs//g}(...)", | ||
"", | ||
"\t\/\/ then", | ||
"}" | ||
] | ||
// }, | ||
"Create Gameboard Integration Test Suite": { | ||
"scope": "csharp", | ||
"description": "Create a Gameboard integration test suite", | ||
"prefix": "test-suite-int", | ||
"body": [ | ||
"namespace Gameboard.Api.Tests.Integration;", | ||
"", | ||
"public class ${0:Some}ControllerTests : IClassFixture<GameboardTestContext>", | ||
"{", | ||
"\tprivate readonly GameboardTestContext _testContext;", | ||
"", | ||
"\tpublic ${0:Some}ControllerTests(GameboardTestContext testContext", | ||
"\t\t=> _testContext = testContext;", | ||
"}" | ||
] | ||
} | ||
} |
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
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
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
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 |
---|---|---|
@@ -1,45 +1,61 @@ | ||
// Copyright 2021 Carnegie Mellon University. All Rights Reserved. | ||
// Released under a MIT (SEI)-style license. See LICENSE.md in the project root for license information. | ||
|
||
using System; | ||
using System.Threading.Tasks; | ||
using Gameboard.Api.Data.Abstractions; | ||
using Gameboard.Api.Data; | ||
using Gameboard.Api.Features.Games; | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
namespace Gameboard.Api.Validators | ||
namespace Gameboard.Api.Validators; | ||
|
||
public class GameValidator : IModelValidator | ||
{ | ||
public class GameValidator : IModelValidator | ||
private readonly IStore _store; | ||
|
||
public GameValidator(IStore store) | ||
{ | ||
_store = store; | ||
} | ||
|
||
public Task Validate(object model) | ||
{ | ||
if (model is Entity) | ||
return _validate(model as Entity); | ||
|
||
if (model is ChangedGame) | ||
return _validate(model as ChangedGame); | ||
|
||
throw new ValidationTypeFailure<GameValidator>(model.GetType()); | ||
} | ||
|
||
private Task _validate(ChangedGame game) | ||
{ | ||
private readonly IGameStore _store; | ||
|
||
public GameValidator( | ||
IGameStore store | ||
) | ||
{ | ||
_store = store; | ||
} | ||
|
||
public Task Validate(object model) | ||
{ | ||
if (model is Entity) | ||
return _validate(model as Entity); | ||
|
||
throw new ValidationTypeFailure<GameValidator>(model.GetType()); | ||
} | ||
|
||
private async Task _validate(Entity model) | ||
{ | ||
if ((await Exists(model.Id)).Equals(false)) | ||
throw new ResourceNotFound<Data.Game>(model.Id); | ||
|
||
await Task.CompletedTask; | ||
} | ||
|
||
private async Task<bool> Exists(string id) | ||
{ | ||
return | ||
id.NotEmpty() && | ||
(await _store.Retrieve(id)) is not null | ||
; | ||
} | ||
if (game.MinTeamSize > game.MaxTeamSize) | ||
throw new InvalidTeamSize(game.Id, game.Name, game.MinTeamSize, game.MaxTeamSize); | ||
|
||
if (game.GameStart.IsNotEmpty() && game.GameEnd.IsNotEmpty() && game.GameStart > game.GameEnd) | ||
throw new InvalidDateRange(new DateRange(game.GameStart, game.GameEnd)); | ||
|
||
if (game.RegistrationType == GameRegistrationType.Open && game.RegistrationOpen > game.RegistrationClose) | ||
throw new InvalidDateRange(new DateRange(game.RegistrationOpen, game.RegistrationClose)); | ||
|
||
return Task.CompletedTask; | ||
} | ||
|
||
private async Task _validate(Entity model) | ||
{ | ||
if ((await Exists(model.Id)).Equals(false)) | ||
throw new ResourceNotFound<Data.Game>(model.Id); | ||
|
||
await Task.CompletedTask; | ||
} | ||
|
||
private async Task<bool> Exists(string id) | ||
{ | ||
return id.IsNotEmpty() && await _store | ||
.WithNoTracking<Data.Game>() | ||
.AnyAsync(g => g.Id == id); | ||
} | ||
|
||
} |
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