/
handlerCollectionGet.go
43 lines (39 loc) · 1.15 KB
/
handlerCollectionGet.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
42
43
package main
import (
"fmt"
"github.com/gorilla/mux"
"net/http"
)
func (handlers *Handlers) makeCollectionGetHandler() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
// get groups object from cache
group, err := handlers.groupFromVars(vars)
if err != nil {
handlers.logger.Errorf("no group: %v", err)
respondWithError(w, http.StatusInternalServerError, fmt.Sprintf("no group: %v", err))
return
}
parentKey, ok := vars["key"]
if !ok {
parentKey = ""
}
name, ok := vars["name"]
if !ok {
handlers.logger.Errorf("no name: %v", err)
respondWithError(w, http.StatusInternalServerError, fmt.Sprintf("no name: %v", err))
return
}
coll, err := group.GetCollectionByNameLocal(name, parentKey)
if err != nil {
handlers.logger.Errorf("cannot get collection: %v", err)
respondWithError(w, http.StatusInternalServerError, fmt.Sprintf("cannot get collection: %v", err))
return
}
if coll == nil {
respondWithJSON(w, http.StatusNotFound, fmt.Sprintf("collection %v.%v <- %v not found", group.Id, name, parentKey))
return
}
respondWithJSON(w, http.StatusOK, coll)
}
}