2022-12-11 15:28:10 +03:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2022-12-13 17:07:32 +03:00
|
|
|
"context"
|
2022-12-11 15:28:10 +03:00
|
|
|
"log"
|
2022-12-11 16:22:24 +03:00
|
|
|
"strconv"
|
2022-12-11 15:28:10 +03:00
|
|
|
"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)
|
2022-12-13 17:07:32 +03:00
|
|
|
log.Println(form)
|
|
|
|
if form.FolderID.IsZero() {
|
|
|
|
log.Println(user)
|
|
|
|
form.FolderID = user.RootFolderId
|
|
|
|
}
|
2022-12-11 15:28:10 +03:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2022-12-13 17:07:32 +03:00
|
|
|
func (s Server) handleDeleteBookmark(c Context) error {
|
|
|
|
user := c.R.Context().Value(CtxValue("user")).(*storage.User)
|
|
|
|
idStr := c.GetVar("id")
|
|
|
|
id, err := primitive.ObjectIDFromHex(idStr)
|
|
|
|
if err != nil {
|
|
|
|
return ApiError{Err: "wrong id", Status: 400}
|
|
|
|
}
|
|
|
|
store := storage.NewStore[storage.Bookmark](&s.Db, "bookmarks")
|
|
|
|
bookmark, err := store.GetOne(context.TODO(), &bson.D{
|
|
|
|
{Key: "_id", Value: id},
|
|
|
|
{Key: "userid", Value: user.Id},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return ApiError{Err: "not found", Status: 404}
|
|
|
|
}
|
|
|
|
err = store.DeleteByID(context.TODO(), bookmark.Id)
|
|
|
|
if err != nil {
|
|
|
|
return ApiError{Err: err.Error(), Status: 500}
|
|
|
|
}
|
|
|
|
return c.WriteJSON(200, "removed")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s Server) handleUpdateBookmark(c Context) error {
|
|
|
|
user := c.R.Context().Value(CtxValue("user")).(*storage.User)
|
|
|
|
var form storage.Bookmark
|
|
|
|
err := DecodeJSON(c.R.Body, &form)
|
|
|
|
if err != nil {
|
|
|
|
return ApiError{Err: "wrong body", Status: 400}
|
|
|
|
}
|
|
|
|
if form.UserID != user.Id {
|
|
|
|
return ApiError{Err: "wrong body", Status: 403}
|
|
|
|
}
|
|
|
|
store := storage.NewStore[storage.Bookmark](&s.Db, "bookmarks")
|
|
|
|
err = store.ReplaceByID(context.TODO(), form.Id, &form)
|
|
|
|
if err != nil {
|
|
|
|
return ApiError{Err: "couldn't update bookmark: " + err.Error(), Status: 500}
|
|
|
|
}
|
|
|
|
return c.WriteJSON(200, "updated")
|
|
|
|
}
|
|
|
|
|
2022-12-11 15:28:10 +03:00
|
|
|
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()
|
2022-12-11 16:22:24 +03:00
|
|
|
filter := storage.QueryFilter(&query)
|
|
|
|
filter = append(filter, bson.E{Key: "userid", Value: user.Id})
|
|
|
|
log.Println(filter)
|
2022-12-11 15:28:10 +03:00
|
|
|
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)
|
|
|
|
}
|
2022-12-11 16:22:24 +03:00
|
|
|
|
2022-12-13 17:07:32 +03:00
|
|
|
func (s Server) handleGetLastBookmarks(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.GetLastBookmarks(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)
|
|
|
|
}
|
|
|
|
|
2022-12-11 16:22:24 +03:00
|
|
|
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)
|
|
|
|
}
|