Skip to content

Commit

Permalink
#28896 including countChildren atribute on the Endpoint response (#29251
Browse files Browse the repository at this point in the history
  • Loading branch information
freddyDOTCMS authored Jul 18, 2024
1 parent 0f67690 commit 694202e
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ public PaginatedArrayList<Category> getItems(final User user, final String filte
.rootInode(inode)
.build();


final PaginatedCategories categories = searchInAllLevels ? searchAllLevels(user, searchingCriteria) :
searchInOneLevel(user, searchingCriteria, childrenCategories);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package com.dotmarketing.portlets.categories.business;

import com.dotcms.util.CloseUtils;
import com.dotcms.util.DotPreconditions;
import com.dotcms.util.JsonUtil;
import com.dotcms.util.ReflectionUtils;
import com.dotcms.util.*;
import com.dotmarketing.beans.Tree;
import com.dotmarketing.business.APILocator;
import com.dotmarketing.business.CacheLocator;
Expand Down Expand Up @@ -638,6 +635,7 @@ private List<HierarchedCategory> convertForHierarchedCategories(final List<Map<S
final String parentsASJsonArray = "[" + row.get("path") + "]";
final List<ShortCategory> parentList = getShortCategories(parentsASJsonArray);
category.setParentList(parentList.subList(0, parentList.size() - 1));
category.setChildrenCount(ConversionUtils.toInt(row.get("childrencount"), 0));
}

categories.add(category);
Expand Down Expand Up @@ -951,7 +949,8 @@ private static String getFindAllSQLQuery(CategorySearchCriteria searchCriteria)
"SELECT c.*, CONCAT(ch.path, ',', json_build_object('inode', c.inode, 'categoryName', c.category_name, 'key', c.category_key)::varchar) AS path " +
"FROM Category c JOIN tree t ON c.inode = t.child JOIN CategoryHierarchy ch ON t.parent = ch.inode " +
") " +
"SELECT distinct *,path FROM CategoryHierarchy %s ORDER BY %s %s";
"SELECT distinct *,path, (SELECT COUNT(*) FROM tree WHERE parent = ch.inode) as childrenCount " +
"FROM CategoryHierarchy ch %s ORDER BY %s %s";

final String rootCategoryFilter = UtilMethods.isSet(searchCriteria.rootInode) ? "WHERE c.inode = ?" : StringPool.BLANK;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
public class HierarchedCategory extends Category{

private List<ShortCategory> parentList;
private int childrenCount;

public void setParentList(final List<ShortCategory> parentList) {
this.parentList = parentList;
Expand All @@ -28,6 +29,14 @@ public List<ShortCategory> getParentList() {
return parentList;
}

public void setChildrenCount(int childrenCount) {
this.childrenCount = childrenCount;
}

public int getChildrenCount() {
return childrenCount;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"info": {
"_postman_id": "c43472ec-b1b2-41c9-8a13-3905b6e3d255",
"_postman_id": "944aa93e-7b0e-4eac-a7d5-8324663ca417",
"name": "Category",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json",
"_exporter_id": "30436704"
Expand Down Expand Up @@ -2541,7 +2541,8 @@
" ",
" pm.expect(jsonData.entity.categoryName).to.eql(\"PostmanTest Child 1\");",
"});",
""
"",
"pm.collectionVariables.set(\"child1Inode\", jsonData.entity.inode);"
],
"type": "text/javascript",
"packages": {}
Expand Down Expand Up @@ -2768,6 +2769,68 @@
},
"response": []
},
{
"name": "Create Grandchild Category 2",
"event": [
{
"listen": "test",
"script": {
"exec": [
"pm.test(\"Status code should be ok 200\", function () {",
" pm.response.to.have.status(200);",
"});",
"",
"var jsonData = pm.response.json();",
"",
"pm.test(\"Information Saved Correctly\", function () {",
" ",
" pm.expect(jsonData.entity.categoryName).to.eql(\"Grandchild 2\");",
"});",
"",
"",
""
],
"type": "text/javascript",
"packages": {}
}
}
],
"request": {
"auth": {
"type": "bearer",
"bearer": [
{
"key": "token",
"value": "{{jwt}}",
"type": "string"
}
]
},
"method": "POST",
"header": [],
"body": {
"mode": "raw",
"raw": "{\n \"key\":\"CGrandchild_2\",\n \"categoryName\" : \"Grandchild 2\",\n \"keywords\":\"This is a child test category\",\n \"categoryVelocityVarName\" : \"GrandcßhildTestKey_2\",\n \"parent\" : \"{{child1Inode}}\"\n}\n\n",
"options": {
"raw": {
"language": "json"
}
}
},
"url": {
"raw": "{{serverURL}}/api/v1/categories",
"host": [
"{{serverURL}}"
],
"path": [
"api",
"v1",
"categories"
]
}
},
"response": []
},
{
"name": "Create Second Top Level category",
"event": [
Expand Down Expand Up @@ -2853,15 +2916,18 @@
" pm.expect('PostmanTest Child 1').to.have.equals(jsonData.entity[i].categoryName);",
" pm.expect(1).to.have.equals(jsonData.entity[i].parentList.length);",
" pm.expect('PostmanTest Top level Category').to.have.equals(jsonData.entity[i].parentList[0].name);",
" pm.expect(1).to.have.equals(jsonData.entity[i].childrenCount);",
" } else if (i === 1) {",
" pm.expect('PostmanTest Child 2').to.have.equals(jsonData.entity[i].categoryName);",
" pm.expect(1).to.have.equals(jsonData.entity[i].parentList.length);",
" pm.expect('PostmanTest Top level Category').to.have.equals(jsonData.entity[i].parentList[0].name);",
" pm.expect(0).to.have.equals(jsonData.entity[i].childrenCount);",
" } else if (i === 2) {",
" pm.expect('PostmanTest Grandchild 1').to.have.equals(jsonData.entity[i].categoryName);",
" pm.expect(2).to.have.equals(jsonData.entity[i].parentList.length);",
" pm.expect('PostmanTest Top level Category').to.have.equals(jsonData.entity[i].parentList[0].name);",
" pm.expect('Child 3').to.have.equals(jsonData.entity[i].parentList[1].name);",
" pm.expect(0).to.have.equals(jsonData.entity[i].childrenCount);",
" } ",
" }",
"",
Expand Down Expand Up @@ -6205,6 +6271,10 @@
{
"key": "grantChildCategoryKeyHierarchy",
"value": ""
},
{
"key": "child1Inode",
"value": ""
}
]
}

0 comments on commit 694202e

Please sign in to comment.