diff --git a/client/src/main/java/com/gdevxy/blog/client/contentful/ContentfulClient.java b/client/src/main/java/com/gdevxy/blog/client/contentful/ContentfulClient.java index 72a9af7..1287d6a 100644 --- a/client/src/main/java/com/gdevxy/blog/client/contentful/ContentfulClient.java +++ b/client/src/main/java/com/gdevxy/blog/client/contentful/ContentfulClient.java @@ -1,5 +1,6 @@ package com.gdevxy.blog.client.contentful; +import com.gdevxy.blog.client.contentful.model.ComponentRichImage; import com.gdevxy.blog.client.contentful.model.FeaturedImage; import com.gdevxy.blog.client.contentful.model.PageBlogPost; import com.gdevxy.blog.client.contentful.model.PageBlogPostCollection; @@ -12,6 +13,6 @@ public interface ContentfulClient { PageBlogPostCollection findBlogPosts(); - Optional findImage(String id); + Optional findImage(String id); } diff --git a/client/src/main/java/com/gdevxy/blog/client/contentful/DefaultContentfulClient.java b/client/src/main/java/com/gdevxy/blog/client/contentful/DefaultContentfulClient.java index 109d09f..d4b2dbd 100644 --- a/client/src/main/java/com/gdevxy/blog/client/contentful/DefaultContentfulClient.java +++ b/client/src/main/java/com/gdevxy/blog/client/contentful/DefaultContentfulClient.java @@ -1,19 +1,18 @@ package com.gdevxy.blog.client.contentful; -import com.fasterxml.jackson.databind.ObjectMapper; +import java.util.Map; +import java.util.Optional; + +import jakarta.enterprise.context.ApplicationScoped; + import com.gdevxy.blog.client.GraphQlQueryLoader; import com.gdevxy.blog.client.contentful.model.ComponentRichImage; -import com.gdevxy.blog.client.contentful.model.FeaturedImage; import com.gdevxy.blog.client.contentful.model.PageBlogPost; import com.gdevxy.blog.client.contentful.model.PageBlogPostCollection; import io.smallrye.graphql.client.GraphQLClient; import io.smallrye.graphql.client.dynamic.api.DynamicGraphQLClient; -import jakarta.enterprise.context.ApplicationScoped; import lombok.extern.slf4j.Slf4j; -import java.util.Map; -import java.util.Optional; - @Slf4j @DefaultContentful @ApplicationScoped @@ -43,11 +42,11 @@ public PageBlogPostCollection findBlogPosts() { } @Override - public Optional findImage(String id) { + public Optional findImage(String id) { var response = executeQuery(() -> queryLoader.loadQuery("find-image"), Map.of("id", id)); - return Optional.ofNullable(asClass(response, ComponentRichImage.class)).map(ComponentRichImage::getImage); + return Optional.ofNullable(asClass(response, ComponentRichImage.class)); } } diff --git a/client/src/main/java/com/gdevxy/blog/client/contentful/PreviewContentfulClient.java b/client/src/main/java/com/gdevxy/blog/client/contentful/PreviewContentfulClient.java index 254a6dc..0ac1784 100644 --- a/client/src/main/java/com/gdevxy/blog/client/contentful/PreviewContentfulClient.java +++ b/client/src/main/java/com/gdevxy/blog/client/contentful/PreviewContentfulClient.java @@ -43,12 +43,12 @@ public PageBlogPostCollection findBlogPosts() { } @Override - public Optional findImage(String id) { + public Optional findImage(String id) { var params = Map.of("preview", true, "id", id); var response = executeQuery(() -> queryLoader.loadQuery("find-image"), params); - return Optional.ofNullable(asClass(response, ComponentRichImage.class)).map(ComponentRichImage::getImage); + return Optional.ofNullable(asClass(response, ComponentRichImage.class)); } } diff --git a/client/src/main/java/com/gdevxy/blog/client/contentful/model/ComponentRichImage.java b/client/src/main/java/com/gdevxy/blog/client/contentful/model/ComponentRichImage.java index c5ee90c..f668525 100644 --- a/client/src/main/java/com/gdevxy/blog/client/contentful/model/ComponentRichImage.java +++ b/client/src/main/java/com/gdevxy/blog/client/contentful/model/ComponentRichImage.java @@ -13,5 +13,6 @@ public class ComponentRichImage { private FeaturedImage image; + private Boolean fullWidth; } \ No newline at end of file diff --git a/client/src/main/resources/graphql/find-image.graphql b/client/src/main/resources/graphql/find-image.graphql index 69fe1b6..1cfdfba 100644 --- a/client/src/main/resources/graphql/find-image.graphql +++ b/client/src/main/resources/graphql/find-image.graphql @@ -7,5 +7,6 @@ query findImage($id: String!){ height url } + fullWidth } } diff --git a/component/src/main/resources/templates/blog/blog-image.html b/component/src/main/resources/templates/blog/blog-image.html index 7588340..0d7f01a 100644 --- a/component/src/main/resources/templates/blog/blog-image.html +++ b/component/src/main/resources/templates/blog/blog-image.html @@ -1,3 +1,7 @@
- {image.title} + {#if image.fullWidth} + {image.title} + {#else} + {image.title} + {/if}
diff --git a/model/src/main/java/com/gdevxy/blog/model/Image.java b/model/src/main/java/com/gdevxy/blog/model/Image.java index b798e68..6af5476 100644 --- a/model/src/main/java/com/gdevxy/blog/model/Image.java +++ b/model/src/main/java/com/gdevxy/blog/model/Image.java @@ -5,7 +5,7 @@ import lombok.ToString; @Getter -@Builder +@Builder(toBuilder = true) @ToString public class Image { @@ -15,5 +15,6 @@ public class Image { private final Integer width; private final Integer height; private final String contentType; + private final Boolean fullWidth; } diff --git a/service/src/main/java/com/gdevxy/blog/service/contentful/blogpost/BlogPostService.java b/service/src/main/java/com/gdevxy/blog/service/contentful/blogpost/BlogPostService.java index 767ae7e..5aa1e8b 100644 --- a/service/src/main/java/com/gdevxy/blog/service/contentful/blogpost/BlogPostService.java +++ b/service/src/main/java/com/gdevxy/blog/service/contentful/blogpost/BlogPostService.java @@ -5,6 +5,8 @@ import com.gdevxy.blog.client.contentful.PreviewContentful; import com.gdevxy.blog.model.BlogPost; import com.gdevxy.blog.service.contentful.ContentfulServiceSupport; +import com.gdevxy.blog.service.contentful.blogpost.converter.BlogPostConverter; + import jakarta.enterprise.context.ApplicationScoped; import java.util.List; diff --git a/service/src/main/java/com/gdevxy/blog/service/contentful/blogpost/BlogPostConverter.java b/service/src/main/java/com/gdevxy/blog/service/contentful/blogpost/converter/BlogPostConverter.java similarity index 95% rename from service/src/main/java/com/gdevxy/blog/service/contentful/blogpost/BlogPostConverter.java rename to service/src/main/java/com/gdevxy/blog/service/contentful/blogpost/converter/BlogPostConverter.java index 79a0265..290b3e5 100644 --- a/service/src/main/java/com/gdevxy/blog/service/contentful/blogpost/BlogPostConverter.java +++ b/service/src/main/java/com/gdevxy/blog/service/contentful/blogpost/converter/BlogPostConverter.java @@ -1,4 +1,4 @@ -package com.gdevxy.blog.service.contentful.blogpost; +package com.gdevxy.blog.service.contentful.blogpost.converter; import com.gdevxy.blog.client.contentful.ContentfulClient; import com.gdevxy.blog.client.contentful.model.PageBlogPost; @@ -9,7 +9,7 @@ import com.gdevxy.blog.model.BlogPost; import com.gdevxy.blog.model.Image; import com.gdevxy.blog.model.contentful.Node; -import com.gdevxy.blog.service.contentful.image.ImageConverter; + import jakarta.annotation.Nullable; import jakarta.enterprise.context.ApplicationScoped; import lombok.RequiredArgsConstructor; @@ -25,6 +25,7 @@ public class BlogPostConverter { private final ImageConverter imageConverter; + private final RichImageConverter richImageConverter; public BlogPost convert(PageBlogPost p) { @@ -94,7 +95,7 @@ private Optional toImage(@Nullable ContentfulClient client, Node nodeType return Optional.empty(); } - return client.findImage(embeddedEntry.getTarget().getSys().getId()).map(imageConverter); + return client.findImage(embeddedEntry.getTarget().getSys().getId()).map(richImageConverter); } } diff --git a/service/src/main/java/com/gdevxy/blog/service/contentful/image/ImageConverter.java b/service/src/main/java/com/gdevxy/blog/service/contentful/blogpost/converter/ImageConverter.java similarity index 57% rename from service/src/main/java/com/gdevxy/blog/service/contentful/image/ImageConverter.java rename to service/src/main/java/com/gdevxy/blog/service/contentful/blogpost/converter/ImageConverter.java index 29631b3..e228512 100644 --- a/service/src/main/java/com/gdevxy/blog/service/contentful/image/ImageConverter.java +++ b/service/src/main/java/com/gdevxy/blog/service/contentful/blogpost/converter/ImageConverter.java @@ -1,23 +1,24 @@ -package com.gdevxy.blog.service.contentful.image; +package com.gdevxy.blog.service.contentful.blogpost.converter; + +import java.util.function.Function; -import com.gdevxy.blog.client.contentful.model.FeaturedImage; -import com.gdevxy.blog.model.Image; import jakarta.enterprise.context.ApplicationScoped; -import java.util.function.Function; +import com.gdevxy.blog.client.contentful.model.FeaturedImage; +import com.gdevxy.blog.model.Image; @ApplicationScoped public class ImageConverter implements Function { @Override - public Image apply(FeaturedImage fi) { + public Image apply(FeaturedImage image) { return Image.builder() - .url(fi.getUrl()) - .width(fi.getWidth()) - .height(fi.getHeight()) - .contentType(fi.getContentType()) - .build(); + .url(image.getUrl()) + .width(image.getWidth()) + .height(image.getHeight()) + .contentType(image.getContentType()) + .build(); } } diff --git a/service/src/main/java/com/gdevxy/blog/service/contentful/blogpost/converter/RichImageConverter.java b/service/src/main/java/com/gdevxy/blog/service/contentful/blogpost/converter/RichImageConverter.java new file mode 100644 index 0000000..4877af7 --- /dev/null +++ b/service/src/main/java/com/gdevxy/blog/service/contentful/blogpost/converter/RichImageConverter.java @@ -0,0 +1,25 @@ +package com.gdevxy.blog.service.contentful.blogpost.converter; + +import java.util.function.Function; + +import jakarta.enterprise.context.ApplicationScoped; + +import com.gdevxy.blog.client.contentful.model.ComponentRichImage; +import com.gdevxy.blog.model.Image; +import lombok.RequiredArgsConstructor; + +@ApplicationScoped +@RequiredArgsConstructor +public class RichImageConverter implements Function { + + private final ImageConverter imageConverter; + + @Override + public Image apply(ComponentRichImage i) { + + return imageConverter.apply(i.getImage()).toBuilder() + .fullWidth(i.getFullWidth()) + .build(); + } + +} diff --git a/service/src/main/java/com/gdevxy/blog/service/contentful/image/ImageService.java b/service/src/main/java/com/gdevxy/blog/service/contentful/image/ImageService.java deleted file mode 100644 index c8ab526..0000000 --- a/service/src/main/java/com/gdevxy/blog/service/contentful/image/ImageService.java +++ /dev/null @@ -1,30 +0,0 @@ -package com.gdevxy.blog.service.contentful.image; - -import com.gdevxy.blog.client.contentful.ContentfulClient; -import com.gdevxy.blog.client.contentful.DefaultContentful; -import com.gdevxy.blog.client.contentful.PreviewContentful; -import com.gdevxy.blog.client.contentful.model.FeaturedImage; -import com.gdevxy.blog.model.Image; -import com.gdevxy.blog.service.contentful.ContentfulServiceSupport; -import jakarta.enterprise.context.ApplicationScoped; - -import java.util.Optional; - -@ApplicationScoped -public class ImageService extends ContentfulServiceSupport { - - private final ImageConverter imageConverter; - - public ImageService(@DefaultContentful ContentfulClient contentfulClient, - @PreviewContentful ContentfulClient previewClient, - ImageConverter imageConverter) { - super(contentfulClient, previewClient); - this.imageConverter = imageConverter; - } - - public Optional findImage(Boolean preview, String id) { - - return client(preview).findImage(id).map(imageConverter); - } - -}