Esempio n. 1
0
func getVotes(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
	funcPrefix := "Getting all votes with 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
	}
	log.Debug.Printf("%s: getting all votes with results from storage...\n", funcPrefix)
	votes := storage.GetAllVotesWithResult(*user)
	res := storage.VotesStatus{
		Votes: votes,
	}
	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) handleGetVotes(packet *PulsePucket) {
	votes := storage.GetAllVotesWithResult(s.user)
	res := storage.VotesStatus{
		Votes: votes,
	}

	var b bytes.Buffer
	err := json.NewEncoder(&b).Encode(res)
	if err == nil {
		packet := InitPacket(SC_GET_VOTES_RESULT, b.Bytes())
		s.SendPacket(&packet)
	}
}