aboutsummaryrefslogtreecommitdiff
path: root/src/guff/handlers/test.cr
blob: f87800e9ca88db96f04949b718f73e2715cccc52 (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"

class Guff::Handlers::TestHandler < Guff::Handler
  def call(context : HTTP::Server::Context)
    case (context.request.path || "") as String
    when /^\/test\/?$/
      draw_page(context)
    when /^\/test\/set_state$/
      set_state(context)
    when /^\/test\/add_post$/
      add_post(context)
    else
      call_next(context)
    end
  end

  private def draw_page(context)
    TestHTMLView.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")
  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")
  end
end