Bookmarks creation and fetching, advanced query search
This commit is contained in:
parent
ed012d3715
commit
7674557182
|
@ -0,0 +1,67 @@
|
||||||
|
package server
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"git.nefrace.ru/nefrace/nashboard/storage"
|
||||||
|
"go.mongodb.org/mongo-driver/bson"
|
||||||
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||||
|
)
|
||||||
|
|
||||||
|
type newBookmarkForm struct {
|
||||||
|
Name string
|
||||||
|
Url string
|
||||||
|
FolderID primitive.ObjectID
|
||||||
|
Tags []string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s Server) handleNewBookmark(c Context) error {
|
||||||
|
var form newBookmarkForm
|
||||||
|
err := DecodeJSON(c.R.Body, &form)
|
||||||
|
if err != nil {
|
||||||
|
return ApiError{Err: "can't decode json to bookmark: " + err.Error(), Status: 400}
|
||||||
|
}
|
||||||
|
ctx := c.R.Context()
|
||||||
|
user := ctx.Value(CtxValue("user")).(*storage.User)
|
||||||
|
bookmark := storage.Bookmark{
|
||||||
|
Id: primitive.NewObjectID(),
|
||||||
|
Name: form.Name,
|
||||||
|
Url: form.Url,
|
||||||
|
FolderID: form.FolderID,
|
||||||
|
Tags: form.Tags,
|
||||||
|
UserID: user.Id,
|
||||||
|
Created: time.Now(),
|
||||||
|
}
|
||||||
|
_, err = s.Db.CreateBookmark(&bookmark)
|
||||||
|
if err != nil {
|
||||||
|
return ApiError{Err: "can't safe bookmark: " + err.Error(), Status: 500}
|
||||||
|
}
|
||||||
|
return c.WriteJSON(201, &bookmark)
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
bookmarks, err := s.Db.GetBookmarks(&filter)
|
||||||
|
if err != nil {
|
||||||
|
log.Println("Error getting bookmarks: ", err)
|
||||||
|
c.WriteJSON(200, []*storage.Bookmark{})
|
||||||
|
}
|
||||||
|
return c.WriteJSON(200, bookmarks)
|
||||||
|
}
|
|
@ -28,7 +28,6 @@ func (s Server) InitHandlers() {
|
||||||
r.Use(s.Logger)
|
r.Use(s.Logger)
|
||||||
r.Use(s.UserInContext)
|
r.Use(s.UserInContext)
|
||||||
apiRouter := r.PathPrefix("/api").Subrouter()
|
apiRouter := r.PathPrefix("/api").Subrouter()
|
||||||
|
|
||||||
authRouter := apiRouter.PathPrefix("/auth").Subrouter()
|
authRouter := apiRouter.PathPrefix("/auth").Subrouter()
|
||||||
authRouter.Path("/reg").Methods("POST").HandlerFunc(MakeHTTPHandler(s.handleRegister))
|
authRouter.Path("/reg").Methods("POST").HandlerFunc(MakeHTTPHandler(s.handleRegister))
|
||||||
authRouter.Path("/login").Methods("POST").HandlerFunc(MakeHTTPHandler(s.handleLogin))
|
authRouter.Path("/login").Methods("POST").HandlerFunc(MakeHTTPHandler(s.handleLogin))
|
||||||
|
@ -36,9 +35,13 @@ func (s Server) InitHandlers() {
|
||||||
|
|
||||||
userRouter := apiRouter.PathPrefix("/user").Subrouter()
|
userRouter := apiRouter.PathPrefix("/user").Subrouter()
|
||||||
userRouter.Use(s.AuthorizedOnly)
|
userRouter.Use(s.AuthorizedOnly)
|
||||||
userRouter.Use(s.AuthorizedOnly)
|
// userRouter.Use(s.AuthorizedOnly)
|
||||||
userRouter.Path("/me").HandlerFunc(MakeHTTPHandler(s.handleGetMe))
|
userRouter.Path("/me").HandlerFunc(MakeHTTPHandler(s.handleGetMe))
|
||||||
userRouter.Path("/all").HandlerFunc(MakeHTTPHandler(s.handleAllUsers))
|
userRouter.Path("/all").HandlerFunc(MakeHTTPHandler(s.handleAllUsers))
|
||||||
|
bookmarkRouter := apiRouter.PathPrefix("/bookmark").Subrouter()
|
||||||
|
bookmarkRouter.Use(s.AuthorizedOnly)
|
||||||
|
bookmarkRouter.Path("").Methods("GET").HandlerFunc(MakeHTTPHandler(s.handleGetBookmarks))
|
||||||
|
bookmarkRouter.Path("").Methods("POST").HandlerFunc(MakeHTTPHandler(s.handleNewBookmark))
|
||||||
|
|
||||||
r.PathPrefix("/").Handler(http.FileServer(http.Dir("./static/")))
|
r.PathPrefix("/").Handler(http.FileServer(http.Dir("./static/")))
|
||||||
}
|
}
|
||||||
|
|
|
@ -64,7 +64,7 @@ func (s Store[T]) GetMany(ctx context.Context, filter *bson.D) ([]*T, error) {
|
||||||
log.Println("Error fetching items: ", err)
|
log.Println("Error fetching items: ", err)
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
var res []*T
|
res := []*T{}
|
||||||
err = cur.All(ctx, &res)
|
err = cur.All(ctx, &res)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Error collecting items to slice: ", err)
|
log.Println("Error collecting items to slice: ", err)
|
||||||
|
|
Loading…
Reference in New Issue