示例#1
0
func getUser(a *application.App, w http.ResponseWriter, r *http.Request) error {
	vars := mux.Vars(r)
	email := strings.ToLower(vars["email"])

	um := models.NewUserModel(a.DB)
	user, err := um.FindOne(nil, squirrel.Eq{"users.email": email})
	if err != nil {
		return errors.Wrap(err, "find one error")
	}

	pm := models.NewPostModel(a.DB)
	createdPosts, err := pm.Find(nil, squirrel.Eq{"posts.creator_user_id": user.ID})
	if err != nil {
		return err
	}

	data := context.TemplateData(r)
	data["User"] = user
	data["CreatedPosts"] = createdPosts
	if err = addUserUpvotedPostIDsToData(r, pm, data); err != nil {
		return errors.Wrap(err, "add upvoted post ids to data error")
	}
	err = libtemplate.Render(w, a.Templates, "user.html", data)
	return errors.Wrap(err, "render template error")
}
示例#2
0
// SetPost sets the post with the id in the url in the context and template data.
func (m *Middleware) SetPost(next http.Handler) http.Handler {
	fn := func(w http.ResponseWriter, r *http.Request) {
		vars := mux.Vars(r)
		postID, err := strconv.ParseInt(vars["postID"], 10, 64)
		if err != nil {
			httperror.HandleError(w, httperror.StatusError{http.StatusBadRequest, err})
			return
		}

		pm := models.NewPostModel(m.App.DB)
		topic := context.Topic(r)
		post, err := pm.FindOne(nil, squirrel.Eq{"posts.id": postID, "posts.topic_id": topic.ID})
		if err != nil {
			httperror.HandleError(w, errors.Wrap(err, "find one error"))
			return
		}
		context.SetPost(r, post)

		templateData := context.TemplateData(r)
		templateData["Post"] = post

		next.ServeHTTP(w, r)
	}

	return http.HandlerFunc(fn)
}
示例#3
0
// SetSessionUser sets the session user in the context and template data.
func (m *Middleware) SetSessionUser(next http.Handler) http.Handler {
	fn := func(w http.ResponseWriter, r *http.Request) {
		templateData := context.TemplateData(r)

		us := session.NewUserSession(m.App.Store)
		userID, ok := us.SessionUserID(r)
		if !ok {
			templateData["SessionUser"] = &models.User{}
			next.ServeHTTP(w, r)
			return
		}

		um := models.NewUserModel(m.App.DB)
		user, err := um.FindOne(nil, squirrel.Eq{"users.id": userID})
		if err != nil {
			us.Delete(w, r)
			http.Redirect(w, r, "/", http.StatusFound)
			return
		}
		context.SetSessionUser(r, user)
		templateData["SessionUser"] = user
		next.ServeHTTP(w, r)
	}

	return http.HandlerFunc(fn)
}
示例#4
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")
}
示例#5
0
func getNewPost(a *application.App, w http.ResponseWriter, r *http.Request) error {
	topic := context.Topic(r)

	tm := models.NewTagModel(a.DB)
	tags, err := tm.Find(nil, squirrel.Eq{"tags.topic_id": topic.ID})
	if err != nil {
		return errors.Wrap(err, "find error")
	}

	data := context.TemplateData(r)
	data["Tags"] = tags
	return libtemplate.Render(w, a.Templates, "new_post.html", data)
}
示例#6
0
func getPostsByTag(a *application.App, w http.ResponseWriter, r *http.Request) error {
	tag := context.Tag(r)

	pm := models.NewPostModel(a.DB)
	posts, err := pm.Find(nil, squirrel.Eq{"post_tags.tag_id": tag.ID})
	if err != nil {
		return errors.Wrap(err, "find error")
	}

	data := context.TemplateData(r)
	data["Posts"] = posts
	if err = addUserUpvotedPostIDsToData(r, pm, data); err != nil {
		return errors.Wrap(err, "add upvoted post ids to data error")
	}

	err = libtemplate.Render(w, a.Templates, "posts_by_tag.html", data)
	return errors.Wrap(err, "render template error")
}
示例#7
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)
}
示例#8
0
func getPosts(a *application.App, w http.ResponseWriter, r *http.Request) error {
	topic := context.Topic(r)

	whereEq := squirrel.Eq{"posts.topic_id": topic.ID, "posts.is_pinned": true}

	pm := models.NewPostModel(a.DB)
	pinnedPosts, err := pm.Find(nil, whereEq)
	switch {
	case err == sql.ErrNoRows:
		pinnedPosts = make([]*models.Post, 0)
	case err != nil:
		return errors.Wrap(err, "find error")
	}

	whereEq["posts.is_pinned"] = false
	unpinnedPosts, err := pm.Find(nil, whereEq)
	switch {
	case err == sql.ErrNoRows:
		unpinnedPosts = make([]*models.Post, 0)
	case err != nil:
		return errors.Wrap(err, "find error")
	}

	tagModel := models.NewTagModel(a.DB)
	tags, err := tagModel.Find(nil, squirrel.Eq{"tags.topic_id": topic.ID})
	if err != nil {
		return errors.Wrap(err, "find error")
	}

	data := context.TemplateData(r)
	data["PinnedPosts"] = pinnedPosts
	data["UnpinnedPosts"] = unpinnedPosts
	data["Tags"] = tags

	if err = addUserUpvotedPostIDsToData(r, pm, data); err != nil {
		return errors.Wrap(err, "add upvoted post ids to data error")
	}

	return libtemplate.Render(w, a.Templates, "posts.html", data)
}
示例#9
0
func getPost(a *application.App, w http.ResponseWriter, r *http.Request) error {
	return libtemplate.Render(w, a.Templates, "post.html", context.TemplateData(r))
}
示例#10
0
func getNewTag(a *application.App, w http.ResponseWriter, r *http.Request) error {
	err := libtemplate.Render(w, a.Templates, "new_tag.html", context.TemplateData(r))
	return errors.Wrap(err, "render template error")
}