use of io.gravitee.rest.api.model.UpdateCategoryEntity in project gravitee-management-rest-api by gravitee-io.
the class CategoryResource method updateCategory.
@PUT
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "Update the category", notes = "User must have the PORTAL_CATEGORY[UPDATE] permission to use this service")
@ApiResponses({ @ApiResponse(code = 200, message = "Category successfully updated", response = CategoryEntity.class), @ApiResponse(code = 500, message = "Internal server error") })
@Permissions({ @Permission(value = RolePermission.ENVIRONMENT_CATEGORY, acls = RolePermissionAction.UPDATE) })
public Response updateCategory(@Valid @NotNull final UpdateCategoryEntity category) {
try {
ImageUtils.verify(category.getPicture());
ImageUtils.verify(category.getBackground());
} catch (InvalidImageException e) {
throw new BadRequestException("Invalid image format");
}
CategoryEntity categoryEntity = categoryService.update(categoryId, category);
setPictures(categoryEntity, false);
return Response.ok(categoryEntity).build();
}
use of io.gravitee.rest.api.model.UpdateCategoryEntity in project gravitee-management-rest-api by gravitee-io.
the class CategoryServiceImpl method update.
@Override
public CategoryEntity update(String categoryId, UpdateCategoryEntity categoryEntity) {
try {
LOGGER.debug("Update Category {}", categoryId);
Optional<Category> optCategoryToUpdate = categoryRepository.findById(categoryId);
if (!optCategoryToUpdate.isPresent()) {
throw new CategoryNotFoundException(categoryId);
}
final Category categoryToUpdate = optCategoryToUpdate.get();
Category category = convert(categoryEntity, categoryToUpdate.getEnvironmentId());
// If no new picture and the current picture url is not the default one, keep the current picture
if (categoryEntity.getPicture() == null && categoryEntity.getPictureUrl() != null && categoryEntity.getPictureUrl().indexOf("?hash") > 0) {
category.setPicture(categoryToUpdate.getPicture());
}
final Date updatedAt = new Date();
category.setCreatedAt(categoryToUpdate.getCreatedAt());
category.setUpdatedAt(updatedAt);
CategoryEntity updatedCategory = convert(categoryRepository.update(category));
auditService.createEnvironmentAuditLog(Collections.singletonMap(CATEGORY, category.getId()), CATEGORY_UPDATED, updatedAt, categoryToUpdate, category);
return updatedCategory;
} catch (TechnicalException ex) {
LOGGER.error("An error occurs while trying to update category {}", categoryEntity.getName(), ex);
throw new TechnicalManagementException("An error occurs while trying to update category " + categoryEntity.getName(), ex);
}
}
use of io.gravitee.rest.api.model.UpdateCategoryEntity in project gravitee-management-rest-api by gravitee-io.
the class CategoryServiceImpl method update.
@Override
public List<CategoryEntity> update(final List<UpdateCategoryEntity> categoriesEntities) {
final List<CategoryEntity> savedCategories = new ArrayList<>(categoriesEntities.size());
categoriesEntities.forEach(categoryEntity -> {
try {
Optional<Category> optCategoryToUpdate = categoryRepository.findById(categoryEntity.getId());
if (optCategoryToUpdate.isPresent()) {
final Category categoryToUpdate = optCategoryToUpdate.get();
Category category = convert(categoryEntity, categoryToUpdate.getEnvironmentId());
// check if picture has been set
if (category.getPicture() == null) {
// Picture can not be updated when re-ordering categories
category.setPicture(categoryToUpdate.getPicture());
}
// check if background has been set
if (category.getBackground() == null) {
// Background can not be updated when re-ordering categories
category.setBackground(categoryToUpdate.getBackground());
}
final Date updatedAt = new Date();
category.setCreatedAt(categoryToUpdate.getCreatedAt());
category.setUpdatedAt(updatedAt);
savedCategories.add(convert(categoryRepository.update(category)));
auditService.createEnvironmentAuditLog(Collections.singletonMap(CATEGORY, category.getId()), CATEGORY_UPDATED, updatedAt, categoryToUpdate, category);
}
} catch (TechnicalException ex) {
LOGGER.error("An error occurs while trying to update category {}", categoryEntity.getName(), ex);
throw new TechnicalManagementException("An error occurs while trying to update category " + categoryEntity.getName(), ex);
}
});
return savedCategories;
}
use of io.gravitee.rest.api.model.UpdateCategoryEntity in project gravitee-management-rest-api by gravitee-io.
the class CategoryResourceTest method init.
@Before
public void init() {
mockCategory = new CategoryEntity();
mockCategory.setId(CATEGORY);
mockCategory.setName(CATEGORY);
mockCategory.setUpdatedAt(new Date());
doReturn(mockCategory).when(categoryService).findById(CATEGORY);
updateCategoryEntity = new UpdateCategoryEntity();
updateCategoryEntity.setDescription("toto");
updateCategoryEntity.setName(CATEGORY);
doReturn(mockCategory).when(categoryService).update(eq(CATEGORY), any());
}
use of io.gravitee.rest.api.model.UpdateCategoryEntity in project gravitee-management-rest-api by gravitee-io.
the class CategoryService_UpdateTest method shouldUpdateCategory_single_mode.
@Test
public void shouldUpdateCategory_single_mode() throws TechnicalException {
UpdateCategoryEntity mockCategory = mock(UpdateCategoryEntity.class);
when(mockCategory.getId()).thenReturn("category-id");
when(mockCategory.getName()).thenReturn("Category ID");
when(mockCategoryRepository.findById("category-id")).thenReturn(Optional.of(new Category()));
Category updatedCategory = mock(Category.class);
when(updatedCategory.getId()).thenReturn("category-id");
when(updatedCategory.getName()).thenReturn("category-name");
when(updatedCategory.getDescription()).thenReturn("category-description");
when(updatedCategory.getOrder()).thenReturn(1);
when(updatedCategory.isHidden()).thenReturn(true);
when(updatedCategory.getUpdatedAt()).thenReturn(new Date(1234567890L));
when(updatedCategory.getCreatedAt()).thenReturn(new Date(9876543210L));
when(mockCategoryRepository.update(argThat(cat -> cat.getUpdatedAt() != null))).thenReturn(updatedCategory);
CategoryEntity category = categoryService.update("category-id", mockCategory);
assertNotNull(category);
assertEquals("Id", "category-id", category.getId());
assertEquals("Name", "category-name", category.getName());
assertEquals("Description", "category-description", category.getDescription());
assertEquals("Total APIs", 0, category.getTotalApis());
assertEquals("Order", 1, category.getOrder());
assertEquals("Hidden", true, category.isHidden());
assertEquals("UpdatedAt", new Date(1234567890L), category.getUpdatedAt());
assertEquals("CreatedAt", new Date(9876543210L), category.getCreatedAt());
verify(mockCategoryRepository, times(1)).findById(any());
verify(mockCategoryRepository, times(1)).update(any());
verify(mockAuditService, times(1)).createEnvironmentAuditLog(any(), eq(CATEGORY_UPDATED), any(), any(), any());
}
Aggregations