Implementation of category updating.

This commit is contained in:
Florian THIERRY
2024-03-12 14:42:57 +01:00
parent b0e682e82e
commit 94180e8efc
4 changed files with 44 additions and 1 deletions

View File

@@ -7,7 +7,9 @@ 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.CategoryEditionException;
import org.codiki.domain.category.exception.CategoryNotFoundException;
import org.codiki.domain.category.model.Category;
import org.codiki.domain.category.port.CategoryPort;
import org.springframework.stereotype.Service;
@@ -44,4 +46,21 @@ public class CategoryUseCases {
return newCategory;
}
public Category updateCategory(UUID categoryId, String name, List<UUID> subCategoryIds) {
if (isNull(name) && isEmpty(subCategoryIds)) {
throw new CategoryEditionException("no any field is filled");
}
Category categoryToUpdate = categoryPort.findById(categoryId)
.orElseThrow(() -> new CategoryNotFoundException(categoryId));
Category updatedCategory = aCategory()
.basedOn(categoryToUpdate)
.build();
categoryPort.save(updatedCategory);
return updatedCategory;
}
}