Exemplo n.º 1
0
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)
	}
}
Exemplo n.º 2
0
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(&params)
	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)
	}
}
Exemplo n.º 3
0
func createVote(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
	funcPrefix := "New vote creation"
	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
	}

	var params ParamSt
	log.Debug.Printf("%s: decoding params...\n", funcPrefix)
	err := json.NewDecoder(r.Body).Decode(&params)
	if err != nil {
		log.Error.Printf("%s: decoding params failed: %s\n", funcPrefix, err.Error())
		w.WriteHeader(400)
		return
	}

	log.Debug.Printf("%s: adding new vote to storage...\n", funcPrefix)
	vote := storage.NewVote(params.Name, user.Id)

	// i think better use new goroutine
	go func() {
		log.Debug.Printf("%s: getting users from storage...\n", funcPrefix)
		users, _ := storage.GetUsers()
		log.Debug.Printf("%s: sending notifications to users...\n", funcPrefix)
		notificationSender.Send(users, *vote)
	}()

	log.Debug.Printf("%s: getting vote result status...\n", funcPrefix)
	res := storage.GetVoteResultStatus(*vote, *user)

	log.Debug.Printf("%s: sending new vote event...\n", funcPrefix)
	*events.GetNewVoteChan() <- events.NewVoteEvent{res}

	log.Info.Printf("%s: new vote '%s' has been succesfully created!\n", funcPrefix, params.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)
	}
}
Exemplo n.º 4
0
func (s *TcpSocket) handleNewVote(packet *PulsePucket) {
	var params CSCreateVoteRequest
	err := json.Unmarshal(packet.content, &params)
	if err != nil {
		fmt.Println(err)
		return
	}

	vote := storage.NewVote(params.Name, s.user.Id)

	res := storage.GetVoteResultStatus(*vote, s.user)

	go func() {
		users, _ := storage.GetUsers()
		notificationSender.Send(users, *vote)
	}()

	*events.GetNewVoteChan() <- events.NewVoteEvent{res}
}
Exemplo n.º 5
0
func (s *TcpSocket) handleVoteFor(packet *PulsePucket) {
	var params CSVoteForRequest
	err := json.Unmarshal(packet.content, &params)
	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}
}
Exemplo n.º 6
0
func (s *TcpSocket) handleGetVote(packet *PulsePucket) {
	var params CSGetVoteRequest
	err := json.Unmarshal(packet.content, &params)
	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)
	}
}