Skip to content

Commit

Permalink
feat: implement squadron creation
Browse files Browse the repository at this point in the history
Add commands, classes, migrations, and controllers for creating a squadron
  • Loading branch information
CarlosPavajeau committed Jun 15, 2021
1 parent d2322b2 commit 4e4ba75
Show file tree
Hide file tree
Showing 19 changed files with 341 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/Armory.Api/Armory.Api.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Squadron\Squadron.csproj" />
<ProjectReference Include="..\Users\Users.csproj" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Armory.Api.Controllers.Squadron.Requests
{
public class CreateSquadronRequest
{
public string Code { get; set; }
public string Name { get; set; }
public string ArmoryUserId { get; set; }
}
}
42 changes: 42 additions & 0 deletions src/Armory.Api/Controllers/Squadron/SquadronsController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System;
using System.Threading.Tasks;
using Armory.Api.Controllers.Squadron.Requests;
using Armory.Shared.Domain.Bus.Command;
using Armory.Shared.Domain.Bus.Query;
using Armory.Squadron.Application.Create;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace Armory.Api.Controllers.Squadron
{
[ApiController]
[Authorize]
[Route("[controller]")]
public class SquadronsController : ControllerBase
{
private readonly ICommandBus _commandBus;
private readonly IQueryBus _queryBus;

public SquadronsController(ICommandBus commandBus, IQueryBus queryBus)
{
_commandBus = commandBus;
_queryBus = queryBus;
}

[HttpPost]
public async Task<IActionResult> RegisterSquadron([FromBody] CreateSquadronRequest request)
{
try
{
await _commandBus.Dispatch(new CreateSquadronCommand(request.Code, request.Name, request.ArmoryUserId));
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}

return Ok();
}
}
}
6 changes: 5 additions & 1 deletion src/Armory.Api/Extensions/Application.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Armory.Shared.Extensions;
using Armory.Shared.Helpers;
using Armory.Squadron.Application.Create;
using Armory.Users.Application.Authenticate;
using Armory.Users.Application.ChangePassword;
using Armory.Users.Application.ConfirmEmail;
Expand All @@ -24,10 +25,13 @@ public static IServiceCollection AddApplication(this IServiceCollection services
services.AddScoped<PasswordChanger, PasswordChanger>();
services.AddScoped<EmailConfirmationTokenGenerator, EmailConfirmationTokenGenerator>();
services.AddScoped<EmailConfirmer, EmailConfirmer>();

services.AddCommandServices(AssemblyHelper.GetInstance(Assemblies.Users));
services.AddQueryServices(AssemblyHelper.GetInstance(Assemblies.Users));

services.AddScoped<SquadronCreator, SquadronCreator>();
services.AddCommandServices(AssemblyHelper.GetInstance(Assemblies.Squadron));
services.AddQueryServices(AssemblyHelper.GetInstance(Assemblies.Squadron));

return services;
}
}
Expand Down
8 changes: 8 additions & 0 deletions src/Armory.Api/Extensions/Infrastructure.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
using Armory.Shared.Infrastructure.Bus.Command;
using Armory.Shared.Infrastructure.Bus.Event;
using Armory.Shared.Infrastructure.Bus.Query;
using Armory.Squadron.Domain;
using Armory.Squadron.Infrastructure.Persistence;
using Armory.Squadron.Infrastructure.Persistence.EntityFramework;
using Armory.Users.Domain;
using Armory.Users.Infrastructure.Identity;
using Armory.Users.Infrastructure.Persistence;
Expand All @@ -24,6 +27,7 @@ public static IServiceCollection AddInfrastructure(this IServiceCollection servi
IConfiguration configuration)
{
services.AddScoped<IArmoryUserRepository, MySqlArmoryUserRepository>();
services.AddScoped<ISquadronRepository, MySqlSquadronRepository>();
services.AddScoped<InMemoryApplicationEventBus, InMemoryApplicationEventBus>();
services.AddScoped<IEventBus, InMemoryApplicationEventBus>();

Expand All @@ -33,9 +37,13 @@ public static IServiceCollection AddInfrastructure(this IServiceCollection servi
.AddDefaultTokenProviders();

services.AddScoped<ArmoryUserDbContext, ArmoryUserDbContext>();
services.AddScoped<SquadronDbContext, SquadronDbContext>();
services.AddDbContext<ArmoryUserDbContext>(
options => options.UseMySQL(configuration.GetConnectionString("DefaultConnection")),
ServiceLifetime.Transient);
services.AddDbContext<SquadronDbContext>(
options => options.UseMySQL(configuration.GetConnectionString("DefaultConnection")),
ServiceLifetime.Transient);

services.AddScoped<ICommandBus, InMemoryCommandBus>();
services.AddScoped<IQueryBus, InMemoryQueryBus>();
Expand Down
1 change: 1 addition & 0 deletions src/Shared/Helpers/AssemblyHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ public static Assembly GetInstance(string key)
public static class Assemblies
{
public const string Users = "Armory.Users";
public const string Squadron = "Armory.Squadron";
}
}
Empty file removed src/Squadron/Application/.gitkeep
Empty file.
18 changes: 18 additions & 0 deletions src/Squadron/Application/Create/CreateSquadronCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using Armory.Shared.Domain.Bus.Command;

namespace Armory.Squadron.Application.Create
{
public class CreateSquadronCommand : Command
{
public string Code { get; }
public string Name { get; }
public string ArmoryUserId { get; }

public CreateSquadronCommand(string code, string name, string armoryUserId)
{
Code = code;
Name = name;
ArmoryUserId = armoryUserId;
}
}
}
20 changes: 20 additions & 0 deletions src/Squadron/Application/Create/CreateSquadronCommandHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System.Threading.Tasks;
using Armory.Shared.Domain.Bus.Command;

namespace Armory.Squadron.Application.Create
{
public class CreateSquadronCommandHandler : ICommandHandler<CreateSquadronCommand>
{
private readonly SquadronCreator _creator;

public CreateSquadronCommandHandler(SquadronCreator creator)
{
_creator = creator;
}

public async Task Handle(CreateSquadronCommand command)
{
await _creator.Create(command.Code, command.Name, command.ArmoryUserId);
}
}
}
22 changes: 22 additions & 0 deletions src/Squadron/Application/Create/SquadronCreator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System.Threading.Tasks;
using Armory.Squadron.Domain;

namespace Armory.Squadron.Application.Create
{
public class SquadronCreator
{
private readonly ISquadronRepository _repository;

public SquadronCreator(ISquadronRepository repository)
{
_repository = repository;
}

public async Task Create(string code, string name, string armoryUserId)
{
var squadron = Domain.Squadron.Create(code, name, armoryUserId);

await _repository.Save(squadron);
}
}
}
9 changes: 9 additions & 0 deletions src/Squadron/Domain/ISquadronRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Threading.Tasks;

namespace Armory.Squadron.Domain
{
public interface ISquadronRepository
{
Task Save(Squadron squadron);
}
}
28 changes: 28 additions & 0 deletions src/Squadron/Domain/Squadron.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System.ComponentModel.DataAnnotations;

namespace Armory.Squadron.Domain
{
public class Squadron
{
[Key, MaxLength(50)] public string Code { get; set; }
[Required, MaxLength(128)] public string Name { get; set; }
[Required] public string ArmoryUserId { get; set; }

public Squadron(string code, string name, string armoryUserId)
{
Code = code;
Name = name;
ArmoryUserId = armoryUserId;
}

private Squadron()
{
}

public static Squadron Create(string code, string name, string armoryUserId)
{
var squadron = new Squadron(code, name, armoryUserId);
return squadron;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace Armory.Squadron.Infrastructure.Persistence.EntityFramework.EntityConfigurations
{
public class SquadronConfiguration : IEntityTypeConfiguration<Domain.Squadron>
{
public void Configure(EntityTypeBuilder<Domain.Squadron> builder)
{
builder.Property(p => p.ArmoryUserId).HasMaxLength(255);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using Armory.Squadron.Infrastructure.Persistence.EntityFramework.EntityConfigurations;
using Microsoft.EntityFrameworkCore;

namespace Armory.Squadron.Infrastructure.Persistence.EntityFramework
{
public class SquadronDbContext : DbContext
{
public DbSet<Armory.Squadron.Domain.Squadron> Squadrons { get; set; }

public SquadronDbContext(DbContextOptions<SquadronDbContext> options) : base(options)
{
}

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);

modelBuilder.ApplyConfiguration(new SquadronConfiguration());
}
}
}
22 changes: 22 additions & 0 deletions src/Squadron/Infrastructure/Persistence/MySqlSquadronRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System.Threading.Tasks;
using Armory.Squadron.Domain;
using Armory.Squadron.Infrastructure.Persistence.EntityFramework;

namespace Armory.Squadron.Infrastructure.Persistence
{
public class MySqlSquadronRepository : ISquadronRepository
{
private readonly SquadronDbContext _context;

public MySqlSquadronRepository(SquadronDbContext context)
{
_context = context;
}

public async Task Save(Domain.Squadron squadron)
{
await _context.Squadrons.AddAsync(squadron);
await _context.SaveChangesAsync();
}
}
}
44 changes: 44 additions & 0 deletions src/Squadron/Migrations/20210615141813_AddSquadron.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 29 additions & 0 deletions src/Squadron/Migrations/20210615141813_AddSquadron.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using Microsoft.EntityFrameworkCore.Migrations;

namespace Armory.Squadron.Migrations
{
public partial class AddSquadron : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Squadrons",
columns: table => new
{
Code = table.Column<string>(type: "varchar(50)", maxLength: 50, nullable: false),
Name = table.Column<string>(type: "varchar(128)", maxLength: 128, nullable: false),
ArmoryUserId = table.Column<string>(type: "varchar(255)", maxLength: 255, nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Squadrons", x => x.Code);
});
}

protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Squadrons");
}
}
}
Loading

0 comments on commit 4e4ba75

Please sign in to comment.