Commit a9025048 authored by jan.koester's avatar jan.koester
Browse files

test

parent b315d3e0
Loading
Loading
Loading
Loading
+46 −0
Original line number Diff line number Diff line
@@ -408,6 +408,10 @@ namespace blogi {
            int n = Args->database[tid]->exec(sql,res);

            json_object *jftable = json_object_new_object();
            json_object_object_add(jftable, "sortable", json_object_new_boolean(1));
            json_object_object_add(jftable, "sort_api", json_object_new_string("navbar_reorder"));
            json_object_object_add(jftable, "sort_param", json_object_new_string("navbar_id"));
            json_object_object_add(jftable, "sort_param_value", json_object_new_string(navid.c_str()));
            json_object *jftheaders = json_object_new_array();
            json_object_array_add(jftheaders, json_object_new_string(tr(_lang,"Item name").c_str()));
            json_object_array_add(jftheaders, json_object_new_string(tr(_lang,"Item URL").c_str()));
@@ -418,6 +422,7 @@ namespace blogi {
            json_object *jftrows = json_object_new_array();
            for(int i=0; i<n; ++i){
                json_object *jrow = json_object_new_object();
                json_object_object_add(jrow, "item_id", json_object_new_string(res[i][0]));
                json_object *jrfields = json_object_new_array();

                std::string hname;
@@ -695,6 +700,47 @@ namespace blogi {
        bool Controller(const int tid,libhttppp::HttpRequest &req,libhtmlpp::HtmlElement *page,const std::string &sessionid){
            return false;
        }

        void JsonApi(const int tid,json_object *request,json_object *response,const uuid::uuid &authid){
            json_object *cmd_obj = nullptr;
            if(!json_object_object_get_ex(request, "command", &cmd_obj))
                return;
            const char *cmd = json_object_get_string(cmd_obj);
            if(!cmd) return;

            if(strcmp(cmd, "navbar_reorder") == 0){
                json_object *navid_obj = nullptr, *order_obj = nullptr;
                if(!json_object_object_get_ex(request, "navbar_id", &navid_obj) ||
                   !json_object_object_get_ex(request, "order", &order_obj)){
                    json_object_object_add(response, "status", json_object_new_string("error"));
                    json_object_object_add(response, "message", json_object_new_string("Missing navbar_id or order"));
                    return;
                }

                const char *navid = json_object_get_string(navid_obj);
                if(!navid || !json_object_is_type(order_obj, json_type_array)){
                    json_object_object_add(response, "status", json_object_new_string("error"));
                    json_object_object_add(response, "message", json_object_new_string("Invalid parameters"));
                    return;
                }

                int len = json_object_array_length(order_obj);
                for(int i = 0; i < len; ++i){
                    json_object *item = json_object_array_get_idx(order_obj, i);
                    const char *itemId = json_object_get_string(item);
                    if(!itemId) continue;

                    dbpp::SQL sql; dbpp::DBResult res;
                    std::vector<char> sbuf;
                    sql << "UPDATE navbar_items SET position=" << i
                        << " WHERE id='" << dbpp::SQL::escaped(sbuf, itemId)
                        << "' AND navbar_id='" << dbpp::SQL::escaped(sbuf, navid) << "'";
                    Args->database[tid]->exec(sql, res);
                }

                json_object_object_add(response, "status", json_object_new_string("ok"));
            }
        }
    };
};