Skip to content

Commit

Permalink
feat: add likedAt order support in project listing API
Browse files Browse the repository at this point in the history
Fixes goplus#1017

Signed-off-by: Aofei Sheng <aofei@aofeisheng.com>
  • Loading branch information
aofei committed Oct 24, 2024
1 parent ecbd0b8 commit affe698
Show file tree
Hide file tree
Showing 18 changed files with 115 additions and 59 deletions.
1 change: 1 addition & 0 deletions docs/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ paths:
- remixCount
- recentLikeCount
- recentRemixCount
- likedAt
default: createdAt
examples:
- createdAt
Expand Down
3 changes: 3 additions & 0 deletions spx-backend/cmd/spx-backend/get_assets_list.yap
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ if visibility := ${visibility}; visibility != "" {
if orderBy := ${orderBy}; orderBy != "" {
params.OrderBy = controller.ListAssetsOrderBy(orderBy)
}
if sortOrder := ${sortOrder}; sortOrder != "" {
params.SortOrder = controller.SortOrder(sortOrder)
}

params.Pagination.Index = ctx.ParamInt("pageIndex", firstPageIndex)
params.Pagination.Size = ctx.ParamInt("pageSize", defaultPageSize)
Expand Down
3 changes: 3 additions & 0 deletions spx-backend/cmd/spx-backend/get_project-releases_list.yap
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ if projectFullName := ${projectFullName}; projectFullName != "" {
if orderBy := ${orderBy}; orderBy != "" {
params.OrderBy = controller.ListProjectReleasesOrderBy(orderBy)
}
if sortOrder := ${sortOrder}; sortOrder != "" {
params.SortOrder = controller.SortOrder(sortOrder)
}

params.Pagination.Index = ctx.ParamInt("pageIndex", firstPageIndex)
params.Pagination.Size = ctx.ParamInt("pageSize", defaultPageSize)
Expand Down
3 changes: 3 additions & 0 deletions spx-backend/cmd/spx-backend/get_projects_list.yap
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ if fromFollowees := ${fromFollowees}; fromFollowees != "" {
if orderBy := ${orderBy}; orderBy != "" {
params.OrderBy = controller.ListProjectsOrderBy(orderBy)
}
if sortOrder := ${sortOrder}; sortOrder != "" {
params.SortOrder = controller.SortOrder(sortOrder)
}

params.Pagination.Index = paramInt("pageIndex", firstPageIndex)
params.Pagination.Size = paramInt("pageSize", defaultPageSize)
Expand Down
3 changes: 3 additions & 0 deletions spx-backend/cmd/spx-backend/get_users_list.yap
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ if followee := ${followee}; followee != "" {
if orderBy := ${orderBy}; orderBy != "" {
params.OrderBy = controller.ListUsersOrderBy(orderBy)
}
if sortOrder := ${sortOrder}; sortOrder != "" {
params.SortOrder = controller.SortOrder(sortOrder)
}

params.Pagination.Index = ctx.ParamInt("pageIndex", firstPageIndex)
params.Pagination.Size = ctx.ParamInt("pageSize", defaultPageSize)
Expand Down
108 changes: 68 additions & 40 deletions spx-backend/cmd/spx-backend/gop_autogen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion spx-backend/internal/controller/asset.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ type ListAssetsParams struct {
func NewListAssetsParams() *ListAssetsParams {
return &ListAssetsParams{
OrderBy: ListAssetsOrderByCreatedAt,
SortOrder: SortOrderDesc,
SortOrder: SortOrderAsc,
Pagination: Pagination{Index: 1, Size: 20},
}
}
Expand Down
2 changes: 1 addition & 1 deletion spx-backend/internal/controller/asset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ func TestControllerListAssets(t *testing.T) {

dbMockStmt = ctrl.db.Session(&gorm.Session{DryRun: true}).
Where(ctrl.db.Where("asset.owner_id = ?", mAuthedUser.ID).Or("asset.visibility = ?", model.VisibilityPublic)).
Order("asset.created_at desc, asset.id").
Order("asset.created_at asc, asset.id").
Limit(params.Pagination.Size).
Find(&[]model.Asset{}).
Statement
Expand Down
11 changes: 9 additions & 2 deletions spx-backend/internal/controller/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ const (
ListProjectsOrderByRemixCount ListProjectsOrderBy = "remixCount"
ListProjectsOrderByRecentLikeCount ListProjectsOrderBy = "recentLikeCount"
ListProjectsOrderByRecentRemixCount ListProjectsOrderBy = "recentRemixCount"
ListProjectsOrderByLikedAt ListProjectsOrderBy = "likedAt"
)

// IsValid reports whether the order by condition is valid.
Expand All @@ -261,7 +262,8 @@ func (ob ListProjectsOrderBy) IsValid() bool {
ListProjectsOrderByLikeCount,
ListProjectsOrderByRemixCount,
ListProjectsOrderByRecentLikeCount,
ListProjectsOrderByRecentRemixCount:
ListProjectsOrderByRecentRemixCount,
ListProjectsOrderByLikedAt:
return true
}
return false
Expand Down Expand Up @@ -328,7 +330,7 @@ type ListProjectsParams struct {
func NewListProjectsParams() *ListProjectsParams {
return &ListProjectsParams{
OrderBy: ListProjectsOrderByCreatedAt,
SortOrder: SortOrderDesc,
SortOrder: SortOrderAsc,
Pagination: Pagination{Index: 1, Size: 20},
}
}
Expand Down Expand Up @@ -451,6 +453,11 @@ func (ctrl *Controller) ListProjects(ctx context.Context, params *ListProjectsPa
break
}
queryOrderByColumn = "COUNT(remixed_project.id)"
case ListProjectsOrderByLikedAt:
if params.Liker == nil {
break
}
queryOrderByColumn = "liker_relationship.liked_at"
}
if queryOrderByColumn == "" {
queryOrderByColumn = "project.created_at"
Expand Down
2 changes: 1 addition & 1 deletion spx-backend/internal/controller/project_release.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ type ListProjectReleasesParams struct {
func NewListProjectReleasesParams() *ListProjectReleasesParams {
return &ListProjectReleasesParams{
OrderBy: ListProjectReleasesOrderByCreatedAt,
SortOrder: SortOrderDesc,
SortOrder: SortOrderAsc,
Pagination: Pagination{Index: 1, Size: 20},
}
}
Expand Down
13 changes: 7 additions & 6 deletions spx-backend/internal/controller/project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,7 @@ func TestListProjectsOrderBy(t *testing.T) {
assert.True(t, ListProjectsOrderByRemixCount.IsValid())
assert.True(t, ListProjectsOrderByRecentLikeCount.IsValid())
assert.True(t, ListProjectsOrderByRecentRemixCount.IsValid())
assert.True(t, ListProjectsOrderByLikedAt.IsValid())
})

t.Run("Invalid", func(t *testing.T) {
Expand Down Expand Up @@ -713,7 +714,7 @@ func TestControllerListProjects(t *testing.T) {

dbMockStmt = ctrl.db.Session(&gorm.Session{DryRun: true}).
Where(ctrl.db.Where("project.owner_id = ?", mAuthedUser.ID).Or("project.visibility = ?", model.VisibilityPublic)).
Order("project.created_at desc, project.id").
Order("project.created_at asc, project.id").
Limit(2).
Find(&[]model.Project{}).
Statement
Expand Down Expand Up @@ -778,7 +779,7 @@ func TestControllerListProjects(t *testing.T) {
dbMockStmt = ctrl.db.Session(&gorm.Session{DryRun: true}).
Joins("JOIN user ON user.id = project.owner_id").
Where("user.username = ?", *params.Owner).
Order("project.created_at desc, project.id").
Order("project.created_at asc, project.id").
Limit(params.Pagination.Size).
Find(&[]model.Project{}).
Statement
Expand Down Expand Up @@ -843,7 +844,7 @@ func TestControllerListProjects(t *testing.T) {
dbMockStmt = ctrl.db.Session(&gorm.Session{DryRun: true}).
Where("project.name LIKE ?", "%"+*params.Keyword+"%").
Where(ctrl.db.Where("project.owner_id = ?", mAuthedUser.ID).Or("project.visibility = ?", model.VisibilityPublic)).
Order("project.created_at desc, project.id").
Order("project.created_at asc, project.id").
Limit(params.Pagination.Size).
Find(&[]model.Project{}).
Statement
Expand Down Expand Up @@ -914,7 +915,7 @@ func TestControllerListProjects(t *testing.T) {
Where(ctrl.db.Where("project.owner_id = ?", mAuthedUser.ID).Or("project.visibility = ?", model.VisibilityPublic)).
Where("liker.username = ?", *params.Liker).
Where("liker_relationship.liked_at IS NOT NULL").
Order("project.created_at desc, project.id").
Order("project.created_at asc, project.id").
Limit(params.Pagination.Size).
Find(&[]model.Project{}).
Statement
Expand Down Expand Up @@ -1063,7 +1064,7 @@ func TestControllerListProjects(t *testing.T) {

dbMockStmt = ctrl.db.Session(&gorm.Session{DryRun: true}).
Where(ctrl.db.Where("project.owner_id = ?", mAuthedUser.ID).Or("project.visibility = ?", model.VisibilityPublic)).
Order("project.created_at desc, project.id").
Order("project.created_at asc, project.id").
Limit(params.Pagination.Size).
Find(&[]model.Project{}).
Statement
Expand Down Expand Up @@ -1124,7 +1125,7 @@ func TestControllerListProjects(t *testing.T) {
Where("remixed_from_user.username = ?", "original_user").
Where("remixed_from_project.name = ?", "original_project").
Where(ctrl.db.Where("project.owner_id = ?", mAuthedUser.ID).Or("project.visibility = ?", model.VisibilityPublic)).
Order("project.created_at desc, project.id").
Order("project.created_at asc, project.id").
Limit(params.Pagination.Size).
Find(&[]model.Project{}).
Statement
Expand Down
2 changes: 1 addition & 1 deletion spx-backend/internal/controller/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ type ListUsersParams struct {
func NewListUsersParams() *ListUsersParams {
return &ListUsersParams{
OrderBy: ListUsersOrderByCreatedAt,
SortOrder: SortOrderDesc,
SortOrder: SortOrderAsc,
Pagination: Pagination{Index: 1, Size: 20},
}
}
Expand Down
Loading

0 comments on commit affe698

Please sign in to comment.