@@ -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