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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
package org.codiki.domain.category.exception;
|
||||
|
||||
import org.codiki.domain.exception.FunctionnalException;
|
||||
|
||||
public class CategoryEditionException extends FunctionnalException {
|
||||
public CategoryEditionException(String reason) {
|
||||
super(String.format("Impossible to edit a category because : %s.", reason));
|
||||
}
|
||||
|
||||
public CategoryEditionException(FunctionnalException cause) {
|
||||
super("Impossible to edit a category due to a root cause.", cause);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package org.codiki.domain.category.model.builder;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.codiki.domain.category.model.Category;
|
||||
|
||||
public class CategoryBuilder {
|
||||
private UUID id;
|
||||
private String name;
|
||||
private List<Category> subCategories;
|
||||
|
||||
public static CategoryBuilder aCategory() {
|
||||
return new CategoryBuilder();
|
||||
}
|
||||
|
||||
private CategoryBuilder() {}
|
||||
|
||||
public CategoryBuilder withId(UUID id) {
|
||||
this.id = id;
|
||||
return this;
|
||||
}
|
||||
|
||||
public CategoryBuilder withName(String name) {
|
||||
this.name = name;
|
||||
return this;
|
||||
}
|
||||
|
||||
public CategoryBuilder withSubCategories(List<Category> subCategories) {
|
||||
this.subCategories = subCategories;
|
||||
return this;
|
||||
}
|
||||
|
||||
public CategoryBuilder withSubCategory(Category subCategory) {
|
||||
if (subCategories == null) {
|
||||
subCategories = new ArrayList<>();
|
||||
}
|
||||
subCategories.add(subCategory);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Category build() {
|
||||
return new Category(id, name, subCategories);
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,15 @@
|
||||
package org.codiki.domain.category.port;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.codiki.domain.category.model.Category;
|
||||
|
||||
public interface CategoryPort {
|
||||
Optional<Category> findById(final UUID uuid);
|
||||
Optional<Category> findById(UUID uuid);
|
||||
|
||||
void save(Category category);
|
||||
|
||||
List<Category> findAllByIds(List<UUID> subCategoryIds);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
package org.codiki.exposition.category;
|
||||
|
||||
import static org.springframework.http.HttpStatus.CREATED;
|
||||
import org.codiki.application.category.CategoryUseCases;
|
||||
import org.codiki.domain.category.model.Category;
|
||||
import org.codiki.exposition.category.model.CategoryDto;
|
||||
import org.codiki.exposition.category.model.CategoryEditionRequest;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/categories")
|
||||
public class CategoryController {
|
||||
private final CategoryUseCases categoryUseCases;
|
||||
|
||||
public CategoryController(CategoryUseCases categoryUseCases) {
|
||||
this.categoryUseCases = categoryUseCases;
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
@ResponseStatus(CREATED)
|
||||
public CategoryDto createCategory(@RequestBody CategoryEditionRequest request) {
|
||||
Category createdCategory = categoryUseCases.createCategory(request.name(), request.subCategoryIds());
|
||||
return new CategoryDto(createdCategory);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package org.codiki.exposition.category.model;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
public record CategoryEditionRequest(
|
||||
String name,
|
||||
List<UUID> subCategoryIds
|
||||
) {}
|
||||
@@ -4,6 +4,7 @@ import static org.springframework.http.HttpStatus.BAD_REQUEST;
|
||||
import static org.springframework.http.HttpStatus.FORBIDDEN;
|
||||
import static org.springframework.http.HttpStatus.NOT_FOUND;
|
||||
import static org.springframework.http.HttpStatus.UNAUTHORIZED;
|
||||
import org.codiki.domain.category.exception.CategoryEditionException;
|
||||
import org.codiki.domain.category.exception.CategoryNotFoundException;
|
||||
import org.codiki.domain.exception.LoginFailureException;
|
||||
import org.codiki.domain.exception.RefreshTokenDoesNotExistException;
|
||||
@@ -66,4 +67,10 @@ public class GlobalControllerExceptionHandler {
|
||||
public void handlePublicationUpdateForbiddenException() {
|
||||
// Do nothing.
|
||||
}
|
||||
|
||||
@ResponseStatus(BAD_REQUEST)
|
||||
@ExceptionHandler(CategoryEditionException.class)
|
||||
public void handleCategoryEditionException() {
|
||||
// Do nothing.
|
||||
}
|
||||
}
|
||||
|
||||
@@ -48,6 +48,10 @@ public class SecurityConfiguration {
|
||||
"/api/users/login",
|
||||
"/api/users/refresh-token"
|
||||
).permitAll()
|
||||
.requestMatchers(
|
||||
POST,
|
||||
"/api/categories"
|
||||
).hasRole("ADMIN")
|
||||
.requestMatchers(OPTIONS).permitAll()
|
||||
.anyRequest().authenticated()
|
||||
);
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package org.codiki.infrastructure.category;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
@@ -22,4 +23,18 @@ public class CategoryJpaAdapter implements CategoryPort {
|
||||
return categoryRepository.findById(categoryId)
|
||||
.map(CategoryEntity::toDomain);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void save(Category category) {
|
||||
CategoryEntity categoryEntity = new CategoryEntity(category);
|
||||
categoryRepository.save(categoryEntity);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Category> findAllByIds(final List<UUID> subCategoryIds) {
|
||||
return categoryRepository.findAllById(subCategoryIds)
|
||||
.stream()
|
||||
.map(CategoryEntity::toDomain)
|
||||
.toList();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,18 +0,0 @@
|
||||
meta {
|
||||
name: Login
|
||||
type: http
|
||||
seq: 1
|
||||
}
|
||||
|
||||
post {
|
||||
url: {{url}}/api/users/login
|
||||
body: json
|
||||
auth: none
|
||||
}
|
||||
|
||||
body:json {
|
||||
{
|
||||
"id": "5ad462b8-8f9e-4a26-bb86-c74fef5d11b6",
|
||||
"password": "password"
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
vars {
|
||||
url: http://localhost:8080
|
||||
publicationId: fce1de27-11c6-4deb-a248-b63288c00037
|
||||
bearerToken: eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI1YWQ0NjJiOC04ZjllLTRhMjYtYmI4Ni1jNzRmZWY1ZDExYjYiLCJleHAiOjE3MTAyNDk1MjJ9.gKS4h4sWXlFn4DImsXk6NDa2wEz8ZpG0qoX-IaGPHHaMJObds4qVqK91WPgrVQ6Ci0_W6wCoDImLrrPEDgtJag
|
||||
bearerToken: eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxNWExM2RjNy0wMjlkLTRlYWItYTYzZC1jMWU5NmY5MDI0MWQiLCJleHAiOjE3MTAyNTE0MzV9.0-KmVfwoyJ1JDZs-f2paEZVAljCPVkcEi33bYra4hoVSvECFsdc0CFlJKpWEeEswIv4jSsnEzs7yFW_XM9WWAA
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user