@@ -27,6 +27,7 @@ module DeckGo.Handler where
2727
2828import Control.Lens hiding ((.=) )
2929import Control.Monad
30+ import Data.Maybe
3031import Control.Monad.Except
3132import Data.Aeson ((.=) , (.:) , (.!=) , (.:?) )
3233import Data.Proxy
@@ -268,15 +269,15 @@ newtype SlideId = SlideId { unSlideId :: T.Text }
268269instance ToParamSchema SlideId
269270
270271data Slide = Slide
271- { slideContent :: T. Text
272+ { slideContent :: Maybe T. Text
272273 , slideTemplate :: T. Text
273274 , slideAttributes :: HMS. HashMap T. Text T. Text
274275 } deriving (Show , Eq )
275276
276277instance FromJSONObject Slide where
277278 parseJSONObject = \ obj ->
278279 Slide <$>
279- obj .:? " content" .!= " " <*>
280+ obj .:? " content" .!= Nothing <*>
280281 obj .: " template" <*>
281282 obj .:? " attributes" .!= HMS. empty
282283
@@ -728,7 +729,11 @@ slidesPut env fuid deckId slideId slide = do
728729
729730 res <- runAWS env $ Aws. send $ DynamoDB. updateItem " Slides" &
730731 DynamoDB. uiUpdateExpression .~ Just
731- " SET SlideContent = :c, SlideTemplate = :t, SlideAttributes = :a" &
732+ (dynamoSet $
733+ (if isJust (slideContent slide)
734+ then [ Set " SlideContent" " :c" ]
735+ else [ Remove " SlideContent" ]) <>
736+ [ Set " SlideTemplate" " :t" , Set " SlideAttributes" " :a" ]) &
732737 DynamoDB. uiExpressionAttributeValues .~ slideToItem' slide &
733738 DynamoDB. uiReturnValues .~ Just DynamoDB. UpdatedNew &
734739 DynamoDB. uiKey .~ HMS. singleton " SlideId"
@@ -915,21 +920,29 @@ deckAttributesFromAttributeValue attr =
915920slideToItem :: SlideId -> Slide -> HMS. HashMap T. Text DynamoDB. AttributeValue
916921slideToItem slideId Slide {slideContent, slideTemplate, slideAttributes} =
917922 HMS. singleton " SlideId" (slideIdToAttributeValue slideId) <>
918- HMS. singleton " SlideContent" (slideContentToAttributeValue slideContent) <>
923+ (maybe
924+ HMS. empty
925+ (\ content -> HMS. singleton " SlideContent" (slideContentToAttributeValue content))
926+ slideContent) <>
919927 HMS. singleton " SlideTemplate" (slideTemplateToAttributeValue slideTemplate) <>
920928 HMS. singleton " SlideAttributes" (slideAttributesToAttributeValue slideAttributes)
921929
922930slideToItem' :: Slide -> HMS. HashMap T. Text DynamoDB. AttributeValue
923931slideToItem' Slide {slideContent, slideTemplate, slideAttributes} =
924- HMS. singleton " :c" (slideContentToAttributeValue slideContent) <>
932+ (maybe
933+ HMS. empty
934+ (\ content -> HMS. singleton " :c" (slideContentToAttributeValue content))
935+ slideContent) <>
925936 HMS. singleton " :t" (slideTemplateToAttributeValue slideTemplate) <>
926937 HMS. singleton " :a" (slideAttributesToAttributeValue slideAttributes)
927938
928939itemToSlide :: HMS. HashMap T. Text DynamoDB. AttributeValue -> Maybe (Item SlideId Slide )
929940itemToSlide item = do
930941 slideId <- HMS. lookup " SlideId" item >>= slideIdFromAttributeValue
931942
932- slideContent <- HMS. lookup " SlideContent" item >>= slideContentFromAttributeValue
943+ slideContent <- case HMS. lookup " SlideContent" item of
944+ Nothing -> Just Nothing
945+ Just c -> Just <$> slideContentFromAttributeValue c
933946
934947 slideTemplate <- HMS. lookup " SlideTemplate" item >>= slideTemplateFromAttributeValue
935948 slideAttributes <- HMS. lookup " SlideAttributes" item >>= slideAttributesFromAttributeValue
@@ -1001,3 +1014,16 @@ randomText len allowedChars = T.pack <$> randomString len allowedChars
10011014
10021015newId :: IO T. Text
10031016newId = randomText 32 ([' 0' .. ' 9' ] <> [' a' .. ' z' ])
1017+
1018+ data DynamoUpdateExpr
1019+ = Set T. Text T. Text
1020+ | Remove T. Text
1021+
1022+ dynamoSet :: [DynamoUpdateExpr ] -> T. Text
1023+ dynamoSet exprs = setExpr <> " " <> removeExpr
1024+ where
1025+ setExpr = " SET " <> T. intercalate " ," sts
1026+ removeExpr = " REMOVE " <> T. intercalate " ," removes
1027+ (sts, removes) = foldr f ([] , [] ) exprs
1028+ f (Set l r) (ls, rs) = (ls <> [l <> " = " <> r], rs)
1029+ f (Remove t ) (ls, rs) = (ls, rs <> [t])
0 commit comments