require "../handler"
require "../views/html/test/blog"
class Guff::Handlers::TestBlogHandler < Guff::Handler
def call(context : HTTP::Server::Context)
case (context.request.path || "") as String
when %r{^/test/blog/?$}
draw_page(context)
when %r{^/test/blog/set_state$}
set_state(context)
when %r{^/test/blog/add_post$}
add_post(context)
else
call_next(context)
end
end
private def draw_page(context)
TestBlogHTMLView.run(@models, context)
end
private def add_post(context)
params = HTTP::Params.parse(context.request.body as String)
@models.post.add_post(
name: params["name"].to_s,
slug: params["slug"].to_s,
body: params["body"].to_s,
tags: ["_blog"] + (params["tags"] || "").to_s.split(" "),
)
redirect(context, "/test/blog")
end
private def set_state(context)
params = HTTP::Params.parse(context.request.body as String)
@models.post.update_post(
post_id: params["post_id"].to_i,
state: params["state"].to_s,
)
redirect(context, "/test/blog")
end
end