http response tooling written

This commit is contained in:
2025-12-30 14:42:04 -07:00
parent 493d15eae8
commit c01d700482
14 changed files with 660 additions and 60 deletions
+6 -7
View File
@@ -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
}