Methods for deleting categories

Jens Pelzetter 2020-06-07 08:01:23 +02:00
parent 0c5c70e002
commit 16bd34b7d7
1 changed files with 122 additions and 84 deletions

View File

@ -81,21 +81,11 @@ public class CategoriesApi {
@PathParam("domainIdentifier") final String domainIdentifierParam, @PathParam("domainIdentifier") final String domainIdentifierParam,
@PathParam("path") final String categoryPath @PathParam("path") final String categoryPath
) { ) {
final Domain domain = repository.findDomain(domainIdentifierParam); return new CategoryData(
final Category category = categoryRepository findCategory(
.findByPath(domain, categoryPath) repository.findDomain(domainIdentifierParam),
.orElseThrow( categoryPath)
() -> new WebApplicationException( );
String.format(
"No category with path %s in Domain %s found.",
categoryPath,
domain.getDomainKey()
),
Response.Status.NOT_FOUND
)
);
return new CategoryData(category);
} }
@GET @GET
@ -107,18 +97,7 @@ public class CategoriesApi {
public CategoryData getCategory( public CategoryData getCategory(
@PathParam("categoryId") final long categoryId @PathParam("categoryId") final long categoryId
) { ) {
return new CategoryData( return new CategoryData(findCategory(categoryId));
categoryRepository
.findById(categoryId)
.orElseThrow(
() -> new WebApplicationException(
String.format(
"No Category with ID %d found.",
categoryId
)
)
)
);
} }
@GET @GET
@ -130,18 +109,7 @@ public class CategoriesApi {
public CategoryData getCategory( public CategoryData getCategory(
@PathParam("categoryId") final String categoryUuid @PathParam("categoryId") final String categoryUuid
) { ) {
return new CategoryData( return new CategoryData(findCategory(categoryUuid));
categoryRepository
.findByUuid(categoryUuid)
.orElseThrow(
() -> new WebApplicationException(
String.format(
"No Category with UUID %s found.",
categoryUuid
)
)
)
);
} }
@PUT @PUT
@ -156,18 +124,7 @@ public class CategoriesApi {
final CategoryData categoryData final CategoryData categoryData
) { ) {
final Domain domain = repository.findDomain(domainIdentifierParam); final Domain domain = repository.findDomain(domainIdentifierParam);
final Category category = categoryRepository final Category category = findCategory(domain, categoryPath);
.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) return updateCategory(category, categoryData)
.entity( .entity(
@ -190,16 +147,7 @@ public class CategoriesApi {
@PathParam("catgoryId") final long categoryId, @PathParam("catgoryId") final long categoryId,
final CategoryData categoryData final CategoryData categoryData
) { ) {
final Category category = categoryRepository final Category category = findCategory(categoryId);
.findById(categoryId)
.orElseThrow(
() -> new WebApplicationException(
String.format(
"No Category with ID %d found.", categoryId
),
Response.Status.NOT_FOUND
)
);
return updateCategory(category, categoryData) return updateCategory(category, categoryData)
.entity( .entity(
@ -220,16 +168,7 @@ public class CategoriesApi {
@PathParam("catgoryUuid") final String uuid, @PathParam("catgoryUuid") final String uuid,
final CategoryData categoryData final CategoryData categoryData
) { ) {
final Category category = categoryRepository final Category category = findCategory(uuid);
.findByUuid(uuid)
.orElseThrow(
() -> new WebApplicationException(
String.format(
"No Category with UIID %s found.", uuid
),
Response.Status.NOT_FOUND
)
);
return updateCategory(category, categoryData) return updateCategory(category, categoryData)
.entity( .entity(
@ -306,12 +245,36 @@ public class CategoriesApi {
@AuthorizationRequired @AuthorizationRequired
@RequiresPrivilege(CoreConstants.PRIVILEGE_ADMIN) @RequiresPrivilege(CoreConstants.PRIVILEGE_ADMIN)
@Transactional(Transactional.TxType.REQUIRED) @Transactional(Transactional.TxType.REQUIRED)
public CategoryData deleteCategory( public Response deleteCategory(
@PathParam("domainIdentifier") final String domainIdentifierParam, @PathParam("domainIdentifier") final String domainIdentifier,
@PathParam("path") final String categoryPathTokens @PathParam("path") final String categoryPath
) { ) {
// Delete only empty categories!!! final Domain domain = repository.findDomain(domainIdentifier);
throw new UnsupportedOperationException(); final Category category = findCategory(domain, categoryPath);
if (categoryManager.hasSubCategories(category)
|| categoryManager.hasObjects(category)) {
return Response
.status(Response.Status.BAD_REQUEST)
.entity(
String.format(
"Category %s of Domain %s is not empty.",
categoryPath,
domain.getDomainKey()
)
).build();
}
categoryRepository.delete(category);
return Response
.ok(
String.format(
"Category %s of Domain %s deleted successfully.",
categoryPath,
domain.getDomainKey()
)
).build();
} }
// delete update category by id @Path("/ID-{categoryId}") // delete update category by id @Path("/ID-{categoryId}")
@ -320,11 +283,28 @@ public class CategoriesApi {
@AuthorizationRequired @AuthorizationRequired
@RequiresPrivilege(CoreConstants.PRIVILEGE_ADMIN) @RequiresPrivilege(CoreConstants.PRIVILEGE_ADMIN)
@Transactional(Transactional.TxType.REQUIRED) @Transactional(Transactional.TxType.REQUIRED)
public CategoryData deleteCategory( public Response deleteCategory(
@PathParam("categoryId") final long categoryIdParam @PathParam("categoryId") final long categoryId
) { ) {
// Delete only empty categories!!! final Category category = findCategory(categoryId);
throw new UnsupportedOperationException();
if (categoryManager.hasSubCategories(category)
|| categoryManager.hasObjects(category)) {
return Response
.status(Response.Status.BAD_REQUEST)
.entity(
String.format(
"Category %d is not empty.", categoryId
)
).build();
}
return Response
.ok(
String.format(
"Category %d deleted successfully.", categoryId
)
).build();
} }
@DELETE @DELETE
@ -332,11 +312,28 @@ public class CategoriesApi {
@AuthorizationRequired @AuthorizationRequired
@RequiresPrivilege(CoreConstants.PRIVILEGE_ADMIN) @RequiresPrivilege(CoreConstants.PRIVILEGE_ADMIN)
@Transactional(Transactional.TxType.REQUIRED) @Transactional(Transactional.TxType.REQUIRED)
public CategoryData deleteCategory( public Response deleteCategory(
@PathParam("categoryId") final String categoryUuidParam @PathParam("categoryId") final String uuid
) { ) {
// Delete only empty categories!!! final Category category = findCategory(uuid);
throw new UnsupportedOperationException();
if (categoryManager.hasSubCategories(category)
|| categoryManager.hasObjects(category)) {
return Response
.status(Response.Status.BAD_REQUEST)
.entity(
String.format(
"Category %s is not empty.", uuid
)
).build();
}
return Response
.ok(
String.format(
"Category %s deleted successfully.", uuid
)
).build();
} }
@GET @GET
@ -609,4 +606,45 @@ public class CategoriesApi {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
private Category findCategory(final Domain domain, final String path) {
return categoryRepository
.findByPath(domain, path)
.orElseThrow(
() -> new WebApplicationException(
String.format(
"No category with path %s in Domain %s found.",
path,
domain.getDomainKey()
),
Response.Status.NOT_FOUND
)
);
}
private Category findCategory(final long categoryId) {
return categoryRepository
.findById(categoryId)
.orElseThrow(
() -> new WebApplicationException(
String.format(
"No Category with ID %d found.", categoryId
),
Response.Status.NOT_FOUND
)
);
}
private Category findCategory(final String uuid) {
return categoryRepository
.findByUuid(uuid)
.orElseThrow(
() -> new WebApplicationException(
String.format(
"No Category with UIID %s found.", uuid
),
Response.Status.NOT_FOUND
)
);
}
} }