htmgo/examples/simple-auth/partials/profile.go

37 lines
730 B
Go
Raw Permalink Normal View History

2024-10-20 12:48:58 +00:00
package partials
import (
"github.com/maddalax/htmgo/framework/h"
"log/slog"
"simpleauth/internal/user"
"simpleauth/ui"
)
func UpdateProfile(ctx *h.RequestContext) *h.Partial {
if !ctx.IsHttpPost() {
return nil
}
patch := map[string]any{
"birthDate": ctx.FormValue("birth-date"),
"favoriteColor": ctx.FormValue("favorite-color"),
"occupation": ctx.FormValue("occupation"),
}
u, ok := user.GetUserOrRedirect(ctx)
if !ok {
return nil
}
err := user.SetMeta(ctx, u.ID, patch)
if err != nil {
slog.Error("failed to update user profile", slog.String("error", err.Error()))
ctx.Response.WriteHeader(400)
return ui.SwapFormError(ctx, "something went wrong")
}
return h.RedirectPartial("/")
}