From d050180bf903aebc946630d7daef685218dbeb15 Mon Sep 17 00:00:00 2001 From: Roman Makarevich Date: Tue, 6 Sep 2022 00:01:48 +0300 Subject: [PATCH 1/4] feat: Moved InitTestEnvironment method from startup to separate controller --- .../Internal/SeedTestData.cs | 14 ++++ .../Internal/SeedTestDataHandler.cs | 66 +++++++++++++++++++ .../Kysect.Shreks.Application.Handlers.csproj | 2 + ...rserProfile.cs => SubjectCourseProfile.cs} | 0 .../Controllers/InternalController.cs | 37 +++++++++++ Source/Kysect.Shreks.WebApi/Program.cs | 36 +--------- 6 files changed, 122 insertions(+), 33 deletions(-) create mode 100644 Source/Application/Kysect.Shreks.Application.Abstractions/Internal/SeedTestData.cs create mode 100644 Source/Application/Kysect.Shreks.Application.Handlers/Internal/SeedTestDataHandler.cs rename Source/Infrastructure/Kysect.Shreks.Mapping/Profiles/{SubjectCourserProfile.cs => SubjectCourseProfile.cs} (100%) create mode 100644 Source/Kysect.Shreks.WebApi/Controllers/InternalController.cs diff --git a/Source/Application/Kysect.Shreks.Application.Abstractions/Internal/SeedTestData.cs b/Source/Application/Kysect.Shreks.Application.Abstractions/Internal/SeedTestData.cs new file mode 100644 index 000000000..0488a0d8c --- /dev/null +++ b/Source/Application/Kysect.Shreks.Application.Abstractions/Internal/SeedTestData.cs @@ -0,0 +1,14 @@ +using MediatR; + +namespace Kysect.Shreks.Application.Abstractions.Internal; + +public static class SeedTestData { + public record Query(string Environment) : IRequest; + + public class UserNotAcknowledgedEnvironmentException : InvalidOperationException { + public UserNotAcknowledgedEnvironmentException() + : base("You must ensure that is it a right environment to execute SeedTestData command") + { + } + } +} \ No newline at end of file diff --git a/Source/Application/Kysect.Shreks.Application.Handlers/Internal/SeedTestDataHandler.cs b/Source/Application/Kysect.Shreks.Application.Handlers/Internal/SeedTestDataHandler.cs new file mode 100644 index 000000000..d758e773d --- /dev/null +++ b/Source/Application/Kysect.Shreks.Application.Handlers/Internal/SeedTestDataHandler.cs @@ -0,0 +1,66 @@ +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 { + private const string ExceptedEnvironment = "Testing"; + + private readonly IShreksDatabaseContext _context; + private readonly IEntityGenerator _userGenerator; + private readonly IEntityGenerator _subjectCourseGenerator; + private readonly TestEnvironmentConfiguration _configuration; + + public SeedTestDataHandler( + IShreksDatabaseContext context, + IEntityGenerator userGenerator, + IEntityGenerator subjectCourseGenerator, + TestEnvironmentConfiguration configuration) { + _context = context; + _userGenerator = userGenerator; + _subjectCourseGenerator = subjectCourseGenerator; + _configuration = configuration; + } + + public async Task 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() { + var subjectCourse = _subjectCourseGenerator.GeneratedEntities[0]; + _context.SubjectCourses.Attach(subjectCourse); + _context.SubjectCourseAssociations.Add( + new GithubSubjectCourseAssociation(subjectCourse, _configuration.Organization, + _configuration.TemplateRepository)); + } + + private void AddUsers() { + var users = _userGenerator.GeneratedEntities; + _context.Users.AttachRange(users); + + for (var index = 0; index < _configuration.Users.Count; index++) { + var user = users[index]; + var login = _configuration.Users[index]; + _context.UserAssociations.Add(new GithubUserAssociation(user, login)); + } + } +} \ No newline at end of file diff --git a/Source/Application/Kysect.Shreks.Application.Handlers/Kysect.Shreks.Application.Handlers.csproj b/Source/Application/Kysect.Shreks.Application.Handlers/Kysect.Shreks.Application.Handlers.csproj index b24820b0f..ca86e1a8f 100644 --- a/Source/Application/Kysect.Shreks.Application.Handlers/Kysect.Shreks.Application.Handlers.csproj +++ b/Source/Application/Kysect.Shreks.Application.Handlers/Kysect.Shreks.Application.Handlers.csproj @@ -7,7 +7,9 @@ + + diff --git a/Source/Infrastructure/Kysect.Shreks.Mapping/Profiles/SubjectCourserProfile.cs b/Source/Infrastructure/Kysect.Shreks.Mapping/Profiles/SubjectCourseProfile.cs similarity index 100% rename from Source/Infrastructure/Kysect.Shreks.Mapping/Profiles/SubjectCourserProfile.cs rename to Source/Infrastructure/Kysect.Shreks.Mapping/Profiles/SubjectCourseProfile.cs diff --git a/Source/Kysect.Shreks.WebApi/Controllers/InternalController.cs b/Source/Kysect.Shreks.WebApi/Controllers/InternalController.cs new file mode 100644 index 000000000..a7e8dace4 --- /dev/null +++ b/Source/Kysect.Shreks.WebApi/Controllers/InternalController.cs @@ -0,0 +1,37 @@ +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 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(); + } +} \ No newline at end of file diff --git a/Source/Kysect.Shreks.WebApi/Program.cs b/Source/Kysect.Shreks.WebApi/Program.cs index 638b7ad51..5c6b733f9 100644 --- a/Source/Kysect.Shreks.WebApi/Program.cs +++ b/Source/Kysect.Shreks.WebApi/Program.cs @@ -25,6 +25,7 @@ using Kysect.Shreks.WebApi.Models; using MediatR; using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.OpenApi.Models; using Serilog; @@ -49,6 +50,7 @@ void InitServiceCollection(WebApplicationBuilder webApplicationBuilder) { + webApplicationBuilder.Services.TryAddSingleton(testEnvironmentConfiguration); webApplicationBuilder.Services.AddControllers(x => x.Filters.Add()).AddNewtonsoftJson(); webApplicationBuilder.Services.AddEndpointsApiExplorer(); webApplicationBuilder.Services.AddSwaggerGen(c => @@ -163,44 +165,12 @@ async Task InitWebApplication(WebApplicationBuilder webApplicationBuilder) using (var scope = app.Services.CreateScope()) { - if (app.Environment.IsDevelopment()) - await InitTestEnvironment(scope.ServiceProvider, testEnvironmentConfiguration); - - await SeedAdmins(scope.ServiceProvider, app.Configuration); + await SeedAdmins(scope.ServiceProvider, app.Configuration); } app.Run(); } -async Task InitTestEnvironment( - IServiceProvider serviceProvider, - TestEnvironmentConfiguration config, - CancellationToken cancellationToken = default) -{ - return; - var dbContext = serviceProvider.GetRequiredService(); - - var userGenerator = serviceProvider.GetRequiredService>(); - var users = userGenerator.GeneratedEntities; - dbContext.Users.AttachRange(users); - - for (var index = 0; index < config.Users.Count; index++) - { - var user = users[index]; - var login = config.Users[index]; - dbContext.UserAssociations.Add(new GithubUserAssociation(user, login)); - } - - - var subjectCourseGenerator = serviceProvider.GetRequiredService>(); - var subjectCourse = subjectCourseGenerator.GeneratedEntities[0]; - dbContext.SubjectCourses.Attach(subjectCourse); - dbContext.SubjectCourseAssociations.Add( - new GithubSubjectCourseAssociation(subjectCourse, config.Organization, config.TemplateRepository)); - - await dbContext.SaveChangesAsync(cancellationToken); -} - async Task SeedAdmins(IServiceProvider provider, IConfiguration configuration) { var mediatr = provider.GetRequiredService(); From 5637b5e9a5a8e924da93270c0ec65911813b3b01 Mon Sep 17 00:00:00 2001 From: Fredi Kats Date: Tue, 6 Sep 2022 01:43:26 +0400 Subject: [PATCH 2/4] refactor: apply ctrl+c and ctrl+e --- .../Internal/SeedTestData.cs | 6 ++-- .../Internal/SeedTestDataHandler.cs | 33 +++++++++++-------- .../Controllers/InternalController.cs | 19 +++++++---- 3 files changed, 35 insertions(+), 23 deletions(-) diff --git a/Source/Application/Kysect.Shreks.Application.Abstractions/Internal/SeedTestData.cs b/Source/Application/Kysect.Shreks.Application.Abstractions/Internal/SeedTestData.cs index 0488a0d8c..9b22b79f1 100644 --- a/Source/Application/Kysect.Shreks.Application.Abstractions/Internal/SeedTestData.cs +++ b/Source/Application/Kysect.Shreks.Application.Abstractions/Internal/SeedTestData.cs @@ -2,10 +2,12 @@ namespace Kysect.Shreks.Application.Abstractions.Internal; -public static class SeedTestData { +public static class SeedTestData +{ public record Query(string Environment) : IRequest; - public class UserNotAcknowledgedEnvironmentException : InvalidOperationException { + public class UserNotAcknowledgedEnvironmentException : InvalidOperationException + { public UserNotAcknowledgedEnvironmentException() : base("You must ensure that is it a right environment to execute SeedTestData command") { diff --git a/Source/Application/Kysect.Shreks.Application.Handlers/Internal/SeedTestDataHandler.cs b/Source/Application/Kysect.Shreks.Application.Handlers/Internal/SeedTestDataHandler.cs index d758e773d..37f762edb 100644 --- a/Source/Application/Kysect.Shreks.Application.Handlers/Internal/SeedTestDataHandler.cs +++ b/Source/Application/Kysect.Shreks.Application.Handlers/Internal/SeedTestDataHandler.cs @@ -10,7 +10,8 @@ namespace Kysect.Shreks.Application.Handlers.Internal; -public class SeedTestDataHandler : IRequestHandler { +public class SeedTestDataHandler : IRequestHandler +{ private const string ExceptedEnvironment = "Testing"; private readonly IShreksDatabaseContext _context; @@ -22,14 +23,16 @@ public SeedTestDataHandler( IShreksDatabaseContext context, IEntityGenerator userGenerator, IEntityGenerator subjectCourseGenerator, - TestEnvironmentConfiguration configuration) { + TestEnvironmentConfiguration configuration) + { _context = context; _userGenerator = userGenerator; _subjectCourseGenerator = subjectCourseGenerator; _configuration = configuration; } - public async Task Handle(Query request, CancellationToken cancellationToken = default) { + public async Task Handle(Query request, CancellationToken cancellationToken = default) + { EnsureUserAcknowledgedEnvironment(request); AddUsers(); AddGithubUserAssociations(); @@ -39,27 +42,29 @@ public async Task Handle(Query request, CancellationToken cancellationToke return Unit.Value; } - private static void EnsureUserAcknowledgedEnvironment(Query request) { - if (!request.Environment.Equals(ExceptedEnvironment, StringComparison.OrdinalIgnoreCase)) { - throw new UserNotAcknowledgedEnvironmentException(); - } + private static void EnsureUserAcknowledgedEnvironment(Query request) + { + if (!request.Environment.Equals(ExceptedEnvironment, StringComparison.OrdinalIgnoreCase)) throw new UserNotAcknowledgedEnvironmentException(); } - private void AddGithubUserAssociations() { - var subjectCourse = _subjectCourseGenerator.GeneratedEntities[0]; + 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() { - var users = _userGenerator.GeneratedEntities; + private void AddUsers() + { + IReadOnlyList users = _userGenerator.GeneratedEntities; _context.Users.AttachRange(users); - for (var index = 0; index < _configuration.Users.Count; index++) { - var user = users[index]; - var login = _configuration.Users[index]; + 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)); } } diff --git a/Source/Kysect.Shreks.WebApi/Controllers/InternalController.cs b/Source/Kysect.Shreks.WebApi/Controllers/InternalController.cs index a7e8dace4..09332ae93 100644 --- a/Source/Kysect.Shreks.WebApi/Controllers/InternalController.cs +++ b/Source/Kysect.Shreks.WebApi/Controllers/InternalController.cs @@ -3,7 +3,6 @@ using MediatR; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; - using static Kysect.Shreks.Application.Abstractions.Internal.SeedTestData; namespace Kysect.Shreks.WebApi.Controllers; @@ -11,7 +10,8 @@ namespace Kysect.Shreks.WebApi.Controllers; [ApiController] [Route("api/internal/")] [Authorize(Roles = ShreksIdentityRole.AdminRoleName)] -public class InternalController : ControllerBase { +public class InternalController : ControllerBase +{ private readonly IMediator _mediator; public InternalController(IMediator mediator) @@ -20,13 +20,18 @@ public InternalController(IMediator mediator) } [HttpPost("seed-test-data")] - public async Task SeedTestData([FromQuery] string environment) { + public async Task SeedTestData([FromQuery] string environment) + { var command = new Query(environment); - try { + try + { await _mediator.Send(command); - } catch (UserNotAcknowledgedEnvironmentException e) { - return StatusCode((int)HttpStatusCode.BadRequest, new ProblemDetails { - Status = (int)HttpStatusCode.BadRequest, + } + 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" }); From a42d5a5ca601c64e6f4b953bb0c7fc5a354b30ee Mon Sep 17 00:00:00 2001 From: Roman Makarevich Date: Tue, 6 Sep 2022 00:57:47 +0300 Subject: [PATCH 3/4] Fixed code style --- .../Internal/SeedTestDataHandler.cs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Source/Application/Kysect.Shreks.Application.Handlers/Internal/SeedTestDataHandler.cs b/Source/Application/Kysect.Shreks.Application.Handlers/Internal/SeedTestDataHandler.cs index 37f762edb..8f38eec67 100644 --- a/Source/Application/Kysect.Shreks.Application.Handlers/Internal/SeedTestDataHandler.cs +++ b/Source/Application/Kysect.Shreks.Application.Handlers/Internal/SeedTestDataHandler.cs @@ -44,7 +44,10 @@ public async Task Handle(Query request, CancellationToken cancellationToke private static void EnsureUserAcknowledgedEnvironment(Query request) { - if (!request.Environment.Equals(ExceptedEnvironment, StringComparison.OrdinalIgnoreCase)) throw new UserNotAcknowledgedEnvironmentException(); + if (!request.Environment.Equals(ExceptedEnvironment, StringComparison.OrdinalIgnoreCase)) + { + throw new UserNotAcknowledgedEnvironmentException(); + } } private void AddGithubUserAssociations() From eb3192b089c9801921796ba519df39721aab7700 Mon Sep 17 00:00:00 2001 From: Roman Makarevich Date: Tue, 6 Sep 2022 01:08:33 +0300 Subject: [PATCH 4/4] Removed redundant usings --- Source/Kysect.Shreks.WebApi/Program.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/Source/Kysect.Shreks.WebApi/Program.cs b/Source/Kysect.Shreks.WebApi/Program.cs index 5c6b733f9..a7f3044fe 100644 --- a/Source/Kysect.Shreks.WebApi/Program.cs +++ b/Source/Kysect.Shreks.WebApi/Program.cs @@ -9,7 +9,6 @@ using Kysect.Shreks.Core.Submissions; using Kysect.Shreks.Core.UserAssociations; using Kysect.Shreks.Core.Users; -using Kysect.Shreks.DataAccess.Abstractions; using Kysect.Shreks.DataAccess.Configuration; using Kysect.Shreks.DataAccess.Extensions; using Kysect.Shreks.Identity.Extensions; @@ -19,7 +18,6 @@ using Kysect.Shreks.Integration.Google.Models; using Kysect.Shreks.Mapping.Extensions; using Kysect.Shreks.Playground.Github.TestEnv; -using Kysect.Shreks.Seeding.EntityGenerators; using Kysect.Shreks.Seeding.Extensions; using Kysect.Shreks.WebApi.Filters; using Kysect.Shreks.WebApi.Models;