/
getpublicplaylists.go
48 lines (45 loc) · 1.12 KB
/
getpublicplaylists.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
44
45
46
47
48
package handler
import (
"encoding/json"
"net/http"
"strconv"
"github.com/collabyt/Backend/logger"
"github.com/collabyt/Backend/model"
)
// GetPublicPlaylists returns a list of playlist based on the Limit and offset.
func GetPublicPlaylists(w http.ResponseWriter, r *http.Request) {
logger.Info.Printf("HIT! Method POST Endpoint:/api/v1/playlists from Client %s", r.RemoteAddr)
w.Header().Set("Content-Type", "application/json")
limitSlc, ok := r.URL.Query()["limit"]
limit := 10
if ok && len(limitSlc[0]) > 0 {
var err error
limit, err = strconv.Atoi(limitSlc[0])
if err != nil {
limit = 10
}
}
if limit > 25 {
WriteErrorReply(w, http.StatusBadRequest)
return
}
offsetSlc, ok := r.URL.Query()["offset"]
var offset int
if ok && len(offsetSlc[0]) > 0 {
var err error
offset, err = strconv.Atoi(offsetSlc[0])
if err != nil {
offset = 0
}
}
ps, err := model.GetPublicPlaylistsByLimitAndOffset(limit, offset)
if err != nil {
WriteErrorReply(w, http.StatusInternalServerError)
return
}
if len(ps) < 1 {
ps = make([]model.Playlist, 0)
}
jsonResponse, _ := json.Marshal(ps)
w.Write(jsonResponse)
}