Exemple #1
0
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)
}
Exemple #2
0
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))
	}
}
Exemple #3
0
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)
}
Exemple #4
0
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")
}
Exemple #5
0
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,
	})
}
Exemple #6
0
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)
}