on master help sign in
Added user search
commitf2346b836c15ba3b932c179e20c012fefc57f10b authoriko <ilyakooo0@gmail.com> dateSat 11 May 2019, 23:28 (7 years ago) parenta24e1e9
2 files changed+63−5
denis/src/Data/Query.hs+58−4 denis/src/Server/API.hs+5−1
denis/src/Data/Query.hs+58−4
@@ -55,7 +55,8 @@ module Data.Query (
5555 UserCreation(..),
5656 createUser,
5757 tryActivateToken,
58− deactivateToken
58+ deactivateToken,
59+ searchUsers
5960 ) where
6061
6162import Squeal.PostgreSQL
@@ -523,6 +524,37 @@ getFacultySearchResultQ = selectStar (from (table #faculties) &
523524 (arrayToText #facultyTags)])
524525 query = tsQuery "russian" (param @1)
525526
527+searchUsersQ :: Query Schema (TuplePG (Only Text)) (RowPG UserRow)
528+searchUsersQ = select (
529+ #users ! #userRowId :*
530+ #users ! #userRowFirstName :*
531+ #users ! #userRowMiddleName :*
532+ #users ! #userRowLastName :*
533+ #users ! #userRowFacultyUrl :*
534+ #users ! #userRowEmail :*
535+ #users ! #userRowIsValidated :*
536+ #faculties ! #facultyName `as` #userRowFacultyName :*
537+ #faculties ! #facultyUrl `as` #userRowFacultyURL :*
538+ #faculties ! #facultyPath `as` #userRowFacultyPath :*
539+ #faculties ! #facultyCampusName `as` #userRowFacultyCampusName :*
540+ #faculties ! #facultyCampusCode `as` #userRowFacultyCampusCode :*
541+ #faculties ! #facultyTags `as` #userRowFacultyTags :*
542+ #faculties ! #facultyAddress `as` #userRowFacultyAddress
543+ ) (from ((table #users) &
544+ innerJoin (table #faculties)
545+ (#users ! #userRowFacultyUrl .== #faculties ! #facultyUrl)) &
546+ where_ (notNull $ #users ! #userRowIsValidated) &
547+ where_ (vector @@ query) &
548+ orderBy [tsRankCd vector query & Desc] &
549+ limit 100)
550+ where
551+ vector = tsVector' (textArrayToText [
552+ #users ! #userRowFirstName,
553+ #users ! #userRowMiddleName,
554+ #users ! #userRowLastName
555+ ])
556+ query = tsQuery' (param @1)
557+
526558tsRankCd
527559 :: Expression schema from grouping params ('NotNull 'PGtext)
528560 -> Expression schema from grouping params ('NotNull 'PGtext)
@@ -537,6 +569,12 @@ tsVector
537569tsVector lang x = UnsafeExpression $
538570 "to_tsvector" <> parenthesized (commaSeparated [singleQuotedUtf8 lang, renderExpression x])
539571
572+tsVector'
573+ :: Expression schema from grouping params ('NotNull 'PGtext)
574+ -> Expression schema from grouping params ('NotNull 'PGtext)
575+tsVector' x = UnsafeExpression $
576+ "to_tsvector" <> parenthesized (renderExpression x)
577+
540578tsQuery
541579 :: ByteString -- ^ Language
542580 -> Expression schema from grouping params ('NotNull 'PGtext)
@@ -544,6 +582,12 @@ tsQuery
544582tsQuery lang x = UnsafeExpression $
545583 "to_tsquery" <> parenthesized (commaSeparated [singleQuotedUtf8 lang, renderExpression x])
546584
585+tsQuery'
586+ :: Expression schema from grouping params ('NotNull 'PGtext)
587+ -> Expression schema from grouping params ('NotNull 'PGtext)
588+tsQuery' x = UnsafeExpression $
589+ "to_tsquery" <> parenthesized (renderExpression x)
590+
547591arrayToText
548592 :: Expression schema from grouping params ('NotNull ('PGvararray ('NotNull 'PGtext)))
549593 -> Expression schema from grouping params ('NotNull 'PGtext)
@@ -914,7 +958,11 @@ getGroupChatForUser uId gId = do
914958
915959getFacultyFromQuery :: T.Text -> StaticPQ [Faculty]
916960getFacultyFromQuery query =
917− runQueryParams getFacultySearchResultQ (Only . queryFromText $ query) >>= getRows
961+ runQueryParams getFacultySearchResultQ (Only . queryFromTextAbbr $ query) >>= getRows
962+
963+searchUsers :: T.Text -> StaticPQ [User Faculty]
964+searchUsers query =
965+ map rowToUser <$> (runQueryParams searchUsersQ (Only . queryFromText $ query) >>= getRows)
918966
919967createToken :: Token -> StaticPQ ()
920968createToken token = void $ manipulateParams createTokenQ token
@@ -1000,8 +1048,8 @@ rowToUser UserRow{..} = User {
10001048 userEmail = userRowEmail
10011049 }
10021050
1003−queryFromText :: Text -> Text
1004−queryFromText = T.unwords . L.intersperse "&" . map processTerm . filter (not . T.null) . T.split (not . isAlphaNum)
1051+queryFromTextAbbr :: Text -> Text
1052+queryFromTextAbbr = T.unwords . L.intersperse "&" . map processTerm . filter (not . T.null) . T.split (not . isAlphaNum)
10051053 where
10061054 processTerm :: Text -> Text
10071055 processTerm t = "(" <> t <> ":*" <> (
@@ -1010,6 +1058,12 @@ queryFromText = T.unwords . L.intersperse "&" . map processTerm . filter (not .
10101058 else mempty
10111059 ) <> ")"
10121060
1061+queryFromText :: Text -> Text
1062+queryFromText = T.unwords . L.intersperse "&" . map processTerm . filter (not . T.null) . T.split (not . isAlphaNum)
1063+ where
1064+ processTerm :: Text -> Text
1065+ processTerm t = t <> ":*"
1066+
10131067directionToOperator
10141068 :: P.PaginationDirection
10151069 -> (ConditionOp schema0 from0 grouping0 params0 nullity0 nullity1 (ty :: PGType),
denis/src/Server/API.hs+5−1
@@ -31,6 +31,7 @@ import Server.API.Completions
3131import Server.API.Messages
3232import Data.Faculty
3333import Server.API.Faculty
34+import Data.Text (Text)
3435
3536-- MARK: Documentation
3637
@@ -38,6 +39,7 @@ instance HasDocs api => HasDocs (Authentication :> api) where
3839 docsFor Proxy (endpoint, action) =
3940 docsFor (Proxy :: Proxy api) (endpoint, action & notes <>~ [DocNote "Authentication" ["This method requires cookies set in the `POST /authenticate` method.\n\nReturns error `498 Invalid Token` if the token is invalid or the token header is missing. Returns `401 Unathorized` if the token doesn't permit access to the requested data."]])
4041
42+type UserSearchDescription = Description "Searches for users with the given string.\n\nFeed in the raw user input string."
4143
4244-- MARK: Implementation
4345
@@ -50,6 +52,7 @@ type API =
5052 "authentication" :> "me" :> Post '[JSON] UserId :<|>
5153 "users" :> ReqBody '[JSON] [UserId] :> Post '[JSON] [User Faculty] :<|>
5254 "users" :> "all" :> Post '[JSON] [User Faculty] :<|>
55+ "users" :> "search" :> UserSearchDescription :> ReqBody '[JSON, PlainText] Text :> Post '[JSON] [User Faculty] :<|>
5356 "posts" :> PostApi :<|>
5457 "channels" :> ChannelsApi :<|>
5558 MessagesApi
@@ -66,7 +69,8 @@ mkServerAPI l =
6669 authenticationAPI :<|> (\uId ->
6770 return uId :<|>
6871 maybeNotFound . runQnotFound . getUsers :<|>
69− runQnotFound getAllUsers :<|>
72+ runQerror getAllUsers :<|>
73+ runQerror . searchUsers :<|>
7074 postApi uId :<|>
7175 channelsApi uId :<|>
7276 messagesServer uId