Skip to content
This repository has been archived by the owner on Dec 11, 2019. It is now read-only.

Flag to ignore disabled sections #408

Open
wants to merge 7 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions back/src/Guide/Api/Methods.hs
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,21 @@ deleteCategory catId =
----------------------------------------------------------------------------

-- | Get item by item ID.
getItem :: Uid Item -> Guider CItemFull
getItem itemId =
--
-- Pass 'True' to ignore disabled sections and get full item.
getItem :: Bool -> Uid Item -> Guider CItemFull
getItem isIgnored itemId =
logHandler "getItem" [attr "itemId" itemId] $ do
toCItemFull <$> getItemOrFail itemId
item <- getItemOrFail itemId
sections <- categoryEnabledSections <$> dbQuery (GetCategoryByItem itemId)
if isIgnored then pure $ toCItemFull allSections item
else pure $ toCItemFull sections item
where
allSections = S.fromList
[ ItemProsConsSection
, ItemEcosystemSection
, ItemNotesSection
]

-- | Create a new item, given the name.
--
Expand Down
31 changes: 23 additions & 8 deletions back/src/Guide/Api/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,10 @@ data ItemSite route = ItemSite
{ _getItem :: route :-
Summary "Get item by id"
:> ErrorResponse 404 "Item not found"
:> "ignore_selections"
:> QueryParam' '[Required, Strict,
Description "Ignore disabled sections to get full item"]
"bool" Bool
:> "item"
:> Capture "itemId" (Uid Item)
:> Get '[JSON] CItemFull
Expand Down Expand Up @@ -505,7 +509,7 @@ toCCategoryFull $(fields 'Category) = CCategoryFull
, ccfStatus = categoryStatus
, ccfDescription = toCMarkdown categoryNotes
, ccfSections = categoryEnabledSections
, ccfItems = fmap toCItemFull categoryItems
, ccfItems = fmap (toCItemFull categoryEnabledSections) categoryItems
}
where
-- Ignored fields
Expand Down Expand Up @@ -674,23 +678,34 @@ instance ToSchema CItemFull where
field "toc" . inlineSchema . description ?= "Table of contents"

-- | Factory to create a 'CItemFull' from an 'Item'
toCItemFull :: Item -> CItemFull
toCItemFull $(fields 'Item) = CItemFull
--
-- Pass to 'Set ItemSection' all kinds of sections
-- to ignore disabled sections and get full item content.
toCItemFull :: Set ItemSection -> Item -> CItemFull
toCItemFull selections $(fields 'Item) = CItemFull
{ cifId = itemUid
, cifName = itemName
, cifCreated = itemCreated
, cifHackage = itemHackage
, cifSummary = toCMarkdown itemSummary
, cifPros = fmap toCTrait itemPros
, cifCons = fmap toCTrait itemCons
, cifEcosystem = toCMarkdown itemEcosystem
, cifNotes = toCMarkdown itemNotes
, cifPros = fmap toCTrait pros
, cifCons = fmap toCTrait cons
, cifEcosystem = toCMarkdown ecosystem
, cifNotes = toCMarkdown notes
, cifLink = itemLink
, cifToc = map toCTocHeading (markdownTreeTOC itemNotes)
}
where
-- Ignored fields
_ = (itemProsDeleted, itemConsDeleted)
ecosystem = if (ItemEcosystemSection `elem` selections)
then itemEcosystem
else toMarkdownBlock ""
notes = if (ItemNotesSection `elem` selections)
then itemNotes
else toMarkdownTree "" ""
(pros, cons) = if (ItemProsConsSection `elem` selections)
then (itemPros, itemCons)
else ([],[])

----------------------------------------------------------------------------
-- CTrait
Expand Down
1 change: 1 addition & 0 deletions back/src/Guide/State.hs
Original file line number Diff line number Diff line change
Expand Up @@ -845,6 +845,7 @@ deriving instance Show GetGlobalState
-- category
deriving instance Show GetCategories
deriving instance Show GetCategoryMaybe
deriving instance Show GetCategoryByItem
deriving instance Show AddCategory
deriving instance Show DeleteCategory
deriving instance Show SetCategoryGroup
Expand Down
67 changes: 38 additions & 29 deletions back/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -725,35 +725,6 @@
}
},
"/item/{itemId}": {
"get": {
"summary": "Get item by id",
"responses": {
"404": {
"description": "Item not found"
},
"200": {
"schema": {
"$ref": "#/definitions/CItemFull"
},
"description": ""
}
},
"produces": [
"application/json;charset=utf-8"
],
"parameters": [
{
"format": "Item ID",
"required": true,
"in": "path",
"name": "itemId",
"type": "string"
}
],
"tags": [
"02. Items"
]
},
"delete": {
"summary": "Delete an item",
"responses": {
Expand Down Expand Up @@ -1066,6 +1037,44 @@
]
}
},
"/ignore_selections/item/{itemId}": {
"get": {
"summary": "Get item by id",
"responses": {
"404": {
"description": "Item not found"
},
"200": {
"schema": {
"$ref": "#/definitions/CItemFull"
},
"description": ""
}
},
"produces": [
"application/json;charset=utf-8"
],
"parameters": [
{
"required": true,
"in": "query",
"name": "bool",
"type": "boolean",
"description": "Ignore disabled sections to get full item"
},
{
"format": "Item ID",
"required": true,
"in": "path",
"name": "itemId",
"type": "string"
}
],
"tags": [
"02. Items"
]
}
},
"/item/{itemId}/trait/{traitId}/move": {
"post": {
"summary": "Move trait",
Expand Down
2 changes: 1 addition & 1 deletion back/tests/ApiSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ tests = describe "api" $ do
it "get item by id" $ do
req <- withItem $ \(Uid itemId) -> do
request <- makeRequest
(Path $ "item/" <> toString itemId)
(Path $ "ignore_selections/item/" <> toString itemId <> "?bool=false")
(Method "GET")
(Status 200 "OK", _ :: CItemFull) <- runRequest request
pure request
Expand Down