Skip to content

Commit

Permalink
update query
Browse files Browse the repository at this point in the history
  • Loading branch information
cp-contact-c committed Nov 29, 2024
1 parent 06718d8 commit 74e5b8a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 43 deletions.
63 changes: 20 additions & 43 deletions post/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,13 +177,21 @@ func (repository *Repository) Show(c *gin.Context) {

if post.IsResource {
recommendedPosts := []RecommendedPost{}
err = repository.Db.Select(&recommendedPosts, `SELECT id, title, content, slug, published_on, is_featured, created_at, updated_at, published_at,
summary, blog_content, meta_description, toc, tags as tag, is_published, keywords, new_content, new_toc, new_blog_content,
is_resource, reading_time
FROM posts
WHERE slug != $1`+publishQuery, slug)

// Fetch recommended posts along with images
query := `
SELECT p.id, p.title, p.content, p.slug, p.published_on, p.is_featured, p.created_at, p.updated_at, p.published_at,
p.summary, p.blog_content, p.meta_description, p.toc, p.tags as tag, p.is_published, p.keywords, p.new_content,
p.new_toc, p.new_blog_content, p.is_resource, p.reading_time,
f.id AS image_id, frm.related_id AS related_id, f.url AS image_url, f.alternative_text AS image_alternative_text
FROM posts p
LEFT JOIN files_related_morphs frm ON frm.related_id = p.id AND frm.related_type = 'api::post.post'
LEFT JOIN files f ON frm.file_id = f.id
WHERE p.slug != $1` + publishQuery

err := repository.Db.Select(&recommendedPosts, query, slug)
if err != nil {
log.Error("Error while fetching recommended posts: ", err)
log.Error("Error while fetching recommended posts and images: ", err)
c.AbortWithStatus(http.StatusInternalServerError)
return
}
Expand Down Expand Up @@ -256,48 +264,17 @@ func (repository *Repository) SetPostImageInPosts(posts []Post) []Post {
}

func (repository *Repository) SetPostImageInRecommendedPosts(recommendedPosts []RecommendedPost) []RecommendedPost {

postIds := []string{}
for _, post := range recommendedPosts {
postIds = append(postIds, strconv.Itoa(post.Id))
}

newPostIds := strings.Join(postIds, ", ")

images := []Image{}
postImageQuery := fmt.Sprintf(`SELECT f.id, frm.related_id as related_id, f.alternative_text, f.url
FROM files f
JOIN files_related_morphs frm ON frm.related_id IN (%s) AND frm.related_type = 'api::post.post' AND frm.file_id = f.id`, newPostIds)

err := repository.Db.Select(&images, postImageQuery)
if err != nil {
log.Error("Error while fetching images for recommended posts: ", err)
return recommendedPosts
}

for i, post := range recommendedPosts {
for _, image := range images {
if post.Id == image.RelatedId {
// Set image in the recommended post
recommendedPosts[i].Image = image

// Set format of the image if exists
if recommendedPosts[i].Image.Format != "" {

var postFormats Formats
if err := json.Unmarshal([]byte(recommendedPosts[i].Image.Format), &postFormats); err != nil {
log.Error("Error while unmarshal recommended post image formats: ", err)
return recommendedPosts
}

recommendedPosts[i].Image.Format = ""
recommendedPosts[i].Image.Formats = postFormats
}
break
if post.ImageUrl != "" && post.RelatedId != 0 {
recommendedPosts[i].Image = Image{
Id: post.ImageId,
RelatedId: post.RelatedId,
Url: post.ImageUrl,
AlternativeText: post.ImageAltText,
}
}
}

return recommendedPosts
}

Expand Down
4 changes: 4 additions & 0 deletions post/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,5 +132,9 @@ type RecommendedPost struct {
IsResource bool `json:"is_resource"`
ReadingTime null.Int `json:"reading_time"`
Author Author `json:"author"`
ImageId int `json:"image_id"`
RelatedId int `json:"related_id"`
ImageUrl string `json:"image_url"`
ImageAltText null.String `json:"image_alternative_text"`
Image Image `json:"image"`
}

0 comments on commit 74e5b8a

Please sign in to comment.