diff options
Diffstat (limited to 'src/guff/api-methods.cr')
-rw-r--r-- | src/guff/api-methods.cr | 66 |
1 files changed, 55 insertions, 11 deletions
diff --git a/src/guff/api-methods.cr b/src/guff/api-methods.cr index d0bf415..c29b324 100644 --- a/src/guff/api-methods.cr +++ b/src/guff/api-methods.cr @@ -248,7 +248,6 @@ module Guff }, }, - "test": { "version": { text: "Get version", @@ -335,37 +334,54 @@ module Guff args : Hash(String, String) ) @models.post.get_posts( - site_id: @models.site.to_site(context.request.headers["host"]?), + site_id: get_site(context), q: args["q"]? || "", - tags: (args.has_key?("tags") && args["tags"].size > 0) ? [args["tags"].split(',')] : NO_TAGS, + tags: get_posts_tags(args["tags"]), page: args.has_key?("page") ? args["page"].to_i : 1, ).to_json end - private def get_site_id(host : String?) - # TODO - 0 - end - private def do_post_add_post( context : HTTP::Server::Context, args : Hash(String, String) ) - @models.post.add_post(args).to_json + post_id = @models.post.add_post( + site_id: get_site(context), + slug: args["slug"], + name: args["name"], + body: args["name"], + tags: get_tags(args["tags"]?), + ) + + # return json + { post_id: post_id }.to_json end private def do_post_remove_posts( context : HTTP::Server::Context, args : Hash(String, String) ) - @models.post.remove_posts(args).to_json + @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 }.to_json end private def do_post_set_tags( context : HTTP::Server::Context, args : Hash(String, String) ) - @models.post.set_tags(args).to_json + @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}.to_json end ############### @@ -486,5 +502,33 @@ module Guff ) raise "some random error" end + + ################### + # utility methods # + ################### + + private def get_site(context : HTTP::Server::Context) : Int? + @models.site.to_site(context.request.headers["host"]?) + end + + private def get_tags( + s : String? + ) : Array(String) + if s && s.size > 0 + Array(String).from_json(s) + else + [] of String + end + end + + private def get_posts_tags( + s : String? + ) : Array(Array(String)) + if s && s.size > 0 + Array(Array(String)).from_json(s) + else + [] of Array(String) + end + end end end |