33 lines
773 B
C#
33 lines
773 B
C#
|
|
using Microsoft.AspNetCore.Mvc;
|
||
|
|
|
||
|
|
namespace PlanTempus.Components.Users.Create
|
||
|
|
{
|
||
|
|
[ApiController]
|
||
|
|
[Route("api/users")]
|
||
|
|
public class CreateUserController(CreateUserHandler handler, CreateUserValidator validator) : ControllerBase
|
||
|
|
{
|
||
|
|
[HttpPost]
|
||
|
|
public async Task<ActionResult<CreateUserResponse>> Create([FromBody] CreateUserCommand command)
|
||
|
|
{
|
||
|
|
try
|
||
|
|
{
|
||
|
|
var validationResult = await validator.ValidateAsync(command);
|
||
|
|
if (!validationResult.IsValid)
|
||
|
|
{
|
||
|
|
return BadRequest(validationResult.Errors);
|
||
|
|
}
|
||
|
|
|
||
|
|
var result = await handler.Handle(command);
|
||
|
|
return CreatedAtAction(
|
||
|
|
nameof(CreateUserCommand),
|
||
|
|
"GetUser",
|
||
|
|
new { id = result.Id },
|
||
|
|
result);
|
||
|
|
}
|
||
|
|
catch (Exception ex)
|
||
|
|
{
|
||
|
|
return BadRequest(ex.Message);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|