http response tooling written
This commit is contained in:
@@ -3,25 +3,24 @@ package site
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"ruben/inventory2/internal/site/response"
|
||||
)
|
||||
|
||||
// POST /accounts
|
||||
func (s *Server) createAccount(w http.ResponseWriter, r *http.Request) {
|
||||
func (s *Server) createAccount(r *http.Request) (response.Response, error) {
|
||||
ctx := r.Context()
|
||||
email := r.FormValue("email")
|
||||
if email == "" {
|
||||
http.Error(w, "no email provided", http.StatusBadRequest)
|
||||
return
|
||||
return nil, response.BadRequest().Msg("no email provided")
|
||||
}
|
||||
|
||||
userID := getAccessTokenClaims(ctx).Subject
|
||||
|
||||
acct, err := s.accts.CreateAccount(ctx, userID, email)
|
||||
if err != nil {
|
||||
http.Error(w, fmt.Sprintf("failed to create account: %v", err), http.StatusInternalServerError)
|
||||
return
|
||||
return nil, response.Errorf("failed to create account: %w", err)
|
||||
}
|
||||
|
||||
//http.Redirect(w, r, fmt.Sprintf("/site/accounts/%d", acct.ID), http.StatusSeeOther)
|
||||
http.Redirect(w, r, fmt.Sprintf("/accounts/%d", acct.ID), http.StatusSeeOther)
|
||||
return response.SeeOther(fmt.Sprintf("/accounts/%d", acct.ID)), nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user