Posts Handlers Changes
* Handles Empty posts * Move Strapi baseurl to ENV
This commit is contained in:
@@ -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"]
|
||||
|
||||
Reference in New Issue
Block a user