Adding page to create votes and view the votes, rearanging some methods
This commit is contained in:
175
server/main.go
175
server/main.go
@@ -2,7 +2,6 @@ package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
@@ -10,32 +9,33 @@ import (
|
||||
"path/filepath"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
|
||||
"go-sjles-pta-vote/server/common"
|
||||
"go-sjles-pta-vote/server/models"
|
||||
"go-sjles-pta-vote/server/services"
|
||||
)
|
||||
|
||||
func voteHandler(w http.ResponseWriter, r *http.Request) {
|
||||
func voteHandler(resWriter http.ResponseWriter, request *http.Request) {
|
||||
var vote models.Vote
|
||||
if err := json.NewDecoder(r.Body).Decode(&vote); err != nil {
|
||||
http.Error(w, "Invalid request payload", http.StatusBadRequest)
|
||||
if err := json.NewDecoder(request.Body).Decode(&vote); err != nil {
|
||||
common.SendError(resWriter, "Invalid JSON", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
err := services.SetVote(&vote)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
if err := services.SetVote(&vote); err != nil {
|
||||
common.SendError(resWriter, "Failed to set vote", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
resWriter.WriteHeader(http.StatusOK)
|
||||
}
|
||||
|
||||
func voteIDHandler(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
func voteIDHandler(resWriter http.ResponseWriter, request *http.Request) {
|
||||
vars := mux.Vars(request)
|
||||
idStr := vars["id"]
|
||||
id, err := strconv.ParseInt(idStr, 10, 64)
|
||||
if err != nil {
|
||||
http.Error(w, "Invalid poll ID", http.StatusBadRequest)
|
||||
common.SendError(resWriter, "Invalid poll ID", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -47,181 +47,84 @@ func voteIDHandler(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
err = services.SetVote(&vote)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
common.SendError(resWriter, "Failed to set vote", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
resWriter.WriteHeader(http.StatusOK)
|
||||
}
|
||||
|
||||
func statsHandler(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method == "GET" {
|
||||
func statsHandler(resWriter http.ResponseWriter, request *http.Request) {
|
||||
if request.Method == http.MethodGet {
|
||||
filePath := "./server/templates/stats.html"
|
||||
http.ServeFile(w, r, filePath)
|
||||
} else if r.Method == "POST" {
|
||||
vars := mux.Vars(r)
|
||||
http.ServeFile(resWriter, request, filePath)
|
||||
} else if request.Method == http.MethodPost {
|
||||
vars := mux.Vars(request)
|
||||
id := vars["id"]
|
||||
|
||||
poll, err := services.GetPollByQuestion(id)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
common.SendError(resWriter, "Failed to get poll", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
json.NewEncoder(w).Encode(poll)
|
||||
json.NewEncoder(resWriter).Encode(poll)
|
||||
} else {
|
||||
w.WriteHeader(http.StatusMethodNotAllowed)
|
||||
resWriter.WriteHeader(http.StatusMethodNotAllowed)
|
||||
}
|
||||
}
|
||||
|
||||
func statsIDHandler(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
func statsIDHandler(resWriter http.ResponseWriter, request *http.Request) {
|
||||
vars := mux.Vars(request)
|
||||
id := vars["id"]
|
||||
|
||||
poll, err := services.GetPollByQuestion(id)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
common.SendError(resWriter, "Failed to get poll", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
json.NewEncoder(w).Encode(poll)
|
||||
json.NewEncoder(resWriter).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 adminLoginHandler(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
w.WriteHeader(http.StatusMethodNotAllowed)
|
||||
json.NewEncoder(w).Encode(map[string]string{"error": "Method not allowed"})
|
||||
func adminLoginHandler(resWriter http.ResponseWriter, request *http.Request) {
|
||||
if request.Method != http.MethodPost {
|
||||
common.SendError(resWriter, "Method not allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
var loginReq services.LoginRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&loginReq); err != nil {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
json.NewEncoder(w).Encode(map[string]string{"error": "Invalid request payload"})
|
||||
if err := json.NewDecoder(request.Body).Decode(&loginReq); err != nil {
|
||||
common.SendError(resWriter, "Invalid JSON", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
// Validate admin credentials
|
||||
isValid, err := services.ValidateAdminLogin(loginReq.Username, loginReq.Password)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
json.NewEncoder(w).Encode(map[string]string{"error": err.Error()})
|
||||
common.SendError(resWriter, "Invalid username or password", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
if !isValid {
|
||||
w.WriteHeader(http.StatusUnauthorized)
|
||||
json.NewEncoder(w).Encode(map[string]string{"error": "Invalid username or password"})
|
||||
common.SendError(resWriter, "Invalid username or password", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
// Generate JWT token
|
||||
token, err := services.GenerateAuthToken(loginReq.Username)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
json.NewEncoder(w).Encode(map[string]string{"error": "Failed to generate token"})
|
||||
common.SendError(resWriter, "Failed to generate auth token", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
json.NewEncoder(w).Encode(services.LoginResponse{
|
||||
resWriter.WriteHeader(http.StatusOK)
|
||||
json.NewEncoder(resWriter).Encode(services.LoginResponse{
|
||||
Success: true,
|
||||
Token: token,
|
||||
})
|
||||
}
|
||||
|
||||
func adminMembersHandler(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
w.WriteHeader(http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
var year int
|
||||
var err error
|
||||
|
||||
if err = r.ParseMultipartForm(10 << 20); err != nil {
|
||||
http.Error(w, "Failed to parse multipart form", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
year_from_form := r.FormValue("year")
|
||||
if year_from_form == "" {
|
||||
http.Error(w, "Year is required", http.StatusBadRequest)
|
||||
return
|
||||
} else {
|
||||
year, err = strconv.Atoi(year_from_form)
|
||||
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
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
fileBytes, err := ioutil.ReadAll(file)
|
||||
if err != nil {
|
||||
http.Error(w, "Failed to read file", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
if err = services.ParseMembersFromBytes(year, fileBytes); err != nil {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
json.NewEncoder(w).Encode(map[string]string{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
json.NewEncoder(w).Encode(map[string]bool{"success": true})
|
||||
}
|
||||
|
||||
func adminMembersView(w http.ResponseWriter, r *http.Request) {
|
||||
yearStr := r.URL.Query().Get("year")
|
||||
if yearStr == "" {
|
||||
http.Error(w, "Year is required", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
year, err := strconv.Atoi(yearStr)
|
||||
if err != nil {
|
||||
http.Error(w, "Invalid year", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
members, err := services.GetMembersByYear(year)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
json.NewEncoder(w).Encode(map[string]interface{}{
|
||||
"success": false,
|
||||
"error": err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
json.NewEncoder(w).Encode(map[string]interface{}{
|
||||
"success": true,
|
||||
"members": members,
|
||||
})
|
||||
}
|
||||
|
||||
func main() {
|
||||
log.SetOutput(os.Stdout)
|
||||
log.SetFlags(log.LstdFlags | log.Lshortfile)
|
||||
@@ -230,11 +133,11 @@ func main() {
|
||||
http.HandleFunc("/api/vote/{id}", voteIDHandler)
|
||||
http.HandleFunc("/api/stats", statsHandler)
|
||||
http.HandleFunc("/api/stats/{id}", statsIDHandler)
|
||||
http.HandleFunc("/api/admin", adminHandler)
|
||||
http.HandleFunc("/api/admin/{id}", adminIDHandler)
|
||||
http.HandleFunc("/api/admin/new-vote", services.AdminNewVoteHandler)
|
||||
http.HandleFunc("/api/admin/view-votes", services.AdminViewVoteHandler)
|
||||
http.HandleFunc("/api/admin/login", adminLoginHandler)
|
||||
http.HandleFunc("/api/admin/members", adminMembersHandler)
|
||||
http.HandleFunc("/api/admin/members/view", adminMembersView)
|
||||
http.HandleFunc("/api/admin/members", services.AdminMembersHandler)
|
||||
http.HandleFunc("/api/admin/members/view", services.AdminMembersView)
|
||||
|
||||
buildPath := filepath.Join(".", "client", "build")
|
||||
fs := http.FileServer(http.Dir(buildPath))
|
||||
|
||||
Reference in New Issue
Block a user