diff --git a/api/memo.go b/api/memo.go index 0883c10f..f627b323 100644 --- a/api/memo.go +++ b/api/memo.go @@ -50,6 +50,9 @@ type MemoCreate struct { // Domain specific fields Visibility Visibility `json:"visibility"` Content string `json:"content"` + + // Related fields + ResourceIDList []int `json:"resourceIdList"` } type MemoPatch struct { diff --git a/server/memo.go b/server/memo.go index 2bc502ab..779209bc 100644 --- a/server/memo.go +++ b/server/memo.go @@ -56,6 +56,20 @@ func (s *Server) registerMemoRoutes(g *echo.Group) { return echo.NewHTTPError(http.StatusInternalServerError, "Failed to create memo").SetInternal(err) } + for _, resourceID := range memoCreate.ResourceIDList { + if _, err := s.Store.UpsertMemoResource(ctx, &api.MemoResourceUpsert{ + MemoID: memo.ID, + ResourceID: resourceID, + }); err != nil { + return echo.NewHTTPError(http.StatusInternalServerError, "Failed to upsert memo resource").SetInternal(err) + } + } + + memo, err = s.Store.ComposeMemo(ctx, memo) + if err != nil { + return echo.NewHTTPError(http.StatusInternalServerError, "Failed to compose memo").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) diff --git a/store/memo.go b/store/memo.go index 9cca2938..d65e9f00 100644 --- a/store/memo.go +++ b/store/memo.go @@ -44,9 +44,7 @@ func (raw *memoRaw) toMemo() *api.Memo { } } -func (s *Store) composeMemo(ctx context.Context, raw *memoRaw) (*api.Memo, error) { - memo := raw.toMemo() - +func (s *Store) ComposeMemo(ctx context.Context, memo *api.Memo) (*api.Memo, error) { memoOrganizer, err := s.FindMemoOrganizer(ctx, &api.MemoOrganizerFind{ MemoID: memo.ID, UserID: memo.CreatorID, @@ -87,7 +85,7 @@ func (s *Store) CreateMemo(ctx context.Context, create *api.MemoCreate) (*api.Me return nil, err } - memo, err := s.composeMemo(ctx, memoRaw) + memo, err := s.ComposeMemo(ctx, memoRaw.toMemo()) if err != nil { return nil, err } @@ -115,7 +113,7 @@ func (s *Store) PatchMemo(ctx context.Context, patch *api.MemoPatch) (*api.Memo, return nil, err } - memo, err := s.composeMemo(ctx, memoRaw) + memo, err := s.ComposeMemo(ctx, memoRaw.toMemo()) if err != nil { return nil, err } @@ -137,7 +135,7 @@ func (s *Store) FindMemoList(ctx context.Context, find *api.MemoFind) ([]*api.Me list := []*api.Memo{} for _, raw := range memoRawList { - memo, err := s.composeMemo(ctx, raw) + memo, err := s.ComposeMemo(ctx, raw.toMemo()) if err != nil { return nil, err } @@ -156,7 +154,7 @@ func (s *Store) FindMemo(ctx context.Context, find *api.MemoFind) (*api.Memo, er return nil, err } if has { - memo, err := s.composeMemo(ctx, memoRaw) + memo, err := s.ComposeMemo(ctx, memoRaw.toMemo()) if err != nil { return nil, err } @@ -184,7 +182,7 @@ func (s *Store) FindMemo(ctx context.Context, find *api.MemoFind) (*api.Memo, er return nil, err } - memo, err := s.composeMemo(ctx, memoRaw) + memo, err := s.ComposeMemo(ctx, memoRaw.toMemo()) if err != nil { return nil, err } diff --git a/web/src/components/MemoEditor.tsx b/web/src/components/MemoEditor.tsx index 0d36ced9..ca88a4f1 100644 --- a/web/src/components/MemoEditor.tsx +++ b/web/src/components/MemoEditor.tsx @@ -134,12 +134,10 @@ const MemoEditor: React.FC = () => { } editorStateService.clearEditMemo(); } else { - const memo = await memoService.createMemo({ + await memoService.createMemo({ content, + resourceIdList: state.resourceList.map((resource) => resource.id), }); - for (const resource of state.resourceList) { - await upsertMemoResource(memo.id, resource.id); - } locationService.clearQuery(); } } catch (error: any) { diff --git a/web/src/services/memoService.ts b/web/src/services/memoService.ts index 332149d0..da700a34 100644 --- a/web/src/services/memoService.ts +++ b/web/src/services/memoService.ts @@ -30,12 +30,14 @@ const memoService = { const timeoutIndex = setTimeout(() => { store.dispatch(setIsFetching(true)); }, 1000); - const memoFind: MemoFind = {}; + const memoFind: MemoFind = { + rowStatus: "NORMAL", + }; if (userService.isVisitorMode()) { memoFind.creatorId = userService.getUserIdFromPath(); } const { data } = (await api.getMemoList(memoFind)).data; - const memos = data.filter((m) => m.rowStatus !== "ARCHIVED").map((m) => convertResponseModelMemo(m)); + const memos = data.map((m) => convertResponseModelMemo(m)); store.dispatch(setMemos(memos)); clearTimeout(timeoutIndex); store.dispatch(setIsFetching(false)); @@ -60,6 +62,7 @@ const memoService = { fetchMemoById: async (memoId: MemoId) => { const { data } = (await api.getMemoById(memoId)).data; const memo = convertResponseModelMemo(data); + store.dispatch(patchMemo(memo)); return memo; }, diff --git a/web/src/types/modules/memo.d.ts b/web/src/types/modules/memo.d.ts index 021753af..7f921f3b 100644 --- a/web/src/types/modules/memo.d.ts +++ b/web/src/types/modules/memo.d.ts @@ -20,6 +20,7 @@ interface Memo { interface MemoCreate { content: string; + resourceIdList: ResourceId[]; visibility?: Visibility; }