[FEAT] SocialList refactoring
[gargantext.git] / src / Gargantext / Database / Schema / User.hs
index 50ba0fed0258af70ce364f8a13d0fece2a3a6a32..791cd70047393bc8cb268353ff528f4061607aba 100644 (file)
@@ -14,36 +14,39 @@ Functions to deal with users, database side.
 {-# OPTIONS_GHC -fno-warn-orphans        #-}
 
 {-# LANGUAGE TemplateHaskell             #-}
-{-# LANGUAGE FlexibleContexts            #-}
-{-# LANGUAGE FlexibleInstances           #-}
-{-# LANGUAGE MultiParamTypeClasses       #-}
 {-# LANGUAGE FunctionalDependencies      #-}
 {-# LANGUAGE Arrows                      #-}
-{-# LANGUAGE NoImplicitPrelude           #-}
-{-# LANGUAGE OverloadedStrings           #-}
-{-# LANGUAGE RankNTypes                  #-}
+{-# LANGUAGE TemplateHaskell            #-}
 
 module Gargantext.Database.Schema.User where
 
-import Control.Lens.TH (makeLensesWith, abbreviatedFields)
-import Data.Maybe (Maybe)
-import Data.Profunctor.Product.TH (makeAdaptorAndInstance)
 import Data.Text (Text)
 import Data.Time (UTCTime)
-import GHC.Show(Show(..))
 import Gargantext.Prelude
-import Opaleye
+import GHC.Generics (Generic)
+import Database.PostgreSQL.Simple.FromField (FromField, fromField)
+import Data.Aeson.TH (deriveJSON)
+import Gargantext.Database.Prelude (fromField')
+import Gargantext.Core.Utils.Prefix (unPrefix)
 
-------------------------------------------------------------------------
-------------------------------------------------------------------------
+-- FIXME PLZ : the import below leads to an error, why ?
+-- import Gargantext.Database.Schema.Prelude hiding (makeLensesWith, abbreviatedFields, makeAdaptorAndInstance)
 
+-- When FIXED : Imports to remove:
+import Control.Lens.TH (makeLensesWith, abbreviatedFields)
+import Data.Profunctor.Product.TH (makeAdaptorAndInstance)
+import Opaleye hiding (FromField)
+
+------------------------------------------------------------------------
 data UserLight = UserLight { userLight_id       :: !Int
                            , userLight_username :: !Text
                            , userLight_email    :: !Text
-                           } deriving (Show)
+                           , userLight_password :: !Text
+                           } deriving (Show, Generic)
 
 toUserLight :: UserDB -> UserLight
-toUserLight (UserDB id _ _ _ u _ _ e _ _ _ ) = UserLight id u e
+toUserLight (UserDB id p _ _ u _ _ e _ _ _ ) = UserLight id u e p
+
 
 data UserPoly id pass llogin suser
               uname fname lname
@@ -61,7 +64,8 @@ data UserPoly id pass llogin suser
            , user_isStaff     :: !staff
            , user_isActive    :: !active
            , user_dateJoined  :: !djoined
-           } deriving (Show)
+           } deriving (Show, Generic)
+
 
 type UserWrite = UserPoly (Maybe (Column PGInt4))        (Column PGText)
                           (Maybe (Column PGTimestamptz)) (Column PGBool)
@@ -84,27 +88,32 @@ type UserReadNull = UserPoly     (Column (Nullable PGInt4))         (Column (Nul
                                  (Column (Nullable PGBool))         (Column (Nullable PGBool))
                                  (Column (Nullable PGTimestamptz))
 
-
-
-
 type UserDB = UserPoly Int Text (Maybe UTCTime) Bool Text Text Text Text Bool Bool UTCTime
 
 $(makeAdaptorAndInstance "pUserDB"   ''UserPoly)
 $(makeLensesWith abbreviatedFields   ''UserPoly)
 
-
 userTable :: Table UserWrite UserRead
-userTable = Table "auth_user" (pUserDB UserDB { user_id      = optional "id"
-                                          , user_password    = required "password"
-                                          , user_lastLogin   = optional "last_login"
-                                          , user_isSuperUser = required "is_superuser"
-                                          , user_username    = required "username"
-                                          , user_firstName   = required "first_name"
-                                          , user_lastName    = required "last_name"
-                                          , user_email       = required "email"
-                                          , user_isStaff     = required "is_staff"
-                                          , user_isActive    = required "is_active"
-                                          , user_dateJoined  = optional "date_joined"
-                                          }
-                              )
-
+userTable = Table "auth_user"
+  (pUserDB UserDB { user_id      = optional "id"
+                  , user_password    = required "password"
+                  , user_lastLogin   = optional "last_login"
+                  , user_isSuperUser = required "is_superuser"
+                  , user_username    = required "username"
+                  , user_firstName   = required "first_name"
+                  , user_lastName    = required "last_name"
+                  , user_email       = required "email"
+                  , user_isStaff     = required "is_staff"
+                  , user_isActive    = required "is_active"
+                  , user_dateJoined  = optional "date_joined"
+                  }
+      )
+
+instance FromField UserLight where
+  fromField = fromField'
+
+instance FromField UserDB where
+  fromField = fromField'
+
+$(deriveJSON (unPrefix "userLight_") ''UserLight)
+$(deriveJSON (unPrefix "user_") ''UserPoly)