Skip to content

Commit

Permalink
feat: add search
Browse files Browse the repository at this point in the history
  • Loading branch information
aripiprazole committed Aug 23, 2023
1 parent 3096eb9 commit 88c235a
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions Rinha/Entities/Person.lean
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ structure Person where
name : Name
birthdate : String
stack : Option (List Stack)
search : String -- All things concatenated
deriving Repr

instance : Ash.ToJSON Person where
Expand All @@ -96,7 +97,8 @@ instance : FromJSON Person where
let name ← json.find? "nome" >>= String.toName?
let birthdate ← json.find? "nascimento"
let stack ← json.find? "stack" <&> String.parseStack
return {id := "", username, name, birthdate, stack }
let search := s!"{username.data}{name.data}{String.intercalate "," $ (stack.getD []).map Stack.data}"
return {id := "", username, name, birthdate, stack, search}

--//////////////////////////////////////////////////////////////////////////////
--//// SECTION: Queries Repository /////////////////////////////////////////////
Expand All @@ -109,11 +111,12 @@ instance : FromResult Person where
let name ← rs.get "name" >>= String.toName?
let birthdate ← rs.get "birth_date"
let stack ← Option.map String.toStack? $ rs.get "stack"
return {id, username, name, birthdate, stack}
let search ← rs.get "search"
return {id, username, name, birthdate, stack, search}

/-- Finds a list person by it's stack -/
def findLike (queryStr : String) (conn : Connection) : IO (List Person) := do
let query := "SELECT * FROM users WHERE stack LIKE $1 OR username LIKE $1 OR name LIKE $1 LIMIT 50;"
let query := "SELECT * FROM users WHERE search LIKE $1 LIMIT 50;"
let result ← exec conn query #[s!"%{queryStr}%"]
match result with
| Except.error _ => return []
Expand Down

0 comments on commit 88c235a

Please sign in to comment.