Skip to content

Commit

Permalink
Remove unused testing resources and caching. Update to default constr…
Browse files Browse the repository at this point in the history
…uctors.
  • Loading branch information
sei-bstein committed Sep 16, 2024
1 parent ced3419 commit b28da5c
Show file tree
Hide file tree
Showing 25 changed files with 361 additions and 560 deletions.
20 changes: 7 additions & 13 deletions src/Gameboard.Api/Features/ApiKeys/ApiKeysValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,22 @@

namespace Gameboard.Api.Features.ApiKeys;

public class ApiKeysValidator : IModelValidator
public class ApiKeysValidator(IStore store, INowService now) : IModelValidator
{
private readonly INowService _now;
private readonly IStore _store;

public ApiKeysValidator(IStore store, INowService now)
{
_now = now;
_store = store;
}
private readonly INowService _now = now;
private readonly IStore _store = store;

public Task Validate(object model)
{
if (model is DeleteApiKeyRequest)
return _validate(model as DeleteApiKeyRequest);
return Validate(model as DeleteApiKeyRequest);
if (model is NewApiKey)
return _validate(model as NewApiKey);
return Validate(model as NewApiKey);

throw new ValidationTypeFailure<ApiKeysValidator>(model.GetType());
}

private async Task _validate(NewApiKey model)
private async Task Validate(NewApiKey model)
{
if (string.IsNullOrWhiteSpace(model.Name))
throw new ApiKeyNoName();
Expand All @@ -39,7 +33,7 @@ private async Task _validate(NewApiKey model)
throw new ResourceNotFound<Data.User>(model.UserId);
}

private async Task _validate(DeleteApiKeyRequest request)
private async Task Validate(DeleteApiKeyRequest request)
{
if (!await _store.AnyAsync<ApiKey>(k => k.Id == request.ApiKeyId, CancellationToken.None))
throw new ResourceNotFound<ApiKey>(request.ApiKeyId);
Expand Down
24 changes: 8 additions & 16 deletions src/Gameboard.Api/Features/Certificates/CertificatesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,15 @@ namespace Gameboard.Api.Features.Certificates;

[Authorize]
[Route("/api/user")]
public class CertificatesController : ControllerBase
public class CertificatesController(
IActingUserService actingUser,
IHtmlToImageService htmlToImage,
IMediator mediator
) : ControllerBase
{
private readonly IActingUserService _actingUser;
private readonly IHtmlToImageService _htmlToImage;
private readonly IMediator _mediator;

public CertificatesController
(
IActingUserService actingUser,
IHtmlToImageService htmlToImage,
IMediator mediator
)
{
_actingUser = actingUser;
_htmlToImage = htmlToImage;
_mediator = mediator;
}
private readonly IActingUserService _actingUser = actingUser;
private readonly IHtmlToImageService _htmlToImage = htmlToImage;
private readonly IMediator _mediator = mediator;

[HttpGet]
[Route("{userId}/certificates/practice")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,37 +13,27 @@ namespace Gameboard.Api.Features.Practice;

public record GetPracticeModeCertificateHtmlQuery(string ChallengeSpecId, string CertificateOwnerUserId, User ActingUser) : IRequest<string>;

internal class GetPracticeModeCertificateHtmlHandler : IRequestHandler<GetPracticeModeCertificateHtmlQuery, string>
internal class GetPracticeModeCertificateHtmlHandler(
EntityExistsValidator<GetPracticeModeCertificateHtmlQuery, Data.User> actingUserExists,
EntityExistsValidator<GetPracticeModeCertificateHtmlQuery, Data.User> certificateOwnerExists,
ICertificatesService certificatesService,
CoreOptions coreOptions,
IPracticeService practiceService,
IValidatorService<GetPracticeModeCertificateHtmlQuery> validatorService
) : IRequestHandler<GetPracticeModeCertificateHtmlQuery, string>
{
private readonly ICertificatesService _certificatesService;
private readonly CoreOptions _coreOptions;
private readonly EntityExistsValidator<GetPracticeModeCertificateHtmlQuery, Data.User> _actingUserExists;
private readonly EntityExistsValidator<GetPracticeModeCertificateHtmlQuery, Data.User> _certificateOwnerExists;
private readonly IPracticeService _practiceService;
private readonly IValidatorService<GetPracticeModeCertificateHtmlQuery> _validatorService;

public GetPracticeModeCertificateHtmlHandler
(
EntityExistsValidator<GetPracticeModeCertificateHtmlQuery, Data.User> actingUserExists,
EntityExistsValidator<GetPracticeModeCertificateHtmlQuery, Data.User> certificateOwnerExists,
ICertificatesService certificatesService,
CoreOptions coreOptions,
IPracticeService practiceService,
IValidatorService<GetPracticeModeCertificateHtmlQuery> validatorService
)
{
_actingUserExists = actingUserExists;
_certificateOwnerExists = certificateOwnerExists;
_certificatesService = certificatesService;
_coreOptions = coreOptions;
_practiceService = practiceService;
_validatorService = validatorService;
}
private readonly ICertificatesService _certificatesService = certificatesService;
private readonly CoreOptions _coreOptions = coreOptions;
private readonly EntityExistsValidator<GetPracticeModeCertificateHtmlQuery, Data.User> _actingUserExists = actingUserExists;
private readonly EntityExistsValidator<GetPracticeModeCertificateHtmlQuery, Data.User> _certificateOwnerExists = certificateOwnerExists;
private readonly IPracticeService _practiceService = practiceService;
private readonly IValidatorService<GetPracticeModeCertificateHtmlQuery> _validatorService = validatorService;

public async Task<string> Handle(GetPracticeModeCertificateHtmlQuery request, CancellationToken cancellationToken)
{
var certificate = (await _certificatesService.GetPracticeCertificates(request.CertificateOwnerUserId))
.FirstOrDefault(c => c.Challenge.ChallengeSpecId == request.ChallengeSpecId);
var certificate = (await _certificatesService
.GetPracticeCertificates(request.CertificateOwnerUserId))
.FirstOrDefault(c => c.Challenge.ChallengeSpecId == request.ChallengeSpecId);

await _validatorService
.AddValidator(_actingUserExists.UseProperty(r => r.ActingUser.Id))
Expand Down
31 changes: 0 additions & 31 deletions src/Gameboard.Api/Features/Challenge/Challenge.http

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.Threading;
using System.Threading.Tasks;
using Gameboard.Api.Common;
using Gameboard.Api.Data;
using Gameboard.Api.Structure.MediatR;
using Gameboard.Api.Structure.MediatR.Validators;
Expand All @@ -10,23 +9,15 @@ namespace Gameboard.Api.Features.Challenges;

public record GetChallengeSolutionGuideQuery(string ChallengeId) : IRequest<ChallengeSolutionGuide>;

internal class GetChallengeSolutionGuideHandler : IRequestHandler<GetChallengeSolutionGuideQuery, ChallengeSolutionGuide>
internal class GetChallengeSolutionGuideHandler(
EntityExistsValidator<GetChallengeSolutionGuideQuery, Data.Challenge> challengeExists,
IStore store,
IValidatorService<GetChallengeSolutionGuideQuery> validatorService
) : IRequestHandler<GetChallengeSolutionGuideQuery, ChallengeSolutionGuide>
{
private readonly EntityExistsValidator<GetChallengeSolutionGuideQuery, Data.Challenge> _challengeExists;
private readonly IStore _store;
private readonly IValidatorService<GetChallengeSolutionGuideQuery> _validatorService;

public GetChallengeSolutionGuideHandler
(
EntityExistsValidator<GetChallengeSolutionGuideQuery, Data.Challenge> challengeExists,
IStore store,
IValidatorService<GetChallengeSolutionGuideQuery> validatorService
)
{
_challengeExists = challengeExists;
_store = store;
_validatorService = validatorService;
}
private readonly EntityExistsValidator<GetChallengeSolutionGuideQuery, Data.Challenge> _challengeExists = challengeExists;
private readonly IStore _store = store;
private readonly IValidatorService<GetChallengeSolutionGuideQuery> _validatorService = validatorService;

public async Task<ChallengeSolutionGuide> Handle(GetChallengeSolutionGuideQuery request, CancellationToken cancellationToken)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,9 @@ namespace Gameboard.Api.Features.ChallengeBonuses;

[Route("api")]
[Authorize]
public class ChallengeBonusController : ControllerBase
public class ChallengeBonusController(IMediator mediator) : ControllerBase()
{
private readonly IMediator _mediator;

public ChallengeBonusController(IMediator mediator) : base()
{
_mediator = mediator;
}
private readonly IMediator _mediator = mediator;

[HttpPut("game/{gameId}/bonus/config")]
public Task<GameScoringConfig> ConfigureAutomaticBonusesForGame([FromRoute] string gameId, [FromBody] GameAutomaticBonusesConfig config)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ internal class ConfigureGameAutoBonusesHandler(
public async Task<GameScoringConfig> Handle(ConfigureGameAutoBonusesCommand request, CancellationToken cancellationToken)
{
// validate
await _requestValidator.Validate(request, cancellationToken);
await _requestValidator
.Validate(request, cancellationToken);

// and go (with a transaction to maintain atomicity)
var specs = await _store
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public async Task Validate(ConfigureGameAutoBonusesCommand request, Cancellation
async (request, context) =>
{
var bonusesAwarded = await _store
.WithNoTracking<Data.AwardedChallengeBonus>()
.WithNoTracking<AwardedChallengeBonus>()
.Include(b => b.Challenge)
.Where(b => b.Challenge.GameId == request.Parameters.GameId)
.Select(b => b.Id)
Expand Down
27 changes: 13 additions & 14 deletions src/Gameboard.Api/Features/ChallengeGate/ChallengeGateController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,15 @@ namespace Gameboard.Api.Controllers;

[Authorize]
public class ChallengeGateController(
IActingUserService actingUserService,
ILogger<ChallengeGateController> logger,
IDistributedCache cache,
ChallengeGateValidator validator,
ChallengeGateService challengeGateService,
IUserRolePermissionsService permissionsService
) : GameboardLegacyController(actingUserService, logger, cache, validator)
IActingUserService actingUserService,
ILogger<ChallengeGateController> logger,
IDistributedCache cache,
ChallengeGateValidator validator,
ChallengeGateService challengeGateService,
IUserRolePermissionsService permissionsService
) : GameboardLegacyController(actingUserService, logger, cache, validator)
{
ChallengeGateService ChallengeGateService { get; } = challengeGateService;

private readonly ChallengeGateService _challengeGateService = challengeGateService;
private readonly IUserRolePermissionsService _permissionsService = permissionsService;

/// <summary>
Expand All @@ -40,7 +39,7 @@ public async Task<ChallengeGate> Create([FromBody] NewChallengeGate model)

await Validate(model);

return await ChallengeGateService.AddOrUpdate(model);
return await _challengeGateService.AddOrUpdate(model);
}

/// <summary>
Expand All @@ -54,7 +53,7 @@ public async Task<ChallengeGate> Retrieve([FromRoute] string id)
if (!await _permissionsService.Can(PermissionKey.Games_CreateEditDelete))
throw new ActionForbidden();

return await ChallengeGateService.Retrieve(id);
return await _challengeGateService.Retrieve(id);
}

/// <summary>
Expand All @@ -68,7 +67,7 @@ public async Task Update([FromBody] ChangedChallengeGate model)
if (!await _permissionsService.Can(PermissionKey.Games_CreateEditDelete))
throw new ActionForbidden();

await ChallengeGateService.Update(model);
await _challengeGateService.Update(model);
return;
}

Expand All @@ -83,7 +82,7 @@ public async Task Delete([FromRoute] string id)
if (!await _permissionsService.Can(PermissionKey.Games_CreateEditDelete))
throw new ActionForbidden();

await ChallengeGateService.Delete(id);
await _challengeGateService.Delete(id);
return;
}

Expand All @@ -98,6 +97,6 @@ public async Task<ChallengeGate[]> List([FromQuery] string g)
if (!await _permissionsService.Can(PermissionKey.Games_CreateEditDelete))
throw new ActionForbidden();

return await ChallengeGateService.List(g);
return await _challengeGateService.List(g);
}
}
31 changes: 0 additions & 31 deletions src/Gameboard.Api/Features/ChallengeSpec/ChallengeSpec.http

This file was deleted.

Loading

0 comments on commit b28da5c

Please sign in to comment.