PlanTempusApp/PlanTempus.Components/Users/Create/CreateUserController.cs

33 lines
773 B
C#
Raw Normal View History

2025-03-04 17:13:02 +01:00
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);
}
}
}
}