-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add commands, classes, migrations, and controllers for creating a squadron
- Loading branch information
1 parent
d2322b2
commit 4e4ba75
Showing
19 changed files
with
341 additions
and
1 deletion.
There are no files selected for viewing
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
9 changes: 9 additions & 0 deletions
9
src/Armory.Api/Controllers/Squadron/Requests/CreateSquadronRequest.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,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
42
src/Armory.Api/Controllers/Squadron/SquadronsController.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; | ||
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(); | ||
} | ||
} | ||
} |
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
Empty file.
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,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
20
src/Squadron/Application/Create/CreateSquadronCommandHandler.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,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); | ||
} | ||
} | ||
} |
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,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); | ||
} | ||
} | ||
} |
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,9 @@ | ||
using System.Threading.Tasks; | ||
|
||
namespace Armory.Squadron.Domain | ||
{ | ||
public interface ISquadronRepository | ||
{ | ||
Task Save(Squadron squadron); | ||
} | ||
} |
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,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; | ||
} | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
.../Infrastructure/Persistence/EntityFramework/EntityConfigurations/SquadronConfiguration.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,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); | ||
} | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/Squadron/Infrastructure/Persistence/EntityFramework/SquadronDbContext.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,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
22
src/Squadron/Infrastructure/Persistence/MySqlSquadronRepository.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,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
44
src/Squadron/Migrations/20210615141813_AddSquadron.Designer.cs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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,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"); | ||
} | ||
} | ||
} |
Oops, something went wrong.