From 03867b94387769c0ca32dbfe2d3311ee5ff83afd Mon Sep 17 00:00:00 2001 From: Indrajith K L Date: Sun, 23 Jun 2024 03:39:08 +0530 Subject: Implements Blog Post Listing * Implements route for listing blogs grouped by date * Adds blog lists renderer macro * Code refactoring --- src/indrajith-dev-crystal.cr | 73 +++++++++++++++++++++++++++++++++++++------- src/views/blog.ecr | 1 + src/views/home.ecr | 3 +- 3 files changed, 65 insertions(+), 12 deletions(-) (limited to 'src') 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 += "
+

#{format_date(published_time)}

+ +
+ " + 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 "
  • No Posts Yet
  • " else html_string = "" data.each do |item| attributes = item["attributes"] - # puts attributes["post_title"] html_string += "
  • #{attributes["post_title"]}

    @@ -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 diff --git a/src/views/blog.ecr b/src/views/blog.ecr index e69de29..c336c58 100644 --- a/src/views/blog.ecr +++ b/src/views/blog.ecr @@ -0,0 +1 @@ +<%= blog_lists %> \ No newline at end of file diff --git a/src/views/home.ecr b/src/views/home.ecr index 2928a32..2935993 100644 --- a/src/views/home.ecr +++ b/src/views/home.ecr @@ -6,7 +6,7 @@

    - +

    Books I'm reading now

    @@ -14,6 +14,7 @@

    Recent Posts

    -- cgit v1.2.3