require "json" module Guff module API module PostAPI GET_POSTS_FILTERS = { q: "q", year: "year", month: "month", day: "day", } 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, ) 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