Nashboard/server/handlersBookmark.go

134 lines
3.7 KiB
Go

package server
import (
"context"
"log"
"strconv"
"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)
log.Println(form)
if form.FolderID.IsZero() {
log.Println(user)
form.FolderID = user.RootFolderId
}
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) 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")
}
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 := 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)
c.WriteJSON(200, []*storage.Bookmark{})
}
return c.WriteJSON(200, bookmarks)
}
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)
}
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)
}