Skip to content
Snippets Groups Projects
Commit fa046d3c authored by mrsyaban's avatar mrsyaban
Browse files

add profile update endpoint

parent d9be6c8a
No related merge requests found
......@@ -4,37 +4,34 @@ class getProfileController
{
public function call()
{
require_once __DIR__ . "/../../views/login/login.php";
session_start();
// print_r($_SESSION);
// if (isset($_SESSION['user_id'])) {
$user_id = "4";
// if (isset($_GET["user_id"])) {
// $user_id = $_GET["user_id"];
// }
$userModel = new UserModel();
$profile = $userModel->getUserInfo(4);
$data = [
"name" => $profile->name,
"username" => $profile->username,
"url_profpic" => $profile->url_profpic,
"is_admin" => $profile->is_admin,
];
header("Access-Control-Allow-Origin: http://localhost:3000");
header("Access-Control-Allow-Credentials: true");
header("Max-Age: 86400");
header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS");
// header("Access-Control-Allow-Headers: $_SERVER[HTTP_ACCESS_CONTROL_REQUEST_HEADERS]");
echo json_encode($data);
// } else {
// session_destroy();
if (!isset($_SESSION["user_id"])) {
session_destroy();
http_response_code(403);
return;
}
// http_response_code(403);
// header("Location: " . BASE_URL . "/login");
$user_id = "";
if (isset($_GET["user_id"])) {
$user_id = $_GET["user_id"];
}
// return;
// }
$userModel = new UserModel();
$profile = $userModel->getUserInfo(4);
$data = [
"name" => $profile->name,
"username" => $profile->username,
"url_profpic" => $profile->url_profpic,
"is_admin" => $profile->is_admin,
];
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Credentials: true");
header("Max-Age: 86400");
header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS");
// header("Access-Control-Allow-Headers: $_SERVER[HTTP_ACCESS_CONTROL_REQUEST_HEADERS]");
http_response_code(200);
echo json_encode($data);
}
}
\ No newline at end of file
<?php
class UpdateProfileController
{
public function call()
{
session_start();
if (!isset($_SESSION["user_id"])) {
session_destroy();
http_response_code(403);
return;
}
if (isset($_POST['name']) && isset($_POST['username']) && isset($_POST['password'])) {
$name = $_POST['name'];
$username = $_POST['username'];
$password = $_POST['password'];
try {
if (!isset($_SERVER["user_id"])) {
http_response_code(401);
exit;
} else {
$model = new UserModel();
$status = $model->updateProfile($_SESSION["user_id"], $name, $username, $password);
if ($status == 200) {
http_response_code(200);
echo json_encode(["message" => "Profile updated successfully"]);
exit;
} else {
http_response_code(500);
echo json_encode(["message" => "Internal server error"]);
exit;
}
}
} catch (PDOException $e) {
echo $e->getCode();
http_response_code(500);
echo json_encode(["message" => "Internal server error"]);
exit;
}
}
}
}
......@@ -8,6 +8,7 @@ class App
$router = new Router();
$router->get("public/profile", new getProfileController());
$router->post("public/profile", new UpdateProfileController());
$router->get("public", new AppController());
$router->get("public/home", new AppController());
......
......@@ -67,4 +67,25 @@ class UserModel
return $rowAffected;
}
public function updateProfile($id_user, $name, $username, $password) {
$query = "UPDATE user( name, username, password)
SET user=:name, username=:username, password=:password
WHERE id_user=:id_user";
$this->db->query($query);
$this->db->bind('name', $name);
$this->db->bind('username', $username);
$this->db->bind('password', $password);
$this->db->bind('id_user', $id_user);
$status = 200;
try {
$user = $this->db->execute();
} catch (PDOException $e) {
$status = 500;
}
return $status;
}
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment