aboutsummaryrefslogtreecommitdiff
path: root/src/indrajith-dev-crystal.cr
diff options
context:
space:
mode:
authorIndrajith K L2024-06-23 03:39:08 +0530
committerIndrajith K L2024-06-23 03:39:08 +0530
commit03867b94387769c0ca32dbfe2d3311ee5ff83afd (patch)
treeff02372fd52d803635c73436663681f56a7f69cb /src/indrajith-dev-crystal.cr
parent646fdccac5f0ec506b80ed532baa4012965b682e (diff)
downloadindrajith-dev-crystal-03867b94387769c0ca32dbfe2d3311ee5ff83afd.tar.gz
indrajith-dev-crystal-03867b94387769c0ca32dbfe2d3311ee5ff83afd.tar.bz2
indrajith-dev-crystal-03867b94387769c0ca32dbfe2d3311ee5ff83afd.zip
Implements Blog Post Listing
* Implements route for listing blogs grouped by date * Adds blog lists renderer macro * Code refactoring
Diffstat (limited to 'src/indrajith-dev-crystal.cr')
-rw-r--r--src/indrajith-dev-crystal.cr73
1 files changed, 62 insertions, 11 deletions
diff --git a/src/indrajith-dev-crystal.cr b/src/indrajith-dev-crystal.cr
index f49f134..ed237e3 100644
--- a/src/indrajith-dev-crystal.cr
+++ b/src/indrajith-dev-crystal.cr
@@ -7,7 +7,7 @@ require "time"
Dotenv.load
-TOKEN = ENV["TOKEN"]
+TOKEN = ENV["TOKEN"]
STRAPI_URL = ENV["STRAPI_URL"]
module Indrajith::Dev::Crystal
@@ -19,10 +19,22 @@ module Indrajith::Dev::Crystal
render "src/views/#{{{filename}}}.ecr", "src/views/layout.ecr"
end
- macro render_404()
+ macro blog_list_renderer(blog_content)
+ page_title = "Blog"
+ published_at=""
+ blog_lists = {{blog_content}}
+ render "src/views/blog.ecr", "src/views/layout.ecr"
+ end
+
+ macro render_404
page_renderer "404", "The VOID"
end
+ macro send_404(context)
+ context.response.status_code = 404
+ context.response.content_type = "text/plain"
+ context.response.print "Not Found"
+ end
get "/" do |context|
context.response.content_type = "text/html"
@@ -45,8 +57,45 @@ module Indrajith::Dev::Crystal
end
get "/blog" do |context|
- context.response.content_type = "text/html"
- page_renderer "blog", "Blog"
+ begin
+ site = Crest::Resource.new("#{STRAPI_URL}")
+
+ response = site.get("/api/posts/group-by-date",
+ headers: {"Authorization" => "Bearer #{TOKEN}"}
+ )
+
+ post_groups = JSON.parse(response.body)
+
+ html_string = ""
+
+ post_groups.as_h.each do |date, posts|
+ published_time = Time.parse(date, "%Y-%m-%d", Time::Location.local)
+ html_string += "<div>
+ <h3 class='date-based'>#{format_date(published_time)}</h3>
+ <ul class='post-lists'>
+ "
+ posts.as_a.each do |post|
+ post_title = post["post_title"]
+ post_slug = post["slug"]
+ html_string += "
+ <li>
+ <a href='/post/#{post_slug}'>#{post_title}</a>
+ </li>
+ "
+ end
+ html_string += "</ul>
+ </div>
+ "
+ end
+ content = html_string
+ context.response.content_type = "text/html"
+ blog_list_renderer html_string
+ rescue ex
+ if ex.responds_to?(:response)
+ puts ex.response
+ end
+ blog_list_renderer "No Recent Posts"
+ end
end
get "/posts" do |context|
@@ -64,14 +113,12 @@ module Indrajith::Dev::Crystal
json_data = JSON.parse(response.body)
data = json_data["data"].as_a
- 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>
@@ -79,9 +126,11 @@ module Indrajith::Dev::Crystal
end
html_string
end
- rescue ex : Crest::NotFound
- puts ex.response
- render_404
+ rescue ex
+ if ex.responds_to?(:response)
+ puts ex.response
+ end
+ send_404 context
end
end
@@ -108,8 +157,10 @@ module Indrajith::Dev::Crystal
page_title = post_attribute["post_title"]
render "src/views/layout.ecr"
rescue ex
- puts ex
- render_404
+ if ex.responds_to?(:response)
+ puts ex.response
+ end
+ send_404 context
end
end