about summary refs log tree commit diff
path: root/src/Trips.hs
diff options
context:
space:
mode:
authorWilliam Carroll <wpcarro@gmail.com>2020-07-31T10·25+0100
committerWilliam Carroll <wpcarro@gmail.com>2020-07-31T10·25+0100
commited557fb6be749b0b06666674e35db4a75655af08 (patch)
tree14e5d5feeba801a782e68c8c18c17adbed4ca6b9 /src/Trips.hs
parent7d64011cbd6b0d6ce2237de2a3dfcc1f9f81a4c9 (diff)
Support PATCH /trips
Support a top-level PATCH request to trips that permits any admin to update any
trip, and any user to update any of their trips.

I'm using Aeson's (:?) combinator to support missing fields from the incoming
JSON requests, and then M.fromMaybe to apply these values to any record that
matches the primary key.

See the TODOs that I introduced for some shortcomings.
Diffstat (limited to 'src/Trips.hs')
-rw-r--r--src/Trips.hs15
1 files changed, 12 insertions, 3 deletions
diff --git a/src/Trips.hs b/src/Trips.hs
index ec52ec58fee9..022631219c62 100644
--- a/src/Trips.hs
+++ b/src/Trips.hs
@@ -14,12 +14,21 @@ create dbFile trip = withConnection dbFile $ \conn ->
   execute conn "INSERT INTO Trips (username,destination,startDate,endDate,comment) VALUES (?,?,?,?,?)"
     (trip |> T.tripFields)
 
--- | Delete a trip from `dbFile` using its `tripPK` Primary Key.
+-- | Attempt to get the trip record from `dbFile` under `tripKey`.
+get :: FilePath -> T.TripPK -> IO (Maybe T.Trip)
+get dbFile tripKey = withConnection dbFile $ \conn -> do
+  res <- query conn "SELECT username,destination,startDate,endDate,comment FROM Trips WHERE username = ? AND destination = ? AND startDate = ? LIMIT 1"
+    (T.tripPKFields tripKey)
+  case res of
+    [x] -> pure (Just x)
+    _ -> pure Nothing
+
+-- | Delete a trip from `dbFile` using its `tripKey` Primary Key.
 delete :: FilePath -> T.TripPK -> IO ()
-delete dbFile tripPK =
+delete dbFile tripKey =
   withConnection dbFile $ \conn -> do
     execute conn "DELETE FROM Trips WHERE username = ? AND destination = ? and startDate = ?"
-      (tripPK |> T.tripPKFields)
+      (T.tripPKFields tripKey)
 
 -- | Return a list of all of the trips in `dbFile`.
 listAll :: FilePath -> IO [T.Trip]