Implementation of category creation.
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
package org.codiki.application.category;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class CategoryCreationValidator {
|
||||
}
|
||||
@@ -1,8 +1,13 @@
|
||||
package org.codiki.application.category;
|
||||
|
||||
import static java.util.Collections.emptyList;
|
||||
import static java.util.Objects.isNull;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
import static org.codiki.domain.category.model.builder.CategoryBuilder.aCategory;
|
||||
import org.codiki.domain.category.exception.CategoryEditionException;
|
||||
import org.codiki.domain.category.model.Category;
|
||||
import org.codiki.domain.category.port.CategoryPort;
|
||||
import org.springframework.stereotype.Service;
|
||||
@@ -18,4 +23,25 @@ public class CategoryUseCases {
|
||||
public Optional<Category> findById(UUID categoryId) {
|
||||
return categoryPort.findById(categoryId);
|
||||
}
|
||||
|
||||
public Category createCategory(String name, List<UUID> subCategoryIds) {
|
||||
if (isNull(name)) {
|
||||
throw new CategoryEditionException("name can not be empty");
|
||||
}
|
||||
|
||||
List<Category> subCategories = emptyList();
|
||||
if (!isNull(subCategoryIds)) {
|
||||
subCategories = categoryPort.findAllByIds(subCategoryIds);
|
||||
}
|
||||
|
||||
Category newCategory = aCategory()
|
||||
.withId(UUID.randomUUID())
|
||||
.withName(name)
|
||||
.withSubCategories(subCategories)
|
||||
.build();
|
||||
|
||||
categoryPort.save(newCategory);
|
||||
|
||||
return newCategory;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user