module Hasura.RQL.DDL.ApiLimit
  ( runRemoveApiLimits,
    runSetApiLimits,
  )
where

import Control.Lens ((.~))
import Hasura.Base.Error
import Hasura.EncJSON
import Hasura.Prelude
import Hasura.RQL.Types.ApiLimit
import Hasura.RQL.Types.Common
import Hasura.RQL.Types.Metadata
import Hasura.RQL.Types.SchemaCache.Build

runSetApiLimits ::
  (MonadError QErr m, MetadataM m, CacheRWM m) =>
  ApiLimit ->
  m EncJSON
runSetApiLimits :: ApiLimit -> m EncJSON
runSetApiLimits ApiLimit
al = do
  m () -> m ()
forall (m :: * -> *) a. (QErrM m, CacheRM m) => m a -> m a
withNewInconsistentObjsCheck (m () -> m ()) -> m () -> m ()
forall a b. (a -> b) -> a -> b
$
    MetadataModifier -> m ()
forall (m :: * -> *).
(MetadataM m, CacheRWM m) =>
MetadataModifier -> m ()
buildSchemaCache (MetadataModifier -> m ()) -> MetadataModifier -> m ()
forall a b. (a -> b) -> a -> b
$
      (Metadata -> Metadata) -> MetadataModifier
MetadataModifier ((Metadata -> Metadata) -> MetadataModifier)
-> (Metadata -> Metadata) -> MetadataModifier
forall a b. (a -> b) -> a -> b
$
        (ApiLimit -> Identity ApiLimit) -> Metadata -> Identity Metadata
Lens' Metadata ApiLimit
metaApiLimits ((ApiLimit -> Identity ApiLimit) -> Metadata -> Identity Metadata)
-> ApiLimit -> Metadata -> Metadata
forall s t a b. ASetter s t a b -> b -> s -> t
.~ ApiLimit
al
  EncJSON -> m EncJSON
forall (m :: * -> *) a. Monad m => a -> m a
return EncJSON
successMsg

runRemoveApiLimits ::
  (MonadError QErr m, MetadataM m, CacheRWM m) =>
  m EncJSON
runRemoveApiLimits :: m EncJSON
runRemoveApiLimits = do
  m () -> m ()
forall (m :: * -> *) a. (QErrM m, CacheRM m) => m a -> m a
withNewInconsistentObjsCheck (m () -> m ()) -> m () -> m ()
forall a b. (a -> b) -> a -> b
$
    MetadataModifier -> m ()
forall (m :: * -> *).
(MetadataM m, CacheRWM m) =>
MetadataModifier -> m ()
buildSchemaCache (MetadataModifier -> m ()) -> MetadataModifier -> m ()
forall a b. (a -> b) -> a -> b
$
      (Metadata -> Metadata) -> MetadataModifier
MetadataModifier ((Metadata -> Metadata) -> MetadataModifier)
-> (Metadata -> Metadata) -> MetadataModifier
forall a b. (a -> b) -> a -> b
$
        (ApiLimit -> Identity ApiLimit) -> Metadata -> Identity Metadata
Lens' Metadata ApiLimit
metaApiLimits ((ApiLimit -> Identity ApiLimit) -> Metadata -> Identity Metadata)
-> ApiLimit -> Metadata -> Metadata
forall s t a b. ASetter s t a b -> b -> s -> t
.~ ApiLimit
emptyApiLimit
  EncJSON -> m EncJSON
forall (m :: * -> *) a. Monad m => a -> m a
return EncJSON
successMsg