Skip to content

Commit

Permalink
feat: moved menuendpoints from id to name (#22)
Browse files Browse the repository at this point in the history
* feat: moved menuendpoints from id to name

* feat: fixed coverage and improved exim
  • Loading branch information
JordenReuter authored Feb 12, 2024
1 parent 7aa1a6f commit 5fadec9
Show file tree
Hide file tree
Showing 8 changed files with 145 additions and 123 deletions.
47 changes: 25 additions & 22 deletions src/main/java/org/tkit/onecx/workspace/domain/daos/MenuItemDAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,30 @@ public void updateMenuItems(String newWorkspaceName, String oldWorkspaceName, St
}
}

/**
* This method delete all menu items by workspace id.
*
* @param name - workspace id
*/
@Transactional
public void deleteAllMenuItemsByWorkspaceName(String name) {
try {
var cb = this.getEntityManager().getCriteriaBuilder();
var cq = this.criteriaQuery();
var root = cq.from(MenuItem.class);

cq.where(cb.and(
cb.equal(root.get(MenuItem_.WORKSPACE_NAME), name),
cb.isNull(root.get(MenuItem_.PARENT))));

var items = getEntityManager().createQuery(cq).getResultList();
delete(items);

} catch (Exception ex) {
throw new DAOException(ErrorKeys.ERROR_DELETE_ALL_MENU_ITEMS_BY_WORKSPACE_NAME, ex);
}
}

/**
* This method delete all menu items by workspace id.
*
Expand Down Expand Up @@ -145,27 +169,6 @@ public List<MenuItem> loadAllMenuItemsByWorkspaceName(String workspaceName) {
}
}

/**
* This method fetches all menuItems assigned to a workspace with just
* id provided as a param
*
* @return List of the menu items
*/
public List<MenuItem> loadAllMenuItemsByWorkspaceId(String workspaceId) {
try {
var cb = this.getEntityManager().getCriteriaBuilder();
var cq = cb.createQuery(MenuItem.class);
var menuItem = cq.from(MenuItem.class);
cq.where(cb.equal(menuItem.get(MenuItem_.WORKSPACE).get(TraceableEntity_.ID), workspaceId));
var menuItemsQuery = getEntityManager().createQuery(cq);
menuItemsQuery.setHint(HINT_LOAD_GRAPH,
this.getEntityManager().getEntityGraph(MENU_ITEM_WORKSPACE_AND_TRANSLATIONS));
return menuItemsQuery.getResultList();
} catch (Exception ex) {
throw new DAOException(ErrorKeys.ERROR_LOAD_ALL_MENU_ITEMS_BY_WORKSPACE_ID, ex);
}
}

@Override
public MenuItem findById(Object id) throws DAOException {
try {
Expand Down Expand Up @@ -204,8 +207,8 @@ public enum ErrorKeys {

LOAD_ENTITY_BY_ID_FAILED,
ERROR_UPDATE_MENU_ITEMS,
ERROR_LOAD_ALL_MENU_ITEMS_BY_WORKSPACE_ID,
ERROR_DELETE_ALL_MENU_ITEMS_BY_WORKSPACE_ID,
ERROR_DELETE_ALL_MENU_ITEMS_BY_WORKSPACE_NAME,
ERROR_LOAD_ALL_MENU_ITEMS_BY_WORKSPACE_NAME,

ERROR_DELETE_ALL_MENU_ITEMS_BY_WORKSPACE_NAME_AND_APP_ID,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,13 @@ default WorkspaceSnapshotDTOV1 create(Map<String, Workspace> workspaces) {
@Mapping(target = "imageUrl", source = "imageUrls")
Workspace create(EximWorkspaceDTOV1 workspaceDTO);

@Mapping(target = "removeWorkspacesItem", ignore = true)
@Mapping(target = "id", source = "request.id")
@Mapping(target = "workspaces", source = "workspaces")
ImportWorkspaceResponseDTOV1 create(WorkspaceSnapshotDTOV1 request,
Map<String, ImportResponseStatusDTOV1> workspaces);

@Mapping(target = "removeWorkspaceRolesItem", ignore = true)
@Mapping(target = "removeSubjectLinksItem", ignore = true)
@Mapping(target = "removeImageUrlsItem", ignore = true)
@Mapping(target = "subjectLinks", source = "subjectLink")
Expand Down Expand Up @@ -130,4 +132,19 @@ default MenuSnapshotDTOV1 mapTree(Collection<MenuItem> entities) {
dto.getMenu().setMenuItems(parentChildrenMap.get("TOP").stream().map(this::map).toList());
return dto;
}

default Set<String> map(String roles) {
if (roles != null && !roles.isBlank()) {
String[] values = roles.split(",");
return new HashSet<>(Arrays.asList(values));
} else
return new HashSet<>();
}

default String map(Set<String> roles) {
if (roles != null && !roles.isEmpty()) {
return roles.stream().map(Object::toString).collect(Collectors.joining(","));
} else
return "";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ public class MenuInternalRestController implements MenuInternalApi {

@Override
@Transactional
public Response createMenuItemForWorkspace(String id, CreateMenuItemDTO menuItemDTO) {
var workspace = workspaceDAO.findById(id);
public Response createMenuItemForWorkspace(String name, CreateMenuItemDTO menuItemDTO) {
var workspace = workspaceDAO.findByWorkspaceName(name);

if (workspace == null) {
throw new ConstraintException("Workspace does not exist", MenuItemErrorKeys.WORKSPACE_DOES_NOT_EXIST, null);
Expand All @@ -69,7 +69,7 @@ public Response createMenuItemForWorkspace(String id, CreateMenuItemDTO menuItem
null);
} else {
// check if parent's portal and child's portal are the same
if (!parentItem.getWorkspace().getId().equals(id)) {
if (!parentItem.getWorkspace().getName().equals(name)) {
throw new ConstraintException("Parent menu item and menu item does not have the same workspace",
MenuItemErrorKeys.WORKSPACE_DIFFERENT, null);
}
Expand All @@ -89,8 +89,8 @@ public Response createMenuItemForWorkspace(String id, CreateMenuItemDTO menuItem

@Override
@Transactional
public Response deleteAllMenuItemsForWorkspace(String id) {
dao.deleteAllMenuItemsByWorkspaceId(id);
public Response deleteAllMenuItemsForWorkspace(String name) {
dao.deleteAllMenuItemsByWorkspaceName(name);

return Response.noContent().build();
}
Expand All @@ -111,15 +111,15 @@ public Response getMenuItemById(String id, String menuItemId) {
}

@Override
public Response getMenuItemsForWorkspaceId(String id) {
var result = dao.loadAllMenuItemsByWorkspaceId(id);
public Response getMenuItemsForWorkspaceName(String name) {
var result = dao.loadAllMenuItemsByWorkspaceName(name);

return Response.ok(mapper.mapList(result)).build();
}

@Override
public Response getMenuStructureForWorkspaceId(String id) {
var result = dao.loadAllMenuItemsByWorkspaceId(id);
public Response getMenuStructureForWorkspaceName(String name) {
var result = dao.loadAllMenuItemsByWorkspaceName(name);

return Response.ok(mapper.mapTree(result)).build();
}
Expand Down Expand Up @@ -176,8 +176,8 @@ private MenuItem update(String menuItemId, UpdateMenuItemRequestDTO menuItemDTO)

@Override
@Transactional
public Response uploadMenuStructureForWorkspaceId(String id, WorkspaceMenuItemStructureDTO menuItemStructureDTO) {
var workspace = workspaceDAO.findById(id);
public Response uploadMenuStructureForWorkspaceName(String name, WorkspaceMenuItemStructureDTO menuItemStructureDTO) {
var workspace = workspaceDAO.findByWorkspaceName(name);
if (workspace == null) {
throw new ConstraintException("Given workspace does not exist", MenuItemErrorKeys.WORKSPACE_DOES_NOT_EXIST, null);
}
Expand All @@ -189,7 +189,7 @@ public Response uploadMenuStructureForWorkspaceId(String id, WorkspaceMenuItemSt
List<MenuItem> items = new LinkedList<>();
mapper.recursiveMappingTreeStructure(menuItemStructureDTO.getMenuItems(), workspace, null, items);

dao.deleteAllMenuItemsByWorkspaceId(id);
dao.deleteAllMenuItemsByWorkspaceName(name);
dao.create(items);

return Response.noContent().build();
Expand Down
5 changes: 4 additions & 1 deletion src/main/openapi/onecx-workspace-exim-v1-openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,10 @@ components:
footerLabel:
type: string
workspaceRoles:
type: string
type: array
uniqueItems: true
items:
type: string
logoUrl:
type: string
address:
Expand Down
32 changes: 16 additions & 16 deletions src/main/openapi/onecx-workspace-internal-openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -140,14 +140,14 @@ paths:
$ref: '#/components/schemas/Workspace'
"404":
description: Not found
/internal/workspaces/{id}/menuItems:
/internal/workspaces/{name}/menuItems:
get:
tags:
- menuInternal
description: Find all menu items belonging to a workspace
operationId: getMenuItemsForWorkspaceId
operationId: getMenuItemsForWorkspaceName
parameters:
- $ref: '#/components/parameters/id'
- $ref: '#/components/parameters/name'
responses:
"200":
description: OK
Expand All @@ -163,7 +163,7 @@ paths:
description: Bulk update menu Items
operationId: patchMenuItems
parameters:
- $ref: '#/components/parameters/id'
- $ref: '#/components/parameters/name'
requestBody:
required: true
content:
Expand Down Expand Up @@ -193,7 +193,7 @@ paths:
description: Add a new menu item to workspace menu
operationId: createMenuItemForWorkspace
parameters:
- $ref: '#/components/parameters/id'
- $ref: '#/components/parameters/name'
requestBody:
required: true
content:
Expand Down Expand Up @@ -225,7 +225,7 @@ paths:
description: Delete all menu items in workspace
operationId: deleteAllMenuItemsForWorkspace
parameters:
- $ref: '#/components/parameters/id'
- $ref: '#/components/parameters/name'
responses:
"204":
description: No content
Expand All @@ -235,14 +235,14 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/ProblemDetailResponse'
/internal/workspaces/{id}/menuItems/tree:
/internal/workspaces/{name}/menuItems/tree:
get:
tags:
- menuInternal
description: Fetch the menuItems of the workspace in the tree structure
operationId: getMenuStructureForWorkspaceId
operationId: getMenuStructureForWorkspaceName
parameters:
- $ref: '#/components/parameters/id'
- $ref: '#/components/parameters/name'
responses:
"200":
description: OK
Expand All @@ -252,14 +252,14 @@ paths:
$ref: '#/components/schemas/WorkspaceMenuItemStructure'
"404":
description: Not found
/internal/workspaces/{id}/menuItems/tree/upload:
/internal/workspaces/{name}/menuItems/tree/upload:
post:
tags:
- menuInternal
description: Upload the menu structure for workspace
operationId: uploadMenuStructureForWorkspaceId
operationId: uploadMenuStructureForWorkspaceName
parameters:
- $ref: '#/components/parameters/id'
- $ref: '#/components/parameters/name'
requestBody:
required: true
content:
Expand All @@ -275,14 +275,14 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/ProblemDetailResponse'
/internal/workspaces/{id}/menuItems/{menuItemId}:
/internal/workspaces/{name}/menuItems/{menuItemId}:
get:
tags:
- menuInternal
description: Retrieve menu item detail info
operationId: getMenuItemById
parameters:
- $ref: '#/components/parameters/id'
- $ref: '#/components/parameters/name'
- $ref: '#/components/parameters/menuItemId'
responses:
"200":
Expand All @@ -299,7 +299,7 @@ paths:
description: Delete a menuItem by the workspace id and the menuItemId
operationId: deleteMenuItemById
parameters:
- $ref: '#/components/parameters/id'
- $ref: '#/components/parameters/name'
- $ref: '#/components/parameters/menuItemId'
responses:
"204":
Expand All @@ -316,7 +316,7 @@ paths:
description: Update an existing menu item
operationId: updateMenuItem
parameters:
- $ref: '#/components/parameters/id'
- $ref: '#/components/parameters/name'
- $ref: '#/components/parameters/menuItemId'
requestBody:
required: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ void beforeAll() {
void methodExceptionTests() {
methodExceptionTests(() -> dao.deleteAllMenuItemsByWorkspaceId(null),
MenuItemDAO.ErrorKeys.ERROR_DELETE_ALL_MENU_ITEMS_BY_WORKSPACE_ID);
methodExceptionTests(() -> dao.deleteAllMenuItemsByWorkspaceName(null),
MenuItemDAO.ErrorKeys.ERROR_DELETE_ALL_MENU_ITEMS_BY_WORKSPACE_NAME);
methodExceptionTests(() -> dao.updateMenuItems(null, null, null), MenuItemDAO.ErrorKeys.ERROR_UPDATE_MENU_ITEMS);
methodExceptionTests(() -> dao.loadAllMenuItemsByWorkspaceName(null),
MenuItemDAO.ErrorKeys.ERROR_LOAD_ALL_MENU_ITEMS_BY_WORKSPACE_NAME);
methodExceptionTests(() -> dao.loadAllMenuItemsByWorkspaceId(null),
MenuItemDAO.ErrorKeys.ERROR_LOAD_ALL_MENU_ITEMS_BY_WORKSPACE_ID);
methodExceptionTests(() -> dao.deleteAllMenuItemsByWorkspaceNameAndAppId(null, null),
MenuItemDAO.ErrorKeys.ERROR_DELETE_ALL_MENU_ITEMS_BY_WORKSPACE_NAME_AND_APP_ID);
methodExceptionTests(() -> dao.loadMenuItemByWorkspaceAndKey(null, null),
Expand Down
Loading

0 comments on commit 5fadec9

Please sign in to comment.