aboutsummaryrefslogtreecommitdiff
path: root/src/guff/api/post.cr
blob: 71415309cdaaa1b6f6906aa1288dccb110ccb71d (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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
require "json"

module Guff
  module API
    module PostAPI
      private def do_post_get_posts(
        context : HTTP::Server::Context,
        args    : Hash(String, String)
      )
        @models.post.get_posts(
          site_id:  get_site(context),
          q:        args["q"]? || "",
          tags:     get_posts_tags(args["tags"]),
          page:     args.has_key?("page") ? args["page"].to_i : 1,
        )
      end

      private def do_post_add_post(
        context : HTTP::Server::Context,
        args    : Hash(String, String)
      )
        post_id = @models.post.add_post(
          site_id:  get_site(context),
          slug:     args["slug"],
          name:     args["name"],
          body:     args["body"],
          tags:     get_tags(args["tags"]?),
        )

        # return json
        { post_id: post_id }
      end

      private def do_post_update_post(
        context : HTTP::Server::Context,
        args    : Hash(String, String)
      )
        post_id = @models.post.update_post(
          site_id:  get_site(context),
          post_id:  args["post_id"].to_i,
          slug:     args["slug"]?,
          name:     args["name"],
          body:     args["body"],
          tags:     args.has_key?("tags") ? get_tags(args["tags"]?) : nil,
          posted:   args.has_key?("posted") ? (args["posted"] == "t") : nil,
        )

        # return json
        { ok: true }
      end

      private def do_post_remove_posts(
        context : HTTP::Server::Context,
        args    : Hash(String, String)
      )
        @models.post.remove_posts(
          site_id:  get_site(context),
          post_ids: args["post_ids"].split(',').map { |post_id|
            post_id.to_s.to_i
          },
        )

        { ok: true }
      end

      private def do_post_set_tags(
        context : HTTP::Server::Context,
        args    : Hash(String, String)
      )
        @models.post.set_tags(
          site_id:  get_site(context),
          post_id:  (args["post_id"] as String).to_i,
          tags:     get_tags(args["tags"]?),
        )

        { ok: true}
      end
    end
  end
end