Set up parent category mechanism.

This commit is contained in:
Florian THIERRY
2024-03-12 18:20:04 +01:00
parent a3295636b4
commit ed766d4c8c
8 changed files with 64 additions and 15 deletions

View File

@@ -38,10 +38,9 @@ public class CategoryJpaAdapter implements CategoryPort {
.map(CategoryEntity::toDomain)
.toList();
Optional<UUID> notFoundCategoryId = categories.stream()
.filter(category -> !categoryIds.contains(category.id()))
.findFirst()
.map(Category::id);
Optional<UUID> notFoundCategoryId = categoryIds.stream()
.filter(categoryId -> categories.stream().map(Category::id).noneMatch(categoryId::equals))
.findFirst();
if (notFoundCategoryId.isPresent()) {
throw new CategoryNotFoundException(notFoundCategoryId.get());
}
@@ -62,4 +61,12 @@ public class CategoryJpaAdapter implements CategoryPort {
public boolean existsById(UUID categoryId) {
return categoryRepository.existsById(categoryId);
}
@Override
public List<Category> findAll() {
return categoryRepository.findAll()
.stream()
.map(CategoryEntity::toDomain)
.toList();
}
}

View File

@@ -1,13 +1,24 @@
package org.codiki.infrastructure.category.model;
import static java.util.Collections.emptyList;
import static java.util.stream.Collectors.toSet;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;
import org.codiki.domain.category.model.Category;
import static jakarta.persistence.CascadeType.ALL;
import static jakarta.persistence.FetchType.LAZY;
import jakarta.persistence.CascadeType;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;
import lombok.AllArgsConstructor;
import lombok.Getter;
@@ -25,12 +36,18 @@ public class CategoryEntity {
private UUID id;
@Column(nullable = false)
private String name;
// List<Category> subCategories
@OneToMany
@JoinColumn(name = "parent_category_id")
private Set<CategoryEntity> subCategories;
public CategoryEntity(Category category) {
this(
category.id(),
category.name()
category.name(),
category.subCategories()
.stream()
.map(CategoryEntity::new)
.collect(toSet())
);
}
@@ -38,7 +55,9 @@ public class CategoryEntity {
return new Category(
id,
name,
emptyList()
subCategories.stream()
.map(CategoryEntity::toDomain)
.toList()
);
}
}

View File

@@ -25,8 +25,11 @@ CREATE INDEX refresh_token_fk_user_id_idx ON user_role (user_id);
CREATE TABLE IF NOT EXISTS category (
id UUID NOT NULL,
name VARCHAR NOT NULL,
CONSTRAINT category_pk PRIMARY KEY (id)
parent_category_id UUID,
CONSTRAINT category_pk PRIMARY KEY (id),
CONSTRAINT category_parent_category_id_fk FOREIGN KEY (parent_category_id) REFERENCES category (id)
);
CREATE INDEX category_parent_category_id_idx ON category (parent_category_id);
CREATE TABLE IF NOT EXISTS publication (
id UUID NOT NULL,