Skip to content

Commit

Permalink
feat: get products by workspace name instead of id in v1 (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
JordenReuter authored Feb 14, 2024
1 parent adfa4c8 commit 7a6ffdd
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 7 deletions.
15 changes: 15 additions & 0 deletions src/main/java/org/tkit/onecx/workspace/domain/daos/ProductDAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.tkit.onecx.workspace.domain.models.MenuItem_;
import org.tkit.onecx.workspace.domain.models.Product;
import org.tkit.onecx.workspace.domain.models.Product_;
import org.tkit.onecx.workspace.domain.models.Workspace_;
import org.tkit.quarkus.jpa.daos.AbstractDAO;
import org.tkit.quarkus.jpa.exceptions.DAOException;
import org.tkit.quarkus.jpa.models.TraceableEntity_;
Expand All @@ -30,6 +31,19 @@ public List<Product> getProductsForWorkspaceId(String id) {
}
}

public List<Product> getProductsForWorkspaceName(String name) {
try {
var cb = this.getEntityManager().getCriteriaBuilder();
var cq = cb.createQuery(Product.class);
var root = cq.from(Product.class);

cq.where(cb.equal(root.get(Product_.WORKSPACE).get(Workspace_.NAME), name));
return this.getEntityManager().createQuery(cq).getResultList();
} catch (Exception ex) {
throw this.handleConstraint(ex, ProductDAO.ErrorKeys.ERROR_FIND_PRODUCTS_BY_WORKSPACE_NAME);
}
}

@Transactional(value = Transactional.TxType.REQUIRED, rollbackOn = DAOException.class)
public void deleteProduct(String id) {
try {
Expand Down Expand Up @@ -93,6 +107,7 @@ public enum ErrorKeys {
ERROR_DELETE_PRODUCT_ID,

ERROR_FIND_PRODUCTS_BY_WORKSPACE_ID,
ERROR_FIND_PRODUCTS_BY_WORKSPACE_NAME,
FIND_ENTITY_BY_NAME_FAILED,

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ class ProductsExternalV1RestController implements ProductExternalV1Api {
ProductDAO productDAO;

@Override
public Response getProducts(String id) {
var result = productDAO.getProductsForWorkspaceId(id);
public Response getProducts(String name) {
var result = productDAO.getProductsForWorkspaceName(name);
return Response.ok(mapper.map(result)).build();
}
}
4 changes: 2 additions & 2 deletions src/main/openapi/onecx-workspace-v1-openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,14 @@ paths:
schema:
$ref: '#/components/schemas/WorkspaceNameList'

/v1/workspaces/{id}/products:
/v1/workspaces/{name}/products:
get:
tags:
- productExternal
description: Find all products belonging to a workspace
operationId: getProducts
parameters:
- $ref: '#/components/parameters/id'
- $ref: '#/components/parameters/name'
responses:
"200":
description: OK
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ void beforeAll() {
void methodExceptionTests() {
methodExceptionTests(() -> dao.getProductsForWorkspaceId(null),
ProductDAO.ErrorKeys.ERROR_FIND_PRODUCTS_BY_WORKSPACE_ID);
methodExceptionTests(() -> dao.getProductsForWorkspaceName(null),
ProductDAO.ErrorKeys.ERROR_FIND_PRODUCTS_BY_WORKSPACE_NAME);
methodExceptionTests(() -> dao.loadById(null),
ProductDAO.ErrorKeys.LOAD_ENTITY_BY_ID_FAILED);
methodExceptionTests(() -> dao.deleteProduct(null),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@
class ProductExternalV1RestControllerTest extends AbstractTest {

@Test
void getProductsForWorkspaceIdTest() {
void getProductsForWorkspaceNameTest() {
// not existing product
var response = given()
.when()
.contentType(APPLICATION_JSON)
.pathParam("id", "does-not-exist")
.pathParam("name", "does-not-exist")
.get()
.then()
.statusCode(OK.getStatusCode())
Expand All @@ -40,7 +40,7 @@ void getProductsForWorkspaceIdTest() {
var dto = given()
.when()
.contentType(APPLICATION_JSON)
.pathParam("id", "11-111")
.pathParam("name", "test01")
.get()
.then()
.statusCode(OK.getStatusCode())
Expand Down

0 comments on commit 7a6ffdd

Please sign in to comment.