示例#1
0
func delHighlightHandler(key string, w http.ResponseWriter, r *http.Request) {
	var arg FetchCommentsEnv
	err := util.StringToJSON(key, &arg)
	if err != nil {
		log.Printf("FATAL ERROR decoding closure %v in setScoreHandler", key)
		w.WriteHeader(500)
		return
	}
	if r.Method != "POST" {
		log.Printf("%v SECURITY ERROR %v trying to %v to %v", r.RemoteAddr,
			arg.ReviewerId, r.Method, r.URL)
		w.WriteHeader(http.StatusBadRequest)
		r.Close = true
		return
	}
	err = dept.DelHighlight(arg.AppId, string(arg.ReviewerId))
	if err != nil {
		w.WriteHeader(http.StatusBadRequest)
		r.Close = true
		log.Printf("%v ERROR DelHighlight(%v, %v): %v", r.RemoteAddr, arg.AppId,
			arg.ReviewerId, err)
		return
	}
	w.WriteHeader(200)
}
示例#2
0
func setHighlightHandler(v string, w http.ResponseWriter, r *http.Request) {
	// TODO: use and enforce PUT
	var arg FetchCommentsEnv
	err := util.StringToJSON(v, &arg)
	if err != nil {
		panic(err)
	}
	var req struct {
		ReaderId string `json:"readerId"`
	}
	err = util.ReaderToJSON(r.Body, int(r.ContentLength), &req)
	if err != nil {
		panic(err)
	}
	now := time.Now().Unix()
	hl := &model.Highlight{
		arg.AppId,
		model.ReviewerId(req.ReaderId),
		arg.ReviewerId,
		arg.ReviewerName,
		float64(now),
	}
	err = dept.SetHighlight(hl)
	if err != nil {
		panic(err)
	}
	w.WriteHeader(200)
}
示例#3
0
func postCommentHandler(v string, w http.ResponseWriter, r *http.Request) {
	// TODO: use and enforce PUT
	var arg FetchCommentsEnv
	err := util.StringToJSON(v, &arg)
	if err != nil {
		panic(err)
	}

	buf := make([]byte, r.ContentLength)
	_, err = io.ReadFull(r.Body, buf)
	if err != nil {
		panic(err)
	}

	now := time.Now().Unix()

	err = dept.NewComment(&model.Comment{arg.AppId, arg.ReviewerId,
		arg.ReviewerName, float64(now), string(buf)})
	if err != nil {
		panic(err)
	}
	w.WriteHeader(200)
}
示例#4
0
func setScoreHandler(key string, w http.ResponseWriter, r *http.Request) {
	var arg FetchCommentsEnv
	err := util.StringToJSON(key, &arg)
	if err != nil {
		log.Printf("FATAL ERROR decoding closure %v in setScoreHandler", key)
		w.WriteHeader(500)
		return
	}
	if r.Method != "POST" {
		log.Printf("%v SECURITY ERROR %v trying to %v to %v", r.RemoteAddr,
			arg.ReviewerId, r.Method, r.URL)
		w.WriteHeader(http.StatusBadRequest)
		r.Close = true
		return
	}
	var req struct {
		Label string `json:"label"`
		Score *int   `json:"score"`
	}
	err = util.ReaderToJSON(r.Body, int(r.ContentLength), &req)
	if err != nil && req.Label != "score" {
		w.WriteHeader(http.StatusBadRequest)
		r.Close = true
		return
	}
	score := &model.Score{arg.AppId, arg.ReviewerId, req.Label, req.Score}
	err = dept.SetScore(score)
	if err != nil {
		w.WriteHeader(500)
		r.Close = true
		log.Printf("%v ERROR SetScore(%v)", r.RemoteAddr, score)
		return
	}
	w.WriteHeader(200)
	return
}