2025-08-26 14:13:09 -06:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"log"
|
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
|
|
"github.com/patel-mann/poll-system/app/internal/models"
|
|
|
|
|
"github.com/patel-mann/poll-system/app/internal/utils"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func ProfileHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
// Extract current user ID from session/jwt
|
|
|
|
|
currentUserID := r.Context().Value("user_id").(int)
|
2025-08-27 13:21:11 -06:00
|
|
|
username,_ := models.GetCurrentUserName(r)
|
2025-08-26 14:13:09 -06:00
|
|
|
|
|
|
|
|
var user models.User
|
|
|
|
|
err := models.DB.QueryRow(`
|
2025-08-27 13:21:11 -06:00
|
|
|
SELECT user_id, first_name, last_name, email, phone, role_id, created_at, updated_at, admin_code
|
2025-08-26 14:13:09 -06:00
|
|
|
FROM "users"
|
|
|
|
|
WHERE user_id = $1
|
|
|
|
|
`, currentUserID).Scan(
|
|
|
|
|
&user.UserID,
|
|
|
|
|
&user.FirstName,
|
|
|
|
|
&user.LastName,
|
|
|
|
|
&user.Email,
|
|
|
|
|
&user.Phone,
|
|
|
|
|
&user.RoleID,
|
|
|
|
|
&user.CreatedAt,
|
|
|
|
|
&user.UpdatedAt,
|
2025-08-27 13:21:11 -06:00
|
|
|
&user.AdminCode,
|
2025-08-26 14:13:09 -06:00
|
|
|
)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Println("Profile query error:", err)
|
|
|
|
|
http.Error(w, "Could not load profile", http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
role := r.Context().Value("user_role").(int)
|
|
|
|
|
adminnav := false
|
|
|
|
|
volunteernav := false
|
|
|
|
|
|
|
|
|
|
if role == 1{
|
|
|
|
|
adminnav = true
|
|
|
|
|
volunteernav = false
|
|
|
|
|
}else{
|
|
|
|
|
adminnav = false
|
2025-08-27 13:21:11 -06:00
|
|
|
volunteernav = true
|
2025-08-26 14:13:09 -06:00
|
|
|
}
|
|
|
|
|
|
2025-09-05 15:39:06 -06:00
|
|
|
utils.Render(w, "profile.html", map[string]interface{}{
|
2025-08-26 14:13:09 -06:00
|
|
|
"Title": "My Profile",
|
|
|
|
|
"IsAuthenticated": true,
|
|
|
|
|
"ShowAdminNav": adminnav,
|
|
|
|
|
"ShowVolunteerNav": volunteernav,
|
2025-08-27 13:21:11 -06:00
|
|
|
"UserName": username,
|
2025-08-26 14:13:09 -06:00
|
|
|
"User": user,
|
|
|
|
|
"ActiveSection": "profile",
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ProfileUpdateHandler handles profile form submissions
|
|
|
|
|
func ProfileUpdateHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
if r.Method != http.MethodPost {
|
|
|
|
|
http.Redirect(w, r, "/profile", http.StatusSeeOther)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Extract current user ID from session/jwt
|
|
|
|
|
currentUserID := r.Context().Value("user_id").(int)
|
|
|
|
|
|
|
|
|
|
// Parse form values
|
|
|
|
|
err := r.ParseForm()
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Println("Form parse error:", err)
|
|
|
|
|
http.Error(w, "Invalid form submission", http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
firstName := r.FormValue("first_name")
|
|
|
|
|
lastName := r.FormValue("last_name")
|
|
|
|
|
phone := r.FormValue("phone")
|
|
|
|
|
|
|
|
|
|
// Update in DB
|
|
|
|
|
_, err = models.DB.Exec(`
|
|
|
|
|
UPDATE "users"
|
|
|
|
|
SET first_name = $1,
|
|
|
|
|
last_name = $2,
|
|
|
|
|
phone = $3,
|
|
|
|
|
updated_at = NOW()
|
|
|
|
|
WHERE user_id = $4
|
|
|
|
|
`, firstName, lastName, phone, currentUserID)
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Println("Profile update error:", err)
|
|
|
|
|
http.Error(w, "Could not update profile", http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Redirect back to profile with success
|
|
|
|
|
http.Redirect(w, r, "/profile?success=1", http.StatusSeeOther)
|
|
|
|
|
}
|