Get some amount of random bookmarks

This commit is contained in:
nefrace 2022-12-11 16:22:24 +03:00
parent 7674557182
commit be36ed3593
5 changed files with 78 additions and 22 deletions

View File

@ -6,6 +6,8 @@ import (
"log" "log"
"net/http" "net/http"
"runtime/debug" "runtime/debug"
"github.com/gorilla/mux"
) )
type ApiError struct { type ApiError struct {
@ -41,6 +43,15 @@ type Context struct {
R *http.Request 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 { func (c Context) Header(header string, value string) *Context {
c.W.Header().Add(header, value) c.W.Header().Add(header, value)
return &c return &c

View File

@ -2,7 +2,7 @@ package server
import ( import (
"log" "log"
"strings" "strconv"
"time" "time"
"git.nefrace.ru/nefrace/nashboard/storage" "git.nefrace.ru/nefrace/nashboard/storage"
@ -45,19 +45,9 @@ func (s Server) handleGetBookmarks(c Context) error {
ctx := c.R.Context() ctx := c.R.Context()
user := ctx.Value(CtxValue("user")).(*storage.User) // Getting currently logged in user user := ctx.Value(CtxValue("user")).(*storage.User) // Getting currently logged in user
query := c.R.URL.Query() query := c.R.URL.Query()
filter := bson.D{{Key: "userid", Value: user.Id}} // Filter only bookmarks owned by User filter := storage.QueryFilter(&query)
for k, v := range query { filter = append(filter, bson.E{Key: "userid", Value: user.Id})
var f bson.E log.Println(filter)
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
}
bookmarks, err := s.Db.GetBookmarks(&filter) bookmarks, err := s.Db.GetBookmarks(&filter)
if err != nil { if err != nil {
log.Println("Error getting bookmarks: ", err) log.Println("Error getting bookmarks: ", err)
@ -65,3 +55,18 @@ func (s Server) handleGetBookmarks(c Context) error {
} }
return c.WriteJSON(200, bookmarks) 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)
}

View File

@ -11,7 +11,7 @@ import (
type Server struct { type Server struct {
Host string Host string
Mux *mux.Router Mux *mux.Router
Db storage.Storage Db storage.MongodbStorage
} }
var ErrUnauthorized ApiError = ApiError{Status: http.StatusUnauthorized, Err: "Unauthorized"} var ErrUnauthorized ApiError = ApiError{Status: http.StatusUnauthorized, Err: "Unauthorized"}
@ -42,6 +42,8 @@ func (s Server) InitHandlers() {
bookmarkRouter.Use(s.AuthorizedOnly) bookmarkRouter.Use(s.AuthorizedOnly)
bookmarkRouter.Path("").Methods("GET").HandlerFunc(MakeHTTPHandler(s.handleGetBookmarks)) bookmarkRouter.Path("").Methods("GET").HandlerFunc(MakeHTTPHandler(s.handleGetBookmarks))
bookmarkRouter.Path("").Methods("POST").HandlerFunc(MakeHTTPHandler(s.handleNewBookmark)) 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/"))) r.PathPrefix("/").Handler(http.FileServer(http.Dir("./static/")))
} }

View File

@ -72,6 +72,7 @@ func (s Store[T]) GetMany(ctx context.Context, filter *bson.D) ([]*T, error) {
} }
return res, nil return res, nil
} }
func (s Store[T]) DeleteByID(ctx context.Context, id primitive.ObjectID) error { func (s Store[T]) DeleteByID(ctx context.Context, id primitive.ObjectID) error {
_, err := s.Coll.DeleteOne(ctx, bson.D{{Key: "_id", Value: id}}) _, err := s.Coll.DeleteOne(ctx, bson.D{{Key: "_id", Value: id}})
return err return err
@ -81,10 +82,3 @@ func (s Store[T]) ReplaceByID(ctx context.Context, id primitive.ObjectID, item *
_, err := s.Coll.ReplaceOne(ctx, bson.D{{Key: "_id", Value: id}}, item) _, err := s.Coll.ReplaceOne(ctx, bson.D{{Key: "_id", Value: id}}, item)
return err return err
} }
func getFilter(f *bson.D) bson.D {
if f == nil {
return bson.D{}
}
return *f
}

View File

@ -2,6 +2,9 @@ package storage
import ( import (
"context" "context"
"log"
"net/url"
"strings"
"go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive" "go.mongodb.org/mongo-driver/bson/primitive"
@ -96,6 +99,23 @@ func (m MongodbStorage) GetBookmarks(filter *bson.D) ([]*Bookmark, error) {
return store.GetMany(ctx, filter) return store.GetMany(ctx, filter)
} }
func (m MongodbStorage) GetRandomBookmarks(count int, filter *bson.D) ([]*Bookmark, error) {
passedFilter := getFilter(filter)
ctx := context.TODO()
store := NewStore[Bookmark](&m, "bookmarks")
bookmarks := []*Bookmark{}
cur, err := store.Coll.Aggregate(ctx, bson.A{
bson.D{{Key: "$match", Value: passedFilter}},
bson.D{{Key: "$sample", Value: bson.D{bson.E{Key: "size", Value: count}}}},
})
if err != nil {
log.Println("Cant aggregate random bookmakrs: ", err)
return bookmarks, err
}
cur.All(ctx, &bookmarks)
return bookmarks, err
}
func (m MongodbStorage) GetUserBookmarks(user *User, filter *bson.D) ([]*Bookmark, error) { func (m MongodbStorage) GetUserBookmarks(user *User, filter *bson.D) ([]*Bookmark, error) {
passedFilter := getFilter(filter) passedFilter := getFilter(filter)
f := bson.D{{Key: "userid", Value: user.Id}} f := bson.D{{Key: "userid", Value: user.Id}}
@ -108,3 +128,27 @@ func (m MongodbStorage) GetUserBookmarks(user *User, filter *bson.D) ([]*Bookmar
func (m MongodbStorage) Collection(col string) *mongo.Collection { func (m MongodbStorage) Collection(col string) *mongo.Collection {
return m.Db.Database("nash").Collection(col) return m.Db.Database("nash").Collection(col)
} }
func getFilter(f *bson.D) bson.D {
if f == nil {
return bson.D{}
}
return *f
}
func QueryFilter(q *url.Values) bson.D {
filter := bson.D{}
for k, v := range *q {
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
}
return filter
}