|
|
|
@ -24,9 +24,7 @@ func (s *Server) registerMemoRoutes(g *echo.Group) {
|
|
|
|
|
return echo.NewHTTPError(http.StatusUnauthorized, "Missing user in session")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
memoCreate := &api.MemoCreate{
|
|
|
|
|
CreatorID: userID,
|
|
|
|
|
}
|
|
|
|
|
memoCreate := &api.MemoCreate{}
|
|
|
|
|
if err := json.NewDecoder(c.Request().Body).Decode(memoCreate); err != nil {
|
|
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, "Malformatted post memo request").SetInternal(err)
|
|
|
|
|
}
|
|
|
|
@ -57,6 +55,7 @@ func (s *Server) registerMemoRoutes(g *echo.Group) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
memoCreate.CreatorID = userID
|
|
|
|
|
memo, err := s.Store.CreateMemo(ctx, memoCreate)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to create memo").SetInternal(err)
|
|
|
|
@ -98,13 +97,15 @@ func (s *Server) registerMemoRoutes(g *echo.Group) {
|
|
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("ID is not a number: %s", c.Param("memoId"))).SetInternal(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
memoFind := &api.MemoFind{
|
|
|
|
|
ID: &memoID,
|
|
|
|
|
CreatorID: &userID,
|
|
|
|
|
}
|
|
|
|
|
if _, err := s.Store.FindMemo(ctx, memoFind); err != nil {
|
|
|
|
|
memo, err := s.Store.FindMemo(ctx, &api.MemoFind{
|
|
|
|
|
ID: &memoID,
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find memo").SetInternal(err)
|
|
|
|
|
}
|
|
|
|
|
if memo.CreatorID != userID {
|
|
|
|
|
return echo.NewHTTPError(http.StatusUnauthorized, "Unauthorized")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
currentTs := time.Now().Unix()
|
|
|
|
|
memoPatch := &api.MemoPatch{
|
|
|
|
@ -115,7 +116,7 @@ func (s *Server) registerMemoRoutes(g *echo.Group) {
|
|
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, "Malformatted patch memo request").SetInternal(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
memo, err := s.Store.PatchMemo(ctx, memoPatch)
|
|
|
|
|
memo, err = s.Store.PatchMemo(ctx, memoPatch)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to patch memo").SetInternal(err)
|
|
|
|
|
}
|
|
|
|
@ -173,7 +174,7 @@ func (s *Server) registerMemoRoutes(g *echo.Group) {
|
|
|
|
|
}
|
|
|
|
|
tag := c.QueryParam("tag")
|
|
|
|
|
if tag != "" {
|
|
|
|
|
contentSearch := "#" + tag + " "
|
|
|
|
|
contentSearch := "#" + tag
|
|
|
|
|
memoFind.ContentSearch = &contentSearch
|
|
|
|
|
}
|
|
|
|
|
visibilityListStr := c.QueryParam("visibility")
|
|
|
|
@ -229,129 +230,6 @@ func (s *Server) registerMemoRoutes(g *echo.Group) {
|
|
|
|
|
return nil
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
g.GET("/memo/amount", func(c echo.Context) error {
|
|
|
|
|
ctx := c.Request().Context()
|
|
|
|
|
normalRowStatus := api.Normal
|
|
|
|
|
memoFind := &api.MemoFind{
|
|
|
|
|
RowStatus: &normalRowStatus,
|
|
|
|
|
}
|
|
|
|
|
if userID, err := strconv.Atoi(c.QueryParam("userId")); err == nil {
|
|
|
|
|
memoFind.CreatorID = &userID
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
memoList, err := s.Store.FindMemoList(ctx, 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(composeResponse(len(memoList))); err != nil {
|
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode memo amount").SetInternal(err)
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
g.GET("/memo/stats", func(c echo.Context) error {
|
|
|
|
|
ctx := c.Request().Context()
|
|
|
|
|
normalStatus := api.Normal
|
|
|
|
|
memoFind := &api.MemoFind{
|
|
|
|
|
RowStatus: &normalStatus,
|
|
|
|
|
}
|
|
|
|
|
if creatorID, err := strconv.Atoi(c.QueryParam("creatorId")); err == nil {
|
|
|
|
|
memoFind.CreatorID = &creatorID
|
|
|
|
|
}
|
|
|
|
|
if memoFind.CreatorID == nil {
|
|
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, "Missing user id to find memo")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
currentUserID, ok := c.Get(getUserIDContextKey()).(int)
|
|
|
|
|
if !ok {
|
|
|
|
|
memoFind.VisibilityList = []api.Visibility{api.Public}
|
|
|
|
|
} else {
|
|
|
|
|
if *memoFind.CreatorID != currentUserID {
|
|
|
|
|
memoFind.VisibilityList = []api.Visibility{api.Public, api.Protected}
|
|
|
|
|
} else {
|
|
|
|
|
memoFind.VisibilityList = []api.Visibility{api.Public, api.Protected, api.Private}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
list, err := s.Store.FindMemoList(ctx, memoFind)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to fetch memo list").SetInternal(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
displayTsList := []int64{}
|
|
|
|
|
for _, memo := range list {
|
|
|
|
|
displayTsList = append(displayTsList, memo.DisplayTs)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
|
|
|
|
|
if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(displayTsList)); err != nil {
|
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode memo stats response").SetInternal(err)
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
g.GET("/memo/all", func(c echo.Context) error {
|
|
|
|
|
ctx := c.Request().Context()
|
|
|
|
|
memoFind := &api.MemoFind{}
|
|
|
|
|
|
|
|
|
|
_, ok := c.Get(getUserIDContextKey()).(int)
|
|
|
|
|
if !ok {
|
|
|
|
|
memoFind.VisibilityList = []api.Visibility{api.Public}
|
|
|
|
|
} else {
|
|
|
|
|
memoFind.VisibilityList = []api.Visibility{api.Public, api.Protected}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pinnedStr := c.QueryParam("pinned")
|
|
|
|
|
if pinnedStr != "" {
|
|
|
|
|
pinned := pinnedStr == "true"
|
|
|
|
|
memoFind.Pinned = &pinned
|
|
|
|
|
}
|
|
|
|
|
tag := c.QueryParam("tag")
|
|
|
|
|
if tag != "" {
|
|
|
|
|
contentSearch := "#" + tag + " "
|
|
|
|
|
memoFind.ContentSearch = &contentSearch
|
|
|
|
|
}
|
|
|
|
|
visibilityListStr := c.QueryParam("visibility")
|
|
|
|
|
if visibilityListStr != "" {
|
|
|
|
|
visibilityList := []api.Visibility{}
|
|
|
|
|
for _, visibility := range strings.Split(visibilityListStr, ",") {
|
|
|
|
|
visibilityList = append(visibilityList, api.Visibility(visibility))
|
|
|
|
|
}
|
|
|
|
|
memoFind.VisibilityList = visibilityList
|
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Only fetch normal status memos.
|
|
|
|
|
normalStatus := api.Normal
|
|
|
|
|
memoFind.RowStatus = &normalStatus
|
|
|
|
|
|
|
|
|
|
list, err := s.Store.FindMemoList(ctx, memoFind)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to fetch all memo list").SetInternal(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sort.Slice(list, func(i, j int) bool {
|
|
|
|
|
return list[i].DisplayTs > list[j].DisplayTs
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
if memoFind.Limit != 0 {
|
|
|
|
|
list = list[memoFind.Offset:common.Min(len(list), memoFind.Offset+memoFind.Limit)]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
|
|
|
|
|
if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(list)); err != nil {
|
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode all memo list response").SetInternal(err)
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
g.GET("/memo/:memoId", func(c echo.Context) error {
|
|
|
|
|
ctx := c.Request().Context()
|
|
|
|
|
memoID, err := strconv.Atoi(c.Param("memoId"))
|
|
|
|
@ -400,13 +278,12 @@ func (s *Server) registerMemoRoutes(g *echo.Group) {
|
|
|
|
|
if !ok {
|
|
|
|
|
return echo.NewHTTPError(http.StatusUnauthorized, "Missing user in session")
|
|
|
|
|
}
|
|
|
|
|
memoOrganizerUpsert := &api.MemoOrganizerUpsert{
|
|
|
|
|
MemoID: memoID,
|
|
|
|
|
UserID: userID,
|
|
|
|
|
}
|
|
|
|
|
memoOrganizerUpsert := &api.MemoOrganizerUpsert{}
|
|
|
|
|
if err := json.NewDecoder(c.Request().Body).Decode(memoOrganizerUpsert); err != nil {
|
|
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, "Malformatted post memo organizer request").SetInternal(err)
|
|
|
|
|
}
|
|
|
|
|
memoOrganizerUpsert.MemoID = memoID
|
|
|
|
|
memoOrganizerUpsert.UserID = userID
|
|
|
|
|
|
|
|
|
|
err = s.Store.UpsertMemoOrganizer(ctx, memoOrganizerUpsert)
|
|
|
|
|
if err != nil {
|
|
|
|
@ -440,12 +317,12 @@ func (s *Server) registerMemoRoutes(g *echo.Group) {
|
|
|
|
|
|
|
|
|
|
currentTs := time.Now().Unix()
|
|
|
|
|
memoResourceUpsert := &api.MemoResourceUpsert{
|
|
|
|
|
MemoID: memoID,
|
|
|
|
|
UpdatedTs: ¤tTs,
|
|
|
|
|
}
|
|
|
|
|
if err := json.NewDecoder(c.Request().Body).Decode(memoResourceUpsert); err != nil {
|
|
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, "Malformatted post memo resource request").SetInternal(err)
|
|
|
|
|
}
|
|
|
|
|
memoResourceUpsert.MemoID = memoID
|
|
|
|
|
|
|
|
|
|
if _, err := s.Store.UpsertMemoResource(ctx, memoResourceUpsert); err != nil {
|
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to upsert memo resource").SetInternal(err)
|
|
|
|
@ -488,26 +365,127 @@ func (s *Server) registerMemoRoutes(g *echo.Group) {
|
|
|
|
|
return nil
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
g.DELETE("/memo/:memoId/resource/:resourceId", func(c echo.Context) error {
|
|
|
|
|
g.GET("/memo/amount", func(c echo.Context) error {
|
|
|
|
|
ctx := c.Request().Context()
|
|
|
|
|
memoID, err := strconv.Atoi(c.Param("memoId"))
|
|
|
|
|
normalRowStatus := api.Normal
|
|
|
|
|
memoFind := &api.MemoFind{
|
|
|
|
|
RowStatus: &normalRowStatus,
|
|
|
|
|
}
|
|
|
|
|
if userID, err := strconv.Atoi(c.QueryParam("userId")); err == nil {
|
|
|
|
|
memoFind.CreatorID = &userID
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
memoList, err := s.Store.FindMemoList(ctx, memoFind)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Memo ID is not a number: %s", c.Param("memoId"))).SetInternal(err)
|
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find memo list").SetInternal(err)
|
|
|
|
|
}
|
|
|
|
|
resourceID, err := strconv.Atoi(c.Param("resourceId"))
|
|
|
|
|
|
|
|
|
|
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
|
|
|
|
|
if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(len(memoList))); err != nil {
|
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode memo amount").SetInternal(err)
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
g.GET("/memo/stats", func(c echo.Context) error {
|
|
|
|
|
ctx := c.Request().Context()
|
|
|
|
|
normalStatus := api.Normal
|
|
|
|
|
memoFind := &api.MemoFind{
|
|
|
|
|
RowStatus: &normalStatus,
|
|
|
|
|
}
|
|
|
|
|
if creatorID, err := strconv.Atoi(c.QueryParam("creatorId")); err == nil {
|
|
|
|
|
memoFind.CreatorID = &creatorID
|
|
|
|
|
}
|
|
|
|
|
if memoFind.CreatorID == nil {
|
|
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, "Missing user id to find memo")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
currentUserID, ok := c.Get(getUserIDContextKey()).(int)
|
|
|
|
|
if !ok {
|
|
|
|
|
memoFind.VisibilityList = []api.Visibility{api.Public}
|
|
|
|
|
} else {
|
|
|
|
|
if *memoFind.CreatorID != currentUserID {
|
|
|
|
|
memoFind.VisibilityList = []api.Visibility{api.Public, api.Protected}
|
|
|
|
|
} else {
|
|
|
|
|
memoFind.VisibilityList = []api.Visibility{api.Public, api.Protected, api.Private}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
list, err := s.Store.FindMemoList(ctx, memoFind)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Resource ID is not a number: %s", c.Param("resourceId"))).SetInternal(err)
|
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to fetch memo list").SetInternal(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
memoResourceDelete := &api.MemoResourceDelete{
|
|
|
|
|
MemoID: &memoID,
|
|
|
|
|
ResourceID: &resourceID,
|
|
|
|
|
displayTsList := []int64{}
|
|
|
|
|
for _, memo := range list {
|
|
|
|
|
displayTsList = append(displayTsList, memo.DisplayTs)
|
|
|
|
|
}
|
|
|
|
|
if err := s.Store.DeleteMemoResource(ctx, memoResourceDelete); err != nil {
|
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to fetch resource list").SetInternal(err)
|
|
|
|
|
|
|
|
|
|
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
|
|
|
|
|
if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(displayTsList)); err != nil {
|
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode memo stats response").SetInternal(err)
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
return c.JSON(http.StatusOK, true)
|
|
|
|
|
g.GET("/memo/all", func(c echo.Context) error {
|
|
|
|
|
ctx := c.Request().Context()
|
|
|
|
|
memoFind := &api.MemoFind{}
|
|
|
|
|
|
|
|
|
|
_, ok := c.Get(getUserIDContextKey()).(int)
|
|
|
|
|
if !ok {
|
|
|
|
|
memoFind.VisibilityList = []api.Visibility{api.Public}
|
|
|
|
|
} else {
|
|
|
|
|
memoFind.VisibilityList = []api.Visibility{api.Public, api.Protected}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pinnedStr := c.QueryParam("pinned")
|
|
|
|
|
if pinnedStr != "" {
|
|
|
|
|
pinned := pinnedStr == "true"
|
|
|
|
|
memoFind.Pinned = &pinned
|
|
|
|
|
}
|
|
|
|
|
tag := c.QueryParam("tag")
|
|
|
|
|
if tag != "" {
|
|
|
|
|
contentSearch := "#" + tag + " "
|
|
|
|
|
memoFind.ContentSearch = &contentSearch
|
|
|
|
|
}
|
|
|
|
|
visibilityListStr := c.QueryParam("visibility")
|
|
|
|
|
if visibilityListStr != "" {
|
|
|
|
|
visibilityList := []api.Visibility{}
|
|
|
|
|
for _, visibility := range strings.Split(visibilityListStr, ",") {
|
|
|
|
|
visibilityList = append(visibilityList, api.Visibility(visibility))
|
|
|
|
|
}
|
|
|
|
|
memoFind.VisibilityList = visibilityList
|
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Only fetch normal status memos.
|
|
|
|
|
normalStatus := api.Normal
|
|
|
|
|
memoFind.RowStatus = &normalStatus
|
|
|
|
|
|
|
|
|
|
list, err := s.Store.FindMemoList(ctx, memoFind)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to fetch all memo list").SetInternal(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sort.Slice(list, func(i, j int) bool {
|
|
|
|
|
return list[i].DisplayTs > list[j].DisplayTs
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
if memoFind.Limit != 0 {
|
|
|
|
|
list = list[memoFind.Offset:common.Min(len(list), memoFind.Offset+memoFind.Limit)]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
|
|
|
|
|
if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(list)); err != nil {
|
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode all memo list response").SetInternal(err)
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
g.DELETE("/memo/:memoId", func(c echo.Context) error {
|
|
|
|
@ -516,19 +494,20 @@ func (s *Server) registerMemoRoutes(g *echo.Group) {
|
|
|
|
|
if !ok {
|
|
|
|
|
return echo.NewHTTPError(http.StatusUnauthorized, "Missing user in session")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
memoFind := &api.MemoFind{
|
|
|
|
|
ID: &memoID,
|
|
|
|
|
CreatorID: &userID,
|
|
|
|
|
}
|
|
|
|
|
if _, err := s.Store.FindMemo(ctx, memoFind); err != nil {
|
|
|
|
|
memo, err := s.Store.FindMemo(ctx, &api.MemoFind{
|
|
|
|
|
ID: &memoID,
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find memo").SetInternal(err)
|
|
|
|
|
}
|
|
|
|
|
if memo.CreatorID != userID {
|
|
|
|
|
return echo.NewHTTPError(http.StatusUnauthorized, "Unauthorized")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
memoDelete := &api.MemoDelete{
|
|
|
|
|
ID: memoID,
|
|
|
|
@ -542,4 +521,40 @@ func (s *Server) registerMemoRoutes(g *echo.Group) {
|
|
|
|
|
|
|
|
|
|
return c.JSON(http.StatusOK, true)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
g.DELETE("/memo/:memoId/resource/:resourceId", func(c echo.Context) error {
|
|
|
|
|
ctx := c.Request().Context()
|
|
|
|
|
userID, ok := c.Get(getUserIDContextKey()).(int)
|
|
|
|
|
if !ok {
|
|
|
|
|
return echo.NewHTTPError(http.StatusUnauthorized, "Missing user in session")
|
|
|
|
|
}
|
|
|
|
|
memoID, err := strconv.Atoi(c.Param("memoId"))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Memo ID is not a number: %s", c.Param("memoId"))).SetInternal(err)
|
|
|
|
|
}
|
|
|
|
|
resourceID, err := strconv.Atoi(c.Param("resourceId"))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Resource ID is not a number: %s", c.Param("resourceId"))).SetInternal(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
memo, err := s.Store.FindMemo(ctx, &api.MemoFind{
|
|
|
|
|
ID: &memoID,
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find memo").SetInternal(err)
|
|
|
|
|
}
|
|
|
|
|
if memo.CreatorID != userID {
|
|
|
|
|
return echo.NewHTTPError(http.StatusUnauthorized, "Unauthorized")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
memoResourceDelete := &api.MemoResourceDelete{
|
|
|
|
|
MemoID: &memoID,
|
|
|
|
|
ResourceID: &resourceID,
|
|
|
|
|
}
|
|
|
|
|
if err := s.Store.DeleteMemoResource(ctx, memoResourceDelete); err != nil {
|
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to fetch resource list").SetInternal(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return c.JSON(http.StatusOK, true)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|