about summary refs log tree commit diff
path: root/src/Types.hs
diff options
context:
space:
mode:
authorWilliam Carroll <wpcarro@gmail.com>2020-07-28T17·40+0100
committerWilliam Carroll <wpcarro@gmail.com>2020-07-28T17·40+0100
commit36a2fea6863e692d815a513d4b0f15b8c1fcb886 (patch)
treea89e17222336c87a265f41ad13ced3f17daa6241 /src/Types.hs
parent012296f156f59fe8581a01f2ddfd2a1067c09108 (diff)
Create Sessions table
TL;DR:
- Create Sessions SQL schema
- Create Sessions module
- Introduce UUID dependency
Diffstat (limited to 'src/Types.hs')
-rw-r--r--src/Types.hs29
1 files changed, 29 insertions, 0 deletions
diff --git a/src/Types.hs b/src/Types.hs
index 6782b9ec3294..6a474a5094bd 100644
--- a/src/Types.hs
+++ b/src/Types.hs
@@ -17,9 +17,11 @@ import GHC.Generics
 import Crypto.Random.Types (MonadRandom)
 
 import qualified Crypto.KDF.BCrypt as BC
+import qualified Data.Time.Clock as Clock
 import qualified Data.ByteString.Char8 as B
 import qualified Data.ByteString as BS
 import qualified Data.Text.Encoding as TE
+import qualified Data.UUID as UUID
 --------------------------------------------------------------------------------
 
 -- TODO(wpcarro): Properly handle NULL for columns like profilePicture.
@@ -340,3 +342,30 @@ createAccountRequestFields request =
   , createAccountRequestEmail request
   , createAccountRequestRole request
   )
+
+newtype SessionUUID = SessionUUID UUID.UUID
+  deriving (Eq, Show, Generic)
+
+instance FromField SessionUUID where
+  fromField field =
+    case fieldData field of
+      (SQLText x) ->
+        case UUID.fromText x of
+          Nothing -> returnError ConversionFailed field ""
+          Just x -> Ok $ SessionUUID x
+      _ -> returnError ConversionFailed field ""
+
+instance ToField SessionUUID where
+  toField (SessionUUID uuid) =
+    uuid |> UUID.toText |> SQLText
+
+data StoredSession = StoredSession
+  { storedSessionUUID :: SessionUUID
+  , storedSessionUsername :: Username
+  , storedSessionTsCreated :: Clock.UTCTime
+  } deriving (Eq, Show, Generic)
+
+instance FromRow StoredSession where
+  fromRow = StoredSession <$> field
+                          <*> field
+                          <*> field