diff --git a/api/v1/memo.go b/api/v1/memo.go index 3269129a..b304bd48 100644 --- a/api/v1/memo.go +++ b/api/v1/memo.go @@ -147,47 +147,48 @@ func (s *APIV1Service) registerMemoRoutes(g *echo.Group) { func (s *APIV1Service) GetMemoList(c echo.Context) error { ctx := c.Request().Context() hasParentFlag := false - findMemoMessage := &store.FindMemo{ - HasParent: &hasParentFlag, + find := &store.FindMemo{ + HasParent: &hasParentFlag, + OrderByPinned: true, } if userID, err := util.ConvertStringToInt32(c.QueryParam("creatorId")); err == nil { - findMemoMessage.CreatorID = &userID + find.CreatorID = &userID } if username := c.QueryParam("creatorUsername"); username != "" { user, _ := s.Store.GetUser(ctx, &store.FindUser{Username: &username}) if user != nil { - findMemoMessage.CreatorID = &user.ID + find.CreatorID = &user.ID } } currentUserID, ok := c.Get(userIDContextKey).(int32) if !ok { // Anonymous use should only fetch PUBLIC memos with specified user - if findMemoMessage.CreatorID == nil { + if find.CreatorID == nil { return echo.NewHTTPError(http.StatusBadRequest, "Missing user to find memo") } - findMemoMessage.VisibilityList = []store.Visibility{store.Public} + find.VisibilityList = []store.Visibility{store.Public} } else { // Authorized user can fetch all PUBLIC/PROTECTED memo visibilityList := []store.Visibility{store.Public, store.Protected} // If Creator is authorized user (as default), PRIVATE memo is OK - if findMemoMessage.CreatorID == nil || *findMemoMessage.CreatorID == currentUserID { - findMemoMessage.CreatorID = ¤tUserID + if find.CreatorID == nil || *find.CreatorID == currentUserID { + find.CreatorID = ¤tUserID visibilityList = append(visibilityList, store.Private) } - findMemoMessage.VisibilityList = visibilityList + find.VisibilityList = visibilityList } rowStatus := store.RowStatus(c.QueryParam("rowStatus")) if rowStatus != "" { - findMemoMessage.RowStatus = &rowStatus + find.RowStatus = &rowStatus } pinnedStr := c.QueryParam("pinned") if pinnedStr != "" { pinned := pinnedStr == "true" - findMemoMessage.Pinned = &pinned + find.Pinned = &pinned } contentSearch := []string{} @@ -199,13 +200,13 @@ func (s *APIV1Service) GetMemoList(c echo.Context) error { if content != "" { contentSearch = append(contentSearch, content) } - findMemoMessage.ContentSearch = contentSearch + find.ContentSearch = contentSearch if limit, err := strconv.Atoi(c.QueryParam("limit")); err == nil { - findMemoMessage.Limit = &limit + find.Limit = &limit } if offset, err := strconv.Atoi(c.QueryParam("offset")); err == nil { - findMemoMessage.Offset = &offset + find.Offset = &offset } memoDisplayWithUpdatedTs, err := s.getMemoDisplayWithUpdatedTsSettingValue(ctx) @@ -213,10 +214,10 @@ func (s *APIV1Service) GetMemoList(c echo.Context) error { return echo.NewHTTPError(http.StatusInternalServerError, "Failed to get memo display with updated ts setting value").SetInternal(err) } if memoDisplayWithUpdatedTs { - findMemoMessage.OrderByUpdatedTs = true + find.OrderByUpdatedTs = true } - list, err := s.Store.ListMemos(ctx, findMemoMessage) + list, err := s.Store.ListMemos(ctx, find) if err != nil { return echo.NewHTTPError(http.StatusInternalServerError, "Failed to fetch memo list").SetInternal(err) } diff --git a/store/db/mysql/memo.go b/store/db/mysql/memo.go index 0aeb2085..71a186ae 100644 --- a/store/db/mysql/memo.go +++ b/store/db/mysql/memo.go @@ -96,7 +96,10 @@ func (d *DB) ListMemos(ctx context.Context, find *store.FindMemo) ([]*store.Memo } where = append(where, fmt.Sprintf("`memo`.`visibility` in (%s)", strings.Join(list, ","))) } - orders := []string{"`pinned` DESC"} + orders := []string{} + if find.OrderByPinned { + orders = append(orders, "`pinned` DESC") + } if find.OrderByUpdatedTs { orders = append(orders, "`updated_ts` DESC") } else { diff --git a/store/db/sqlite/memo.go b/store/db/sqlite/memo.go index 9a729e5f..dd662a71 100644 --- a/store/db/sqlite/memo.go +++ b/store/db/sqlite/memo.go @@ -96,7 +96,10 @@ func (d *DB) ListMemos(ctx context.Context, find *store.FindMemo) ([]*store.Memo } } - orders := []string{"pinned DESC"} + orders := []string{} + if find.OrderByPinned { + orders = append(orders, "pinned DESC") + } if find.OrderByUpdatedTs { orders = append(orders, "updated_ts DESC") } else { diff --git a/store/memo.go b/store/memo.go index 35a6031b..8158d679 100644 --- a/store/memo.go +++ b/store/memo.go @@ -70,6 +70,7 @@ type FindMemo struct { Limit *int Offset *int OrderByUpdatedTs bool + OrderByPinned bool } type UpdateMemo struct {