Fixing up the go code, adding initial work for the stats pages, adding a function to pre-populate the database with some example polls for testing. Will be removed later
This commit is contained in:
@@ -2,11 +2,14 @@ package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"math/rand"
|
||||
"net/http"
|
||||
"os"
|
||||
"strconv"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
|
||||
@@ -42,7 +45,7 @@ func voteIDHandler(resWriter http.ResponseWriter, request *http.Request) {
|
||||
vote := models.Vote{
|
||||
PollId: id,
|
||||
Email: "example@example.com", // Replace with actual email retrieval logic
|
||||
Vote: true, // Replace with actual vote retrieval logic
|
||||
Vote: true, // Replace with actual vote retrieval logic
|
||||
}
|
||||
|
||||
err = services.SetVote(&vote)
|
||||
@@ -74,12 +77,19 @@ func statsHandler(resWriter http.ResponseWriter, request *http.Request) {
|
||||
}
|
||||
}
|
||||
|
||||
func statsIDHandler(resWriter http.ResponseWriter, request *http.Request) {
|
||||
func pollsIDHandler(resWriter http.ResponseWriter, request *http.Request) {
|
||||
vars := mux.Vars(request)
|
||||
id := vars["id"]
|
||||
|
||||
poll, err := services.GetPollByQuestion(id)
|
||||
id, err := strconv.ParseInt(vars["id"], 10, 64)
|
||||
if err != nil {
|
||||
common.SendError(resWriter, "Invalid poll ID", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
poll, err := services.GetPollById(id)
|
||||
if err == services.ErrPollNotFound {
|
||||
common.SendError(resWriter, "Poll not found", http.StatusNotFound)
|
||||
return
|
||||
} else if err != nil {
|
||||
common.SendError(resWriter, "Failed to get poll", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
@@ -125,14 +135,73 @@ func adminLoginHandler(resWriter http.ResponseWriter, request *http.Request) {
|
||||
})
|
||||
}
|
||||
|
||||
func initDatabase() error {
|
||||
// Seed random generator for reproducible results in tests
|
||||
rand.Seed(42)
|
||||
|
||||
polls := []models.Poll{
|
||||
{
|
||||
ID: 1,
|
||||
Question: "Should we increase the budget?",
|
||||
MemberYes: rand.Int63n(50),
|
||||
MemberNo: rand.Int63n(50),
|
||||
NonMemberYes: rand.Int63n(20),
|
||||
NonMemberNo: rand.Int63n(20),
|
||||
TotalVotes: int(rand.Int63n(100)),
|
||||
WhoVoted: []string{"email1@example.com", "email2@example.com", "email3@example.com", "email4@example.com"},
|
||||
CreatedAt: time.Now().Format(time.RFC3339),
|
||||
UpdatedAt: time.Now().Format(time.RFC3339),
|
||||
ExpiresAt: time.Now().Add(24 * time.Hour).Format(time.RFC3339),
|
||||
},
|
||||
{
|
||||
ID: 2,
|
||||
Question: "Should we hire more staff?",
|
||||
MemberYes: rand.Int63n(50),
|
||||
MemberNo: rand.Int63n(50),
|
||||
NonMemberYes: rand.Int63n(20),
|
||||
NonMemberNo: rand.Int63n(20),
|
||||
TotalVotes: int(rand.Int63n(100)),
|
||||
WhoVoted: []string{"email1@example.com", "email2@example.com", "email3@example.com", "email4@example.com"},
|
||||
CreatedAt: time.Now().Format(time.RFC3339),
|
||||
UpdatedAt: time.Now().Format(time.RFC3339),
|
||||
ExpiresAt: time.Now().Add(24 * time.Hour).Format(time.RFC3339),
|
||||
},
|
||||
{
|
||||
ID: 3,
|
||||
Question: "Should we renovate the building?",
|
||||
MemberYes: rand.Int63n(50),
|
||||
MemberNo: rand.Int63n(50),
|
||||
NonMemberYes: rand.Int63n(20),
|
||||
NonMemberNo: rand.Int63n(20),
|
||||
TotalVotes: int(rand.Int63n(100)),
|
||||
WhoVoted: []string{"email1@example.com", "email2@example.com", "email3@example.com", "email4@example.com"},
|
||||
CreatedAt: time.Now().Format(time.RFC3339),
|
||||
UpdatedAt: time.Now().Format(time.RFC3339),
|
||||
ExpiresAt: time.Now().Add(24 * time.Hour).Format(time.RFC3339),
|
||||
},
|
||||
}
|
||||
|
||||
for _, poll := range polls {
|
||||
if err := services.CreatePollIgnore(&poll); err != nil {
|
||||
return fmt.Errorf("failed to create poll %d: %v", poll.ID, err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
log.SetOutput(os.Stdout)
|
||||
log.SetFlags(log.LstdFlags | log.Lshortfile)
|
||||
|
||||
// Initialize database with sample data
|
||||
if err := initDatabase(); err != nil {
|
||||
log.Fatalf("Failed to initialize database: %v", err)
|
||||
}
|
||||
|
||||
http.HandleFunc("/api/vote", voteHandler)
|
||||
http.HandleFunc("/api/vote/{id}", voteIDHandler)
|
||||
http.HandleFunc("/api/stats", statsHandler)
|
||||
http.HandleFunc("/api/stats/{id}", statsIDHandler)
|
||||
http.HandleFunc("/api/polls/{id}", pollsIDHandler)
|
||||
http.HandleFunc("/api/admin/new-vote", services.AdminNewVoteHandler)
|
||||
http.HandleFunc("/api/admin/view-votes", services.AdminViewVoteHandler)
|
||||
http.HandleFunc("/api/admin/login", adminLoginHandler)
|
||||
@@ -141,7 +210,7 @@ func main() {
|
||||
|
||||
buildPath := filepath.Join(".", "client", "build")
|
||||
fs := http.FileServer(http.Dir(buildPath))
|
||||
|
||||
|
||||
http.Handle("/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
// If the file exists on disk, let the file server handle it.
|
||||
if _, err := os.Stat(filepath.Join(buildPath, r.URL.Path)); err == nil {
|
||||
|
||||
Reference in New Issue
Block a user