Fix and polish category CRUD.
This commit is contained in:
@@ -7,10 +7,11 @@ import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
import static org.codiki.domain.category.model.builder.CategoryBuilder.aCategory;
|
||||
import static org.springframework.util.CollectionUtils.isEmpty;
|
||||
import org.codiki.domain.category.exception.CategoryDeletionException;
|
||||
import org.codiki.domain.category.exception.CategoryEditionException;
|
||||
import org.codiki.domain.category.exception.CategoryNotFoundException;
|
||||
import org.codiki.domain.category.model.Category;
|
||||
import org.codiki.domain.category.model.builder.CategoryBuilder;
|
||||
import org.codiki.domain.category.port.CategoryPort;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@@ -33,7 +34,11 @@ public class CategoryUseCases {
|
||||
|
||||
List<Category> subCategories = emptyList();
|
||||
if (!isNull(subCategoryIds)) {
|
||||
subCategories = categoryPort.findAllByIds(subCategoryIds);
|
||||
try {
|
||||
subCategories = categoryPort.findAllByIds(subCategoryIds);
|
||||
} catch (CategoryNotFoundException exception) {
|
||||
throw new CategoryEditionException(exception);
|
||||
}
|
||||
}
|
||||
|
||||
Category newCategory = aCategory()
|
||||
@@ -48,19 +53,44 @@ public class CategoryUseCases {
|
||||
}
|
||||
|
||||
public Category updateCategory(UUID categoryId, String name, List<UUID> subCategoryIds) {
|
||||
if (isNull(name) && isEmpty(subCategoryIds)) {
|
||||
if (isNull(name) && isNull(subCategoryIds)) {
|
||||
throw new CategoryEditionException("no any field is filled");
|
||||
}
|
||||
|
||||
Category categoryToUpdate = categoryPort.findById(categoryId)
|
||||
.orElseThrow(() -> new CategoryNotFoundException(categoryId));
|
||||
|
||||
Category updatedCategory = aCategory()
|
||||
.basedOn(categoryToUpdate)
|
||||
.build();
|
||||
CategoryBuilder categoryBuilder = aCategory()
|
||||
.basedOn(categoryToUpdate);
|
||||
|
||||
if (!isNull(name)) {
|
||||
categoryBuilder.withName(name);
|
||||
}
|
||||
|
||||
if (!isNull(subCategoryIds)) {
|
||||
List<Category> subCategories;
|
||||
try {
|
||||
subCategories = categoryPort.findAllByIds(subCategoryIds);
|
||||
} catch (CategoryNotFoundException exception) {
|
||||
throw new CategoryEditionException(exception);
|
||||
}
|
||||
categoryBuilder.withSubCategories(subCategories);
|
||||
}
|
||||
|
||||
Category updatedCategory = categoryBuilder.build();
|
||||
categoryPort.save(updatedCategory);
|
||||
|
||||
return updatedCategory;
|
||||
}
|
||||
|
||||
public void deleteCategory(UUID categoryId) {
|
||||
if (!categoryPort.existsById(categoryId)) {
|
||||
throw new CategoryNotFoundException(categoryId);
|
||||
}
|
||||
|
||||
if (categoryPort.existsAnyAssociatedPublication(categoryId)) {
|
||||
throw new CategoryDeletionException(categoryId, "some publications are associated to the category");
|
||||
}
|
||||
|
||||
categoryPort.deleteById(categoryId);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user