示例#1
0
func getTopics(a *application.App, w http.ResponseWriter, r *http.Request) error {
	tm := models.NewTopicModel(a.DB)
	topics, err := tm.Find(nil)
	if err != nil {
		return errors.Wrap(err, "find error")
	}

	data := context.TemplateData(r)
	data["Topics"] = topics
	err = libtemplate.Render(w, a.Templates, "topics.html", data)
	return errors.Wrap(err, "render template error")
}
示例#2
0
func postNewTopic(a *application.App, w http.ResponseWriter, r *http.Request) error {
	name := r.FormValue("name")
	title := r.FormValue("title")
	description := r.FormValue("description")

	tm := models.NewTopicModel(a.DB)

	topic := &models.Topic{Name: name, Title: title, Description: description}
	if err := tm.Add(nil, topic); err != nil {
		return err
	}

	http.Redirect(w, r, topic.URL(), http.StatusFound)
	return nil
}
示例#3
0
// SetTopic sets the topic with the name in the url in the context and template data.
func (m *Middleware) SetTopic(next http.Handler) http.Handler {
	fn := func(w http.ResponseWriter, r *http.Request) {
		vars := mux.Vars(r)
		topicName := strings.ToLower(vars["topicName"])
		tm := models.NewTopicModel(m.App.DB)
		topic, err := tm.FindOne(nil, squirrel.Eq{"topics.name": topicName})
		if err != nil {
			httperror.HandleError(w, errors.Wrap(err, "find one error"))
			return
		}

		context.SetTopic(r, topic)

		templateData := context.TemplateData(r)
		templateData["Topic"] = topic
		next.ServeHTTP(w, r)
	}

	return http.HandlerFunc(fn)
}