aboutsummaryrefslogtreecommitdiff
path: root/src/guff/handlers/test-blog.cr
blob: 61f8241c9d228348325a79eff20fc96dc5296460 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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 /^\/test\/blog\/?$/
      draw_page(context)
    when /^\/test\/blog\/set_state$/
      set_state(context)
    when /^\/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