From 0c5c70e002cd04df7966e07b9f861403e2d5aa38 Mon Sep 17 00:00:00 2001 From: Jens Pelzetter Date: Sat, 6 Jun 2020 18:09:43 +0200 Subject: [PATCH] Update methods for CategoriesApi --- .../admin/categorization/CategoriesApi.java | 135 ++++++++++++++++-- 1 file changed, 125 insertions(+), 10 deletions(-) diff --git a/ccm-core/src/main/java/org/libreccm/api/admin/categorization/CategoriesApi.java b/ccm-core/src/main/java/org/libreccm/api/admin/categorization/CategoriesApi.java index 893aeb3fb..3d0a56555 100644 --- a/ccm-core/src/main/java/org/libreccm/api/admin/categorization/CategoriesApi.java +++ b/ccm-core/src/main/java/org/libreccm/api/admin/categorization/CategoriesApi.java @@ -29,6 +29,7 @@ import org.libreccm.core.CoreConstants; import org.libreccm.security.AuthorizationRequired; import org.libreccm.security.RequiresPrivilege; +import java.util.Objects; import java.util.stream.Collectors; import javax.enterprise.context.RequestScoped; @@ -145,17 +146,37 @@ public class CategoriesApi { @PUT @Path("/{domainIdentifier}/{path:^[\\w\\-/]+$}") - @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) @AuthorizationRequired @RequiresPrivilege(CoreConstants.PRIVILEGE_ADMIN) @Transactional(Transactional.TxType.REQUIRED) - public CategoryData updateCategory( + public Response updateCategory( @PathParam("domainIdentifier") final String domainIdentifierParam, - @PathParam("path") final String categoryPathTokens, + @PathParam("path") final String categoryPath, final CategoryData categoryData ) { - throw new UnsupportedOperationException(); + final Domain domain = repository.findDomain(domainIdentifierParam); + final Category category = categoryRepository + .findByPath(domain, categoryPath) + .orElseThrow( + () -> new WebApplicationException( + String.format( + "No category with path %s in Domain %s found.", + categoryPath, + domain.getDomainKey() + ), + Response.Status.NOT_FOUND + ) + ); + + return updateCategory(category, categoryData) + .entity( + String.format( + "Category %s/%s updated successfully.", + domain.getDomainKey(), + categoryPath + ) + ).build(); } @PUT @@ -165,11 +186,27 @@ public class CategoriesApi { @AuthorizationRequired @RequiresPrivilege(CoreConstants.PRIVILEGE_ADMIN) @Transactional(Transactional.TxType.REQUIRED) - public CategoryData updateCategory( - @PathParam("catgoryId") final long categoryIdParam, + public Response updateCategory( + @PathParam("catgoryId") final long categoryId, final CategoryData categoryData ) { - throw new UnsupportedOperationException(); + final Category category = categoryRepository + .findById(categoryId) + .orElseThrow( + () -> new WebApplicationException( + String.format( + "No Category with ID %d found.", categoryId + ), + Response.Status.NOT_FOUND + ) + ); + + return updateCategory(category, categoryData) + .entity( + String.format( + "Category %d updated successfully.", categoryId + ) + ).build(); } @PUT @@ -179,11 +216,89 @@ public class CategoriesApi { @AuthorizationRequired @RequiresPrivilege(CoreConstants.PRIVILEGE_ADMIN) @Transactional(Transactional.TxType.REQUIRED) - public CategoryData updateCategory( - @PathParam("catgoryUid") final String categoryIdParam, + public Response updateCategory( + @PathParam("catgoryUuid") final String uuid, final CategoryData categoryData ) { - throw new UnsupportedOperationException(); + final Category category = categoryRepository + .findByUuid(uuid) + .orElseThrow( + () -> new WebApplicationException( + String.format( + "No Category with UIID %s found.", uuid + ), + Response.Status.NOT_FOUND + ) + ); + + return updateCategory(category, categoryData) + .entity( + String.format( + "Category %s updated successfully.", uuid + ) + ).build(); + } + + private Response.ResponseBuilder updateCategory( + final Category category, + final CategoryData categoryData + ) { + if (category.getParentCategory() != null + && categoryData.getParentCategory() != null) { + // Check if parent category changed. If yes move category + if (!category.getParentCategory().getUuid().equals( + categoryData.getParentCategory().getUuid() + )) { + final Category target = categoryRepository + .findByUuid(categoryData.getParentCategory().getUuid()) + .orElseThrow( + () -> new WebApplicationException( + String.format( + "Target category with UUID %s not found.", + categoryData.getParentCategory().getUuid() + ) + ) + ); + categoryManager.addSubCategoryToCategory(category, target); + } + } + + boolean updated = false; + + if (category.getCategoryOrder() != categoryData.getCategoryOrder()) { + category.setCategoryOrder(categoryData.getCategoryOrder()); + updated = true; + } + + if (!Objects.equals( + category.getDescription(), categoryData.getDescription() + )) { + category.setDescription(categoryData.getDescription()); + updated = true; + } + + if (!Objects.equals(category.getName(), categoryData.getName())) { + category.setName(categoryData.getName()); + updated = true; + } + + if (!category.getTitle().equals(categoryData.getTitle())) { + category.setTitle(categoryData.getTitle()); + updated = true; + } + + if (!Objects.equals( + category.getUniqueId(), categoryData.getUniqueId() + )) { + category.setUniqueId(categoryData.getUniqueId()); + updated = true; + } + + if (updated) { + categoryRepository.save(category); + } + + return Response.ok(); } @DELETE