2023-07-06 16:56:42 +03:00
|
|
|
package v1
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/labstack/echo/v4"
|
2023-08-04 16:55:07 +03:00
|
|
|
"github.com/usememos/memos/common/util"
|
2023-07-06 16:56:42 +03:00
|
|
|
"github.com/usememos/memos/store"
|
|
|
|
)
|
|
|
|
|
|
|
|
type MemoOrganizer struct {
|
2023-08-04 16:55:07 +03:00
|
|
|
MemoID int32 `json:"memoId"`
|
|
|
|
UserID int32 `json:"userId"`
|
|
|
|
Pinned bool `json:"pinned"`
|
2023-07-06 16:56:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type UpsertMemoOrganizerRequest struct {
|
|
|
|
Pinned bool `json:"pinned"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *APIV1Service) registerMemoOrganizerRoutes(g *echo.Group) {
|
2023-08-09 17:30:27 +03:00
|
|
|
g.POST("/memo/:memoId/organizer", s.CreateMemoOrganizer)
|
2023-08-09 16:53:06 +03:00
|
|
|
}
|
2023-07-06 16:56:42 +03:00
|
|
|
|
2023-08-09 17:30:27 +03:00
|
|
|
// CreateMemoOrganizer godoc
|
2023-08-09 16:53:06 +03:00
|
|
|
//
|
|
|
|
// @Summary Organize memo (pin/unpin)
|
|
|
|
// @Tags memo-organizer
|
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
|
|
|
// @Param memoId path int true "ID of memo to organize"
|
|
|
|
// @Param body body UpsertMemoOrganizerRequest true "Memo organizer object"
|
|
|
|
// @Success 200 {object} store.Memo "Memo information"
|
|
|
|
// @Failure 400 {object} nil "ID is not a number: %s | Malformatted post memo organizer request"
|
|
|
|
// @Failure 401 {object} nil "Missing user in session | Unauthorized"
|
|
|
|
// @Failure 404 {object} nil "Memo not found: %v"
|
|
|
|
// @Failure 500 {object} nil "Failed to find memo | Failed to upsert memo organizer | Failed to find memo by ID: %v | Failed to compose memo response"
|
|
|
|
// @Router /api/v1/memo/{memoId}/organizer [POST]
|
2023-08-09 17:30:27 +03:00
|
|
|
func (s *APIV1Service) CreateMemoOrganizer(c echo.Context) error {
|
2023-08-09 16:53:06 +03:00
|
|
|
ctx := c.Request().Context()
|
|
|
|
memoID, err := util.ConvertStringToInt32(c.Param("memoId"))
|
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("ID is not a number: %s", c.Param("memoId"))).SetInternal(err)
|
|
|
|
}
|
2023-07-06 16:56:42 +03:00
|
|
|
|
2023-09-14 15:16:17 +03:00
|
|
|
userID, ok := c.Get(userIDContextKey).(int32)
|
2023-08-09 16:53:06 +03:00
|
|
|
if !ok {
|
|
|
|
return echo.NewHTTPError(http.StatusUnauthorized, "Missing user in session")
|
|
|
|
}
|
2023-07-06 16:56:42 +03:00
|
|
|
|
2023-08-09 16:53:06 +03:00
|
|
|
memo, err := s.Store.GetMemo(ctx, &store.FindMemo{
|
|
|
|
ID: &memoID,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find memo").SetInternal(err)
|
|
|
|
}
|
|
|
|
if memo == nil {
|
|
|
|
return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Memo not found: %v", memoID))
|
|
|
|
}
|
|
|
|
if memo.CreatorID != userID {
|
|
|
|
return echo.NewHTTPError(http.StatusUnauthorized, "Unauthorized")
|
|
|
|
}
|
2023-07-06 16:56:42 +03:00
|
|
|
|
2023-08-09 16:53:06 +03:00
|
|
|
request := &UpsertMemoOrganizerRequest{}
|
|
|
|
if err := json.NewDecoder(c.Request().Body).Decode(request); err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, "Malformatted post memo organizer request").SetInternal(err)
|
|
|
|
}
|
2023-07-06 16:56:42 +03:00
|
|
|
|
2023-08-09 16:53:06 +03:00
|
|
|
upsert := &store.MemoOrganizer{
|
|
|
|
MemoID: memoID,
|
|
|
|
UserID: userID,
|
|
|
|
Pinned: request.Pinned,
|
|
|
|
}
|
|
|
|
_, err = s.Store.UpsertMemoOrganizer(ctx, upsert)
|
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to upsert memo organizer").SetInternal(err)
|
|
|
|
}
|
2023-07-06 16:56:42 +03:00
|
|
|
|
2023-08-09 16:53:06 +03:00
|
|
|
memo, err = s.Store.GetMemo(ctx, &store.FindMemo{
|
|
|
|
ID: &memoID,
|
2023-07-06 16:56:42 +03:00
|
|
|
})
|
2023-08-09 16:53:06 +03:00
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to find memo by ID: %v", memoID)).SetInternal(err)
|
|
|
|
}
|
|
|
|
if memo == nil {
|
|
|
|
return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Memo not found: %v", memoID))
|
|
|
|
}
|
|
|
|
|
|
|
|
memoResponse, err := s.convertMemoFromStore(ctx, memo)
|
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to compose memo response").SetInternal(err)
|
|
|
|
}
|
|
|
|
return c.JSON(http.StatusOK, memoResponse)
|
2023-07-06 16:56:42 +03:00
|
|
|
}
|