about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorWilliam Carroll <wpcarro@gmail.com>2020-07-28T09·13+0100
committerWilliam Carroll <wpcarro@gmail.com>2020-07-28T09·13+0100
commit0637da36ccac7e609041bc8999e3da348171f95f (patch)
treebc8c7b4422ff5dd4d7aa4523abd6d2c9f5765ccc /src
parent2f73d1db6c2ec107a9af1572f023b6c95133229c (diff)
Support GET /trips
In the spirit of support CRUDL, I added a GET /trips, which lists all of the
trips in the Trips table.
Diffstat (limited to 'src')
-rw-r--r--src/API.hs3
-rw-r--r--src/App.hs5
-rw-r--r--src/Types.hs7
3 files changed, 15 insertions, 0 deletions
diff --git a/src/API.hs b/src/API.hs
index ef185e246cd0..a42bf804b471 100644
--- a/src/API.hs
+++ b/src/API.hs
@@ -18,3 +18,6 @@ type API = "user"
       :<|> "trip"
            :> ReqBody '[JSON] T.Trip
            :> Post '[JSON] NoContent
+      -- Read
+      :<|> "trips"
+           :> Get '[JSON] [T.Trip]
diff --git a/src/App.hs b/src/App.hs
index d244c9b56740..c4203137a4fc 100644
--- a/src/App.hs
+++ b/src/App.hs
@@ -20,10 +20,12 @@ server :: FilePath -> Server API
 server dbFile = userAddH
            :<|> userGetH
            :<|> createTripH
+           :<|> listTripsH
   where
     userAddH newUser = liftIO $ userAdd newUser
     userGetH name    = liftIO $ userGet name
     createTripH trip = liftIO $ createTrip trip
+    listTripsH         = liftIO $ listTrips
 
     -- TODO(wpcarro): Handle failed CONSTRAINTs instead of sending 500s
     userAdd :: T.Account -> IO (Maybe T.Session)
@@ -48,6 +50,9 @@ server dbFile = userAddH
         (trip & T.tripFields)
       pure NoContent
 
+    listTrips :: IO [T.Trip]
+    listTrips = withConnection dbFile $ \conn -> do
+      query_ conn "SELECT * FROM Trips"
 mkApp :: FilePath -> IO Application
 mkApp dbFile = do
   pure $ serve (Proxy @ API) $ server dbFile
diff --git a/src/Types.hs b/src/Types.hs
index 14536ae8c3ba..112b17c53ab2 100644
--- a/src/Types.hs
+++ b/src/Types.hs
@@ -192,6 +192,13 @@ data Trip = Trip
   , tripComment :: Comment
   } deriving (Eq, Show, Generic)
 
+instance FromRow Trip where
+  fromRow = Trip <$> field
+                 <*> field
+                 <*> field
+                 <*> field
+                 <*> field
+
 -- | Return the tuple representation of a Trip record for SQL.
 tripFields :: Trip -> (Username, Destination, Date, Date, Comment)
 tripFields (Trip{ tripUsername