summaryrefslogtreecommitdiff
path: root/system/api/handlers.go
diff options
context:
space:
mode:
authorSteve Manuel <nilslice@gmail.com>2016-12-14 09:57:55 -0800
committerSteve Manuel <nilslice@gmail.com>2016-12-14 09:57:55 -0800
commitb3aa9440f62db5a530397c525a42b4fca7b27bab (patch)
tree8775b01a283c32dce3297dbddad1ddf14a114c33 /system/api/handlers.go
parentbd96fae14bc6201cb6e81b1cb2c51b00314810b6 (diff)
adding db method ContentBySlug to lookup the type & id of content by its slug and return it directly
Diffstat (limited to 'system/api/handlers.go')
-rw-r--r--system/api/handlers.go26
1 files changed, 26 insertions, 0 deletions
diff --git a/system/api/handlers.go b/system/api/handlers.go
index c238ca9..7a2073d 100644
--- a/system/api/handlers.go
+++ b/system/api/handlers.go
@@ -91,6 +91,12 @@ func contentHandler(res http.ResponseWriter, req *http.Request) {
q := req.URL.Query()
id := q.Get("id")
t := q.Get("type")
+ slug := q.Get("slug")
+
+ if slug != "" {
+ contentHandlerBySlug(res, req)
+ return
+ }
if _, ok := content.Types[t]; !ok {
res.WriteHeader(http.StatusNotFound)
@@ -117,6 +123,26 @@ func contentHandler(res http.ResponseWriter, req *http.Request) {
sendData(res, j, http.StatusOK)
}
+func contentHandlerBySlug(res http.ResponseWriter, req *http.Request) {
+ slug := req.URL.Query().Get("slug")
+
+ // lookup type:id by slug key in __contentIndex
+ post, err := db.ContentBySlug(slug)
+ if err != nil {
+ log.Println("Error finding content by slug:", slug, err)
+ res.WriteHeader(http.StatusInternalServerError)
+ return
+ }
+
+ j, err := fmtJSON(json.RawMessage(post))
+ if err != nil {
+ res.WriteHeader(http.StatusInternalServerError)
+ return
+ }
+
+ sendData(res, j, http.StatusOK)
+}
+
func fmtJSON(data ...json.RawMessage) ([]byte, error) {
var msg = []json.RawMessage{}
for _, d := range data {