on master help sign in
Added users/update
commitc96dd550cc83c5919a0fbe97161ab1b5b3ac6453 authoriko <ilyakooo0@gmail.com> dateSun 12 May 2019, 02:11 (7 years ago) parenta978e5e
3 files changed+51−4
denis/src/Data/Query.hs+27−1 denis/src/Server/API.hs+23−2 denis/src/Server/API/Completions.hs+1−1
denis/src/Data/Query.hs+27−1
@@ -57,7 +57,8 @@ module Data.Query (
5757 tryActivateToken,
5858 deactivateToken,
5959 searchUsers,
60− searchTags
60+ searchTags,
61+ updateUser
6162 ) where
6263
6364import Squeal.PostgreSQL
@@ -286,6 +287,18 @@ getChannelForUserQ = selectStar $
286287 from (table #channels) &
287288 where_ (#namedChannelFullOwner .== param @1 .&& #namedChannelFullId .== param @2)
288289
290+updateUserQ :: Manipulation Schema (TuplePG (User FacultyUrl)) (RowPG (Only UserId))
291+updateUserQ = update #users (
292+ Same `as` #userRowId :*
293+ Set (param @2) `as` #userRowFirstName :*
294+ Set (param @3) `as` #userRowMiddleName :*
295+ Set (param @4) `as` #userRowLastName :*
296+ Set (param @5) `as` #userRowFacultyUrl :*
297+ Same `as` #userRowEmail :*
298+ Same `as` #userRowIsValidated)
299+ (#userRowId .== param @1 .&& #userRowEmail .== param @6)
300+ (Returning $ #userRowId `as` #fromOnly)
301+
289302getAllChannelsForUserQ :: Query Schema (TuplePG (Only UserId)) (RowPG NamedChannelFull)
290303getAllChannelsForUserQ = selectStar $
291304 from (table #channels) &
@@ -1041,8 +1054,21 @@ deactivateToken uId dData = fmap fromOnly <$> (manipulateParams deactivateTokenQ
10411054getVerifiedToken :: UserId -> ByteString -> StaticPQ (Maybe Token)
10421055getVerifiedToken uId tokenData = runQueryParams getVerifiedTokenQ (uId, hash tokenData) >>= firstRow
10431056
1057+updateUser :: (User FacultyUrl) -> StaticPQ (Maybe UserId)
1058+updateUser user = fmap fromOnly <$> (manipulateParams updateUserQ (normalizeUser user) >>= firstRow)
1059+
10441060-- MARK: Utils
10451061
1062+normalizeUser :: User FacultyUrl -> User FacultyUrl
1063+normalizeUser User{..} = User {
1064+ userId = userId,
1065+ firstName = T.strip $ firstName,
1066+ middleName = T.strip $ middleName,
1067+ lastName = T.strip $ lastName,
1068+ userFaculty = T.toLower . T.strip $ userFaculty,
1069+ userEmail = T.toLower . T.strip $ userEmail
1070+}
1071+
10461072data UserRow = UserRow {
10471073 userRowId :: UserId,
10481074 userRowFirstName :: Text,
denis/src/Server/API.hs+23−2
@@ -3,7 +3,8 @@
33 DataKinds,
44 TypeOperators,
55 OverloadedStrings,
6− ScopedTypeVariables #-}
6+ ScopedTypeVariables,
7+ RecordWildCards #-}
78{-# OPTIONS_GHC -Wno-orphans #-}
89
910module Server.API (
@@ -32,6 +33,8 @@ import Server.API.Messages
3233import Data.Faculty
3334import Server.API.Faculty
3435import Data.Text (Text)
36+import Data.Maybe (isJust)
37+import Control.Monad.Except
3538
3639-- MARK: Documentation
3740
@@ -41,6 +44,8 @@ instance HasDocs api => HasDocs (Authentication :> api) where
4144
4245type UserSearchDescription = Description "Searches for users with the given string.\n\nFeed in the raw user input string."
4346
47+type UpdateUserDescription = Description "Updates the current user.\n\nThrow 401 if you try to change email or something like that."
48+
4449-- MARK: Implementation
4550
4651type Authentication = AuthProtect "basicAuth"
@@ -53,6 +58,7 @@ type API =
5358 "users" :> ReqBody '[JSON] [UserId] :> Post '[JSON] [User Faculty] :<|>
5459 "users" :> "all" :> Post '[JSON] [User Faculty] :<|>
5560 "users" :> "search" :> UserSearchDescription :> ReqBody '[JSON, PlainText] Text :> Post '[JSON] [User Faculty] :<|>
61+ "users" :> "update" :> UpdateUserDescription :> ReqBody '[JSON] UserCreation :> PostNoContent '[JSON, PlainText] NoContent :<|>
5662 "posts" :> PostApi :<|>
5763 "channels" :> ChannelsApi :<|>
5864 MessagesApi
@@ -71,9 +77,24 @@ mkServerAPI l =
7177 maybeNotFound . runQnotFound . getUsers :<|>
7278 runQerror getAllUsers :<|>
7379 runQerror . searchUsers :<|>
80+ updateUserApi uId :<|>
7481 postApi uId :<|>
7582 channelsApi uId :<|>
7683 messagesServer uId
7784 ) :<|>
7885 tagsServer :<|>
79− facultyServer
86+ facultyServer
87+
88+updateUserApi :: UserId -> UserCreation -> App NoContent
89+updateUserApi uId UserCreation{..} = do
90+ res <- runQerror $ updateUser User {
91+ userId = uId,
92+ firstName = userCreationFirstName,
93+ middleName = userCreationMiddleName,
94+ lastName = userCreationLastName,
95+ userFaculty = userCreationUserFaculty,
96+ userEmail = userCreationUserEmail
97+ }
98+ if isJust res
99+ then return NoContent
100+ else throwError err401
denis/src/Server/API/Completions.hs+1−1
@@ -59,7 +59,7 @@ type CompletionsDescription = Description "\
5959 \}\n\
6060 \```"
6161
62−type SearchDescription = Description "Returns tag completions for given user input."
62+type SearchDescription = Description "Returns top 1000 tag completions for given user input."
6363
6464type TagsAPI = "completions" :> CompletionsDescription :> Get '[JSON] CompletionTree :<|>
6565 "search" :> SearchDescription :> ReqBody '[JSON, PlainText] Text :> Post '[JSON] [Text]