mirror of
https://github.com/Growstuff/growstuff.git
synced 2026-05-18 21:56:55 -04:00
* Add crops search API endpoint - Added GET /api/v1/crops/search endpoint. - Updated CropSearchService to support additional search options. - Manually updated Swagger documentation in swagger/v1/swagger.json. - Added request specs to verify the new endpoint. Co-authored-by: CloCkWeRX <365751+CloCkWeRX@users.noreply.github.com> * Add crops search API endpoint - Added GET /api/v1/crops/search endpoint. - Updated CropSearchService to support additional search options. - Manually updated Swagger documentation in swagger/v1/swagger.json. - Added request specs to verify the new endpoint. Co-authored-by: CloCkWeRX <365751+CloCkWeRX@users.noreply.github.com> --------- Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
39 lines
924 B
Ruby
39 lines
924 B
Ruby
# frozen_string_literal: true
|
|
|
|
module Api
|
|
module V1
|
|
class CropsController < BaseController
|
|
def search
|
|
term = params[:term]
|
|
page = params.dig(:page, :number) || 1
|
|
per_page = params.dig(:page, :size) || Crop.per_page
|
|
|
|
search_results = CropSearchService.search(
|
|
term,
|
|
page: page,
|
|
per_page: per_page,
|
|
load: true
|
|
)
|
|
|
|
resources = search_results.map do |crop|
|
|
Api::V1::CropResource.new(crop, context)
|
|
end
|
|
|
|
serializer = JSONAPI::ResourceSerializer.new(Api::V1::CropResource)
|
|
|
|
data = resources.map do |resource|
|
|
serializer.object_hash(resource, {})
|
|
end
|
|
|
|
render json: {
|
|
data: data,
|
|
meta: {
|
|
record_count: search_results.total_count,
|
|
page_count: search_results.total_pages
|
|
}
|
|
}
|
|
end
|
|
end
|
|
end
|
|
end
|