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.
This commit is contained in:
parent
7d64011cbd
commit
ed557fb6be
4 changed files with 58 additions and 3 deletions
28
src/Types.hs
28
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
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue