chore: fix order by pinned

pull/2536/head
Steven 1 year ago
parent 94c71cb834
commit ec2995d64a

@ -147,47 +147,48 @@ func (s *APIV1Service) registerMemoRoutes(g *echo.Group) {
func (s *APIV1Service) GetMemoList(c echo.Context) error { func (s *APIV1Service) GetMemoList(c echo.Context) error {
ctx := c.Request().Context() ctx := c.Request().Context()
hasParentFlag := false hasParentFlag := false
findMemoMessage := &store.FindMemo{ find := &store.FindMemo{
HasParent: &hasParentFlag, HasParent: &hasParentFlag,
OrderByPinned: true,
} }
if userID, err := util.ConvertStringToInt32(c.QueryParam("creatorId")); err == nil { if userID, err := util.ConvertStringToInt32(c.QueryParam("creatorId")); err == nil {
findMemoMessage.CreatorID = &userID find.CreatorID = &userID
} }
if username := c.QueryParam("creatorUsername"); username != "" { if username := c.QueryParam("creatorUsername"); username != "" {
user, _ := s.Store.GetUser(ctx, &store.FindUser{Username: &username}) user, _ := s.Store.GetUser(ctx, &store.FindUser{Username: &username})
if user != nil { if user != nil {
findMemoMessage.CreatorID = &user.ID find.CreatorID = &user.ID
} }
} }
currentUserID, ok := c.Get(userIDContextKey).(int32) currentUserID, ok := c.Get(userIDContextKey).(int32)
if !ok { if !ok {
// Anonymous use should only fetch PUBLIC memos with specified user // 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") return echo.NewHTTPError(http.StatusBadRequest, "Missing user to find memo")
} }
findMemoMessage.VisibilityList = []store.Visibility{store.Public} find.VisibilityList = []store.Visibility{store.Public}
} else { } else {
// Authorized user can fetch all PUBLIC/PROTECTED memo // Authorized user can fetch all PUBLIC/PROTECTED memo
visibilityList := []store.Visibility{store.Public, store.Protected} visibilityList := []store.Visibility{store.Public, store.Protected}
// If Creator is authorized user (as default), PRIVATE memo is OK // If Creator is authorized user (as default), PRIVATE memo is OK
if findMemoMessage.CreatorID == nil || *findMemoMessage.CreatorID == currentUserID { if find.CreatorID == nil || *find.CreatorID == currentUserID {
findMemoMessage.CreatorID = &currentUserID find.CreatorID = &currentUserID
visibilityList = append(visibilityList, store.Private) visibilityList = append(visibilityList, store.Private)
} }
findMemoMessage.VisibilityList = visibilityList find.VisibilityList = visibilityList
} }
rowStatus := store.RowStatus(c.QueryParam("rowStatus")) rowStatus := store.RowStatus(c.QueryParam("rowStatus"))
if rowStatus != "" { if rowStatus != "" {
findMemoMessage.RowStatus = &rowStatus find.RowStatus = &rowStatus
} }
pinnedStr := c.QueryParam("pinned") pinnedStr := c.QueryParam("pinned")
if pinnedStr != "" { if pinnedStr != "" {
pinned := pinnedStr == "true" pinned := pinnedStr == "true"
findMemoMessage.Pinned = &pinned find.Pinned = &pinned
} }
contentSearch := []string{} contentSearch := []string{}
@ -199,13 +200,13 @@ func (s *APIV1Service) GetMemoList(c echo.Context) error {
if content != "" { if content != "" {
contentSearch = append(contentSearch, content) contentSearch = append(contentSearch, content)
} }
findMemoMessage.ContentSearch = contentSearch find.ContentSearch = contentSearch
if limit, err := strconv.Atoi(c.QueryParam("limit")); err == nil { 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 { if offset, err := strconv.Atoi(c.QueryParam("offset")); err == nil {
findMemoMessage.Offset = &offset find.Offset = &offset
} }
memoDisplayWithUpdatedTs, err := s.getMemoDisplayWithUpdatedTsSettingValue(ctx) 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) return echo.NewHTTPError(http.StatusInternalServerError, "Failed to get memo display with updated ts setting value").SetInternal(err)
} }
if memoDisplayWithUpdatedTs { 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 { if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to fetch memo list").SetInternal(err) return echo.NewHTTPError(http.StatusInternalServerError, "Failed to fetch memo list").SetInternal(err)
} }

@ -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, ","))) 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 { if find.OrderByUpdatedTs {
orders = append(orders, "`updated_ts` DESC") orders = append(orders, "`updated_ts` DESC")
} else { } else {

@ -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 { if find.OrderByUpdatedTs {
orders = append(orders, "updated_ts DESC") orders = append(orders, "updated_ts DESC")
} else { } else {

@ -70,6 +70,7 @@ type FindMemo struct {
Limit *int Limit *int
Offset *int Offset *int
OrderByUpdatedTs bool OrderByUpdatedTs bool
OrderByPinned bool
} }
type UpdateMemo struct { type UpdateMemo struct {

Loading…
Cancel
Save