about summary refs log tree commit diff
path: root/src/Types.hs
diff options
context:
space:
mode:
authorWilliam Carroll <wpcarro@gmail.com>2020-07-28T08·10+0100
committerWilliam Carroll <wpcarro@gmail.com>2020-07-28T08·12+0100
commit52ac4d79bda2c5f5cc2ff636e79b4bf3b5979868 (patch)
tree79d32e30c2e0c0227d30e542878fa3a816ee81d7 /src/Types.hs
parent475f62fb16fb29e55548cc8b238caea8bf60bd8f (diff)
Allow API users to create Trip entries
Next up:
- list trips
- update existing trip entries
- delete existing trip entries
Diffstat (limited to 'src/Types.hs')
-rw-r--r--src/Types.hs67
1 files changed, 66 insertions, 1 deletions
diff --git a/src/Types.hs b/src/Types.hs
index d57fa92ed31e..14536ae8c3ba 100644
--- a/src/Types.hs
+++ b/src/Types.hs
@@ -107,8 +107,9 @@ data Account = Account
   , accountProfilePicture :: ProfilePicture
   } deriving (Eq, Show, Generic)
 
-instance FromJSON Account
+-- TODO(wpcarro): Prefer username to accountUsername for JSON
 instance ToJSON Account
+instance FromJSON Account
 
 -- | Return a tuple with all of the fields for an Account record to use for SQL.
 accountFields :: Account -> (Username, Password, Email, Role, ProfilePicture)
@@ -144,3 +145,67 @@ instance ToJSON Session where
            , "password" .= password
            , "role" .= role
            ]
+
+newtype Comment = Comment Text
+  deriving (Eq, Show, Generic)
+
+instance ToJSON Comment
+instance FromJSON Comment
+
+instance ToField Comment where
+  toField (Comment x) = SQLText x
+
+instance FromField Comment where
+  fromField = forNewtype Comment
+
+-- TODO(wpcarro): Replace this with a different type.
+newtype Date = Date Text
+  deriving (Eq, Show, Generic)
+
+instance ToJSON Date
+instance FromJSON Date
+
+instance ToField Date where
+  toField (Date x) = SQLText x
+
+instance FromField Date where
+  fromField = forNewtype Date
+
+newtype Destination = Destination Text
+  deriving (Eq, Show, Generic)
+
+-- TODO(wpcarro): Prefer username to tripUsername for JSON
+instance ToJSON Destination
+instance FromJSON Destination
+
+instance ToField Destination where
+  toField (Destination x) = SQLText x
+
+instance FromField Destination where
+  fromField = forNewtype Destination
+
+data Trip = Trip
+  { tripUsername :: Username
+  , tripDestination :: Destination
+  , tripStartDate :: Date
+  , tripEndDate :: Date
+  , tripComment :: Comment
+  } deriving (Eq, Show, Generic)
+
+-- | Return the tuple representation of a Trip record for SQL.
+tripFields :: Trip -> (Username, Destination, Date, Date, Comment)
+tripFields (Trip{ tripUsername
+                , tripDestination
+                , tripStartDate
+                , tripEndDate
+                , tripComment
+                })
+  = ( tripUsername
+    , tripDestination
+    , tripStartDate
+    , tripEndDate
+    , tripComment
+    )
+
+instance ToJSON Trip
+instance FromJSON Trip