<commit_before>{-# LANGUAGE OverloadedStrings #-}
module Controller.Funding
  ( queryFunderHandler
  , postVolumeFunding
  , deleteVolumeFunder
  ) where

import Control.Monad (liftM2)
import qualified Data.Text as T

import Has (focusIO)
import qualified JSON as JSON
import Model.Id
import Model.Permission
import Model.Volume
import Model.Funding
import Model.Funding.FundRef
import HTTP.Form.Deform
import HTTP.Path.Parser
import Action
import Controller.Paths
import Controller.Form
import Controller.Permission
import Controller.Volume

queryFunderHandler :: Action -- TODO: GET only
queryFunderHandler = withAuth $ do
  _ <- authAccount
  (q, a) <- runForm Nothing $ liftM2 (,)
    ("query" .:> (deformRequired =<< deform))
    ("all" .:> deform)
  r <- if a
    then focusIO $ searchFundRef q
    else findFunders q
  return $ okResponse [] $ JSON.mapObjects funderJSON r

postVolumeFunding :: ActionRoute (Id Volume, Id Funder)
postVolumeFunding = action POST (pathJSON >/> pathId </> pathId) $ \(vi, fi) -> withAuth $ do
  v <- getVolume PermissionEDIT vi
  f <- maybeAction =<< lookupFunderRef fi
  a <- runForm Nothing $ do
    csrfForm
    "awards" .:> filter (not . T.null) <$> withSubDeforms (\_ -> deform)
  let fa = Funding f a
  _ <- changeVolumeFunding v fa
  return $ okResponse [] $ JSON.pairs $ fundingJSON fa

deleteVolumeFunder :: ActionRoute (Id Volume, Id Funder)
deleteVolumeFunder = action DELETE (pathJSON >/> pathId </> pathId) $ \(vi, fi) -> withAuth $ do
  guardVerfHeader
  v <- getVolume PermissionEDIT vi
  _ <- removeVolumeFunder v fi
  return $ okResponse [] $ JSON.recordEncoding $ volumeJSONSimple v<commit_msg>Explicit types for funding requests<commit_after>{-# LANGUAGE OverloadedStrings #-}
module Controller.Funding
  ( queryFunderHandler
  , postVolumeFunding
  , deleteVolumeFunder
  ) where

import Control.Monad (liftM2)
import qualified Data.Text as T

import Has (focusIO)
import qualified JSON as JSON
import Model.Id
import Model.Permission
import Model.Volume
import Model.Funding
import Model.Funding.FundRef
import HTTP.Form.Deform
import HTTP.Path.Parser
import Action
import Controller.Paths
import Controller.Form
import Controller.Permission
import Controller.Volume

data QueryFundersRequest = QueryFundersRequest T.Text Bool

queryFunderHandler :: Action -- TODO: GET only
queryFunderHandler = withAuth $ do
  _ <- authAccount
  QueryFundersRequest q a <- runForm Nothing $ liftM2 QueryFundersRequest
    ("query" .:> (deformRequired =<< deform))
    ("all" .:> deform)
  r <- if a
    then focusIO $ searchFundRef q
    else findFunders q
  return $ okResponse [] $ JSON.mapObjects funderJSON r

data CreateOrUpdateVolumeFundingRequest =
    CreateOrUpdateVolumeFundingRequest [T.Text]

postVolumeFunding :: ActionRoute (Id Volume, Id Funder)
postVolumeFunding = action POST (pathJSON >/> pathId </> pathId) $ \(vi, fi) -> withAuth $ do
  v <- getVolume PermissionEDIT vi
  f <- maybeAction =<< lookupFunderRef fi
  CreateOrUpdateVolumeFundingRequest a <- runForm Nothing $ do
    csrfForm
    CreateOrUpdateVolumeFundingRequest <$> ("awards" .:> filter (not . T.null) <$> withSubDeforms (\_ -> deform))
  let fa = Funding f a
  _ <- changeVolumeFunding v fa
  return $ okResponse [] $ JSON.pairs $ fundingJSON fa

deleteVolumeFunder :: ActionRoute (Id Volume, Id Funder)
deleteVolumeFunder = action DELETE (pathJSON >/> pathId </> pathId) $ \(vi, fi) -> withAuth $ do
  guardVerfHeader
  v <- getVolume PermissionEDIT vi
  _ <- removeVolumeFunder v fi
  return $ okResponse [] $ JSON.recordEncoding $ volumeJSONSimple v
{'fraction_non_alphanumeric': 0.09575656058068119, 'fraction_numerical': 0.0011166945840312675, 'mean_word_length': 4.177745664739884, 'pattern_counts': {'":': 0, '<': 36, '<?xml version=': 0, '>': 30, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2101257', 'n_tokens_mistral': 1135, 'n_tokens_neox': 1052, 'n_words': 336}