Posts Handlers Changes
* Handles Empty posts * Move Strapi baseurl to ENV
This commit is contained in:
@@ -6,6 +6,7 @@ require "dotenv"
|
|||||||
Dotenv.load
|
Dotenv.load
|
||||||
|
|
||||||
TOKEN = ENV["TOKEN"]
|
TOKEN = ENV["TOKEN"]
|
||||||
|
STRAPI_URL = ENV["STRAPI_URL"]
|
||||||
|
|
||||||
module Indrajith::Dev::Crystal
|
module Indrajith::Dev::Crystal
|
||||||
VERSION = "0.1.0"
|
VERSION = "0.1.0"
|
||||||
@@ -42,7 +43,7 @@ module Indrajith::Dev::Crystal
|
|||||||
|
|
||||||
get "/posts" do |context|
|
get "/posts" do |context|
|
||||||
begin
|
begin
|
||||||
site = Crest::Resource.new("http://localhost:1337")
|
site = Crest::Resource.new("#{STRAPI_URL}")
|
||||||
|
|
||||||
response = site.get("/api/posts",
|
response = site.get("/api/posts",
|
||||||
params: {
|
params: {
|
||||||
@@ -55,6 +56,10 @@ module Indrajith::Dev::Crystal
|
|||||||
|
|
||||||
json_data = JSON.parse(response.body)
|
json_data = JSON.parse(response.body)
|
||||||
data = json_data["data"].as_a
|
data = json_data["data"].as_a
|
||||||
|
puts data.size
|
||||||
|
if data.size == 0
|
||||||
|
"<li>No Posts Yet</li>"
|
||||||
|
else
|
||||||
html_string = ""
|
html_string = ""
|
||||||
data.each do |item|
|
data.each do |item|
|
||||||
attributes = item["attributes"]
|
attributes = item["attributes"]
|
||||||
@@ -65,6 +70,7 @@ module Indrajith::Dev::Crystal
|
|||||||
</li>"
|
</li>"
|
||||||
end
|
end
|
||||||
html_string
|
html_string
|
||||||
|
end
|
||||||
rescue ex : Crest::NotFound
|
rescue ex : Crest::NotFound
|
||||||
puts ex.response
|
puts ex.response
|
||||||
render_404
|
render_404
|
||||||
@@ -85,7 +91,6 @@ module Indrajith::Dev::Crystal
|
|||||||
|
|
||||||
json_data = JSON.parse(response.body)
|
json_data = JSON.parse(response.body)
|
||||||
data = json_data["data"].as_a
|
data = json_data["data"].as_a
|
||||||
|
|
||||||
post_item = data[0]
|
post_item = data[0]
|
||||||
post_attribute = post_item["attributes"]
|
post_attribute = post_item["attributes"]
|
||||||
content = post_attribute["post_content"]
|
content = post_attribute["post_content"]
|
||||||
|
|||||||
Reference in New Issue
Block a user