Files
GoMFT/internal/web/handlers/profile_handlers.go
T
2025-03-07 23:21:12 -08:00

78 lines
1.8 KiB
Go

package handlers
import (
"fmt"
"net/http"
"github.com/gin-gonic/gin"
"github.com/starfleetcptn/gomft/components"
"github.com/starfleetcptn/gomft/internal/db"
)
// HandleProfile handles the GET /profile route
func (h *Handlers) HandleProfile(c *gin.Context) {
userID := c.GetUint("userID")
var user db.User
if err := h.DB.First(&user, userID).Error; err != nil {
c.String(http.StatusInternalServerError, "Failed to retrieve user profile")
return
}
components.Profile(c.Request.Context(), user).Render(c, c.Writer)
}
// HandleUpdateTheme handles the POST /profile/theme route
func (h *Handlers) HandleUpdateTheme(c *gin.Context) {
userID := c.GetUint("userID")
theme := c.PostForm("theme")
// Validate theme value
validThemes := map[string]bool{
"light": true,
"dark": true,
"system": true,
}
if !validThemes[theme] {
c.Status(http.StatusBadRequest)
return
}
// Update user theme preference
var user db.User
if err := h.DB.First(&user, userID).Error; err != nil {
c.Status(http.StatusInternalServerError)
return
}
user.Theme = theme
if err := h.DB.Save(&user).Error; err != nil {
c.Status(http.StatusInternalServerError)
return
}
// Set theme cookie for client-side theme switching
c.SetCookie("theme", theme, 60*60*24*365, "/", "", false, false)
c.Status(http.StatusOK)
}
// HandleUpdateProfile handles the POST /profile/update route
func (h *Handlers) HandleUpdateProfile(c *gin.Context) {
userID := c.GetUint("userID")
var user db.User
if err := h.DB.First(&user, userID).Error; err != nil {
c.String(http.StatusNotFound, "User not found")
return
}
// Update user fields
user.Email = c.PostForm("email")
if err := h.DB.Save(&user).Error; err != nil {
c.String(http.StatusInternalServerError, fmt.Sprintf("Failed to update profile: %v", err))
return
}
c.Redirect(http.StatusFound, "/profile")
}