Change category field of publication entity to its id.
This commit is contained in:
@@ -27,6 +27,10 @@ public class CategoryUseCases {
|
|||||||
return categoryPort.findById(categoryId);
|
return categoryPort.findById(categoryId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean existsById(UUID categoryId) {
|
||||||
|
return categoryPort.existsById(categoryId);
|
||||||
|
}
|
||||||
|
|
||||||
public Category createCategory(String name, List<UUID> subCategoryIds) {
|
public Category createCategory(String name, List<UUID> subCategoryIds) {
|
||||||
if (isNull(name)) {
|
if (isNull(name)) {
|
||||||
throw new CategoryEditionException("name can not be empty");
|
throw new CategoryEditionException("name can not be empty");
|
||||||
|
|||||||
@@ -62,10 +62,11 @@ public class PublicationUseCases {
|
|||||||
User authenticatedUser = userUseCases.getAuthenticatedUser()
|
User authenticatedUser = userUseCases.getAuthenticatedUser()
|
||||||
.orElseThrow(AuthenticationRequiredException::new);
|
.orElseThrow(AuthenticationRequiredException::new);
|
||||||
|
|
||||||
Category category = categoryUseCases.findById(request.categoryId())
|
if (!categoryUseCases.existsById(request.categoryId())) {
|
||||||
.orElseThrow(() -> new PublicationEditionException(
|
throw new PublicationEditionException(
|
||||||
new CategoryNotFoundException(request.categoryId())
|
new CategoryNotFoundException(request.categoryId())
|
||||||
));
|
);
|
||||||
|
}
|
||||||
|
|
||||||
if (!pictureUseCases.existsById(request.illustrationId())) {
|
if (!pictureUseCases.existsById(request.illustrationId())) {
|
||||||
throw new PublicationEditionException(
|
throw new PublicationEditionException(
|
||||||
@@ -79,10 +80,10 @@ public class PublicationUseCases {
|
|||||||
.withTitle(request.title())
|
.withTitle(request.title())
|
||||||
.withText(request.text())
|
.withText(request.text())
|
||||||
.withDescription(request.description())
|
.withDescription(request.description())
|
||||||
.withIllustrationId(request.illustrationId())
|
|
||||||
.withCreationDate(ZonedDateTime.now(clock))
|
.withCreationDate(ZonedDateTime.now(clock))
|
||||||
|
.withIllustrationId(request.illustrationId())
|
||||||
|
.withCategoryId(request.categoryId())
|
||||||
.withAuthor(anAuthor().basedOn(authenticatedUser).build())
|
.withAuthor(anAuthor().basedOn(authenticatedUser).build())
|
||||||
.withCategory(category)
|
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
publicationPort.save(newPublication);
|
publicationPort.save(newPublication);
|
||||||
@@ -127,12 +128,13 @@ public class PublicationUseCases {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!isNull(request.categoryId())) {
|
if (!isNull(request.categoryId())) {
|
||||||
Category newCategory = categoryUseCases.findById(request.categoryId())
|
if (!categoryUseCases.existsById(request.categoryId())) {
|
||||||
.orElseThrow(() -> new PublicationEditionException(
|
throw new PublicationEditionException(
|
||||||
new CategoryNotFoundException(request.categoryId())
|
new CategoryNotFoundException(request.categoryId())
|
||||||
));
|
);
|
||||||
|
}
|
||||||
|
|
||||||
publicationBuilder.withCategory(newCategory);
|
publicationBuilder.withCategoryId(request.categoryId());
|
||||||
}
|
}
|
||||||
|
|
||||||
Publication updatedPublication = publicationBuilder.build();
|
Publication updatedPublication = publicationBuilder.build();
|
||||||
|
|||||||
@@ -3,9 +3,6 @@ package org.codiki.domain.publication.model;
|
|||||||
import java.time.ZonedDateTime;
|
import java.time.ZonedDateTime;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
import org.codiki.domain.category.model.Category;
|
|
||||||
import org.codiki.domain.picture.model.Picture;
|
|
||||||
|
|
||||||
public record Publication(
|
public record Publication(
|
||||||
UUID id,
|
UUID id,
|
||||||
String key,
|
String key,
|
||||||
@@ -14,7 +11,7 @@ public record Publication(
|
|||||||
String description,
|
String description,
|
||||||
ZonedDateTime creationDate,
|
ZonedDateTime creationDate,
|
||||||
UUID illustrationId,
|
UUID illustrationId,
|
||||||
Author author,
|
UUID categoryId,
|
||||||
Category category
|
Author author
|
||||||
) {
|
) {
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,10 +13,10 @@ public class PublicationBuilder {
|
|||||||
private String title;
|
private String title;
|
||||||
private String text;
|
private String text;
|
||||||
private String description;
|
private String description;
|
||||||
private UUID illustrationId;
|
|
||||||
private ZonedDateTime creationDate;
|
private ZonedDateTime creationDate;
|
||||||
|
private UUID illustrationId;
|
||||||
|
private UUID categoryId;
|
||||||
private Author author;
|
private Author author;
|
||||||
private Category category;
|
|
||||||
|
|
||||||
private PublicationBuilder() {}
|
private PublicationBuilder() {}
|
||||||
|
|
||||||
@@ -31,10 +31,10 @@ public class PublicationBuilder {
|
|||||||
.withTitle(publication.title())
|
.withTitle(publication.title())
|
||||||
.withText(publication.text())
|
.withText(publication.text())
|
||||||
.withDescription(publication.description())
|
.withDescription(publication.description())
|
||||||
.withIllustrationId(publication.illustrationId())
|
|
||||||
.withCreationDate(publication.creationDate())
|
.withCreationDate(publication.creationDate())
|
||||||
.withAuthor(publication.author())
|
.withIllustrationId(publication.illustrationId())
|
||||||
.withCategory(publication.category());
|
.withCategoryId(publication.categoryId())
|
||||||
|
.withAuthor(publication.author());
|
||||||
}
|
}
|
||||||
|
|
||||||
public PublicationBuilder withId(UUID id) {
|
public PublicationBuilder withId(UUID id) {
|
||||||
@@ -62,23 +62,23 @@ public class PublicationBuilder {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public PublicationBuilder withIllustrationId(UUID illustrationId) {
|
|
||||||
this.illustrationId = illustrationId;
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public PublicationBuilder withCreationDate(ZonedDateTime creationDate) {
|
public PublicationBuilder withCreationDate(ZonedDateTime creationDate) {
|
||||||
this.creationDate = creationDate;
|
this.creationDate = creationDate;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public PublicationBuilder withAuthor(Author author) {
|
public PublicationBuilder withIllustrationId(UUID illustrationId) {
|
||||||
this.author = author;
|
this.illustrationId = illustrationId;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public PublicationBuilder withCategory(Category category) {
|
public PublicationBuilder withCategoryId(UUID categoryId) {
|
||||||
this.category = category;
|
this.categoryId = categoryId;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public PublicationBuilder withAuthor(Author author) {
|
||||||
|
this.author = author;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -91,8 +91,8 @@ public class PublicationBuilder {
|
|||||||
description,
|
description,
|
||||||
creationDate,
|
creationDate,
|
||||||
illustrationId,
|
illustrationId,
|
||||||
author,
|
categoryId,
|
||||||
category
|
author
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,8 +14,8 @@ public record PublicationDto(
|
|||||||
String description,
|
String description,
|
||||||
ZonedDateTime creationDate,
|
ZonedDateTime creationDate,
|
||||||
UUID illustrationId,
|
UUID illustrationId,
|
||||||
AuthorDto author,
|
UUID categoryId,
|
||||||
CategoryDto category
|
AuthorDto author
|
||||||
) {
|
) {
|
||||||
public PublicationDto(Publication publication) {
|
public PublicationDto(Publication publication) {
|
||||||
this(
|
this(
|
||||||
@@ -26,8 +26,8 @@ public record PublicationDto(
|
|||||||
publication.description(),
|
publication.description(),
|
||||||
publication.creationDate(),
|
publication.creationDate(),
|
||||||
publication.illustrationId(),
|
publication.illustrationId(),
|
||||||
new AuthorDto(publication.author()),
|
publication.categoryId(),
|
||||||
new CategoryDto(publication.category())
|
new AuthorDto(publication.author())
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -39,12 +39,11 @@ public class PublicationEntity {
|
|||||||
private ZonedDateTime creationDate;
|
private ZonedDateTime creationDate;
|
||||||
@Column(nullable = false)
|
@Column(nullable = false)
|
||||||
private UUID illustrationId;
|
private UUID illustrationId;
|
||||||
|
@Column(nullable = false)
|
||||||
|
private UUID categoryId;
|
||||||
@ManyToOne(fetch = LAZY)
|
@ManyToOne(fetch = LAZY)
|
||||||
@JoinColumn(name = "author_id")
|
@JoinColumn(name = "author_id")
|
||||||
private AuthorEntity author;
|
private AuthorEntity author;
|
||||||
@ManyToOne(fetch = LAZY)
|
|
||||||
@JoinColumn(name = "category_id")
|
|
||||||
private CategoryEntity category;
|
|
||||||
|
|
||||||
public PublicationEntity(Publication publication) {
|
public PublicationEntity(Publication publication) {
|
||||||
this(
|
this(
|
||||||
@@ -55,8 +54,8 @@ public class PublicationEntity {
|
|||||||
publication.description(),
|
publication.description(),
|
||||||
publication.creationDate(),
|
publication.creationDate(),
|
||||||
publication.illustrationId(),
|
publication.illustrationId(),
|
||||||
new AuthorEntity(publication.author()),
|
publication.categoryId(),
|
||||||
new CategoryEntity(publication.category())
|
new AuthorEntity(publication.author())
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -69,8 +68,8 @@ public class PublicationEntity {
|
|||||||
description,
|
description,
|
||||||
creationDate,
|
creationDate,
|
||||||
illustrationId,
|
illustrationId,
|
||||||
author.toDomain(),
|
categoryId,
|
||||||
category.toDomain()
|
author.toDomain()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,7 +13,6 @@ public interface PublicationRepository extends JpaRepository<PublicationEntity,
|
|||||||
SELECT p
|
SELECT p
|
||||||
FROM PublicationEntity p
|
FROM PublicationEntity p
|
||||||
JOIN FETCH p.author a
|
JOIN FETCH p.author a
|
||||||
JOIN FETCH p.category c
|
|
||||||
WHERE p.id = :publicationId
|
WHERE p.id = :publicationId
|
||||||
""")
|
""")
|
||||||
Optional<PublicationEntity> findById(@Param("publicationId") UUID publicationId);
|
Optional<PublicationEntity> findById(@Param("publicationId") UUID publicationId);
|
||||||
|
|||||||
Reference in New Issue
Block a user