// Add this to your handlers package (create volunteer_posts.go or add to existing file) package handlers import ( "fmt" "net/http" "strconv" "time" "github.com/patel-mann/poll-system/app/internal/models" "github.com/patel-mann/poll-system/app/internal/utils" ) // VolunteerPostsHandler - Read-only posts view for volunteers func VolunteerPostsHandler(w http.ResponseWriter, r *http.Request) { // Only allow GET requests for volunteers if r.Method != http.MethodGet { http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) return } // Get user info from context role := r.Context().Value("user_role").(int) CurrentUserID := models.GetCurrentUserID(w, r) username,_ := models.GetCurrentUserName(r) // Fetch posts from database rows, err := models.DB.Query(` SELECT p.post_id, p.author_id, u.first_name || ' ' || u.last_name AS author_name, p.content, COALESCE(p.image_url, '') as image_url, p.created_at FROM post p JOIN users u ON p.author_id = u.user_id JOIN admin_volunteers x ON u.user_id = x.admin_id WHERE x.volunteer_id = $1 ORDER BY p.created_at DESC `,CurrentUserID) if err != nil { fmt.Printf("Database query error: %v\n", err) http.Error(w, "Failed to fetch posts", http.StatusInternalServerError) return } defer rows.Close() var posts []models.Post for rows.Next() { var p models.Post err := rows.Scan(&p.PostID, &p.AuthorID, &p.AuthorName, &p.Content, &p.ImageURL, &p.CreatedAt) if err != nil { fmt.Printf("Row scan error: %v\n", err) continue } posts = append(posts, p) } // Add cache busting parameter to image URLs for i := range posts { if posts[i].ImageURL != "" { posts[i].ImageURL += "?t=" + strconv.FormatInt(time.Now().UnixNano(), 10) } } // Get navigation flags showAdminNav, showVolunteerNav := getNavFlags(role) fmt.Printf("Volunteer viewing %d posts\n", len(posts)) utils.Render(w, "dashboard/volunteer_dashboard.html", map[string]interface{}{ "Title": "Community Posts", "IsAuthenticated": true, "ShowAdminNav": showAdminNav, "ShowVolunteerNav": showVolunteerNav, "UserName": username, "Posts": posts, "ActiveSection": "posts", "IsVolunteer": true, // Flag to indicate this is volunteer view }) }