Implementation of category creation.

This commit is contained in:
Florian THIERRY
2024-03-12 14:24:17 +01:00
parent 47876cc775
commit b0e682e82e
12 changed files with 164 additions and 20 deletions

View File

@@ -0,0 +1,7 @@
package org.codiki.application.category;
import org.springframework.stereotype.Component;
@Component
public class CategoryCreationValidator {
}

View File

@@ -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;
}
}