128 lines
2.9 KiB
Go
128 lines
2.9 KiB
Go
package vote
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
|
|
"tomatentum.net/outfit-voting-abi26/internal/database"
|
|
)
|
|
|
|
type SetVoteRequest struct {
|
|
Votes int
|
|
}
|
|
|
|
type AddVoteEntryRequest struct {
|
|
Name string
|
|
Category string
|
|
}
|
|
|
|
type BulkAddVoteEntryRequest struct {
|
|
Category string
|
|
names []string
|
|
}
|
|
|
|
func GetVoteOptionsEndpoint(w http.ResponseWriter, r *http.Request) {
|
|
entries, err := database.GetVoteOptions()
|
|
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
w.Header().Add("Content-Type", "application/json")
|
|
json.NewEncoder(w).Encode(entries)
|
|
}
|
|
|
|
func SetVoteEndpoint(w http.ResponseWriter, r *http.Request) {
|
|
id := r.PathValue("id")
|
|
var request SetVoteRequest
|
|
if err := json.NewDecoder(r.Body).Decode(&request); err != nil {
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
return
|
|
}
|
|
r.Body.Close()
|
|
|
|
if err := database.SetVote(id, request.Votes); err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
}
|
|
|
|
func IncVoteEndpoint(w http.ResponseWriter, r *http.Request) {
|
|
id := r.PathValue("id")
|
|
|
|
if err := database.IncVote(id); err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
}
|
|
|
|
func DelVoteEntryEndpoint(w http.ResponseWriter, r *http.Request) {
|
|
id := r.PathValue("id")
|
|
|
|
if err := database.DelVoteEntry(id); err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
}
|
|
|
|
func BulkDelVoteEntryEndpoint(w http.ResponseWriter, r *http.Request) {
|
|
var ids []string
|
|
if err := json.NewDecoder(r.Body).Decode(&ids); err != nil {
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
return
|
|
}
|
|
r.Body.Close()
|
|
|
|
if err := database.BulkDelVoteEntry(ids); err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
}
|
|
|
|
func AddVoteEntryEndpoint(w http.ResponseWriter, r *http.Request) {
|
|
var request AddVoteEntryRequest
|
|
if err := json.NewDecoder(r.Body).Decode(&request); err != nil {
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
return
|
|
}
|
|
r.Body.Close()
|
|
|
|
entry, err := database.InsertVoteEntry(request.Name, request.Category)
|
|
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
w.Header().Add("Content-Type", "application/json")
|
|
json.NewEncoder(w).Encode(entry)
|
|
}
|
|
|
|
func BulkVoteEntryEndpoint(w http.ResponseWriter, r *http.Request) {
|
|
var request BulkAddVoteEntryRequest
|
|
if err := json.NewDecoder(r.Body).Decode(&request); err != nil {
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
return
|
|
}
|
|
r.Body.Close()
|
|
|
|
entry, err := database.BulkInsertVoteEntry(request.names, request.Category)
|
|
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
w.Header().Add("Content-Type", "application/json")
|
|
json.NewEncoder(w).Encode(entry)
|
|
}
|