示例#1
0
文件: vote.go 项目: firba1/irq
func Vote(db model.Model, req *http.Request, r render.Render, params martini.Params) {
	id, err := strconv.Atoi(params["id"])
	if err != nil {
		r.JSON(404, ErrorEnv{"invalid quote id"})
		return
	}

	count, err := strconv.Atoi(req.FormValue("count"))
	if err != nil {
		r.JSON(404, ErrorEnv{"invalid vote count"})
		return
	}

	err = db.VoteQuote(id, count)
	if err != nil {
		r.JSON(500, ErrorEnv{"unable to vote quote"})
		return
	}

	quote, err := db.GetQuote(id)
	if err != nil {
		r.JSON(404, ErrorEnv{"quote not found"})
		return
	}

	env := struct {
		Score int `json:"score"`
	}{quote.Score}
	r.JSON(200, env)
}
示例#2
0
文件: quote.go 项目: firba1/irq
func Quote(db model.Model, r render.Render, params martini.Params, isJson IsJson) {
	id, err := strconv.Atoi(params["id"])
	if err != nil {
		RenderError(r, 404, isJson, "invalid quote id")
		return
	}

	quote, err := db.GetQuote(id)
	if err != nil {
		RenderError(r, 404, isJson, "quote not found")
		return
	}

	if isJson {
		r.JSON(200, quote)
		return
	}

	env := quotePageEnv{
		PageEnv:        PageEnv{Title: fmt.Sprintf("#%d", quote.ID)},
		Quotes:         []model.Quote{quote},
		ShowPagination: false,
	}
	r.HTML(200, "quote", env)
}