Get some amount of random bookmarks
This commit is contained in:
@ -6,6 +6,8 @@ import (
|
||||
"log"
|
||||
"net/http"
|
||||
"runtime/debug"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
)
|
||||
|
||||
type ApiError struct {
|
||||
@ -41,6 +43,15 @@ type Context struct {
|
||||
R *http.Request
|
||||
}
|
||||
|
||||
func (c Context) GetVar(name string) string {
|
||||
vars := mux.Vars(c.R)
|
||||
val, ok := vars[name]
|
||||
if ok {
|
||||
return val
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (c Context) Header(header string, value string) *Context {
|
||||
c.W.Header().Add(header, value)
|
||||
return &c
|
||||
|
@ -2,7 +2,7 @@ package server
|
||||
|
||||
import (
|
||||
"log"
|
||||
"strings"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"git.nefrace.ru/nefrace/nashboard/storage"
|
||||
@ -45,19 +45,9 @@ func (s Server) handleGetBookmarks(c Context) error {
|
||||
ctx := c.R.Context()
|
||||
user := ctx.Value(CtxValue("user")).(*storage.User) // Getting currently logged in user
|
||||
query := c.R.URL.Query()
|
||||
filter := bson.D{{Key: "userid", Value: user.Id}} // Filter only bookmarks owned by User
|
||||
for k, v := range query {
|
||||
var f bson.E
|
||||
q := strings.Split(k, "_")
|
||||
if len(q) == 1 { // If param is like "name" or "url" we're using exact matching
|
||||
f = bson.E{Key: k, Value: v[0]}
|
||||
} else {
|
||||
if q[1] == "like" { // If it's like "name_like", we're using regex
|
||||
f = bson.E{Key: q[0], Value: bson.D{{Key: "$regex", Value: v[0]}}}
|
||||
}
|
||||
}
|
||||
filter = append(filter, f) // Applying query filters to user filter
|
||||
}
|
||||
filter := storage.QueryFilter(&query)
|
||||
filter = append(filter, bson.E{Key: "userid", Value: user.Id})
|
||||
log.Println(filter)
|
||||
bookmarks, err := s.Db.GetBookmarks(&filter)
|
||||
if err != nil {
|
||||
log.Println("Error getting bookmarks: ", err)
|
||||
@ -65,3 +55,18 @@ func (s Server) handleGetBookmarks(c Context) error {
|
||||
}
|
||||
return c.WriteJSON(200, bookmarks)
|
||||
}
|
||||
|
||||
func (s Server) handleGetRandomBookmarks(c Context) error {
|
||||
ctx := c.R.Context()
|
||||
user := ctx.Value(CtxValue("user")).(*storage.User)
|
||||
countVar := c.GetVar("count")
|
||||
count, err := strconv.Atoi(countVar)
|
||||
if err != nil {
|
||||
count = 1
|
||||
}
|
||||
bookmarks, err := s.Db.GetRandomBookmarks(count, &bson.D{{Key: "userid", Value: user.Id}})
|
||||
if err != nil {
|
||||
return ApiError{Err: "can't get bookmarks: " + err.Error(), Status: 400}
|
||||
}
|
||||
return c.WriteJSON(200, bookmarks)
|
||||
}
|
||||
|
@ -11,7 +11,7 @@ import (
|
||||
type Server struct {
|
||||
Host string
|
||||
Mux *mux.Router
|
||||
Db storage.Storage
|
||||
Db storage.MongodbStorage
|
||||
}
|
||||
|
||||
var ErrUnauthorized ApiError = ApiError{Status: http.StatusUnauthorized, Err: "Unauthorized"}
|
||||
@ -42,6 +42,8 @@ func (s Server) InitHandlers() {
|
||||
bookmarkRouter.Use(s.AuthorizedOnly)
|
||||
bookmarkRouter.Path("").Methods("GET").HandlerFunc(MakeHTTPHandler(s.handleGetBookmarks))
|
||||
bookmarkRouter.Path("").Methods("POST").HandlerFunc(MakeHTTPHandler(s.handleNewBookmark))
|
||||
bookmarkRouter.Path("/random").HandlerFunc(MakeHTTPHandler(s.handleGetRandomBookmarks))
|
||||
bookmarkRouter.Path("/random/{count}").HandlerFunc(MakeHTTPHandler(s.handleGetRandomBookmarks))
|
||||
|
||||
r.PathPrefix("/").Handler(http.FileServer(http.Dir("./static/")))
|
||||
}
|
||||
|
Reference in New Issue
Block a user