Skip to content
Snippets Groups Projects
Commit 0e846069 authored by Kenny Benaya Nathan's avatar Kenny Benaya Nathan
Browse files

feat: add subscribe feature to premium app

parent 9b5db805
Branches
Tags
No related merge requests found
......@@ -21,6 +21,7 @@ if(isset($decoded['email']) && isset($decoded['password'])){
$_SESSION['password'] = $dataUser['password'];
$_SESSION['role'] = $dataUser['isAdmin'];
$_SESSION['profile_photo'] = $dataUser['profile_img'];
$_SESSION['subs'] = $dataUser['isSubs'];
echo "<script type='text/javascript'> alert('Login Successful'); </script>";
if($dataUser['isAdmin'] == 0){
......
......@@ -21,7 +21,7 @@ if(isset($decoded['name']) && isset($decoded['email']) && isset($decoded['passwo
echo "<script>location.href='/Login'</script>";
}else{
echo "<script type='text/javascript'> alert('Email already registered, Please use another email address'); </script>";
echo "<script>location.href='.Register'</script>";
echo "<script>location.href='/Register'</script>";
}
}
?>
<?php
require_once '../app/core/db.php';
require_once '../app/models/user.php';
require_once '../app/core/cURL.php';
session_start();
$user = new User;
$apiURL = 'http://eatsnow-rest:8010/api/user/';
if(isset($_SESSION['email'])) {
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if ($_SESSION['subs'] == 0) {
subscribeAction($user, $apiURL);
} else {
unsubscribeAction($user, $apiURL);
}
} else {
echo "<script type='text/javascript'> alert('Invalid Request!'); </script>";
}
} else {
echo "<script type='text/javascript'> alert('No user logged in!'); </script>";
}
echo "<script>location.href='/Profile'</script>";
function subscribeAction($user, $apiURL) {
try {
$data = array(
'email' => $_SESSION['email'],
'username' => $_SESSION['name'],
'password' => $_SESSION['password'],
'profile_img' => $_SESSION['profile_photo'],
);
$update = callAPI('POST', $apiURL, json_encode($data));
switch ($update[1]) {
case 201:
echo "<script type='text/javascript'> alert('You have Subscribed!'); </script>";
$user->updateUserSubs($_SESSION['email'], 1);
$_SESSION['subs'] = 1;
break;
case 400:
echo "<script type='text/javascript'> alert('Wrong format!'); </script>";
break;
case 500:
echo "<script type='text/javascript'> alert('Internal Server Error!'); </script>";
break;
case 409:
echo "<script type='text/javascript'> alert('Email already registered!'); </script>";
break;
default:
break;
}
} catch (Exception $e) {
echo "<script type='text/javascript'> alert('Subscribe Failed!'); </script>";
}
}
function unsubscribeAction($user, $apiURL) {
try {
$update = callAPI('DELETE', $apiURL . $_SESSION['email'], false);
switch ($update[1]) {
case 204:
echo "<script type='text/javascript'> alert('You have Unsubscribed!'); </script>";
$user->updateUserSubs($_SESSION['email'], 0);
$_SESSION['subs'] = 0;
break;
case 500:
echo "<script type='text/javascript'> alert('Internal Server Error!'); </script>";
break;
default:
break;
}
} catch (Exception $e) {
echo "<script type='text/javascript'> alert('Unsubcribe failed!'); </script>";
}
}
\ No newline at end of file
......@@ -21,5 +21,10 @@ class User{
$query = "UPDATE $this->table SET user_name = '$name', email = '$email', password = '$password', profile_img = '$profile_img' WHERE email = '$previousemail'";
$this->database->execute($query);
}
public function updateUserSubs($email, $subs) {
$query = "UPDATE $this->table SET isSubs = $subs WHERE email = '$email'";
$this->database->execute($query);
}
}
?>
\ No newline at end of file
......@@ -38,27 +38,33 @@ $page = "Profile";
<section class="container">
<div class="profile">
<form class="form" action="/api/updateProfile.php" method="POST" enctype="multipart/form-data">
<div class="image">
<div class="profile-container">
<div id="profileImage">
<img src="../../../public/assets/img/<?php echo $_SESSION['profile_photo']; ?>" alt="Profile Photo" id="profile-preview">
<div class="image">
<div class="profile-container">
<div id="profileImage">
<img src="../../../public/assets/img/<?php echo $_SESSION['profile_photo']; ?>"
alt="Profile Photo" id="profile-preview">
</div>
</div>
</div>
<input class="imageUpload" type="file" id="profile-img"
name="profile_photo" accept=".jpg,.jpeg,.png" capture>
<input class="imageUpload" type="file" id="profile-img"
name="profile_photo" accept=".jpg,.jpeg,.png" capture>
</div>
<label for="name">Name</label><br>
<input type="text" class="input-form" name="name" value="<?php echo $_SESSION['name']?>" required><br>
<label for="email">Email</label><br>
<input type="email" class="input-form" name="email" value=<?php echo $_SESSION['email']?> required><br>
<label for="pw">Change Password</label><br>
<input type="password" class="input-form" name="password" value="<?php echo $_SESSION['password']?>" required><br>
<input type="password" class="input-form" name="password"
value="<?php echo $_SESSION['password']?>" required><br>
<div class="update-btn">
<!-- <a href="#" type="submit" class="update">Update</a> -->
<input class="update" type="submit" name="update" value="Update" href="">
</div>
</form>
</div>
<form class="subs-form" action="/api/subscribe.php" method="POST">
<input class="subs" type="submit" name="subs"
value=<?php $_SESSION['subs'] == 1 ? print "Unsubscribe" : print "Subscribe"?> href="">
</form>
</section>
<script>
document.addEventListener("DOMContentLoaded", function() {
......
......@@ -21,7 +21,7 @@ function generateCard($name, $category, $address, $rating, $rowSchedule, $linkPa
$card = <<<EOT
<a href=$linkPath class="restaurant">
<img src="/public/assets/img/$imgPath" alt="restoran" class="restaurant-img">
<img src="/public/assets/img/$imgPath" alt="$name" class="restaurant-img">
<div class="restaurant-info">
<div class="restaurant-name">$name</div>
<div class="restaurant-category">$category</div>
......
This diff is collapsed.
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