void Controller::searchSimilarRequest(std::map<std::string, std::string>& params, const Poco::Dynamic::Var& request) {
    Record record;
    vector<uint8_t>* image_data = NULL;
    int count = 10;
    
    if (params.count("blockHash")) {
        record.getHashes()[BlockHash] = str2bin(params["blockHash"]);
    }
    if (params.count("dHash")) {
        record.getHashes()[DHash] = str2bin(params["dHash"]);
    }
    if (params.count("gaussDHash")) {
        record.getHashes()[GaussDHash] = str2bin(params["gaussDHash"]);
    }
    if (params.count("gauss2DHash")) {
        record.getHashes()[Gauss2DHash] = str2bin(params["gauss2DHash"]);
    }
    if (params.count("gaussBlockHash")) {
        record.getHashes()[GaussBlockHash] = str2bin(params["gaussBlockHash"]);
    }
    if (params.count("url")) {
        image_data = Image::fromUrl(params["url"]);
    }
    if (params.count("count")) {
        count = atoi(params["count"].c_str());
    }
    
    if (!request.isEmpty()) {
        Poco::JSON::Object::Ptr json = request.extract<Poco::JSON::Object::Ptr>();
        if (!image_data && json->has("image_base64")) {
            image_data = Image::fromBase64(json->getValue<string>("image_base64"));
        }
        if (!image_data && json->has("image_url")) {
            image_data = Image::fromUrl(json->getValue<string>("image_url"));
        }
    }
    
    if (image_data) {
        HashAlgorithmManager::getInstance().attachSimilarHashes(record, image_data);
        delete image_data;
    }
    
    appio::print_ok(HashManager::getInstance().searchSimilar(record, count));
}
void Controller::ingestRequest(const Poco::Dynamic::Var& request) {
    if (request.type() != typeid(Poco::JSON::Array::Ptr)) {
        throw RequestException("Invalid JSON format, root must be array.");
    }
    
    try {
        Poco::JSON::Array::Ptr jsonArray = request.extract<Poco::JSON::Array::Ptr>();
        
        for (Poco::JSON::Array::ConstIterator it = jsonArray->begin(); it != jsonArray->end(); it++) {
            Poco::JSON::Object::Ptr json = it->extract<Poco::JSON::Object::Ptr>();
            
            Record record;
            vector<uint8_t>* image_data = NULL;
            
            if (json->has("id")) {
                record.setId(json->getValue<string>("id"));
            }
            if (json->has("thumbnail")) {
                record.setThumbnail(json->getValue<string>("thumbnail"));
            }
            if (json->has("metadata")) {
                record.setMetadata(json->getValue<string>("metadata"));
            }
            if (json->has("blockHash")) {
                record.getHashes()[BlockHash] = str2bin(json->getValue<string>("blockHash"));
            }
            if (json->has("dHash")) {
                record.getHashes()[DHash] = str2bin(json->getValue<string>("dHash"));
            }
            if (json->has("gaussDHash")) {
                record.getHashes()[GaussDHash] = str2bin(json->getValue<string>("gaussDHash"));
            }
            if (json->has("gauss2DHash")) {
                record.getHashes()[Gauss2DHash] = str2bin(json->getValue<string>("gauss2DHash"));
            }
            if (json->has("gaussBlockHash")) {
                record.getHashes()[GaussBlockHash] = str2bin(json->getValue<string>("gaussBlockHash"));
            }
            if (json->has("image_base64")) {
                image_data = Image::fromBase64(json->getValue<string>("image_base64"));
            }
            if (!image_data && json->has("image_url")) {
                image_data = Image::fromUrl(json->getValue<string>("image_url"));
            }
            
            if (json->has("status")) {
                string status = json->getValue<string>("status");
                if (status == "deleted") {
                    deleteRequest(record);
                    continue;
                } else {
                    throw RequestException("Invalid status value.");
                }
            }

            if (image_data) {
                HashAlgorithmManager::getInstance().attachHashes(record, image_data);
                delete image_data;
            }
            putRequest(record);
        }
    } catch (Poco::JSON::JSONException& e) {
        throw RequestException("Invalid JSON format.");
    }
    appio::print_ok("Data was successfully changed.");
}