memo_relation.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. package server
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net/http"
  6. "strconv"
  7. "github.com/usememos/memos/api"
  8. "github.com/usememos/memos/store"
  9. "github.com/labstack/echo/v4"
  10. )
  11. func (s *Server) registerMemoRelationRoutes(g *echo.Group) {
  12. g.POST("/memo/:memoId/relation", func(c echo.Context) error {
  13. ctx := c.Request().Context()
  14. memoID, err := strconv.Atoi(c.Param("memoId"))
  15. if err != nil {
  16. return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("ID is not a number: %s", c.Param("memoId"))).SetInternal(err)
  17. }
  18. memoRelationUpsert := &api.MemoRelationUpsert{}
  19. if err := json.NewDecoder(c.Request().Body).Decode(memoRelationUpsert); err != nil {
  20. return echo.NewHTTPError(http.StatusBadRequest, "Malformatted post memo relation request").SetInternal(err)
  21. }
  22. memoRelation, err := s.Store.UpsertMemoRelation(ctx, &store.MemoRelationMessage{
  23. MemoID: memoID,
  24. RelatedMemoID: memoRelationUpsert.RelatedMemoID,
  25. Type: store.MemoRelationType(memoRelationUpsert.Type),
  26. })
  27. if err != nil {
  28. return echo.NewHTTPError(http.StatusInternalServerError, "Failed to upsert memo relation").SetInternal(err)
  29. }
  30. return c.JSON(http.StatusOK, composeResponse(memoRelation))
  31. })
  32. g.GET("/memo/:memoId/relation", func(c echo.Context) error {
  33. ctx := c.Request().Context()
  34. memoID, err := strconv.Atoi(c.Param("memoId"))
  35. if err != nil {
  36. return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("ID is not a number: %s", c.Param("memoId"))).SetInternal(err)
  37. }
  38. memoRelationList, err := s.Store.ListMemoRelations(ctx, &store.FindMemoRelationMessage{
  39. MemoID: &memoID,
  40. })
  41. if err != nil {
  42. return echo.NewHTTPError(http.StatusInternalServerError, "Failed to list memo relations").SetInternal(err)
  43. }
  44. return c.JSON(http.StatusOK, composeResponse(memoRelationList))
  45. })
  46. g.DELETE("/memo/:memoId/relation/:relatedMemoId/type/:relationType", func(c echo.Context) error {
  47. ctx := c.Request().Context()
  48. memoID, err := strconv.Atoi(c.Param("memoId"))
  49. if err != nil {
  50. return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Memo ID is not a number: %s", c.Param("memoId"))).SetInternal(err)
  51. }
  52. relatedMemoID, err := strconv.Atoi(c.Param("relatedMemoId"))
  53. if err != nil {
  54. return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Related memo ID is not a number: %s", c.Param("resourceId"))).SetInternal(err)
  55. }
  56. relationType := store.MemoRelationType(c.Param("relationType"))
  57. if err := s.Store.DeleteMemoRelation(ctx, &store.DeleteMemoRelationMessage{
  58. MemoID: &memoID,
  59. RelatedMemoID: &relatedMemoID,
  60. Type: &relationType,
  61. }); err != nil {
  62. return echo.NewHTTPError(http.StatusInternalServerError, "Failed to delete memo relation").SetInternal(err)
  63. }
  64. return c.JSON(http.StatusOK, true)
  65. })
  66. }
  67. func convertMemoRelationMessageToMemoRelation(memoRelation *store.MemoRelationMessage) *api.MemoRelation {
  68. return &api.MemoRelation{
  69. MemoID: memoRelation.MemoID,
  70. RelatedMemoID: memoRelation.RelatedMemoID,
  71. Type: api.MemoRelationType(memoRelation.Type),
  72. }
  73. }