diff options
author | William Carroll <wpcarro@gmail.com> | 2020-07-31T10·25+0100 |
---|---|---|
committer | William Carroll <wpcarro@gmail.com> | 2020-07-31T10·25+0100 |
commit | ed557fb6be749b0b06666674e35db4a75655af08 (patch) | |
tree | 14e5d5feeba801a782e68c8c18c17adbed4ca6b9 /src/Types.hs | |
parent | 7d64011cbd6b0d6ce2237de2a3dfcc1f9f81a4c9 (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/Types.hs')
-rw-r--r-- | src/Types.hs | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/src/Types.hs b/src/Types.hs index d03aae9c7f38..273d4aecca70 100644 --- a/src/Types.hs +++ b/src/Types.hs @@ -449,3 +449,31 @@ instance FromRow PendingAccount where pendingAccountRole <- field pendingAccountEmail <- field pure PendingAccount {..} + +data UpdateTripRequest = UpdateTripRequest + { updateTripRequestTripPK :: TripPK + , updateTripRequestDestination :: Maybe Destination + , updateTripRequestStartDate :: Maybe Date + , updateTripRequestEndDate :: Maybe Date + , updateTripRequestComment :: Maybe Comment + } deriving (Eq, Show) + +instance FromJSON UpdateTripRequest where + parseJSON = withObject "UpdateTripRequest" $ \x -> do + updateTripRequestTripPK <- x .: "tripKey" + -- the following four fields might not be present + updateTripRequestDestination <- x .:? "destination" + updateTripRequestStartDate <- x .:? "startDate" + updateTripRequestEndDate <- x .:? "endDate" + updateTripRequestComment <- x .:? "comment" + pure UpdateTripRequest{..} + +-- | Apply the updates in the UpdateTripRequest to Trip. +updateTrip :: UpdateTripRequest -> Trip -> Trip +updateTrip UpdateTripRequest{..} Trip{..} = Trip + { tripUsername = tripUsername + , tripDestination = M.fromMaybe tripDestination updateTripRequestDestination + , tripStartDate = M.fromMaybe tripStartDate updateTripRequestStartDate + , tripEndDate = M.fromMaybe tripEndDate updateTripRequestEndDate + , tripComment = M.fromMaybe tripComment updateTripRequestComment + } |