Skip to content

Commit

Permalink
feat(guests controller): improve search query
Browse files Browse the repository at this point in the history
tokenises on space and supports inverted commas
  • Loading branch information
stakach committed Sep 2, 2021
1 parent b762fc3 commit f54980c
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions src/controllers/guests.cr
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require "csv"

class Guests < Application
base "/api/staff/v1/guests"

Expand Down Expand Up @@ -139,11 +141,17 @@ class Guests < Application
.limit(1500).map { |g| attending_guest(nil, g) }
else
# Return guests based on the filter query
query = "%#{query}%"
render json: Guest.query
.by_tenant(tenant.id)
.where("searchable LIKE :query", query: query)
.limit(1500).map { |g| attending_guest(nil, g) }
csv = CSV.new(query, strip: true, separator: ' ')
csv.next
parts = csv.row.to_a

sql_query = Guest.query.by_tenant(tenant.id)
parts.each do |part|
next if part.empty?
sql_query = sql_query.where("searchable LIKE :query", query: "#{part}%")
end

render json: sql_query.order_by("name").limit(1500).map { |g| attending_guest(nil, g) }
end
end

Expand Down

0 comments on commit f54980c

Please sign in to comment.