Пример #1
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)
}
Пример #2
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)
}