1 {-# LANGUAGE ScopedTypeVariables #-} 2 module Databrary.Controller.Metric 3 ( postVolumeMetric 4 , deleteVolumeMetric 5 ) where 6 7 import Control.Invertible.Monoidal ((>|<)) 8 9 import qualified Databrary.JSON as JSON 10 import Databrary.Model.Id 11 import Databrary.Model.Permission 12 import Databrary.Model.Volume 13 import Databrary.Model.Category 14 import Databrary.Model.Metric 15 import Databrary.Model.VolumeMetric 16 import Databrary.HTTP.Path.Parser 17 import Databrary.Action.Route 18 import Databrary.Action 19 import Databrary.Controller.Paths 20 import Databrary.Controller.Volume 21 22 postVolumeMetric :: ActionRoute (Id Volume, Either (Id Category) (Id Metric)) 23 postVolumeMetric = action PUT (pathJSON >/> pathId </> (pathId >|< pathId)) $ \(vi, cm) -> withAuth $ do 24 v <- getVolume PermissionEDIT vi 25 (addedMetrics :: [Id Metric]) <- 26 either 27 (\catId -> addVolumeCategory v catId) 28 (\metricId' -> do 29 metricAdded <- addVolumeMetric v metricId' 30 return $ if metricAdded then [metricId'] else []) 31 cm 32 return $ okResponse [] $ JSON.toEncoding addedMetrics 33 34 deleteVolumeMetric :: ActionRoute (Id Volume, Either (Id Category) (Id Metric)) 35 deleteVolumeMetric = action DELETE (pathJSON >/> pathId </> (pathId >|< pathId)) $ \(vi, cm) -> withAuth $ do 36 v <- getVolume PermissionEDIT vi 37 r <- either (removeVolumeCategory v) (fmap fromEnum . removeVolumeMetric v) cm 38 return $ okResponse [] $ JSON.toEncoding r