versia-go/internal/handlers/user_handler/app_user_create.go

36 lines
834 B
Go
Raw Permalink Normal View History

2024-08-11 03:51:22 +02:00
package user_handler
import (
"github.com/gofiber/fiber/v2"
2024-08-28 00:25:25 +02:00
"github.com/versia-pub/versia-go/ent"
"github.com/versia-pub/versia-go/internal/api_schema"
2024-08-11 03:51:22 +02:00
)
func (i *Handler) CreateUser(c *fiber.Ctx) error {
var req api_schema.CreateUserRequest
if err := c.BodyParser(&req); err != nil {
return api_schema.ErrInvalidRequestBody(nil)
}
if err := i.bodyValidator.Validate(req); err != nil {
return err
}
u, err := i.userService.NewUser(c.UserContext(), req.Username, req.Password)
if err != nil {
// TODO: Wrap this in a custom error
if ent.IsConstraintError(err) {
return api_schema.ErrUsernameTaken(nil)
}
i.log.Error(err, "Failed to create user", "username", req.Username)
return api_schema.ErrInternalServerError(nil)
}
return c.JSON(api_schema.User{
ID: u.ID,
Username: u.Username,
})
}