]> Git — Sourcephile - gargantext.git/blob - src/Gargantext/API/Node/Document/Export.hs
[docs download] fix CSV return value
[gargantext.git] / src / Gargantext / API / Node / Document / Export.hs
1 {-|
2 Module : Gargantext.API.Node.Document.Export
3 Description : Document export
4 Copyright : (c) CNRS, 2017-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
8 Portability : POSIX
9 -}
10
11 module Gargantext.API.Node.Document.Export
12 where
13
14 import qualified Data.ByteString.Lazy.Char8 as BSC
15 import Data.Csv (encodeDefaultOrderedByName)
16 import qualified Data.Text as T
17 import Data.Version (showVersion)
18 import Gargantext.API.Node.Document.Export.Types
19 import Gargantext.API.Prelude (GargNoServer, GargServer)
20 import Gargantext.Core (toDBid)
21 import Gargantext.Core.Types
22 -- import Gargantext.Database.Admin.Types.Hyperdata (HyperdataDocument(..))
23 import Gargantext.Database.Query.Facet (runViewDocuments, Facet(..))
24 import Gargantext.Database.Query.Table.Node (getClosestParentIdByType)
25 import Gargantext.Database.Schema.Node (NodePoly(..))
26 import Gargantext.Prelude
27 import qualified Paths_gargantext as PG -- cabal magic build module
28 import Servant
29
30 api :: UserId -> DocId -> GargServer API
31 api uid dId = getDocumentsJSON uid dId
32 :<|> getDocumentsCSV uid dId
33
34 --------------------------------------------------
35 -- | Hashes are ordered by Set
36 getDocumentsJSON :: UserId
37 -> DocId
38 -> GargNoServer DocumentExport
39 getDocumentsJSON uId pId = do
40 printDebug "[getDocuments] pId" pId
41 mcId <- getClosestParentIdByType pId NodeCorpus
42 let cId = maybe (panic "[G.A.N.D.Export] Node has no parent") identity mcId
43 printDebug "[getDocuments] cId" cId
44 --docs <- getDocumentsWithParentId cId -- NodeDocument (Proxy :: Proxy HyperdataDocument)
45 docs <- runViewDocuments cId False Nothing Nothing Nothing Nothing
46 printDebug "[getDocuments] got docs" docs
47 --de_docs <- mapM mapFacetDoc docs
48 pure $ DocumentExport { _de_documents = mapFacetDoc <$> docs
49 , _de_garg_version = T.pack $ showVersion PG.version }
50 where
51 mapFacetDoc (FacetDoc { .. }) =
52 Document { _d_document =
53 Node { _node_id = facetDoc_id
54 , _node_hash_id = Nothing
55 , _node_typename = toDBid NodeDocument
56 , _node_user_id = uId
57 , _node_parent_id = Nothing
58 , _node_name = facetDoc_title
59 , _node_date = facetDoc_created
60 , _node_hyperdata = facetDoc_hyperdata }
61 , _d_ngrams = Ngrams { _ng_ngrams = []
62 , _ng_hash = "" }
63 , _d_hash = "" }
64 _mapDoc d = Document { _d_document = d
65 , _d_ngrams = Ngrams { _ng_ngrams = []
66 , _ng_hash = "" }
67 , _d_hash = ""}
68
69 getDocumentsCSV :: UserId
70 -> DocId
71 -> GargNoServer T.Text -- [Document]
72 getDocumentsCSV uId pId = do
73 DocumentExport { _de_documents } <- getDocumentsJSON uId pId
74
75 pure $ T.pack $ BSC.unpack $ encodeDefaultOrderedByName _de_documents
76
77