make user updatable via API
This commit is contained in:
parent
7f3dfc9c29
commit
55ab85f927
3 changed files with 39 additions and 9 deletions
12
src/API.hs
12
src/API.hs
|
@ -21,9 +21,9 @@ import Model as M
|
||||||
|
|
||||||
import Types
|
import Types
|
||||||
|
|
||||||
type UserAPI = "user" :>
|
type UserAPI =
|
||||||
( "list" :> QueryParam "refine" Refine :> Get '[JSON] [User]
|
"user" :>
|
||||||
:<|> "new" :> ReqBody '[JSON] UserSubmit :> Post '[JSON] Int
|
( "list" :> QueryParam "refine" Refine :> Get '[JSON] [User]
|
||||||
)
|
:<|> "new" :> ReqBody '[JSON] UserSubmit :> Post '[JSON] Int
|
||||||
|
:<|> "update" :> ReqBody '[JSON] (Int, UserSubmit) :> Post '[JSON] ()
|
||||||
data SortBy = Name
|
)
|
||||||
|
|
|
@ -37,7 +37,8 @@ userApi = Proxy
|
||||||
users :: Connection -> Server UserAPI
|
users :: Connection -> Server UserAPI
|
||||||
users conn =
|
users conn =
|
||||||
userList :<|>
|
userList :<|>
|
||||||
userNew
|
userNew :<|>
|
||||||
|
userUpdate
|
||||||
where
|
where
|
||||||
userList :: Maybe Refine -> Handler [User]
|
userList :: Maybe Refine -> Handler [User]
|
||||||
userList ref = liftIO $ userSelect conn ref
|
userList ref = liftIO $ userSelect conn ref
|
||||||
|
@ -45,3 +46,7 @@ users conn =
|
||||||
userNew us = liftIO $ do
|
userNew us = liftIO $ do
|
||||||
now <- getCurrentTime
|
now <- getCurrentTime
|
||||||
head <$> runInsert_ conn (insertUser us (utctDay now))
|
head <$> runInsert_ conn (insertUser us (utctDay now))
|
||||||
|
userUpdate :: (Int, UserSubmit) -> Handler ()
|
||||||
|
userUpdate (id, us) = liftIO $ do
|
||||||
|
now <- getCurrentTime
|
||||||
|
void $ runUpdate_ conn (updateUser id us (utctDay now))
|
||||||
|
|
|
@ -38,7 +38,15 @@ data User = User
|
||||||
deriving (Generic, Show)
|
deriving (Generic, Show)
|
||||||
|
|
||||||
instance ToJSON User where
|
instance ToJSON User where
|
||||||
toEncoding = genericToEncoding defaultOptions
|
toEncoding (User id ident balance ts email avatar _) =
|
||||||
|
pairs
|
||||||
|
( "userId" .= id
|
||||||
|
<> "userIdent" .= ident
|
||||||
|
<> "userBalance" .= balance
|
||||||
|
<> "userTimeStamp" .= ts
|
||||||
|
<> "userEmail" .= email
|
||||||
|
<> "userAvatar" .= avatar
|
||||||
|
)
|
||||||
|
|
||||||
instance FromJSON User
|
instance FromJSON User
|
||||||
|
|
||||||
|
@ -126,4 +134,21 @@ insertUser us now = Insert
|
||||||
]
|
]
|
||||||
, iReturning = rReturning (\(id, _, _, _, _, _, _) -> id)
|
, iReturning = rReturning (\(id, _, _, _, _, _, _) -> id)
|
||||||
, iOnConflict = Nothing
|
, iOnConflict = Nothing
|
||||||
}
|
}
|
||||||
|
|
||||||
|
updateUser :: Int -> UserSubmit -> Day -> Update Int64
|
||||||
|
updateUser id us now = Update
|
||||||
|
{ uTable = userTable
|
||||||
|
, uUpdateWith = updateEasy (\(id_, _, i3, _, _, i6, _) ->
|
||||||
|
( id_
|
||||||
|
, C.constant (userSubmitIdent us)
|
||||||
|
, i3
|
||||||
|
, C.constant (now)
|
||||||
|
, C.constant (userSubmitEmail us)
|
||||||
|
, i6
|
||||||
|
, C.constant (userSubmitPin us)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
, uWhere = (\(i1, _, _, _, _, _, _) -> i1 .== C.constant id)
|
||||||
|
, uReturning = rCount
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue