forked from boombuler/knowledgedb
/
serve_tags.go
41 lines (35 loc) · 906 Bytes
/
serve_tags.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package main
import (
"encoding/json"
"github.com/boombuler/knowledgedb/log"
"github.com/boombuler/knowledgedb/storage"
"github.com/gorilla/mux"
"net/http"
"net/url"
"sort"
)
func (s *httpServer) serveAllTags(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(storage.GetAllTags())
}
func (s *httpServer) serveTagDetails(w http.ResponseWriter, r *http.Request) {
tagId, ok := mux.Vars(r)["tag"]
if !ok {
http.Error(w, "Bad Request", http.StatusBadRequest)
return
}
tagId, err := url.QueryUnescape(tagId)
if err != nil {
http.Error(w, "Bad Request", http.StatusBadRequest)
log.Warning(err)
return
}
items := storage.EntriesByDateTime(storage.GetAllEntriesWithTag(tagId))
sort.Sort(items)
for _, item := range items {
item.Body = item.GetBodySnippet()
}
renderTemplate(w, "tag", struct {
Name string
Items []*storage.EntryWithId
}{tagId, items})
}