[NGRAMS-TABLE] WIP add ngramsType param to PUT

parent 359dc4b5
...@@ -57,6 +57,7 @@ import GHC.Generics (Generic) ...@@ -57,6 +57,7 @@ import GHC.Generics (Generic)
import Gargantext.Core.Utils.Prefix (unPrefix) import Gargantext.Core.Utils.Prefix (unPrefix)
import Gargantext.Database.Types.Node (NodeType(..)) import Gargantext.Database.Types.Node (NodeType(..))
import Gargantext.Database.Schema.Node (defaultList, HasNodeError) import Gargantext.Database.Schema.Node (defaultList, HasNodeError)
import Gargantext.Database.Schema.Ngrams (NgramsType, NgramsTypeId, ngramsTypeId)
import qualified Gargantext.Database.Schema.Ngrams as Ngrams import qualified Gargantext.Database.Schema.Ngrams as Ngrams
import Gargantext.Database.Schema.NodeNgram import Gargantext.Database.Schema.NodeNgram
import Gargantext.Database.Schema.NodeNgramsNgrams import Gargantext.Database.Schema.NodeNgramsNgrams
...@@ -244,6 +245,7 @@ type TableNgramsApiGet = Summary " Table Ngrams API Get" ...@@ -244,6 +245,7 @@ type TableNgramsApiGet = Summary " Table Ngrams API Get"
:> Get '[JSON] (Versioned NgramsTable) :> Get '[JSON] (Versioned NgramsTable)
type TableNgramsApi = Summary " Table Ngrams API Change" type TableNgramsApi = Summary " Table Ngrams API Change"
:> QueryParam "ngramsType" TabType
:> QueryParam "list" ListId :> QueryParam "list" ListId
:> ReqBody '[JSON] (Versioned NgramsTablePatch) :> ReqBody '[JSON] (Versioned NgramsTablePatch)
:> Put '[JSON] (Versioned NgramsTablePatch) :> Put '[JSON] (Versioned NgramsTablePatch)
...@@ -268,9 +270,9 @@ ngramError nne = throwError $ _NgramError # nne ...@@ -268,9 +270,9 @@ ngramError nne = throwError $ _NgramError # nne
-- `GraphList` and that the patch is `Replace CandidateList StopList` then -- `GraphList` and that the patch is `Replace CandidateList StopList` then
-- the list is going to be `StopList` while it should keep `GraphList`. -- the list is going to be `StopList` while it should keep `GraphList`.
-- However this should not happen in non conflicting situations. -- However this should not happen in non conflicting situations.
mkListsUpdate :: ListId -> NgramsTablePatch -> [(ListId, NgramsTypeId, NgramsTerm, ListTypeId)] mkListsUpdate :: ListId -> NgramsType -> NgramsTablePatch -> [(ListId, NgramsTypeId, NgramsTerm, ListTypeId)]
mkListsUpdate lId patches = mkListsUpdate lId nt patches =
[ (lId, ng, listTypeId lt) [ (lId, ngramsTypeId nt, ng, listTypeId lt)
| (ng, patch) <- patches ^.. ntp_ngrams_patches . ifolded . withIndex | (ng, patch) <- patches ^.. ntp_ngrams_patches . ifolded . withIndex
, lt <- patch ^.. patch_list . new , lt <- patch ^.. patch_list . new
] ]
...@@ -285,20 +287,34 @@ mkChildrenGroups lId addOrRem patches = ...@@ -285,20 +287,34 @@ mkChildrenGroups lId addOrRem patches =
, child <- patch ^.. patch_children . to addOrRem . folded , child <- patch ^.. patch_children . to addOrRem . folded
] ]
ngramsTypeFromTabType :: Maybe TabType -> NgramsType
ngramsTypeFromTabType maybeTabType =
let lieu = "Garg.API.Ngrams: " :: Text in
case maybeTabType of
Nothing -> Ngrams.Sources -- panic (lieu <> "Indicate the Table")
Just tab -> case tab of
Sources -> Ngrams.Sources
Authors -> Ngrams.Authors
Institutes -> Ngrams.Institutes
Terms -> Ngrams.NgramsTerms
_ -> panic $ lieu <> "No Ngrams for this tab"
-- Apply the given patch to the DB and returns the patch to be applied on the -- Apply the given patch to the DB and returns the patch to be applied on the
-- cilent. -- cilent.
-- TODO: -- TODO:
-- In this perliminary version the OT aspect is missing, therefore the version -- In this perliminary version the OT aspect is missing, therefore the version
-- number is always 1 and the returned patch is always empty. -- number is always 1 and the returned patch is always empty.
tableNgramsPatch :: (HasNgramError err, HasNodeError err) tableNgramsPatch :: (HasNgramError err, HasNodeError err)
=> CorpusId -> Maybe ListId => CorpusId -> Maybe TabType -> Maybe ListId
-> Versioned NgramsTablePatch -> Versioned NgramsTablePatch
-> Cmd err (Versioned NgramsTablePatch) -> Cmd err (Versioned NgramsTablePatch)
tableNgramsPatch corpusId maybeList (Versioned version patch) = do tableNgramsPatch corpusId maybeTabType maybeList (Versioned version patch) = do
when (version /= 1) $ ngramError UnsupportedVersion when (version /= 1) $ ngramError UnsupportedVersion
let ngramsType = ngramsTypeFromTabType maybeTabType
listId <- maybe (defaultList corpusId) pure maybeList listId <- maybe (defaultList corpusId) pure maybeList
updateNodeNgrams $ NodeNgramsUpdate updateNodeNgrams $ NodeNgramsUpdate
{ _nnu_lists_update = mkListsUpdate listId patch { _nnu_lists_update = mkListsUpdate listId ngramsType patch
, _nnu_rem_children = mkChildrenGroups listId _rem patch , _nnu_rem_children = mkChildrenGroups listId _rem patch
, _nnu_add_children = mkChildrenGroups listId _add patch , _nnu_add_children = mkChildrenGroups listId _add patch
} }
...@@ -312,15 +328,7 @@ getTableNgrams :: HasNodeError err ...@@ -312,15 +328,7 @@ getTableNgrams :: HasNodeError err
-> Cmd err (Versioned NgramsTable) -> Cmd err (Versioned NgramsTable)
getTableNgrams cId maybeTabType maybeListId mlimit moffset = do getTableNgrams cId maybeTabType maybeListId mlimit moffset = do
let lieu = "Garg.API.Ngrams: " :: Text let lieu = "Garg.API.Ngrams: " :: Text
let ngramsType = case maybeTabType of let ngramsType = ngramsTypeFromTabType maybeTabType
Nothing -> Ngrams.Sources -- panic (lieu <> "Indicate the Table")
Just tab -> case tab of
Sources -> Ngrams.Sources
Authors -> Ngrams.Authors
Institutes -> Ngrams.Institutes
Terms -> Ngrams.NgramsTerms
_ -> panic $ lieu <> "No Ngrams for this tab"
listId <- maybe (defaultList cId) pure maybeListId listId <- maybe (defaultList cId) pure maybeListId
let let
......
...@@ -109,8 +109,8 @@ instance ToField NgramsTypeId where ...@@ -109,8 +109,8 @@ instance ToField NgramsTypeId where
toField (NgramsTypeId n) = toField n toField (NgramsTypeId n) = toField n
instance FromField NgramsTypeId where instance FromField NgramsTypeId where
fromField field mdata = do fromField fld mdata = do
n <- fromField field mdata n <- fromField fld mdata
if (n :: Int) > 0 then return $ NgramsTypeId n if (n :: Int) > 0 then return $ NgramsTypeId n
else mzero else mzero
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment