Skip to content

Commit

Permalink
Enhanced blog-image usage
Browse files Browse the repository at this point in the history
  • Loading branch information
gdevxy committed Nov 29, 2024
1 parent 3dfce20 commit 63cf4d6
Show file tree
Hide file tree
Showing 12 changed files with 62 additions and 56 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -12,6 +13,6 @@ public interface ContentfulClient {

PageBlogPostCollection findBlogPosts();

Optional<FeaturedImage> findImage(String id);
Optional<ComponentRichImage> findImage(String id);

}
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -43,11 +42,11 @@ public PageBlogPostCollection findBlogPosts() {
}

@Override
public Optional<FeaturedImage> findImage(String id) {
public Optional<ComponentRichImage> 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));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ public PageBlogPostCollection findBlogPosts() {
}

@Override
public Optional<FeaturedImage> findImage(String id) {
public Optional<ComponentRichImage> findImage(String id) {

var params = Map.<String, Object>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));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@
public class ComponentRichImage {

private FeaturedImage image;
private Boolean fullWidth;

}
1 change: 1 addition & 0 deletions client/src/main/resources/graphql/find-image.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ query findImage($id: String!){
height
url
}
fullWidth
}
}
6 changes: 5 additions & 1 deletion component/src/main/resources/templates/blog/blog-image.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
<div class="py-4">
<img src="{image.url}" style="width: 100%; height: 250px; object-fit: cover; object-position: 50% 50%;" alt="{image.title}">
{#if image.fullWidth}
<img src="{image.url}" style="width: 100%; object-fit: cover; object-position: 50% 50%;" alt="{image.title}">
{#else}
<img src="{image.url}" style="width: {image.width}px; height: {image.height};" alt="{image.title}">
{/if}
</div>
3 changes: 2 additions & 1 deletion model/src/main/java/com/gdevxy/blog/model/Image.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import lombok.ToString;

@Getter
@Builder
@Builder(toBuilder = true)
@ToString
public class Image {

Expand All @@ -15,5 +15,6 @@ public class Image {
private final Integer width;
private final Integer height;
private final String contentType;
private final Boolean fullWidth;

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -25,6 +25,7 @@
public class BlogPostConverter {

private final ImageConverter imageConverter;
private final RichImageConverter richImageConverter;

public BlogPost convert(PageBlogPost p) {

Expand Down Expand Up @@ -94,7 +95,7 @@ private Optional<Image> 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);
}

}
Original file line number Diff line number Diff line change
@@ -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<FeaturedImage, Image> {

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

}
Original file line number Diff line number Diff line change
@@ -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<ComponentRichImage, Image> {

private final ImageConverter imageConverter;

@Override
public Image apply(ComponentRichImage i) {

return imageConverter.apply(i.getImage()).toBuilder()
.fullWidth(i.getFullWidth())
.build();
}

}

This file was deleted.

0 comments on commit 63cf4d6

Please sign in to comment.