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

View File

@@ -17,6 +17,13 @@ public class CategoryBuilder {
private CategoryBuilder() {}
public CategoryBuilder basedOn(Category category) {
this.id = category.id();
this.name = category.name();
this.subCategories = category.subCategories();
return this;
}
public CategoryBuilder withId(UUID id) {
this.id = id;
return this;

View File

@@ -1,12 +1,15 @@
package org.codiki.exposition.category;
import java.util.UUID;
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.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
@@ -27,4 +30,17 @@ public class CategoryController {
Category createdCategory = categoryUseCases.createCategory(request.name(), request.subCategoryIds());
return new CategoryDto(createdCategory);
}
@PutMapping("/{categoryId}")
public CategoryDto updateCategory(
@PathVariable("categoryId") UUID categoryId,
@RequestBody CategoryEditionRequest request
) {
Category createdCategory = categoryUseCases.updateCategory(
categoryId,
request.name(),
request.subCategoryIds()
);
return new CategoryDto(createdCategory);
}
}

View File

@@ -2,4 +2,5 @@ vars {
url: http://localhost:8080
publicationId: fce1de27-11c6-4deb-a248-b63288c00037
bearerToken: eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxNWExM2RjNy0wMjlkLTRlYWItYTYzZC1jMWU5NmY5MDI0MWQiLCJleHAiOjE3MTAyNTE0MzV9.0-KmVfwoyJ1JDZs-f2paEZVAljCPVkcEi33bYra4hoVSvECFsdc0CFlJKpWEeEswIv4jSsnEzs7yFW_XM9WWAA
categoryId: 25742844-f0c2-454a-a4d1-71ad3bda28df
}