This repository has been archived by the owner on Sep 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #338 from kysect/feat/seeding-endpoint-321
feat: Moved InitTestEnvironment method from startup to separate controller
- Loading branch information
Showing
6 changed files
with
137 additions
and
35 deletions.
There are no files selected for viewing
16 changes: 16 additions & 0 deletions
16
Source/Application/Kysect.Shreks.Application.Abstractions/Internal/SeedTestData.cs
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 MediatR; | ||
|
||
namespace Kysect.Shreks.Application.Abstractions.Internal; | ||
|
||
public static class SeedTestData | ||
{ | ||
public record Query(string Environment) : IRequest<Unit>; | ||
|
||
public class UserNotAcknowledgedEnvironmentException : InvalidOperationException | ||
{ | ||
public UserNotAcknowledgedEnvironmentException() | ||
: base("You must ensure that is it a right environment to execute SeedTestData command") | ||
{ | ||
} | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
Source/Application/Kysect.Shreks.Application.Handlers/Internal/SeedTestDataHandler.cs
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,74 @@ | ||
using Kysect.Shreks.Core.Study; | ||
using Kysect.Shreks.Core.SubjectCourseAssociations; | ||
using Kysect.Shreks.Core.UserAssociations; | ||
using Kysect.Shreks.Core.Users; | ||
using Kysect.Shreks.DataAccess.Abstractions; | ||
using Kysect.Shreks.Playground.Github.TestEnv; | ||
using Kysect.Shreks.Seeding.EntityGenerators; | ||
using MediatR; | ||
using static Kysect.Shreks.Application.Abstractions.Internal.SeedTestData; | ||
|
||
namespace Kysect.Shreks.Application.Handlers.Internal; | ||
|
||
public class SeedTestDataHandler : IRequestHandler<Query> | ||
{ | ||
private const string ExceptedEnvironment = "Testing"; | ||
|
||
private readonly IShreksDatabaseContext _context; | ||
private readonly IEntityGenerator<User> _userGenerator; | ||
private readonly IEntityGenerator<SubjectCourse> _subjectCourseGenerator; | ||
private readonly TestEnvironmentConfiguration _configuration; | ||
|
||
public SeedTestDataHandler( | ||
IShreksDatabaseContext context, | ||
IEntityGenerator<User> userGenerator, | ||
IEntityGenerator<SubjectCourse> subjectCourseGenerator, | ||
TestEnvironmentConfiguration configuration) | ||
{ | ||
_context = context; | ||
_userGenerator = userGenerator; | ||
_subjectCourseGenerator = subjectCourseGenerator; | ||
_configuration = configuration; | ||
} | ||
|
||
public async Task<Unit> Handle(Query request, CancellationToken cancellationToken = default) | ||
{ | ||
EnsureUserAcknowledgedEnvironment(request); | ||
AddUsers(); | ||
AddGithubUserAssociations(); | ||
|
||
await _context.SaveChangesAsync(cancellationToken); | ||
|
||
return Unit.Value; | ||
} | ||
|
||
private static void EnsureUserAcknowledgedEnvironment(Query request) | ||
{ | ||
if (!request.Environment.Equals(ExceptedEnvironment, StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
throw new UserNotAcknowledgedEnvironmentException(); | ||
} | ||
} | ||
|
||
private void AddGithubUserAssociations() | ||
{ | ||
SubjectCourse subjectCourse = _subjectCourseGenerator.GeneratedEntities[0]; | ||
_context.SubjectCourses.Attach(subjectCourse); | ||
_context.SubjectCourseAssociations.Add( | ||
new GithubSubjectCourseAssociation(subjectCourse, _configuration.Organization, | ||
_configuration.TemplateRepository)); | ||
} | ||
|
||
private void AddUsers() | ||
{ | ||
IReadOnlyList<User> users = _userGenerator.GeneratedEntities; | ||
_context.Users.AttachRange(users); | ||
|
||
for (var index = 0; index < _configuration.Users.Count; index++) | ||
{ | ||
User user = users[index]; | ||
string login = _configuration.Users[index]; | ||
_context.UserAssociations.Add(new GithubUserAssociation(user, login)); | ||
} | ||
} | ||
} |
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.
42 changes: 42 additions & 0 deletions
42
Source/Kysect.Shreks.WebApi/Controllers/InternalController.cs
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,42 @@ | ||
using System.Net; | ||
using Kysect.Shreks.Identity.Entities; | ||
using MediatR; | ||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.Mvc; | ||
using static Kysect.Shreks.Application.Abstractions.Internal.SeedTestData; | ||
|
||
namespace Kysect.Shreks.WebApi.Controllers; | ||
|
||
[ApiController] | ||
[Route("api/internal/")] | ||
[Authorize(Roles = ShreksIdentityRole.AdminRoleName)] | ||
public class InternalController : ControllerBase | ||
{ | ||
private readonly IMediator _mediator; | ||
|
||
public InternalController(IMediator mediator) | ||
{ | ||
_mediator = mediator; | ||
} | ||
|
||
[HttpPost("seed-test-data")] | ||
public async Task<IActionResult> SeedTestData([FromQuery] string environment) | ||
{ | ||
var command = new Query(environment); | ||
try | ||
{ | ||
await _mediator.Send(command); | ||
} | ||
catch (UserNotAcknowledgedEnvironmentException e) | ||
{ | ||
return StatusCode((int) HttpStatusCode.BadRequest, new ProblemDetails | ||
{ | ||
Status = (int) HttpStatusCode.BadRequest, | ||
Title = e.Message, | ||
Detail = "You must put string 'Testing' into environment field if you have 100% ensured that it is not a production environment" | ||
}); | ||
} | ||
|
||
return NoContent(); | ||
} | ||
} |
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