1 {-# LANGUAGE OverloadedStrings #-}
    2 module Databrary.Controller.VolumeState
    3   ( postVolumeState
    4   , deleteVolumeState
    5   ) where
    6 
    7 import Data.Maybe (fromMaybe)
    8 import Network.HTTP.Types (noContent204)
    9 import qualified Web.Route.Invertible as R
   10 
   11 import qualified Databrary.JSON as JSON
   12 import Databrary.Model.Id
   13 import Databrary.Model.Permission
   14 import Databrary.Model.Volume
   15 import Databrary.Model.VolumeState
   16 import Databrary.HTTP.Path.Parser
   17 import Databrary.HTTP.Form.Deform
   18 import Databrary.Action.Route
   19 import Databrary.Action
   20 import Databrary.Controller.Form
   21 import Databrary.Controller.Paths
   22 import Databrary.Controller.Volume
   23 
   24 postVolumeState :: ActionRoute (Id Volume, VolumeStateKey)
   25 postVolumeState = action PUT (pathJSON >/> pathId </> "state" >/> R.parameter) $ \(vi, k) -> withAuth $ do
   26   v <- getVolume PermissionEDIT vi
   27   s <- runForm Nothing $ do
   28     j <- deform
   29     p <- "public" .:> fromMaybe False <$> deformOptional deform
   30     return VolumeState
   31       { stateVolume = v
   32       , volumeStateKey = k
   33       , volumeStatePublic = p
   34       , volumeStateValue = j
   35       }
   36   changeVolumeState s
   37   return $ emptyResponse noContent204 []
   38 
   39 deleteVolumeState :: ActionRoute (Id Volume, VolumeStateKey)
   40 deleteVolumeState = action DELETE (pathJSON >/> pathId </> "state" >/> R.parameter) $ \(vi, k) -> withAuth $ do
   41   v <- getVolume PermissionEDIT vi
   42   r <- removeVolumeState v k
   43   return $ okResponse [] $ JSON.toEncoding r