Adding not just the api but also some initial code for posting members
This commit is contained in:
100
server/main.go
100
server/main.go
@@ -5,13 +5,16 @@ import (
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"strconv"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
"go-sjles-pta-vote/server/models"
|
||||
"go-sjles-pta-vote/server/services"
|
||||
)
|
||||
|
||||
func voteHandler(w http.ResponseWriter, r *http.Request) {
|
||||
var vote services.Vote
|
||||
var vote models.Vote
|
||||
if err := json.NewDecoder(r.Body).Decode(&vote); err != nil {
|
||||
http.Error(w, "Invalid request payload", http.StatusBadRequest)
|
||||
return
|
||||
@@ -28,15 +31,20 @@ func voteHandler(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
func voteIDHandler(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
id := vars["id"]
|
||||
idStr := vars["id"]
|
||||
id, err := strconv.ParseInt(idStr, 10, 64)
|
||||
if err != nil {
|
||||
http.Error(w, "Invalid poll ID", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
vote := services.Vote{
|
||||
vote := models.Vote{
|
||||
PollId: id,
|
||||
Email: "example@example.com", // Replace with actual email retrieval logic
|
||||
Vote: true, // Replace with actual vote retrieval logic
|
||||
}
|
||||
|
||||
err := services.SetVote(&vote)
|
||||
err = services.SetVote(&vote)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
@@ -46,16 +54,24 @@ func voteIDHandler(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func statsHandler(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
id := vars["id"]
|
||||
if r.Method == "GET" {
|
||||
filePath := "./server/templates/stats.html"
|
||||
log.Printf("Serving stats.html from %s", filePath)
|
||||
http.ServeFile(w, r, filePath)
|
||||
} else if r.Method == "POST" {
|
||||
vars := mux.Vars(r)
|
||||
id := vars["id"]
|
||||
|
||||
poll, err := services.GetPollByQuestion(id)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
poll, err := services.GetPollByQuestion(id)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
json.NewEncoder(w).Encode(poll)
|
||||
} else {
|
||||
w.WriteHeader(http.StatusMethodNotAllowed)
|
||||
}
|
||||
|
||||
json.NewEncoder(w).Encode(poll)
|
||||
}
|
||||
|
||||
func statsIDHandler(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -77,16 +93,31 @@ func adminHandler(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func adminIDHandler(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
id := vars["id"]
|
||||
//vars := mux.Vars(r)
|
||||
//id := vars["id"]
|
||||
|
||||
// Add admin functionality here
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
|
||||
func membersHandler(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method == "POST" {
|
||||
file, handler, err := r.FormFile("members.csv")
|
||||
func adminMembersHandler(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method == "GET" {
|
||||
filePath := "./server/templates/members.html"
|
||||
log.Printf("Serving members.html from %s", filePath)
|
||||
http.ServeFile(w, r, filePath)
|
||||
} else if r.Method == "POST" {
|
||||
var year int
|
||||
var err error
|
||||
r.ParseForm()
|
||||
if y := r.FormValue("year"); y != "" {
|
||||
year, err = strconv.Atoi(y)
|
||||
if err != nil {
|
||||
http.Error(w, "Invalid year", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
file, _, err := r.FormFile("members.csv")
|
||||
if err != nil {
|
||||
http.Error(w, "Failed to upload file", http.StatusBadRequest)
|
||||
return
|
||||
@@ -99,28 +130,37 @@ func membersHandler(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
members, err := services.ParseMembersFromBytes(2023, fileBytes) // Assuming year 2023 for demonstration purposes
|
||||
err = services.ParseMembersFromBytes(year, fileBytes)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
json.NewEncoder(w).Encode(map[string]string{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
json.NewEncoder(w).Encode(members)
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
json.NewEncoder(w).Encode(map[string]bool{"success": true})
|
||||
} else {
|
||||
w.WriteHeader(http.StatusMethodNotAllowed)
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
r := mux.NewRouter()
|
||||
log.SetOutput(os.Stdout)
|
||||
log.SetFlags(log.LstdFlags | log.Lshortfile)
|
||||
|
||||
r.HandleFunc("/vote", voteHandler).Methods("POST")
|
||||
r.HandleFunc("/vote/{id}", voteIDHandler).Methods("POST")
|
||||
r.HandleFunc("/stats", statsHandler).Methods("POST")
|
||||
r.HandleFunc("/stats/{id}", statsIDHandler).Methods("POST")
|
||||
r.HandleFunc("/admin", adminHandler).Methods("GET")
|
||||
r.HandleFunc("/admin/{id}", adminIDHandler).Methods("GET")
|
||||
r.HandleFunc("/admin/members", membersHandler).Methods("POST")
|
||||
log.Printf("Starting server on :8080")
|
||||
|
||||
log.Fatal(http.ListenAndServe(":8080", r))
|
||||
http.HandleFunc("/vote", voteHandler)
|
||||
http.HandleFunc("/vote/{id}", voteIDHandler)
|
||||
http.HandleFunc("/stats", statsHandler)
|
||||
http.HandleFunc("/stats/{id}", statsIDHandler)
|
||||
http.HandleFunc("/admin", adminHandler)
|
||||
http.HandleFunc("/admin/{id}", adminIDHandler)
|
||||
http.HandleFunc("/admin/members", adminMembersHandler)
|
||||
http.HandleFunc("/favicon.ico", func(w http.ResponseWriter, r *http.Request) {
|
||||
filePath := "./server/icons/favicon.ico"
|
||||
http.ServeFile(w, r, filePath)
|
||||
})
|
||||
|
||||
log.Fatal(http.ListenAndServe(":8080", nil))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user