2022-02-03 15:32:03 +08:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2022-02-04 16:51:48 +08:00
|
|
|
"encoding/json"
|
2022-02-03 15:32:03 +08:00
|
|
|
"fmt"
|
|
|
|
"memos/api"
|
|
|
|
"memos/common"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/labstack/echo/v4"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (s *Server) registerMemoRoutes(g *echo.Group) {
|
|
|
|
g.POST("/memo", func(c echo.Context) error {
|
2022-05-03 02:05:43 +08:00
|
|
|
userID := c.Get(getUserIDContextKey()).(int)
|
2022-02-03 15:32:03 +08:00
|
|
|
memoCreate := &api.MemoCreate{
|
2022-05-03 02:05:43 +08:00
|
|
|
CreatorID: userID,
|
2022-02-03 15:32:03 +08:00
|
|
|
}
|
2022-02-04 16:51:48 +08:00
|
|
|
if err := json.NewDecoder(c.Request().Body).Decode(memoCreate); err != nil {
|
2022-02-03 15:32:03 +08:00
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, "Malformatted post memo request").SetInternal(err)
|
|
|
|
}
|
|
|
|
|
2022-05-16 07:37:23 +08:00
|
|
|
memo, err := s.Store.CreateMemo(memoCreate)
|
2022-02-03 15:32:03 +08:00
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to create memo").SetInternal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
|
2022-02-04 17:06:04 +08:00
|
|
|
if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(memo)); err != nil {
|
2022-02-05 11:43:25 +08:00
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode memo response").SetInternal(err)
|
2022-02-03 15:32:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
2022-02-18 22:21:10 +08:00
|
|
|
|
2022-02-03 15:32:03 +08:00
|
|
|
g.PATCH("/memo/:memoId", func(c echo.Context) error {
|
2022-05-03 02:05:43 +08:00
|
|
|
memoID, err := strconv.Atoi(c.Param("memoId"))
|
2022-02-03 15:32:03 +08:00
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("ID is not a number: %s", c.Param("memoId"))).SetInternal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
memoPatch := &api.MemoPatch{
|
2022-05-03 02:05:43 +08:00
|
|
|
ID: memoID,
|
2022-02-03 15:32:03 +08:00
|
|
|
}
|
2022-02-04 16:51:48 +08:00
|
|
|
if err := json.NewDecoder(c.Request().Body).Decode(memoPatch); err != nil {
|
2022-02-03 15:32:03 +08:00
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, "Malformatted patch memo request").SetInternal(err)
|
|
|
|
}
|
|
|
|
|
2022-05-16 07:37:23 +08:00
|
|
|
memo, err := s.Store.PatchMemo(memoPatch)
|
2022-02-03 15:32:03 +08:00
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to patch memo").SetInternal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
|
2022-02-04 17:06:04 +08:00
|
|
|
if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(memo)); err != nil {
|
2022-02-05 11:43:25 +08:00
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode memo response").SetInternal(err)
|
2022-02-03 15:32:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
2022-02-18 22:21:10 +08:00
|
|
|
|
2022-02-03 15:32:03 +08:00
|
|
|
g.GET("/memo", func(c echo.Context) error {
|
2022-05-03 02:05:43 +08:00
|
|
|
userID := c.Get(getUserIDContextKey()).(int)
|
2022-02-03 15:32:03 +08:00
|
|
|
memoFind := &api.MemoFind{
|
2022-05-03 02:05:43 +08:00
|
|
|
CreatorID: &userID,
|
2022-02-03 15:32:03 +08:00
|
|
|
}
|
2022-05-19 18:32:04 +08:00
|
|
|
|
|
|
|
rowStatus := api.RowStatus(c.QueryParam("rowStatus"))
|
2022-03-29 00:01:34 +08:00
|
|
|
if rowStatus != "" {
|
|
|
|
memoFind.RowStatus = &rowStatus
|
2022-02-04 21:24:21 +08:00
|
|
|
}
|
2022-05-19 18:32:04 +08:00
|
|
|
pinnedStr := c.QueryParam("pinned")
|
|
|
|
if pinnedStr != "" {
|
|
|
|
pinned := pinnedStr == "true"
|
|
|
|
memoFind.Pinned = &pinned
|
|
|
|
}
|
2022-06-14 23:09:03 +08:00
|
|
|
tag := c.QueryParam("tag")
|
|
|
|
if tag != "" {
|
2022-06-21 21:58:33 +08:00
|
|
|
contentSearch := "#" + tag + " "
|
|
|
|
memoFind.ContentSearch = &contentSearch
|
|
|
|
}
|
|
|
|
if limit, err := strconv.Atoi(c.QueryParam("limit")); err == nil {
|
|
|
|
memoFind.Limit = limit
|
|
|
|
}
|
|
|
|
if offset, err := strconv.Atoi(c.QueryParam("offset")); err == nil {
|
|
|
|
memoFind.Offset = offset
|
2022-06-14 23:09:03 +08:00
|
|
|
}
|
2022-02-04 21:24:21 +08:00
|
|
|
|
2022-05-16 07:37:23 +08:00
|
|
|
list, err := s.Store.FindMemoList(memoFind)
|
2022-02-03 15:32:03 +08:00
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to fetch memo list").SetInternal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
|
2022-02-04 17:06:04 +08:00
|
|
|
if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(list)); err != nil {
|
2022-02-05 11:43:25 +08:00
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode memo list response").SetInternal(err)
|
2022-02-03 15:32:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
2022-02-18 22:21:10 +08:00
|
|
|
|
2022-05-19 18:32:04 +08:00
|
|
|
g.POST("/memo/:memoId/organizer", func(c echo.Context) error {
|
|
|
|
memoID, err := strconv.Atoi(c.Param("memoId"))
|
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("ID is not a number: %s", c.Param("memoId"))).SetInternal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
userID := c.Get(getUserIDContextKey()).(int)
|
|
|
|
memoOrganizerUpsert := &api.MemoOrganizerUpsert{
|
|
|
|
MemoID: memoID,
|
|
|
|
UserID: userID,
|
|
|
|
}
|
|
|
|
if err := json.NewDecoder(c.Request().Body).Decode(memoOrganizerUpsert); err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, "Malformatted post memo organizer request").SetInternal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = s.Store.UpsertMemoOrganizer(memoOrganizerUpsert)
|
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to upsert memo organizer").SetInternal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
memo, err := s.Store.FindMemo(&api.MemoFind{
|
|
|
|
ID: &memoID,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
if common.ErrorCode(err) == common.NotFound {
|
|
|
|
return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Memo ID not found: %d", memoID)).SetInternal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to find memo by ID: %v", memoID)).SetInternal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
|
|
|
|
if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(memo)); err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode memo response").SetInternal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
2022-02-03 15:32:03 +08:00
|
|
|
g.GET("/memo/:memoId", func(c echo.Context) error {
|
2022-05-03 02:05:43 +08:00
|
|
|
memoID, err := strconv.Atoi(c.Param("memoId"))
|
2022-02-03 15:32:03 +08:00
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("ID is not a number: %s", c.Param("memoId"))).SetInternal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
memoFind := &api.MemoFind{
|
2022-05-03 02:05:43 +08:00
|
|
|
ID: &memoID,
|
2022-02-03 15:32:03 +08:00
|
|
|
}
|
2022-05-16 07:37:23 +08:00
|
|
|
memo, err := s.Store.FindMemo(memoFind)
|
2022-02-03 15:32:03 +08:00
|
|
|
if err != nil {
|
|
|
|
if common.ErrorCode(err) == common.NotFound {
|
2022-05-03 02:05:43 +08:00
|
|
|
return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Memo ID not found: %d", memoID)).SetInternal(err)
|
2022-02-03 15:32:03 +08:00
|
|
|
}
|
2022-02-06 00:25:41 +08:00
|
|
|
|
2022-05-03 02:05:43 +08:00
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to find memo by ID: %v", memoID)).SetInternal(err)
|
2022-02-03 15:32:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
|
2022-02-04 17:06:04 +08:00
|
|
|
if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(memo)); err != nil {
|
2022-02-05 11:43:25 +08:00
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode memo response").SetInternal(err)
|
2022-02-03 15:32:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
2022-02-18 22:21:10 +08:00
|
|
|
|
2022-02-03 15:32:03 +08:00
|
|
|
g.DELETE("/memo/:memoId", func(c echo.Context) error {
|
2022-05-03 02:05:43 +08:00
|
|
|
memoID, err := strconv.Atoi(c.Param("memoId"))
|
2022-02-03 15:32:03 +08:00
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("ID is not a number: %s", c.Param("memoId"))).SetInternal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
memoDelete := &api.MemoDelete{
|
2022-05-17 21:21:13 +08:00
|
|
|
ID: memoID,
|
2022-02-03 15:32:03 +08:00
|
|
|
}
|
|
|
|
|
2022-05-16 07:37:23 +08:00
|
|
|
err = s.Store.DeleteMemo(memoDelete)
|
2022-02-03 15:32:03 +08:00
|
|
|
if err != nil {
|
2022-05-03 02:05:43 +08:00
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to delete memo ID: %v", memoID)).SetInternal(err)
|
2022-02-03 15:32:03 +08:00
|
|
|
}
|
|
|
|
|
2022-02-05 13:17:43 +08:00
|
|
|
c.JSON(http.StatusOK, true)
|
2022-02-03 15:32:03 +08:00
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
2022-06-21 21:58:33 +08:00
|
|
|
|
|
|
|
g.GET("/memo/amount", func(c echo.Context) error {
|
|
|
|
userID := c.Get(getUserIDContextKey()).(int)
|
|
|
|
normalRowStatus := api.Normal
|
|
|
|
memoFind := &api.MemoFind{
|
|
|
|
CreatorID: &userID,
|
|
|
|
RowStatus: &normalRowStatus,
|
|
|
|
}
|
|
|
|
|
|
|
|
memoList, err := s.Store.FindMemoList(memoFind)
|
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find memo list").SetInternal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
|
|
|
|
if err := json.NewEncoder(c.Response().Writer).Encode(len(memoList)); err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode memo amount").SetInternal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
2022-02-03 15:32:03 +08:00
|
|
|
}
|