Change category field of publication entity to its id.

This commit is contained in:
Florian THIERRY
2024-03-14 14:06:16 +01:00
parent 5d267111a9
commit 9fcfc04117
7 changed files with 43 additions and 42 deletions

View File

@@ -27,6 +27,10 @@ public class CategoryUseCases {
return categoryPort.findById(categoryId);
}
public boolean existsById(UUID categoryId) {
return categoryPort.existsById(categoryId);
}
public Category createCategory(String name, List<UUID> subCategoryIds) {
if (isNull(name)) {
throw new CategoryEditionException("name can not be empty");

View File

@@ -62,10 +62,11 @@ public class PublicationUseCases {
User authenticatedUser = userUseCases.getAuthenticatedUser()
.orElseThrow(AuthenticationRequiredException::new);
Category category = categoryUseCases.findById(request.categoryId())
.orElseThrow(() -> new PublicationEditionException(
if (!categoryUseCases.existsById(request.categoryId())) {
throw new PublicationEditionException(
new CategoryNotFoundException(request.categoryId())
));
);
}
if (!pictureUseCases.existsById(request.illustrationId())) {
throw new PublicationEditionException(
@@ -79,10 +80,10 @@ public class PublicationUseCases {
.withTitle(request.title())
.withText(request.text())
.withDescription(request.description())
.withIllustrationId(request.illustrationId())
.withCreationDate(ZonedDateTime.now(clock))
.withIllustrationId(request.illustrationId())
.withCategoryId(request.categoryId())
.withAuthor(anAuthor().basedOn(authenticatedUser).build())
.withCategory(category)
.build();
publicationPort.save(newPublication);
@@ -127,12 +128,13 @@ public class PublicationUseCases {
}
if (!isNull(request.categoryId())) {
Category newCategory = categoryUseCases.findById(request.categoryId())
.orElseThrow(() -> new PublicationEditionException(
if (!categoryUseCases.existsById(request.categoryId())) {
throw new PublicationEditionException(
new CategoryNotFoundException(request.categoryId())
));
);
}
publicationBuilder.withCategory(newCategory);
publicationBuilder.withCategoryId(request.categoryId());
}
Publication updatedPublication = publicationBuilder.build();