diff options
author | Indrajith K L | 2024-06-21 13:28:34 +0530 |
---|---|---|
committer | Indrajith K L | 2024-06-21 13:28:34 +0530 |
commit | fff60d6f9fa9baf1bb87ced8713b744ea0c6ed5a (patch) | |
tree | 58b754f79ed0cf562b4be01cc262bb5eb2a7dde2 /src | |
parent | ff1da010408d49320253d5cda24777c547621ece (diff) | |
download | indrajith-dev-crystal-fff60d6f9fa9baf1bb87ced8713b744ea0c6ed5a.tar.gz indrajith-dev-crystal-fff60d6f9fa9baf1bb87ced8713b744ea0c6ed5a.tar.bz2 indrajith-dev-crystal-fff60d6f9fa9baf1bb87ced8713b744ea0c6ed5a.zip |
Posts Handlers Changes
* Handles Empty posts
* Move Strapi baseurl to ENV
Diffstat (limited to 'src')
-rw-r--r-- | src/indrajith-dev-crystal.cr | 27 |
1 files changed, 16 insertions, 11 deletions
diff --git a/src/indrajith-dev-crystal.cr b/src/indrajith-dev-crystal.cr index 74fecc2..f5ddabd 100644 --- a/src/indrajith-dev-crystal.cr +++ b/src/indrajith-dev-crystal.cr @@ -6,6 +6,7 @@ require "dotenv" Dotenv.load TOKEN = ENV["TOKEN"] +STRAPI_URL = ENV["STRAPI_URL"] module Indrajith::Dev::Crystal VERSION = "0.1.0" @@ -42,7 +43,7 @@ module Indrajith::Dev::Crystal get "/posts" do |context| begin - site = Crest::Resource.new("http://localhost:1337") + site = Crest::Resource.new("#{STRAPI_URL}") response = site.get("/api/posts", params: { @@ -55,16 +56,21 @@ module Indrajith::Dev::Crystal json_data = JSON.parse(response.body) data = json_data["data"].as_a - html_string = "" - data.each do |item| - attributes = item["attributes"] - # puts attributes["post_title"] - html_string += " - <li> - <p><a href='/post/#{attributes["slug"]}'>#{attributes["post_title"]}</a></p> - </li>" + puts data.size + if data.size == 0 + "<li>No Posts Yet</li>" + else + html_string = "" + data.each do |item| + attributes = item["attributes"] + # puts attributes["post_title"] + html_string += " + <li> + <p><a href='/post/#{attributes["slug"]}'>#{attributes["post_title"]}</a></p> + </li>" + end + html_string end - html_string rescue ex : Crest::NotFound puts ex.response render_404 @@ -85,7 +91,6 @@ module Indrajith::Dev::Crystal json_data = JSON.parse(response.body) data = json_data["data"].as_a - post_item = data[0] post_attribute = post_item["attributes"] content = post_attribute["post_content"] |