Skip to content

Commit

Permalink
Merge pull request #583 from canopas/fix-getting-draft-blogs-in-live-…
Browse files Browse the repository at this point in the history
…by-url

Fix: Getting draft blog in live using url
  • Loading branch information
cp-dharti-r authored Jul 10, 2024
2 parents a10ce99 + 2ab9e5d commit 7fe4a45
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
4 changes: 3 additions & 1 deletion nuxt-frontend/pages/[slug].vue
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ const assets =
slug.value === "favicon.ico" || slug.value === "flutter_service_worker.js";
if (!assets) {
await useAsyncData("blog", () => store.loadResource(slug.value));
await useAsyncData("blog", () =>
store.loadResource(slug.value, config.SHOW_DRAFT_POSTS),
);
}
if (status.value !== config.SUCCESS) {
Expand Down
7 changes: 5 additions & 2 deletions nuxt-frontend/stores/resources/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,14 @@ export const useBlogDetailStore = defineStore("resources-detail", {
};
},
actions: {
async loadResource(slug) {
async loadResource(slug, showDrafts) {
return new Promise((resolve, reject) => {
this.isLoading = true;
this.error = null;
let url = config.API_BASE + "/api/posts/" + slug;

let published = `is_published=${!showDrafts}`;

let url = config.API_BASE + "/api/posts/" + slug + "?" + published;

axios
.get(url)
Expand Down
18 changes: 15 additions & 3 deletions post/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,24 @@ func (repository *Repository) Show(c *gin.Context) {
return
}

isPublished, err := strconv.ParseBool(c.DefaultQuery("is_published", "false"))
if err != nil {
log.Warn(err)
c.AbortWithStatus(http.StatusBadRequest)
return
}

publishQuery := ""
if isPublished {
publishQuery = " AND is_published = true AND published_at IS NOT NULL"
}

post := Post{}
err := repository.Db.Get(&post, `SELECT id, title, content, slug, published_on, is_featured, created_at, updated_at, published_at,
err = repository.Db.Get(&post, `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`, slug)
WHERE slug = $1`+publishQuery, slug)
if err != nil {
if err == sql.ErrNoRows {
c.AbortWithStatus(http.StatusNotFound)
Expand Down Expand Up @@ -169,7 +181,7 @@ func (repository *Repository) Show(c *gin.Context) {
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`, slug)
WHERE slug != $1`+publishQuery, slug)
if err != nil {
log.Error("Error while fetching recommended post: ", err)
c.AbortWithStatus(http.StatusInternalServerError)
Expand Down

0 comments on commit 7fe4a45

Please sign in to comment.