diff --git a/app/api/mobile/apis/project_packages.rb b/app/api/mobile/apis/project_packages.rb index 01af989b..591e54b1 100644 --- a/app/api/mobile/apis/project_packages.rb +++ b/app/api/mobile/apis/project_packages.rb @@ -21,6 +21,7 @@ module Mobile optional :category, type: String, desc: '类型' optional :keyword, type: String, desc: '搜索关键字' optional :sort_by, type: String, desc: '排序' + optional :sort_direction, type: String, desc: '排序方向' optional :page, type: Integer, desc: '页数' optional :per_page, type: Integer, desc: '分页大小' end @@ -34,11 +35,10 @@ module Mobile count = packages.count - if params[:sort_by] == 'price' - packages = packages.order('min_price desc') - else - packages = packages.order('published_at desc') - end + direction = params[:sort_direction] == 'asc' ? 'asc' : 'desc' + sort = params[:sort_by] == 'price' ? 'min_price' : 'published_at' + packages = packages.order("#{sort} #{direction}") + packages = paginate packages.includes(:creator, :attachments, bidding_users: :user) present :count, count