|
|
|
@ -203,33 +203,17 @@ func (s *Server) registerMemoRoutes(g *echo.Group) {
|
|
|
|
|
return nil
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
g.POST("/memo/:memoId/organizer", func(c echo.Context) error {
|
|
|
|
|
g.GET("/memo/:memoId", func(c echo.Context) error {
|
|
|
|
|
ctx := c.Request().Context()
|
|
|
|
|
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, ok := c.Get(getUserIDContextKey()).(int)
|
|
|
|
|
if !ok {
|
|
|
|
|
return echo.NewHTTPError(http.StatusUnauthorized, "Missing user in session")
|
|
|
|
|
}
|
|
|
|
|
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(ctx, memoOrganizerUpsert)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to upsert memo organizer").SetInternal(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
memo, err := s.Store.FindMemo(ctx, &api.MemoFind{
|
|
|
|
|
memoFind := &api.MemoFind{
|
|
|
|
|
ID: &memoID,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
memo, err := s.Store.FindMemo(ctx, memoFind)
|
|
|
|
|
if err != nil {
|
|
|
|
|
if common.ErrorCode(err) == common.NotFound {
|
|
|
|
|
return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Memo ID not found: %d", memoID)).SetInternal(err)
|
|
|
|
@ -238,6 +222,15 @@ func (s *Server) registerMemoRoutes(g *echo.Group) {
|
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to find memo by ID: %v", memoID)).SetInternal(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if memo.Visibility == api.Privite {
|
|
|
|
|
return echo.NewHTTPError(http.StatusForbidden, "this memo is private only")
|
|
|
|
|
} else if memo.Visibility == api.Protected {
|
|
|
|
|
_, ok := c.Get(getUserIDContextKey()).(int)
|
|
|
|
|
if !ok {
|
|
|
|
|
return echo.NewHTTPError(http.StatusForbidden, "this memo is protected, missing user in session")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
@ -245,17 +238,33 @@ func (s *Server) registerMemoRoutes(g *echo.Group) {
|
|
|
|
|
return nil
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
g.GET("/memo/:memoId", func(c echo.Context) error {
|
|
|
|
|
g.POST("/memo/:memoId/organizer", func(c echo.Context) error {
|
|
|
|
|
ctx := c.Request().Context()
|
|
|
|
|
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,
|
|
|
|
|
userID, ok := c.Get(getUserIDContextKey()).(int)
|
|
|
|
|
if !ok {
|
|
|
|
|
return echo.NewHTTPError(http.StatusUnauthorized, "Missing user in session")
|
|
|
|
|
}
|
|
|
|
|
memo, err := s.Store.FindMemo(ctx, memoFind)
|
|
|
|
|
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(ctx, memoOrganizerUpsert)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to upsert memo organizer").SetInternal(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
memo, err := s.Store.FindMemo(ctx, &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)
|
|
|
|
|