Implements Blog Post Listing
* Implements route for listing blogs grouped by date * Adds blog lists renderer macro * Code refactoring
This commit is contained in:
@@ -7,7 +7,7 @@ require "time"
|
|||||||
|
|
||||||
Dotenv.load
|
Dotenv.load
|
||||||
|
|
||||||
TOKEN = ENV["TOKEN"]
|
TOKEN = ENV["TOKEN"]
|
||||||
STRAPI_URL = ENV["STRAPI_URL"]
|
STRAPI_URL = ENV["STRAPI_URL"]
|
||||||
|
|
||||||
module Indrajith::Dev::Crystal
|
module Indrajith::Dev::Crystal
|
||||||
@@ -19,10 +19,22 @@ module Indrajith::Dev::Crystal
|
|||||||
render "src/views/#{{{filename}}}.ecr", "src/views/layout.ecr"
|
render "src/views/#{{{filename}}}.ecr", "src/views/layout.ecr"
|
||||||
end
|
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"
|
page_renderer "404", "The VOID"
|
||||||
end
|
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|
|
get "/" do |context|
|
||||||
context.response.content_type = "text/html"
|
context.response.content_type = "text/html"
|
||||||
@@ -45,8 +57,45 @@ module Indrajith::Dev::Crystal
|
|||||||
end
|
end
|
||||||
|
|
||||||
get "/blog" do |context|
|
get "/blog" do |context|
|
||||||
context.response.content_type = "text/html"
|
begin
|
||||||
page_renderer "blog", "Blog"
|
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
|
end
|
||||||
|
|
||||||
get "/posts" do |context|
|
get "/posts" do |context|
|
||||||
@@ -64,14 +113,12 @@ 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
|
if data.size == 0
|
||||||
"<li>No Posts Yet</li>"
|
"<li>No Posts Yet</li>"
|
||||||
else
|
else
|
||||||
html_string = ""
|
html_string = ""
|
||||||
data.each do |item|
|
data.each do |item|
|
||||||
attributes = item["attributes"]
|
attributes = item["attributes"]
|
||||||
# puts attributes["post_title"]
|
|
||||||
html_string += "
|
html_string += "
|
||||||
<li>
|
<li>
|
||||||
<p><a href='/post/#{attributes["slug"]}'>#{attributes["post_title"]}</a></p>
|
<p><a href='/post/#{attributes["slug"]}'>#{attributes["post_title"]}</a></p>
|
||||||
@@ -79,9 +126,11 @@ module Indrajith::Dev::Crystal
|
|||||||
end
|
end
|
||||||
html_string
|
html_string
|
||||||
end
|
end
|
||||||
rescue ex : Crest::NotFound
|
rescue ex
|
||||||
puts ex.response
|
if ex.responds_to?(:response)
|
||||||
render_404
|
puts ex.response
|
||||||
|
end
|
||||||
|
send_404 context
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -108,8 +157,10 @@ module Indrajith::Dev::Crystal
|
|||||||
page_title = post_attribute["post_title"]
|
page_title = post_attribute["post_title"]
|
||||||
render "src/views/layout.ecr"
|
render "src/views/layout.ecr"
|
||||||
rescue ex
|
rescue ex
|
||||||
puts ex
|
if ex.responds_to?(:response)
|
||||||
render_404
|
puts ex.response
|
||||||
|
end
|
||||||
|
send_404 context
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1 @@
|
|||||||
|
<%= blog_lists %>
|
||||||
@@ -6,7 +6,7 @@
|
|||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
</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>
|
||||||
<div>
|
<div>
|
||||||
<h4>Books I'm reading now</h4>
|
<h4>Books I'm reading now</h4>
|
||||||
@@ -14,6 +14,7 @@
|
|||||||
</div>
|
</div>
|
||||||
<h4>Recent Posts</h4>
|
<h4>Recent Posts</h4>
|
||||||
<ul id="result">
|
<ul id="result">
|
||||||
|
<li id="not-found" hx-swap-oob="true">No Recent Posts</li>
|
||||||
</ul>
|
</ul>
|
||||||
</div>
|
</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>
|
<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>
|
||||||
|
|||||||
Reference in New Issue
Block a user