33 lines
741 B
Go
33 lines
741 B
Go
package site
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"net/http"
|
|
|
|
"ruben/inventory2/internal/consts"
|
|
"ruben/inventory2/internal/site/response"
|
|
)
|
|
|
|
// POST /accounts
|
|
func (s *Server) createAccount(r *http.Request) (response.Response, error) {
|
|
ctx := r.Context()
|
|
email := r.FormValue("email")
|
|
if email == "" {
|
|
return nil, response.BadRequest().Msg("no email provided")
|
|
}
|
|
|
|
userID := getIdentity(ctx).User.UserID
|
|
|
|
acct, err := s.accts.CreateAccount(ctx, userID, email)
|
|
if err != nil {
|
|
if errors.Is(err, consts.ErrConflict) {
|
|
return nil, response.Conflict().
|
|
Msg("user already has an account")
|
|
}
|
|
return nil, response.Errorf("failed to create account: %w", err)
|
|
}
|
|
|
|
return response.SeeOther(fmt.Sprintf("/accounts/%d", acct.ID)), nil
|
|
}
|