Implementation of category updating.
This commit is contained in:
@@ -7,7 +7,9 @@ import java.util.Optional;
|
|||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
import static org.codiki.domain.category.model.builder.CategoryBuilder.aCategory;
|
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.CategoryEditionException;
|
||||||
|
import org.codiki.domain.category.exception.CategoryNotFoundException;
|
||||||
import org.codiki.domain.category.model.Category;
|
import org.codiki.domain.category.model.Category;
|
||||||
import org.codiki.domain.category.port.CategoryPort;
|
import org.codiki.domain.category.port.CategoryPort;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
@@ -44,4 +46,21 @@ public class CategoryUseCases {
|
|||||||
|
|
||||||
return newCategory;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,6 +17,13 @@ public class CategoryBuilder {
|
|||||||
|
|
||||||
private 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) {
|
public CategoryBuilder withId(UUID id) {
|
||||||
this.id = id;
|
this.id = id;
|
||||||
return this;
|
return this;
|
||||||
|
|||||||
@@ -1,12 +1,15 @@
|
|||||||
package org.codiki.exposition.category;
|
package org.codiki.exposition.category;
|
||||||
|
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
import static org.springframework.http.HttpStatus.CREATED;
|
import static org.springframework.http.HttpStatus.CREATED;
|
||||||
import org.codiki.application.category.CategoryUseCases;
|
import org.codiki.application.category.CategoryUseCases;
|
||||||
import org.codiki.domain.category.model.Category;
|
import org.codiki.domain.category.model.Category;
|
||||||
import org.codiki.exposition.category.model.CategoryDto;
|
import org.codiki.exposition.category.model.CategoryDto;
|
||||||
import org.codiki.exposition.category.model.CategoryEditionRequest;
|
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.PostMapping;
|
||||||
|
import org.springframework.web.bind.annotation.PutMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestBody;
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||||
@@ -27,4 +30,17 @@ public class CategoryController {
|
|||||||
Category createdCategory = categoryUseCases.createCategory(request.name(), request.subCategoryIds());
|
Category createdCategory = categoryUseCases.createCategory(request.name(), request.subCategoryIds());
|
||||||
return new CategoryDto(createdCategory);
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,4 +2,5 @@ vars {
|
|||||||
url: http://localhost:8080
|
url: http://localhost:8080
|
||||||
publicationId: fce1de27-11c6-4deb-a248-b63288c00037
|
publicationId: fce1de27-11c6-4deb-a248-b63288c00037
|
||||||
bearerToken: eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxNWExM2RjNy0wMjlkLTRlYWItYTYzZC1jMWU5NmY5MDI0MWQiLCJleHAiOjE3MTAyNTE0MzV9.0-KmVfwoyJ1JDZs-f2paEZVAljCPVkcEi33bYra4hoVSvECFsdc0CFlJKpWEeEswIv4jSsnEzs7yFW_XM9WWAA
|
bearerToken: eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxNWExM2RjNy0wMjlkLTRlYWItYTYzZC1jMWU5NmY5MDI0MWQiLCJleHAiOjE3MTAyNTE0MzV9.0-KmVfwoyJ1JDZs-f2paEZVAljCPVkcEi33bYra4hoVSvECFsdc0CFlJKpWEeEswIv4jSsnEzs7yFW_XM9WWAA
|
||||||
|
categoryId: 25742844-f0c2-454a-a4d1-71ad3bda28df
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user