示例#1
0
/*
Retrieves all tags used in blog posts. Tags are ordered by most frequently used first.

URL: /v1/tags
*/
func GetTags_v1(writer http.ResponseWriter, request *http.Request) {
	tags, err := postservice.GetTags()
	if err != nil {
		log.Println("ERROR - ", err.Error())
		httpservice.Error(writer, "There was an error getting tags")
		return
	}

	httpservice.WriteJson(writer, tags, 200)
}
示例#2
0
/*
Retrieves a single page of blog posts. Only published posts are retrieved.
This method is bested used when displaying published posts, such as on
a blog home page.

URL: /v1/posts/{pageNumber}
*/
func GetPageOfPublicPosts_v1(writer http.ResponseWriter, request *http.Request) {
	vars := mux.Vars(request)

	pageNumber, _ := strconv.Atoi(vars["pageNumber"])

	posts, err := postservice.GetPublishedPosts(pageNumber, environment.GetPostsPerPageSetting())
	if err != nil {
		log.Println("ERROR - ", err.Error())
		httpservice.Error(writer, "There was an error getting published posts")
		return
	}

	httpservice.WriteJson(writer, posts, 200)
}
示例#3
0
/*
Retrieves a single tag by ID.

URL: /v1/tag/{id}
*/
func GetTag_v1(writer http.ResponseWriter, request *http.Request) {
	vars := mux.Vars(request)

	id, _ := strconv.Atoi(vars["id"])

	tag, err := postservice.GetTag(id)
	if err != nil {
		log.Println("ERROR - ", err.Error())
		httpservice.Error(writer, fmt.Sprintf("There was an error getting the tag with ID %d", id))
		return
	}

	if tag.Id == 0 {
		httpservice.NotFound(writer, fmt.Sprintf("Could not find tag %d", id))
		return
	}

	httpservice.WriteJson(writer, tag, 200)
}
示例#4
0
/*
Retrieves a single post entry by year, month, and slug.

URL: /v1/post/{year}/{month}/{slug}
*/
func GetPost_v1(writer http.ResponseWriter, request *http.Request) {
	vars := mux.Vars(request)

	year, _ := strconv.Atoi(vars["year"])
	month, _ := strconv.Atoi(vars["month"])
	slug := vars["slug"]

	post, err := postservice.GetPost(year, month, slug)
	if err != nil {
		log.Println("ERROR - ", err.Error())
		httpservice.Error(writer, "There was a problem getting your post")
		return
	}

	/*
	 * If the post comes back empty return a 404
	 */
	if post.Id == 0 {
		httpservice.NotFound(writer, fmt.Sprintf("Post for %s could not be found", postservice.CreatePermalink(year, month, slug)))
		return
	}

	httpservice.WriteJson(writer, post, 200)
}