Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
No results found
Show changes
Commits on Source (13)
Showing
with 557 additions and 170 deletions
This diff is collapsed.
......@@ -60,6 +60,27 @@
return $book;
}
function getReviewsDetailByBookId($book_id) {
$reviews = [];
$sql = 'SELECT * FROM review WHERE book_id = ?';
$stmt = $this->conn->prepare($sql);
$stmt->execute([$book_id]);
while ($row = $stmt->fetch()) {
$review_id = (int) $row["review_id"];
$user_id = (int) $row["user_id"];
$book_id = (int) $row["book_id"];
$content = $row["content"];
$rating = (float) $row["rating"];
$username = getJwtData($_COOKIE["Authorization"])->username;
$review = new Review($review_id, $user_id, $book_id, $content, $rating, $username);
array_push($reviews, $review);
}
return $reviews;
}
}
?>
\ No newline at end of file
......@@ -11,11 +11,12 @@
function getBookDetail(Request $request) {
$id = (int)$request->params["book_id"];
$books = $this->bookDb->getBookById($id);
$reviews = $this->bookDb->getReviewsDetailByBookId($id);
if ($books) {
$data = [
"books" => $books,
"reviews" => $reviews,
];
render('bookDetail.php', $data);
} else {
writeResponse(500, 'Failed get book detail');
......
<?php
function addBookRoutes($router, $bookUsecase){
$router->add("/api/book/", "GET", array($bookUsecase, 'searchBook'));
$router->add("/api/book/:book_id/", "GET", array($bookUsecase, 'getBookDetail'));
$router->add("/results/", "GET", array($bookUsecase, 'searchBook'), new AuthMiddleware());
$router->add("/book/:book_id/", "GET", array($bookUsecase, 'getBookDetail'));
// $router->add("/", "GET", $homepage, $middlewareExample);
// $router->add("/book/:book_id/user/:user_id/", "POST", $postCallbackExample, $middlewareExample);
// $router->add("/book/:book_id/", "POST", $postCallbackExample, $middlewareExample);
......
......@@ -5,13 +5,15 @@
public $book_id;
public $content;
public $rating;
public $username;
function __construct($review_id, $user_id, $book_id, $content, $rating) {
function __construct($review_id, $user_id, $book_id, $content, $rating, $username = null) {
$this->review_id = $review_id;
$this->user_id = $user_id;
$this->book_id = $book_id;
$this->content = $content;
$this->rating = $rating;
$this->username = $username;
}
}
?>
\ No newline at end of file
......@@ -59,6 +59,25 @@
return $reviewRes;
}
function getBookById($id) {
$books = [];
$sql = 'SELECT * FROM book WHERE book_id = ?';
$stmt = $this->conn->prepare($sql);
$stmt->execute([$id]);
while($row = $stmt->fetch()) {
$book_id = (int) $row["book_id"];
$title = $row["title"];
$author = $row["author"];
$description = $row["description"];
$rating = $row["rating"];
$book = new Book($book_id, $title, $author, $description, $rating);
array_push($books, $book);
}
return $books;
}
}
?>
\ No newline at end of file
......@@ -15,18 +15,31 @@
}
function addReview(Request $request) {
$user_id = (int)$request->payload["userid"];
$book_id = (int)$request->payload["bookid"];
$user_id = (int)$request->payload["user_id"];
$book_id = (int)$request->payload["book_id"];
$content = $request->payload["content"];
$rating = (int)$request->payload["rating"];
$review = new Review(null, $user_id, $book_id, $content, $rating);
$review = $this->reviewDb->createReview($review);
if ($review) {
writeResponse(200, "Success add review", $review);
header('Location: /history/');
} else {
writeResponse(500, "Failed add review");
}
exit();
}
function getReviewBookDetail(Request $request) {
$book_id = (int)$request->params["book_id"];
$books = $this->reviewDb->getBookById($book_id);
$user_id = $_COOKIE["user_id"];
$books->user_id = $user_id;
$data = [
"books" => $books,
];
render("review.php", $data);
}
}
?>
\ No newline at end of file
<?php
function addReviewRoutes($router, $reviewUsecase){
$router->add("/api/review/reviews/:book_id/", "GET", array($reviewUsecase,'getReviews'));
$router->add("/review/:book_id/", "GET", array($reviewUsecase, "getReviewBookDetail"));
$router->add("/api/review/", "POST", array($reviewUsecase, 'addReview'));
return $router;
......
......@@ -6,17 +6,18 @@
class AuthMiddleware implements IMiddleware {
function run($next, $nextRequest) {
if ($_COOKIE["Authorization"]){
$user_id = verifyJWT($_COOKIE["Authorization"]);
if ($user_id){
setcookie("user_id", $user_id, time()+3000, "/");
setcookie("user_id", $user_id, time()+300, "/");
$next($nextRequest);
exit;
}
}
$url = APP_CONFIG["base_url"]."/login/";
header('Location: '.$url);
exit;
} else {
$url = APP_CONFIG["base_url"]."login/";
header('Location: '.$url);
exit;
}
}
}
......
......@@ -12,12 +12,12 @@
}
function getProfile(Request $request){
$id = (int)$request->params["user_id"];
$users = $this->userDb->getUserById($id);
if ($users){
writeResponse(200, "Success get profile", $users);
$id = getJwtData($_COOKIE["Authorization"]["user_id"]);
$user= $this->userDb->getUserById($id);
if ($user){
$data = ["user"=>$user];
render('profile.php', $data);
} else {
writeResponse(500, "Failed get profile");
}
}
......@@ -85,12 +85,15 @@
if ($user_id) {
$payload = array(
"user_id"=> (int)$user_id,
"exp"=> time()+3000
"exp"=> time()+3000,
"username" => $username
);
$jwt = generateJWT($payload);
writeResponse(200, "Login Success", $jwt);
setcookie("Authorization", $jwt["token"], time()+APP_CONFIG["jwt_duration"],"/");
var_dump($_COOKIE["Authorization"]);
header('Location: /browse/');
} else {
writeResponse(400, "Incorrect username or password");
render('login.php',array("isError"=>true));
}
}
......
......@@ -2,10 +2,12 @@
function addUserRoutes($router, $userUsecase){
$router->add("/api/user/", "POST", array($userUsecase,'registerUser'));
$router->add("/api/user/:user_id/", "DELETE", array($userUsecase,'removeUser'));
$router->add("/api/user/:user_id/", "GET", array($userUsecase,'getProfile'));
$router->add("/api/user/:user_id/", "PUT", array($userUsecase,'editProfile'));
// Auth
$router->add("/auth/login/", "POST", array($userUsecase,'login'));
$router->add("/login/", "POST", array($userUsecase,'login'));
$router->add("/login/", "GET", 'render', null, 'login.php');
$router->add("/profile/", "GET", array($userUsecase,'getProfile'));
$router->add("/api/user/validateEmail/", "GET", array($userUsecase,'validateEmail'));
$router->add("/api/user/validateUsername/", "GET", array($userUsecase,'validateUsername'));
return $router;
......
......@@ -58,36 +58,26 @@
</div><br>
<h2 id="subtitle">Reviews</h2>
<div class="review-container">
<div class="review">
<img src="/static/img/tayoblue.jpg" class="img-review-user">
<div class="username">@tayotayo</div>
<div class="review-desc">
Buku ini keren! Nama Rogi dan Lani, temanku, ada di sini, keberuntungan hebat menanti mereka! ( ^_^)
</div>
</div>
<div class="rate">
<img src="/static/img/starfull.png" class="starfull">
<!-- <div class="starhole">★</div> -->
<div class="rating">5.0 / 5.0</div>
</div>
</div>
<br>
<div class="review-container">
<div class="review">
<img src="/static/img/tayogreen.jpg" class="img-review-user">
<div class="username">@ROGreen</div>
<div class="review-desc">
Aku membeli buku ini atas rekomendasi Tayo, aku menemukan namaku sendiri di buku ini. Aku tidak tahu harus senang atau sedih ( '_')
</div>
</div>
<div class="rate">
<img src="/static/img/starfull.png" class="starfull">
<!-- <div class="starhole">★</div> -->
<div class="rating">4.0 / 5.0</div>
</div>
</div>
<?php
foreach($reviews as $review) {
echo("
<div class=\"review-container\">
<div class=\"review\">
<img src=\"/static/img/tayoblue.jpg\" class=\"img-review-user\">
<div class=\"username\">@$review->username</div>
<div class=\"review-desc\">
$review->content
</div>
</div>
<div class=\"rate\">
<img src=\"/static/img/starfull.png\" class=\"starfull\">
<!-- <div class=\"starhole\">★</div> -->
<div class=\"rating\">$review->rating / 5.0</div>
</div>
</div>
");
}
?>
</div>
<script src="/static/js/book_detail.js"></script>
......
<?php
include __STATIC__.'/html/header.html';
render('header.php');
include __STATIC__.'/html/browse.html';
// Include content here;
include __STATIC__.'/html/footer.html';
......
<?php
$username = getJwtData($_COOKIE["Authorization"])->username;
?>
<link rel="stylesheet" href="/static/css/base.css">
<link rel="stylesheet" href="/static/css/header.css">
<nav>
<div id="header">
<div id="logo">
<span style="color:#FFEC5D;">Pro</span><span>-Book</span>
</div>
<div class="right-header">
<a href="" id="header_username">Hi <?= $username ?> </a>
<a href="/logout/"><img id="logout_img" src="/static/img/logout.png"></a>
</div>
</div>
<div id="menu">
<a href ="/browse/" class="menu-item" id="menu_browse"> BROWSE </a>
<a href ="/history/" class="menu-item" id="menu_history"> HISTORY </a>
<a href ="/profile/" class="menu-item" id="menu_profile"> PROFILE </a>
</div>
</nav>
<link rel="stylesheet" href="/static/css/login.css">
<link rel="stylesheet" href="/static/css/base.css">
<div class="container">
<form class="form" action="/login/" method="POST">
<h1 class="form-title">LOGIN</h1>
<div class="row">
<label for="username_form" class="form-label">Username</label>
<input class="form-input" type="text" name="username" id="username_form">
</div>
<div class="row">
<label for="password_form" class="form-label">Password</label>
<input class="form-input" type="password" name="password" id="password_form">
</div>
<a class="register-link" href="/register/">Don't have an account?</a>
<br>
<div class="submit-row">
<input class="submit-button" type="submit" id="login_button" value="LOGIN">
</div>
</form>
</div>
<?php
include __STATIC__.'/html/login.html';
if ($isError){
echo "<script>alert(\"Wrong username or password\")</script>";
}
include __STATIC__.'/html/footer.html';
?>
\ No newline at end of file
<?php
include __STATIC__.'/html/header.html';
// Include content here;
include __STATIC__.'/html/profile.html';
include __STATIC__.'/html/footer.html';
render('header.php');
?>
<link rel="stylesheet" href="/static/css/profile.css">
<div class="main-container">
<div class="profile-picture-jumbotron">
<div class="profile-picture-container">
</div>
<div class="profile-picture-container">
<img src="/static/img/tayoblue.jpg" class="profile-img"></img>
<h1 class="profile-name" id="profile_name"><?= $user->fullname?></h1>
</div>
<div class="profile-picture-container">
<a href="/profile/edit/"><img src="/static/img/pencil.png" class="edit-icon"></a>
</div>
</div>
<div class="detail-container">
<h1 class="table-headline">My Profile</h1>
<table class="profile-table" cellpadding="15">
<tr class="table-row" height=75>
<td width="10%" ><img class="profile-icon" src="/static/img/profile.png"></td>
<td width="40%"><?= $user->username ?></td>
<td id="username_column">Tayo</td>
</tr>
<tr class="table-row" height=75>
<td><img class="profile-icon" src="/static/img/email.png"></td>
<td>Email</td>
<td id="email_column"><?= $user->email ?></td>
</tr>
<tr class="table-row" height=75>
<td><img class="profile-icon" src="/static/img/house.png"></td>
<td>Address</td>
<td id="address_column"><?= $user->address?></td>
</tr>
<tr class="table-row" height=75>
<td><img class="profile-icon" src="/static/img/phone.png"></td>
<td>Phone Number</td>
<td id="phone_column"><?= $user->phonoe?></td>
</tr>
</table>
</div>
</div>
<script src="/static/js/profile.js"></script>
<?php
include __STATIC__.'/html/footer.html';
?>
\ No newline at end of file
<?php
include __STATIC__.'/html/header.html';
include __STATIC__.'/html/review.html';
// Include content here;
include __STATIC__.'/html/footer.html';
// echo $_COOKIE["user"];
?>
<link rel="stylesheet" href="/static/css/base.css">
<link rel="stylesheet" href="/static/css/review.css">
<div class="review-container">
<div class="row justify-content-between">
<div class="review-title">
<div class="title">
<h1 id="review-title">
<?php
echo($books[0]->title)
?>
</h1>
</div>
<h2 id="review-author">
<?php
echo($books[0]->author)
?>
</h2>
</div>
<img
src="/static/img/contoh_buku.png"
/>
</div>
<div class="review-header">
<h1>Add Rating</h1>
<div class="row justify-content-center review-rating">
<img
id=star-0
key=0
src="/static/img/empty_star.png"
/>
<img
id=star-1
key=1
src="/static/img/empty_star.png"
/>
<img
id=star-2
key=2
src="/static/img/empty_star.png"
/>
<img
id=star-3
key=3
src="/static/img/empty_star.png"
/>
<img
id=star-4
key=4
src="/static/img/empty_star.png"
/>
</div>
</div>
<div class="review-header">
<h1>Add Comment</h1>
</div>
<form action="/api/review/" method="POST">
<textarea
name="content"
rows="10"
class="review-comment"
></textarea>
<input
type="hidden"
name="rating"
id="rating"
/>
<?php
$user_id = $_COOKIE["user_id"];
echo("
<input
type=\"hidden\"
name=\"user_id\"
value=$user_id
/>
")
?>
<?php
$book_id = $books[0]->book_id;
echo("
<input
type=\"hidden\"
name=\"book_id\"
value=$book_id
/>
")
?>
<div class="justify-content-between">
<div class='review-back'>
<a href="/history/">
<span>Back</span>
</a>
</div>
<button
class='review-submit'
id='review-submit'
>
<span>Submit</span>
</button>
</div>
</form>
</div>
<script src='/static/js/review.js'></script>
<?php
include __STATIC__.'/html/footer.html';
?>
\ No newline at end of file
<?php
include __STATIC__.'/html/header.html';
// Include content here;
// echo $_COOKIE["user"];
render('header.php');
?>
<link rel="stylesheet" href="/static/css/base.css">
......@@ -35,7 +33,7 @@
$book->description
</p>
<div class=\"justify-content-end\">
<a href=\"/api/book/$book->book_id/\">
<a href=\"/book/$book->book_id/\">
<button type=\"submit\"><span>Detail</span></button>
</a>
</div>
......
......@@ -8,6 +8,7 @@
),
"base_url" => "http://localhost:4000/",
"jwt_alg"=>"sha256",
"jwt_key"=>"weirdgenius2018"
"jwt_key"=>"weirdgenius2018",
"jwt_duration"=>3000
)
?>
\ No newline at end of file
......@@ -48,15 +48,12 @@
$router = addReviewRoutes($router, $reviewUsecase);
$router->add("/browse/", "GET", 'render', $authMiddleware, 'browse.php');
$router->add("/results/", "GET", "render", $authMiddleware, "result.php");
$router->add("/login/", "GET", 'render', null, 'login.php');
$router->add("/book-detail/", "GET", 'render', null, 'book_detail.php');
$router->add("/register/", "GET", 'render', null, 'register.php');
$router->add("/history/", "GET", 'render', $authMiddleware, 'history.php');
$router->add("/profile/", "GET", 'render', $authMiddleware, 'profile.php');
$router->add("/profile/edit/", "GET", "render", $authMiddleware, "edit-profile.php");
$router->add("/logout/", "GET", 'render', $authMiddleware, 'logout.php');
$router->add("/review/", "GET", "render", $authMiddleware, "review.php");
// TODO: Add Order Routes
$errorCallback = function(){
......