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();

View File

@@ -3,9 +3,6 @@ package org.codiki.domain.publication.model;
import java.time.ZonedDateTime;
import java.util.UUID;
import org.codiki.domain.category.model.Category;
import org.codiki.domain.picture.model.Picture;
public record Publication(
UUID id,
String key,
@@ -14,7 +11,7 @@ public record Publication(
String description,
ZonedDateTime creationDate,
UUID illustrationId,
Author author,
Category category
UUID categoryId,
Author author
) {
}

View File

@@ -13,10 +13,10 @@ public class PublicationBuilder {
private String title;
private String text;
private String description;
private UUID illustrationId;
private ZonedDateTime creationDate;
private UUID illustrationId;
private UUID categoryId;
private Author author;
private Category category;
private PublicationBuilder() {}
@@ -31,10 +31,10 @@ public class PublicationBuilder {
.withTitle(publication.title())
.withText(publication.text())
.withDescription(publication.description())
.withIllustrationId(publication.illustrationId())
.withCreationDate(publication.creationDate())
.withAuthor(publication.author())
.withCategory(publication.category());
.withIllustrationId(publication.illustrationId())
.withCategoryId(publication.categoryId())
.withAuthor(publication.author());
}
public PublicationBuilder withId(UUID id) {
@@ -62,23 +62,23 @@ public class PublicationBuilder {
return this;
}
public PublicationBuilder withIllustrationId(UUID illustrationId) {
this.illustrationId = illustrationId;
return this;
}
public PublicationBuilder withCreationDate(ZonedDateTime creationDate) {
this.creationDate = creationDate;
return this;
}
public PublicationBuilder withAuthor(Author author) {
this.author = author;
public PublicationBuilder withIllustrationId(UUID illustrationId) {
this.illustrationId = illustrationId;
return this;
}
public PublicationBuilder withCategory(Category category) {
this.category = category;
public PublicationBuilder withCategoryId(UUID categoryId) {
this.categoryId = categoryId;
return this;
}
public PublicationBuilder withAuthor(Author author) {
this.author = author;
return this;
}
@@ -91,8 +91,8 @@ public class PublicationBuilder {
description,
creationDate,
illustrationId,
author,
category
categoryId,
author
);
}
}

View File

@@ -14,8 +14,8 @@ public record PublicationDto(
String description,
ZonedDateTime creationDate,
UUID illustrationId,
AuthorDto author,
CategoryDto category
UUID categoryId,
AuthorDto author
) {
public PublicationDto(Publication publication) {
this(
@@ -26,8 +26,8 @@ public record PublicationDto(
publication.description(),
publication.creationDate(),
publication.illustrationId(),
new AuthorDto(publication.author()),
new CategoryDto(publication.category())
publication.categoryId(),
new AuthorDto(publication.author())
);
}
}

View File

@@ -39,12 +39,11 @@ public class PublicationEntity {
private ZonedDateTime creationDate;
@Column(nullable = false)
private UUID illustrationId;
@Column(nullable = false)
private UUID categoryId;
@ManyToOne(fetch = LAZY)
@JoinColumn(name = "author_id")
private AuthorEntity author;
@ManyToOne(fetch = LAZY)
@JoinColumn(name = "category_id")
private CategoryEntity category;
public PublicationEntity(Publication publication) {
this(
@@ -55,8 +54,8 @@ public class PublicationEntity {
publication.description(),
publication.creationDate(),
publication.illustrationId(),
new AuthorEntity(publication.author()),
new CategoryEntity(publication.category())
publication.categoryId(),
new AuthorEntity(publication.author())
);
}
@@ -69,8 +68,8 @@ public class PublicationEntity {
description,
creationDate,
illustrationId,
author.toDomain(),
category.toDomain()
categoryId,
author.toDomain()
);
}
}

View File

@@ -13,7 +13,6 @@ public interface PublicationRepository extends JpaRepository<PublicationEntity,
SELECT p
FROM PublicationEntity p
JOIN FETCH p.author a
JOIN FETCH p.category c
WHERE p.id = :publicationId
""")
Optional<PublicationEntity> findById(@Param("publicationId") UUID publicationId);