func emailVote(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { funcPrefix := "Processing email voting" log.Debug.Printf("%s: start\n", funcPrefix) defer log.Debug.Printf("%s: end\n", funcPrefix) token := r.FormValue("token") log.Debug.Printf("%s: authenticating by token '%s' from request...\n", funcPrefix, token) _, error := authenticate(token) if error != nil { log.Error.Printf("%s: authentication failed\n", funcPrefix) w.WriteHeader(400) return } id := r.PostFormValue("vote") log.Debug.Printf("%s: getting vote with id '%s' from storage...\n", funcPrefix, id) vote, _ := storage.GetVote(id) value, _ := strconv.Atoi(r.PostFormValue("value")) log.Debug.Printf("%s: modifying vote...\n", funcPrefix) res := storage.DoVoteStatus{ Vote: storage.DoVote{ Name: vote.Name, Value: value, }, } log.Info.Printf("%s: vote '%s' has been succesfully updated!\n", funcPrefix, vote.Name) w.Header().Set("Content-Type", "application/json; charset=utf-8") w.Header().Set("Access-Control-Allow-Origin", "*") w.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS") w.Header().Set("Access-Control-Allow-Headers", "*") if json.NewEncoder(w).Encode(res) != nil { log.Error.Printf("%s: encoding response failed\n", funcPrefix) w.WriteHeader(500) } }
func getVote(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { funcPrefix := "Getting vote results" log.Debug.Printf("%s: start\n", funcPrefix) defer log.Debug.Printf("%s: end\n", funcPrefix) log.Debug.Printf("%s: authenticating user...\n", funcPrefix) user, error := authenticate(r.Header.Get("auth_token")) if error != nil { log.Error.Printf("%s: user authentication failed\n", funcPrefix) w.WriteHeader(400) return } id := ps.ByName("id") log.Debug.Printf("%s: getting vote with id '%s' from storage...\n", funcPrefix, id) vote, err := storage.GetVote(id) if err != nil { log.Error.Printf("%s: getting vote with id '%s' from storage failed: %s\n", funcPrefix, id, err.Error()) w.WriteHeader(400) return } log.Debug.Printf("%s: getting vote result status...\n", funcPrefix) res := storage.GetVoteResultStatus(*vote, *user) w.Header().Set("Content-Type", "application/json; charset=utf-8") w.Header().Set("Access-Control-Allow-Origin", "*") w.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS") w.Header().Set("Access-Control-Allow-Headers", "*") if json.NewEncoder(w).Encode(res) != nil { log.Error.Printf("%s: encoding response failed\n", funcPrefix) w.WriteHeader(500) } }
func doVote(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { funcPrefix := "Processing voting" log.Debug.Printf("%s: start\n", funcPrefix) defer log.Debug.Printf("%s: end\n", funcPrefix) log.Debug.Printf("%s: authenticating user...\n", funcPrefix) user, error := authenticate(r.Header.Get("auth_token")) if error != nil { log.Error.Printf("%s: user authentication failed\n", funcPrefix) w.WriteHeader(400) return } id := ps.ByName("id") log.Debug.Printf("%s: getting vote with id '%s' from storage...\n", funcPrefix, id) vote, err := storage.GetVote(id) if err != nil { log.Error.Printf("%s: getting vote with id '%s' from storage failed: %s\n", funcPrefix, id, err.Error()) w.WriteHeader(400) return } var params DoVotePrm log.Debug.Printf("%s: decoding params...\n", funcPrefix) err = json.NewDecoder(r.Body).Decode(¶ms) if err != nil { log.Error.Printf("%s: decoding params failed: %s\n", funcPrefix, err.Error()) w.WriteHeader(400) return } log.Debug.Printf("%s: modifying vote...\n", funcPrefix) storage.VoteProcessing(*vote, *user, params.Value) log.Debug.Printf("%s: getting vote result status...\n", funcPrefix) res := storage.GetVoteResultStatus(*vote, *user) log.Debug.Printf("%s: sending vote update event...\n", funcPrefix) *events.GetVoteUpdateChan() <- events.VoteUpdateEvent{res} log.Info.Printf("%s: vote '%s' has been succesfully updated!\n", funcPrefix, vote.Name) w.Header().Set("Content-Type", "application/json; charset=utf-8") w.Header().Set("Access-Control-Allow-Origin", "*") w.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS") w.Header().Set("Access-Control-Allow-Headers", "*") if json.NewEncoder(w).Encode(res) != nil { log.Error.Printf("%s: encoding response failed\n", funcPrefix) w.WriteHeader(500) } }
func (s *TcpSocket) handleVoteFor(packet *PulsePucket) { var params CSVoteForRequest err := json.Unmarshal(packet.content, ¶ms) if err != nil { fmt.Println(err) return } vote, err := storage.GetVote(params.Id) if err != nil { fmt.Println(err) return } storage.VoteProcessing(*vote, s.user, params.ColorId) res := storage.GetVoteResultStatus(*vote, s.user) *events.GetVoteUpdateChan() <- events.VoteUpdateEvent{res} }
func (s *TcpSocket) handleGetVote(packet *PulsePucket) { var params CSGetVoteRequest err := json.Unmarshal(packet.content, ¶ms) if err != nil { fmt.Println(err) return } vote, err := storage.GetVote(params.Id) if err != nil { fmt.Println(err) return } res := storage.GetVoteResultStatus(*vote, s.user) var b bytes.Buffer err = json.NewEncoder(&b).Encode(*res) if err == nil { packet := InitPacket(SC_GET_VOTE_RESULT, b.Bytes()) s.SendPacket(&packet) } }