chore: update memo detail access handler

pull/244/head
Steven 3 years ago
parent 2acd5d4af2
commit 63468dbaf3

@ -104,7 +104,7 @@ func aclMiddleware(s *Server, next echo.HandlerFunc) echo.HandlerFunc {
} }
} }
if common.HasPrefixes(path, "/api/memo/all") && c.Request().Method == http.MethodGet { if common.HasPrefixes(path, "/api/memo/all", "/api/memo/:memoId") && c.Request().Method == http.MethodGet {
return next(c) return next(c)
} }

@ -203,33 +203,17 @@ func (s *Server) registerMemoRoutes(g *echo.Group) {
return nil 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() ctx := c.Request().Context()
memoID, err := strconv.Atoi(c.Param("memoId")) memoID, err := strconv.Atoi(c.Param("memoId"))
if err != nil { if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("ID is not a number: %s", c.Param("memoId"))).SetInternal(err) return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("ID is not a number: %s", c.Param("memoId"))).SetInternal(err)
} }
userID, ok := c.Get(getUserIDContextKey()).(int) memoFind := &api.MemoFind{
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{
ID: &memoID, ID: &memoID,
}) }
memo, err := s.Store.FindMemo(ctx, memoFind)
if err != nil { if err != nil {
if common.ErrorCode(err) == common.NotFound { 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.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) 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) c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(memo)); err != nil { 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 echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode memo response").SetInternal(err)
@ -245,17 +238,33 @@ func (s *Server) registerMemoRoutes(g *echo.Group) {
return nil 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() ctx := c.Request().Context()
memoID, err := strconv.Atoi(c.Param("memoId")) memoID, err := strconv.Atoi(c.Param("memoId"))
if err != nil { if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("ID is not a number: %s", c.Param("memoId"))).SetInternal(err) return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("ID is not a number: %s", c.Param("memoId"))).SetInternal(err)
} }
memoFind := &api.MemoFind{ userID, ok := c.Get(getUserIDContextKey()).(int)
ID: &memoID, 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 err != nil {
if common.ErrorCode(err) == common.NotFound { 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.StatusNotFound, fmt.Sprintf("Memo ID not found: %d", memoID)).SetInternal(err)

@ -115,11 +115,6 @@ const MemoCardDialog: React.FC<Props> = (props: Props) => {
}, []); }, []);
const handleGotoMemoLinkBtnClick = () => { const handleGotoMemoLinkBtnClick = () => {
if (memo.visibility === "PRIVATE") {
toastHelper.error(t("message.private-only"));
return;
}
window.open(`/m/${memo.id}`); window.open(`/m/${memo.id}`);
}; };

@ -94,7 +94,7 @@ const initialToastHelper = () => {
return showToast({ type: "success", content, duration }); return showToast({ type: "success", content, duration });
}; };
const error = (content: string, duration = 3000) => { const error = (content: string, duration = -1) => {
return showToast({ type: "error", content, duration }); return showToast({ type: "error", content, duration });
}; };

@ -7,6 +7,7 @@ import { UNKNOWN_ID } from "../helpers/consts";
import { isNullorUndefined } from "../helpers/utils"; import { isNullorUndefined } from "../helpers/utils";
import { useAppSelector } from "../store"; import { useAppSelector } from "../store";
import useLoading from "../hooks/useLoading"; import useLoading from "../hooks/useLoading";
import toastHelper from "../components/Toast";
import MemoContent from "../components/MemoContent"; import MemoContent from "../components/MemoContent";
import MemoResources from "../components/MemoResources"; import MemoResources from "../components/MemoResources";
import "../less/explore.less"; import "../less/explore.less";
@ -37,11 +38,17 @@ const MemoDetail = () => {
const memoId = Number(params.memoId); const memoId = Number(params.memoId);
if (memoId && !isNaN(memoId)) { if (memoId && !isNaN(memoId)) {
memoService.fetchMemoById(memoId).then((memo) => { memoService
.fetchMemoById(memoId)
.then((memo) => {
setState({ setState({
memo, memo,
}); });
loadingState.setFinish(); loadingState.setFinish();
})
.catch((error) => {
console.error(error);
toastHelper.error(error.response.data.message);
}); });
} }
}, [location]); }, [location]);

Loading…
Cancel
Save