use of io.gravitee.repository.management.model.Audit.AuditProperties.CATEGORY in project gravitee-management-rest-api by gravitee-io.
the class CategoryServiceImpl method create.
@Override
public CategoryEntity create(NewCategoryEntity newCategory) {
// First we prevent the duplicate category name
final List<CategoryEntity> categories = findAll();
final Optional<CategoryEntity> optionalCategory = categories.stream().filter(v -> v.getName().equals((newCategory.getName()))).findAny();
if (optionalCategory.isPresent()) {
throw new DuplicateCategoryNameException(optionalCategory.get().getName());
}
try {
// check if environment exists
String environment = GraviteeContext.getCurrentEnvironment();
this.environmentService.findById(environment);
Category category = convert(newCategory);
final Date createdAt = new Date();
category.setCreatedAt(createdAt);
category.setUpdatedAt(createdAt);
category.setEnvironmentId(environment);
category.setOrder(categories.size());
CategoryEntity createdCategory = convert(categoryRepository.create(category));
auditService.createEnvironmentAuditLog(Collections.singletonMap(CATEGORY, category.getId()), CATEGORY_CREATED, createdAt, null, category);
return createdCategory;
} catch (TechnicalException ex) {
LOGGER.error("An error occurs while trying to create category {}", newCategory.getName(), ex);
throw new TechnicalManagementException("An error occurs while trying to create category " + newCategory.getName(), ex);
}
}
Aggregations