feat: create memo with resourceIdList

This commit is contained in:
steven 2022-10-01 10:57:14 +08:00
parent 9f8c0ce567
commit ae8f292306
6 changed files with 31 additions and 14 deletions

View File

@ -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 {

View File

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

View File

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

View File

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

View File

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

View File

@ -20,6 +20,7 @@ interface Memo {
interface MemoCreate {
content: string;
resourceIdList: ResourceId[];
visibility?: Visibility;
}