aboutsummaryrefslogtreecommitdiff
path: root/src/guff/api/post.cr
blob: f856d5bc5f029feb3f099c9a137f7effc2871f9f (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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
require "json"

module Guff
  module API
    module PostAPI
      GET_POSTS_FILTERS = {
        q:              "q",
        posted_year:    "posted_year",
        posted_month:   "posted_month",
        posted_day:     "posted_day",
        created_year:   "created_year",
        created_month:  "created_month",
        created_day:    "created_day",
        slug:           "slug",
        state:          "state",
      }

      private def do_post_get_posts(
        context : HTTP::Server::Context,
        args    : Hash(String, String)
      )
        # build filters
        filters = GET_POSTS_FILTERS.reduce({} of Symbol => String) do |r, k, s|
          r[k] = args[s] if args.has_key?(s)
          r
        end

        # get and return posts
        @models.post.get_posts(
          site_id:  get_site(context),
          filters:  filters,
          tags:     get_posts_tags(args["tags"]?),
          page:     args.has_key?("page") ? args["page"].to_i : 1,
          sort:     get_posts_sort(args["sort"]?),
        )
      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"]?),
          state:    args["state"]?,
        )

        # 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,
          state:    args["state"]?,
        )

        # 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