<commit_before>{-# LANGUAGE ExtendedDefaultRules #-}
{-# LANGUAGE OverloadedStrings    #-}

module Controllers.Tag
       ( routes
       , saveTags
       , filterExistsTags
       ) where

import           Data.Bson           (ObjectId)
import qualified Data.ByteString     as BS
import           Data.List           (deleteFirstsBy)
import qualified Data.Text           as T
import           Snap.Core
import qualified Snap.Core           as Snap
import           Snap.Snaplet

import           Application
import           Control.Monad.Trans
import           Controllers.Types
import           Models.Tag
import           Views.Utils

------------------------------------------------------------------------------

-- | Routes
--  /tags -> get all tags
--
routes :: [(BS.ByteString, Handler App App ())]
routes =  [ ("/tags",  Snap.method GET getTags)
          ]

------------------------------------------------------------------------------

-- | Fetch all tags
--
-- MAYBE: 1. if content-tye is not json, return empty
--
getTags :: AppHandler ()
getTags = findAllTags
          >>= toJSONResponse

------------------------------------------------------------------------------

-- | Save a list of tags and return them getting ID has been insert.
--   Perform save if is new otherwise ignore.
--
saveTags :: [T.Text] -> AppHandler [Tag]
saveTags input = do
  xs <- findSomeTagsName input
  ys <- mapM insertTag $ filterExistsTags maybeNewTags xs
  return (xs ++ ys)
  where maybeNewTags = map textToTag input
        textToTag name = emptyTag { _tagName = name }

filterExistsTags :: [Tag]    -- ^ Tags input from web
                    -> [Tag] -- ^ Exists Tags per input
                    -> [Tag] -- ^ Those new ones
filterExistsTags = deleteFirstsBy eqName
  where eqName x y = _tagName x == _tagName y


----------------------------------------<commit_msg>follow name convension. postfix H for handlers<commit_after>{-# LANGUAGE ExtendedDefaultRules #-}
{-# LANGUAGE OverloadedStrings    #-}

module Controllers.Tag
       ( routes
       , saveTags
       , filterExistsTags
       ) where

import           Data.Bson           (ObjectId)
import qualified Data.ByteString     as BS
import           Data.List           (deleteFirstsBy)
import qualified Data.Text           as T
import           Snap.Core
import qualified Snap.Core           as Snap
import           Snap.Snaplet

import           Application
import           Control.Monad.Trans
import           Controllers.Types
import           Models.Tag
import           Views.Utils

------------------------------------------------------------------------------

-- | Routes
--  /tags -> get all tags
--
routes :: [(BS.ByteString, Handler App App ())]
routes =  [ ("/tags",  Snap.method GET getTagsH)
          ]

tagIdParam :: BS.ByteString
tagIdParam = "tagid"

------------------------------------------------------------------------------

-- | Fetch all tags
--
-- MAYBE: 1. if content-tye is not json, return empty
--
getTagsH :: AppHandler ()
getTagsH = findAllTags
           >>= toJSONResponse

------------------------------------------------------------------------------

-- | Save a list of tags and return them getting ID has been insert.
--   Perform save if is new otherwise ignore.
--
saveTags :: [T.Text] -> AppHandler [Tag]
saveTags input = do
  xs <- findSomeTagsName input
  ys <- mapM insertTag $ filterExistsTags maybeNewTags xs
  return (xs ++ ys)
  where maybeNewTags = map textToTag input
        textToTag name = emptyTag { _tagName = name }

filterExistsTags :: [Tag]    -- ^ Tags input from web
                    -> [Tag] -- ^ Exists Tags per input
                    -> [Tag] -- ^ Those new ones
filterExistsTags = deleteFirstsBy eqName
  where eqName x y = _tagName x == _tagName y


----------------------------------------
{'fraction_non_alphanumeric': 0.2204539525176102, 'fraction_numerical': 0.0005217845030002609, 'mean_word_length': 1.3564843269821758, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 15, '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': '6575667', 'n_tokens_mistral': 995, 'n_tokens_neox': 967, 'n_words': 316}