Adding initial code for the main.go as well as initial code for uploading members
This commit is contained in:
126
server/main.go
Normal file
126
server/main.go
Normal file
@@ -0,0 +1,126 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
"go-sjles-pta-vote/server/services"
|
||||
)
|
||||
|
||||
func voteHandler(w http.ResponseWriter, r *http.Request) {
|
||||
var vote services.Vote
|
||||
if err := json.NewDecoder(r.Body).Decode(&vote); err != nil {
|
||||
http.Error(w, "Invalid request payload", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
err := services.SetVote(&vote)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
|
||||
func voteIDHandler(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
id := vars["id"]
|
||||
|
||||
vote := services.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)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
|
||||
func statsHandler(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
id := vars["id"]
|
||||
|
||||
poll, err := services.GetPollByQuestion(id)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
json.NewEncoder(w).Encode(poll)
|
||||
}
|
||||
|
||||
func statsIDHandler(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
id := vars["id"]
|
||||
|
||||
poll, err := services.GetPollByQuestion(id)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
json.NewEncoder(w).Encode(poll)
|
||||
}
|
||||
|
||||
func adminHandler(w http.ResponseWriter, r *http.Request) {
|
||||
// Add admin functionality here
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
|
||||
func adminIDHandler(w http.ResponseWriter, r *http.Request) {
|
||||
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")
|
||||
if err != nil {
|
||||
http.Error(w, "Failed to upload file", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
fileBytes, err := ioutil.ReadAll(file)
|
||||
if err != nil {
|
||||
http.Error(w, "Failed to read file", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
members, err := services.ParseMembersFromBytes(2023, fileBytes) // Assuming year 2023 for demonstration purposes
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
json.NewEncoder(w).Encode(members)
|
||||
} else {
|
||||
w.WriteHeader(http.StatusMethodNotAllowed)
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
r := mux.NewRouter()
|
||||
|
||||
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.Fatal(http.ListenAndServe(":8080", r))
|
||||
}
|
||||
Reference in New Issue
Block a user