From f1fa5e8b910bce19aadbfc70d16c8d70efcece2e Mon Sep 17 00:00:00 2001 From: Howl Date: Wed, 10 Aug 2016 09:45:01 +0200 Subject: [PATCH] Allow to get a blog post's content by slug --- app/v1/blog.go | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/app/v1/blog.go b/app/v1/blog.go index 30ca9ab..da99f73 100644 --- a/app/v1/blog.go +++ b/app/v1/blog.go @@ -74,11 +74,22 @@ func BlogPostsContentGET(md common.MethodData) common.CodeMessager { if _, present := md.C.GetQuery("html"); present { field = "html" } - if md.C.Query("id") == "" { - return ErrMissingField("id") + var ( + by string + val string + ) + switch { + case md.C.Query("slug") != "": + by = "slug" + val = md.C.Query("slug") + case md.C.Query("id") != "": + by = "id" + val = md.C.Query("id") + default: + return ErrMissingField("id|slug") } var r blogPostContent - err := md.DB.QueryRow("SELECT "+field+" FROM anchor_posts WHERE id = ? AND status = 'published'", md.C.Query("id")).Scan(&r.Content) + err := md.DB.QueryRow("SELECT "+field+" FROM anchor_posts WHERE "+by+" = ? AND status = 'published'", val).Scan(&r.Content) if err != nil { return common.SimpleResponse(404, "no blog post found") }