116 lines
3.2 KiB
Go
116 lines
3.2 KiB
Go
|
package server
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"time"
|
||
|
|
||
|
"git.nefrace.ru/nefrace/nashboard/storage"
|
||
|
"go.mongodb.org/mongo-driver/bson"
|
||
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
||
|
)
|
||
|
|
||
|
type newFolderForm struct {
|
||
|
Name string
|
||
|
ParentID primitive.ObjectID
|
||
|
}
|
||
|
|
||
|
func (s Server) handleNewFolder(c Context) error {
|
||
|
var form newFolderForm
|
||
|
err := DecodeJSON(c.R.Body, &form)
|
||
|
if err != nil {
|
||
|
return ApiError{Err: "can't decode json to folder: " + err.Error(), Status: 400}
|
||
|
}
|
||
|
ctx := c.R.Context()
|
||
|
user := ctx.Value(CtxValue("user")).(*storage.User)
|
||
|
if form.ParentID.IsZero() {
|
||
|
rootFolder, _ := s.Db.GetFolder(&bson.D{{Key: "name", Value: "root"}})
|
||
|
form.ParentID = rootFolder.Id
|
||
|
}
|
||
|
folder := storage.Folder{
|
||
|
Id: primitive.NewObjectID(),
|
||
|
Name: form.Name,
|
||
|
UserID: user.Id,
|
||
|
ParentID: form.ParentID,
|
||
|
// Tags: form.Tags,
|
||
|
Created: time.Now(),
|
||
|
}
|
||
|
_, err = s.Db.CreateFolder(&folder)
|
||
|
if err != nil {
|
||
|
return ApiError{Err: "can't safe folder: " + err.Error(), Status: 500}
|
||
|
}
|
||
|
return c.WriteJSON(201, &folder)
|
||
|
}
|
||
|
|
||
|
type folderResult struct {
|
||
|
Folder *storage.Folder
|
||
|
ChildFolders []*storage.Folder
|
||
|
ChildBookmarks []*storage.Bookmark
|
||
|
}
|
||
|
|
||
|
func (s Server) handleGetFolder(c Context) error {
|
||
|
user := c.R.Context().Value(CtxValue("user")).(*storage.User) // Getting currently logged in user
|
||
|
userFilter := bson.E{Key: "userid", Value: user.Id}
|
||
|
idStr := c.GetVar("id")
|
||
|
query := c.R.URL.Query()
|
||
|
filter := storage.QueryFilter(&query)
|
||
|
filter = append(filter, userFilter)
|
||
|
if idStr != "" {
|
||
|
id, err := primitive.ObjectIDFromHex(idStr)
|
||
|
if err != nil {
|
||
|
return ApiError{Err: "wrong id", Status: 400}
|
||
|
}
|
||
|
filter = bson.D{{Key: "_id", Value: id}, userFilter}
|
||
|
}
|
||
|
folder, err := s.Db.GetFolder(&filter)
|
||
|
if err != nil {
|
||
|
return ApiError{Err: "not found", Status: 404}
|
||
|
}
|
||
|
folders, _ := s.Db.GetChildrenFolders(folder.Id)
|
||
|
bookmarks, _ := s.Db.GetChildrenBookmarks(folder.Id)
|
||
|
return c.WriteJSON(200, &folderResult{
|
||
|
Folder: folder,
|
||
|
ChildFolders: folders,
|
||
|
ChildBookmarks: bookmarks,
|
||
|
})
|
||
|
}
|
||
|
|
||
|
func (s Server) handleUpdateFolder(c Context) error {
|
||
|
user := c.R.Context().Value(CtxValue("user")).(*storage.User)
|
||
|
var form storage.Folder
|
||
|
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.Folder](&s.Db, "folders")
|
||
|
err = store.ReplaceByID(context.TODO(), form.Id, &form)
|
||
|
if err != nil {
|
||
|
return ApiError{Err: "couldn't update folder: " + err.Error(), Status: 500}
|
||
|
}
|
||
|
return c.WriteJSON(200, "updated")
|
||
|
}
|
||
|
|
||
|
func (s Server) handleDeleteFolder(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.Folder](&s.Db, "folders")
|
||
|
folder, 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(), folder.Id)
|
||
|
if err != nil {
|
||
|
return ApiError{Err: err.Error(), Status: 500}
|
||
|
}
|
||
|
return c.WriteJSON(200, "removed")
|
||
|
}
|