chore: update memo detail access handler

This commit is contained in:
Steven 2022-09-21 19:31:02 +08:00
parent 2acd5d4af2
commit 63468dbaf3
5 changed files with 49 additions and 38 deletions

View File

@ -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)
} }

View File

@ -203,6 +203,41 @@ func (s *Server) registerMemoRoutes(g *echo.Group) {
return nil return nil
}) })
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)
}
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)
}
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)
}
return nil
})
g.POST("/memo/:memoId/organizer", 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"))
@ -245,32 +280,6 @@ func (s *Server) registerMemoRoutes(g *echo.Group) {
return nil return nil
}) })
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)
}
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)
}
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
})
g.DELETE("/memo/:memoId", func(c echo.Context) error { g.DELETE("/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"))

View File

@ -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}`);
}; };

View File

@ -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 });
}; };

View File

@ -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,12 +38,18 @@ 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
setState({ .fetchMemoById(memoId)
memo, .then((memo) => {
setState({
memo,
});
loadingState.setFinish();
})
.catch((error) => {
console.error(error);
toastHelper.error(error.response.data.message);
}); });
loadingState.setFinish();
});
} }
}, [location]); }, [location]);