From 7674557182291c07a81a393346dfa4d908d87592 Mon Sep 17 00:00:00 2001 From: nefrace Date: Sun, 11 Dec 2022 15:28:10 +0300 Subject: [PATCH] Bookmarks creation and fetching, advanced query search --- server/handlersBookmark.go | 67 ++++++++++++++++++++++++++++++++++++++ server/server.go | 7 ++-- storage/mongoStore.go | 2 +- 3 files changed, 73 insertions(+), 3 deletions(-) create mode 100644 server/handlersBookmark.go diff --git a/server/handlersBookmark.go b/server/handlersBookmark.go new file mode 100644 index 0000000..03c0887 --- /dev/null +++ b/server/handlersBookmark.go @@ -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) +} diff --git a/server/server.go b/server/server.go index 964b245..21be901 100644 --- a/server/server.go +++ b/server/server.go @@ -28,7 +28,6 @@ func (s Server) InitHandlers() { r.Use(s.Logger) r.Use(s.UserInContext) apiRouter := r.PathPrefix("/api").Subrouter() - authRouter := apiRouter.PathPrefix("/auth").Subrouter() authRouter.Path("/reg").Methods("POST").HandlerFunc(MakeHTTPHandler(s.handleRegister)) authRouter.Path("/login").Methods("POST").HandlerFunc(MakeHTTPHandler(s.handleLogin)) @@ -36,9 +35,13 @@ func (s Server) InitHandlers() { userRouter := apiRouter.PathPrefix("/user").Subrouter() userRouter.Use(s.AuthorizedOnly) - userRouter.Use(s.AuthorizedOnly) + // userRouter.Use(s.AuthorizedOnly) userRouter.Path("/me").HandlerFunc(MakeHTTPHandler(s.handleGetMe)) 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/"))) } diff --git a/storage/mongoStore.go b/storage/mongoStore.go index a6b4316..933e7e0 100644 --- a/storage/mongoStore.go +++ b/storage/mongoStore.go @@ -64,7 +64,7 @@ func (s Store[T]) GetMany(ctx context.Context, filter *bson.D) ([]*T, error) { log.Println("Error fetching items: ", err) return nil, err } - var res []*T + res := []*T{} err = cur.All(ctx, &res) if err != nil { log.Println("Error collecting items to slice: ", err)