From 273bfb1d130366b7e05b0dbfba887e9b72f9cb3d Mon Sep 17 00:00:00 2001 From: Steven Date: Sat, 17 Sep 2022 11:35:44 +0800 Subject: [PATCH] chore: add shortcut creator field --- api/shortcut.go | 1 + server/shortcut.go | 18 ++++++++++++++++++ store/shortcut.go | 14 ++++++++++++++ web/src/components/ShortcutListView.tsx | 9 +++++---- web/src/components/WorkspaceListView.tsx | 14 +++++++------- web/src/types/modules/shortcut.d.ts | 1 + 6 files changed, 46 insertions(+), 11 deletions(-) diff --git a/api/shortcut.go b/api/shortcut.go index 8f3eb81..0e3648a 100644 --- a/api/shortcut.go +++ b/api/shortcut.go @@ -29,6 +29,7 @@ type Shortcut struct { // Standard fields CreatorID int `json:"creatorId"` + Creator *User `json:"creator"` CreatedTs int64 `json:"createdTs"` UpdatedTs int64 `json:"updatedTs"` WorkspaceID int `json:"workspaceId"` diff --git a/server/shortcut.go b/server/shortcut.go index d232039..1158d63 100644 --- a/server/shortcut.go +++ b/server/shortcut.go @@ -31,6 +31,10 @@ func (s *Server) registerShortcutRoutes(g *echo.Group) { return echo.NewHTTPError(http.StatusInternalServerError, "Failed to create shortcut").SetInternal(err) } + if err := s.Store.ComposeShortcut(ctx, shortcut); err != nil { + return echo.NewHTTPError(http.StatusInternalServerError, "Failed to compose shortcut").SetInternal(err) + } + c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8) if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(shortcut)); err != nil { return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode shortcut response").SetInternal(err) @@ -57,6 +61,10 @@ func (s *Server) registerShortcutRoutes(g *echo.Group) { return echo.NewHTTPError(http.StatusInternalServerError, "Failed to patch shortcut").SetInternal(err) } + if err := s.Store.ComposeShortcut(ctx, shortcut); err != nil { + return echo.NewHTTPError(http.StatusInternalServerError, "Failed to compose shortcut").SetInternal(err) + } + c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8) if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(shortcut)); err != nil { return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode shortcut response").SetInternal(err) @@ -101,6 +109,12 @@ func (s *Server) registerShortcutRoutes(g *echo.Group) { } list = append(list, privateShortcutList...) + for _, shortcut := range list { + if err := s.Store.ComposeShortcut(ctx, shortcut); err != nil { + return echo.NewHTTPError(http.StatusInternalServerError, "Failed to compose shortcut").SetInternal(err) + } + } + c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8) if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(list)); err != nil { return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode shortcut list response").SetInternal(err) @@ -123,6 +137,10 @@ func (s *Server) registerShortcutRoutes(g *echo.Group) { return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch shortcut by ID %d", *shortcutFind.ID)).SetInternal(err) } + if err := s.Store.ComposeShortcut(ctx, shortcut); err != nil { + return echo.NewHTTPError(http.StatusInternalServerError, "Failed to compose shortcut").SetInternal(err) + } + c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8) if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(shortcut)); err != nil { return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode shortcut response").SetInternal(err) diff --git a/store/shortcut.go b/store/shortcut.go index f8a7902..e9a2ab0 100644 --- a/store/shortcut.go +++ b/store/shortcut.go @@ -46,6 +46,20 @@ func (raw *shortcutRaw) toShortcut() *api.Shortcut { } } +func (s *Store) ComposeShortcut(ctx context.Context, shortcut *api.Shortcut) error { + user, err := s.FindUser(ctx, &api.UserFind{ + ID: &shortcut.CreatorID, + }) + if err != nil { + return err + } + user.OpenID = "" + user.UserSettingList = nil + shortcut.Creator = user + + return nil +} + func (s *Store) CreateShortcut(ctx context.Context, create *api.ShortcutCreate) (*api.Shortcut, error) { tx, err := s.db.BeginTx(ctx, nil) if err != nil { diff --git a/web/src/components/ShortcutListView.tsx b/web/src/components/ShortcutListView.tsx index 6e08156..7130e5d 100644 --- a/web/src/components/ShortcutListView.tsx +++ b/web/src/components/ShortcutListView.tsx @@ -33,14 +33,15 @@ const ShortcutListView: React.FC = (props: Props) => { ({shortcut.description})
- {shortcut.creator.name} + @@ -49,14 +50,14 @@ const ShortcutListView: React.FC = (props: Props) => { <> } actionsClassName="!w-24" diff --git a/web/src/types/modules/shortcut.d.ts b/web/src/types/modules/shortcut.d.ts index 6547d16..3a0c86d 100644 --- a/web/src/types/modules/shortcut.d.ts +++ b/web/src/types/modules/shortcut.d.ts @@ -6,6 +6,7 @@ interface Shortcut { id: ShortcutId; creatorId: UserId; + creator: User; createdTs: TimeStamp; updatedTs: TimeStamp; workspaceId: WorkspaceId;