From 257bba67a414842d634b7cf921e563144bf29dbc Mon Sep 17 00:00:00 2001 From: almdgustadev Date: Mon, 28 Jul 2025 21:39:15 -0300 Subject: [PATCH] Desafio Apiki - Plugin de favoritos via REST API --- .../includes/FavoriteController.php | 45 +++++++++++++++++++ wp-post-favorites/includes/Install.php | 25 +++++++++++ wp-post-favorites/includes/Routes.php | 29 ++++++++++++ wp-post-favorites/post-favorites.php | 25 +++++++++++ 4 files changed, 124 insertions(+) create mode 100644 wp-post-favorites/includes/FavoriteController.php create mode 100644 wp-post-favorites/includes/Install.php create mode 100644 wp-post-favorites/includes/Routes.php create mode 100644 wp-post-favorites/post-favorites.php diff --git a/wp-post-favorites/includes/FavoriteController.php b/wp-post-favorites/includes/FavoriteController.php new file mode 100644 index 00000000..87b32568 --- /dev/null +++ b/wp-post-favorites/includes/FavoriteController.php @@ -0,0 +1,45 @@ +get_param('post_id'); + $table = $wpdb->prefix . 'post_favorites'; + + // Verifica se o post já está favoritado pelo usuário + $exists = $wpdb->get_var($wpdb->prepare( + "SELECT COUNT(*) FROM $table WHERE user_id = %d AND post_id = %d", + $user_id, + $post_id + )); + + if ($exists) { + // Se já existe, remove (desfavorita) + $wpdb->delete($table, ['user_id' => $user_id, 'post_id' => $post_id]); + return ['status' => 'removed']; + } else { + // Se não existe, insere (favorita) + $wpdb->insert($table, ['user_id' => $user_id, 'post_id' => $post_id]); + return ['status' => 'added']; + } + } + + // Método para listar os posts favoritados pelo usuário logado + public static function list($request) { + global $wpdb; + + $user_id = get_current_user_id(); + $table = $wpdb->prefix . 'post_favorites'; + + // Busca todos os IDs dos posts favoritados pelo usuário + $favorites = $wpdb->get_col($wpdb->prepare( + "SELECT post_id FROM $table WHERE user_id = %d", + $user_id + )); + + return $favorites; + } +} diff --git a/wp-post-favorites/includes/Install.php b/wp-post-favorites/includes/Install.php new file mode 100644 index 00000000..328ff815 --- /dev/null +++ b/wp-post-favorites/includes/Install.php @@ -0,0 +1,25 @@ +prefix . 'post_favorites'; + $charset_collate = $wpdb->get_charset_collate(); + + $sql = "CREATE TABLE $table_name ( + id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, + user_id BIGINT UNSIGNED NOT NULL, + post_id BIGINT UNSIGNED NOT NULL, + PRIMARY KEY (id), + UNIQUE KEY user_post (user_id, post_id) + ) $charset_collate;"; + + require_once ABSPATH . 'wp-admin/includes/upgrade.php'; + dbDelta($sql); + } +} diff --git a/wp-post-favorites/includes/Routes.php b/wp-post-favorites/includes/Routes.php new file mode 100644 index 00000000..95bad59b --- /dev/null +++ b/wp-post-favorites/includes/Routes.php @@ -0,0 +1,29 @@ + 'POST', + 'callback' => ['FavoriteController', 'toggle'], + 'permission_callback' => function () { + return is_user_logged_in(); + }, + 'args' => [ + 'post_id' => [ + 'required' => true, + 'type' => 'integer', + ] + ] + ]); + + // Rota para listar favoritos do usuário logado + register_rest_route('post-favorites/v1', '/list', [ + 'methods' => 'GET', + 'callback' => ['FavoriteController', 'list'], + 'permission_callback' => function () { + return is_user_logged_in(); + } + ]); + } +} diff --git a/wp-post-favorites/post-favorites.php b/wp-post-favorites/post-favorites.php new file mode 100644 index 00000000..b35e9271 --- /dev/null +++ b/wp-post-favorites/post-favorites.php @@ -0,0 +1,25 @@ +