diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/indrajith-dev-crystal.cr | 73 | ||||
-rw-r--r-- | src/views/blog.ecr | 1 | ||||
-rw-r--r-- | src/views/home.ecr | 3 |
3 files changed, 65 insertions, 12 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 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 @@ </p> </div> </div> -<span hx-get="/posts" hx-trigger="load" hx-target="#result"></span> +<span hx-get="/posts" hx-trigger="load" hx-target="#result" hx-target-404="#not-found"></span> <div> <div> <h4>Books I'm reading now</h4> @@ -14,6 +14,7 @@ </div> <h4>Recent Posts</h4> <ul id="result"> + <li id="not-found" hx-swap-oob="true">No Recent Posts</li> </ul> </div> <script src="https://www.goodreads.com/review/grid_widget/162656738.Indrajith's%20currently-reading%20book%20montage?cover_size=medium&hide_link=&hide_title=true&num_books=20&order=a&shelf=currently-reading&sort=date_added&widget_id=1676363812" type="text/javascript" charset="utf-8"></script> |