Update methods for CategoriesApi
parent
68f5b47da6
commit
0c5c70e002
|
|
@ -29,6 +29,7 @@ import org.libreccm.core.CoreConstants;
|
||||||
import org.libreccm.security.AuthorizationRequired;
|
import org.libreccm.security.AuthorizationRequired;
|
||||||
import org.libreccm.security.RequiresPrivilege;
|
import org.libreccm.security.RequiresPrivilege;
|
||||||
|
|
||||||
|
import java.util.Objects;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
import javax.enterprise.context.RequestScoped;
|
import javax.enterprise.context.RequestScoped;
|
||||||
|
|
@ -145,17 +146,37 @@ public class CategoriesApi {
|
||||||
|
|
||||||
@PUT
|
@PUT
|
||||||
@Path("/{domainIdentifier}/{path:^[\\w\\-/]+$}")
|
@Path("/{domainIdentifier}/{path:^[\\w\\-/]+$}")
|
||||||
@Produces(MediaType.APPLICATION_JSON)
|
|
||||||
@Consumes(MediaType.APPLICATION_JSON)
|
@Consumes(MediaType.APPLICATION_JSON)
|
||||||
@AuthorizationRequired
|
@AuthorizationRequired
|
||||||
@RequiresPrivilege(CoreConstants.PRIVILEGE_ADMIN)
|
@RequiresPrivilege(CoreConstants.PRIVILEGE_ADMIN)
|
||||||
@Transactional(Transactional.TxType.REQUIRED)
|
@Transactional(Transactional.TxType.REQUIRED)
|
||||||
public CategoryData updateCategory(
|
public Response updateCategory(
|
||||||
@PathParam("domainIdentifier") final String domainIdentifierParam,
|
@PathParam("domainIdentifier") final String domainIdentifierParam,
|
||||||
@PathParam("path") final String categoryPathTokens,
|
@PathParam("path") final String categoryPath,
|
||||||
final CategoryData categoryData
|
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
|
@PUT
|
||||||
|
|
@ -165,11 +186,27 @@ public class CategoriesApi {
|
||||||
@AuthorizationRequired
|
@AuthorizationRequired
|
||||||
@RequiresPrivilege(CoreConstants.PRIVILEGE_ADMIN)
|
@RequiresPrivilege(CoreConstants.PRIVILEGE_ADMIN)
|
||||||
@Transactional(Transactional.TxType.REQUIRED)
|
@Transactional(Transactional.TxType.REQUIRED)
|
||||||
public CategoryData updateCategory(
|
public Response updateCategory(
|
||||||
@PathParam("catgoryId") final long categoryIdParam,
|
@PathParam("catgoryId") final long categoryId,
|
||||||
final CategoryData categoryData
|
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
|
@PUT
|
||||||
|
|
@ -179,11 +216,89 @@ public class CategoriesApi {
|
||||||
@AuthorizationRequired
|
@AuthorizationRequired
|
||||||
@RequiresPrivilege(CoreConstants.PRIVILEGE_ADMIN)
|
@RequiresPrivilege(CoreConstants.PRIVILEGE_ADMIN)
|
||||||
@Transactional(Transactional.TxType.REQUIRED)
|
@Transactional(Transactional.TxType.REQUIRED)
|
||||||
public CategoryData updateCategory(
|
public Response updateCategory(
|
||||||
@PathParam("catgoryUid") final String categoryIdParam,
|
@PathParam("catgoryUuid") final String uuid,
|
||||||
final CategoryData categoryData
|
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
|
@DELETE
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue