module Gargantext.Database.Query.Table.User
( insertUsers
, toUserWrite
+ , deleteUsers
+ , updateUserDB
, queryUserTable
, getUser
, insertUsersDemo
, userWithId
, userLightWithId
, getUsersWith
+ , getUsersWithId
, module Gargantext.Database.Schema.User
)
where
import Control.Arrow (returnA)
-import Data.Eq(Eq(..))
import Data.List (find)
-import Data.Maybe (Maybe)
import Data.Text (Text)
import Data.Time (UTCTime)
import Gargantext.Core.Types.Individu
where
insert = Insert userTable us rCount Nothing
+deleteUsers :: [Username] -> Cmd err Int64
+deleteUsers us = mkCmd $ \c -> runDelete c userTable
+ (\user -> in_ (map pgStrictText us) (user_username user))
+
+-- Updates email or password only (for now)
+updateUserDB :: UserWrite -> Cmd err Int64
+updateUserDB us = mkCmd $ \c -> runUpdate_ c (updateUserQuery us)
+ where
+ updateUserQuery :: UserWrite -> Update Int64
+ updateUserQuery us = Update
+ { uTable = userTable
+ , uUpdateWith = updateEasy (\ (UserDB _id _p ll su un fn ln _em is ia dj)
+ -> UserDB _id p' ll su un fn ln em' is ia dj
+ )
+ , uWhere = (\row -> user_username row .== un')
+ , uReturning = rCount
+ }
+ where
+ UserDB _ p' _ _ un' _ _ em' _ _ _ = us
+
-----------------------------------------------------------------------
toUserWrite :: NewUser HashPassword -> UserWrite
toUserWrite (NewUser u m (Auth.PasswordHash p)) =
restrict -< user_username row .== pgStrictText u
returnA -< row
+----------------------------------------------------------
+
+getUsersWithId :: Int -> Cmd err [UserLight]
+getUsersWithId i = map toUserLight <$> runOpaQuery (selectUsersLightWithId i)
+ where
+ selectUsersLightWithId :: Int -> Query UserRead
+ selectUsersLightWithId i = proc () -> do
+ row <- queryUserTable -< ()
+ restrict -< user_id row .== pgInt4 i
+ returnA -< row
+
+
+
queryUserTable :: Query UserRead
queryUserTable = queryTable userTable