示例#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
文件: random.go 项目: firba1/irq
func Random(db model.Model, r render.Render, req *http.Request, isJson IsJson) {
	qs := req.URL.Query()

	search := qs.Get("query")

	maxLines, err := strconv.Atoi(qs.Get("max-lines"))
	if err != nil || maxLines < 1 {
		maxLines = 0
	}

	quotes, err := db.GetQuotes(model.Query{
		Limit:       1,
		Search:      search,
		MaxLines:    maxLines,
		OrderBy:     []string{"rand()"},
		IncludeTags: qs["tags"],
		ExcludeTags: qs["exclude-tags"],
	})

	if err != nil || len(quotes) == 0 {
		RenderError(r, 500, isJson, fmt.Sprint("quote not found", err))
		return
	}

	quote := quotes[0]

	if isJson {
		r.JSON(200, quote)
	} else {
		r.Redirect(fmt.Sprintf("/quote/%d", quote.ID))
	}
}
示例#3
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)
}
示例#4
0
文件: submit.go 项目: firba1/irq
func SubmitForm(db model.Model, r render.Render, request *http.Request) {
	text := request.FormValue("text")
	tags := strings.Split(strings.ToLower(request.FormValue("tags")), ",")

	err := db.AddQuote(model.Quote{
		Text: text,
		Tags: tags,
	})
	if err != nil {
		RenderError(r, 404, IsJson(false), fmt.Sprint("unable to add quote", err))
	}

	r.Redirect("/latest")
}
示例#5
0
文件: submit.go 项目: firba1/irq
func Submit(db model.Model, r render.Render) {
	popularTags, err := db.GetPopularTags(10)
	if err != nil {
		RenderError(r, 404, IsJson(false), fmt.Sprint("unable to get tags", err))
		return
	}

	r.HTML(200, "submit", submitPageEnv{
		PageEnv: PageEnv{
			Title: "Submit",
		},
		PopularTags: popularTags,
	})
}
示例#6
0
文件: delete.go 项目: firba1/irq
func Delete(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
	}

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

	r.JSON(200, nil)
}