diff --git a/app/controller/book/Book.php b/app/controller/book/Book.php index b88b9ac79bc16bf2d5292e35d58f5ff90b051ae9..1ee134025802e6d07a220302913203dea30ef788 100644 --- a/app/controller/book/Book.php +++ b/app/controller/book/Book.php @@ -1,7 +1,49 @@ <?php class Book { - function __construct(){ - + public $book_id; + public $title; + public $author; + public $description; + public $rating; + + function __construct($book_id, $title, $author, $description, $rating) { + $this->book_id = $book_id; + $this->title = $title; + $this->author = $author; + $this->description = $description; + $this->rating = $rating; + } + + function getBookId() { + return $this->book_id; + } + function getTitle() { + return $this->title; + } + function getAuthor() { + return $this->author; + } + function getDescription() { + return $this->description; + } + function getRating() { + return $this->$rating; + } + + function setBookId($book_id) { + $this->book_id = $book_id; + } + function setTitle($title) { + $this->title = $title; + } + function setAuthor($author) { + $this->author = $author; + } + function setDescription($description) { + $this->description = $description; + } + function setRating($rating) { + $this->rating = $rating; } } diff --git a/app/controller/book/BookDb.php b/app/controller/book/BookDb.php index 6ef091b4d294158e805c15cf5d3e5b1f312bedaf..6386e4d44b4ac02f1597d0b9d4641acc2aced0cf 100644 --- a/app/controller/book/BookDb.php +++ b/app/controller/book/BookDb.php @@ -1,8 +1,50 @@ <?php require_once __ROOT__."/util/Database.php"; + require_once __ROOT__."/app/controller/book/Book.php"; class BookDb extends Database{ - // Impelement + function __construct(PDO $conn) { + parent::__construct($conn); + } + + 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; + } + + function getBookByTitle($title) { + $books = []; + $sql = "SELECT * FROM book WHERE title LIKE ?"; + $stmt = $this->conn->prepare($sql); + $stmt->execute(["%$title%"]); + + 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 diff --git a/app/controller/book/BookUsecase.php b/app/controller/book/BookUsecase.php index d635e20a71eebf5689dd9a459b34f67079598701..6c048281a19d0dc1156da37c6519971add5322df 100644 --- a/app/controller/book/BookUsecase.php +++ b/app/controller/book/BookUsecase.php @@ -8,6 +8,21 @@ $this->bookDb = $bookDb; } + function getBookDetail(Request $request) { + $id = (int)$request->params["book_id"]; + $books = $this->bookDb->getBookById($id); + if ($books) { + writeResponse(200, "Success get book detail", $books); + } else { + writeResponse(500, 'Failed get book detail'); + } + } + + function searchBook(Request $request) { + $title = $request->queries["title"]; + $books = $this->bookDb->getBookByTitle($title); + writeResponse(200, "Success get book by title : %" + $title + "%", $books); + } } ?> \ No newline at end of file diff --git a/app/controller/book/routes.php b/app/controller/book/routes.php index e88aad34104d7b0569ffe7ed0eea97af218c544c..78e079e0b2a22d4c1e6226debf93e8177b2e6fee 100644 --- a/app/controller/book/routes.php +++ b/app/controller/book/routes.php @@ -1,6 +1,8 @@ <?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("/", "GET", $homepage, $middlewareExample); // $router->add("/book/:book_id/user/:user_id/", "POST", $postCallbackExample, $middlewareExample); // $router->add("/book/:book_id/", "POST", $postCallbackExample, $middlewareExample); diff --git a/app/controller/user/AuthMiddleware.php b/app/controller/user/AuthMiddleware.php index 01670519edf107f8f31a71d7686582f501330a31..baa8c596cf88199775712145afde94a56f2fec8d 100644 --- a/app/controller/user/AuthMiddleware.php +++ b/app/controller/user/AuthMiddleware.php @@ -8,7 +8,7 @@ if ($_COOKIE["Authorization"]){ $user_id = verifyJWT($_COOKIE["Authorization"]); if ($user_id){ - setcookie("user_id", $user_id, time()+300, "/"); + setcookie("user_id", $user_id, time()+30000, "/"); $next($nextRequest); exit; } diff --git a/app/view/browse.php b/app/view/browse.php index 13fa810a02bbffc76007a1fb0ba408edd8d104c2..a68f53d2fbb14b94034f75655a91267d7a4142c3 100644 --- a/app/view/browse.php +++ b/app/view/browse.php @@ -1,5 +1,6 @@ <?php include __STATIC__.'/html/header.html'; + include __STATIC__.'/html/browse.html'; // Include content here; include __STATIC__.'/html/footer.html'; // echo $_COOKIE["user"]; diff --git a/app/view/homepage.php b/app/view/homepage.php new file mode 100644 index 0000000000000000000000000000000000000000..6eb1b261df2376755d8602a430f0bf56dca18019 --- /dev/null +++ b/app/view/homepage.php @@ -0,0 +1,13 @@ +<?php + $homepage = function() { + include __VIEW__.'/static/html/header.html'; + include __VIEW__.'/static/html/browse.html'; + if ($_COOKIE["user"]){ + echo $_COOKIE["user"]; + setcookie("user","", time()-3600); + } else { + echo "gada cookie"; + setcookie("user",1); + } + } +?> \ No newline at end of file diff --git a/app/view/result.php b/app/view/result.php new file mode 100644 index 0000000000000000000000000000000000000000..9fae434fbaf4985cca6024790f7187cfb53b15b2 --- /dev/null +++ b/app/view/result.php @@ -0,0 +1,7 @@ +<?php + include __STATIC__.'/html/header.html'; + include __STATIC__.'/html/search_result.html'; + // Include content here; + include __STATIC__.'/html/footer.html'; + // echo $_COOKIE["user"]; +?> \ No newline at end of file diff --git a/config/devel.php b/config/devel.php new file mode 100644 index 0000000000000000000000000000000000000000..3da4d5bcf4b2498ad3aaad777e214abdd84675a2 --- /dev/null +++ b/config/devel.php @@ -0,0 +1,11 @@ +<?php + return array( + "db" => array( + "host" => "localhost", + "user" => "root", + "password" => "06071998", + "db_name" => "tayo_book_store" + ), + "base_url" => "localhost:4000/" + ) +?> \ No newline at end of file diff --git a/public/index.php b/public/index.php index 814ba6524504a70dff08a2790c8d2b43d51ee2bc..660a71f204f03f0c7742e0a2a923a0fd6255f27c 100644 --- a/public/index.php +++ b/public/index.php @@ -32,12 +32,16 @@ $authMiddleware = new AuthMiddleware(); $userDb = new UserDb($conn); $userUsecase = new UserUsecase($userDb); + $bookDb = new BookDb($conn); + $bookUsecase = new BookUsecase($bookDb); $router = new Router(); // TODO: Add Book Routes $router = addUserRoutes($router, $userUsecase); - + $router = addBookRoutes($router, $bookUsecase); + $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("/register/", "GET", 'render', null, 'register.php'); $router->add("/history/", "GET", 'render', $authMiddleware, 'history.php'); diff --git a/public/static/css/base.css b/public/static/css/base.css index af4810b81d17a30dde55ef408ff21f41425f0ab8..52e1e1158848e1b972dd12f311febabf4c1be4ce 100644 --- a/public/static/css/base.css +++ b/public/static/css/base.css @@ -2,4 +2,38 @@ body { height: 100%; margin:0 auto; padding:0px; +} + +.title { + font-size: 55px; + color: #f26600; +} + +.title h1 { + margin-top: 0px; + margin-bottom: 30px; +} + +.content { + margin-left: 20px; + margin-top: 40px; + margin-right: 20px; + padding-left: 20px; + padding-right: 20px; +} + +.justify-content-end { + display: flex; + justify-content: flex-end; +} + +.justify-content-between { + display: flex; + justify-content: space-between; +} + +.row { + display: flex; + flex-direction: row; + flex-wrap: wrap; } \ No newline at end of file diff --git a/public/static/css/browse.css b/public/static/css/browse.css new file mode 100644 index 0000000000000000000000000000000000000000..59d17d5c6a82669b3b778ee7b6ab24cb479155f3 --- /dev/null +++ b/public/static/css/browse.css @@ -0,0 +1,22 @@ +.search-book { + height: 40px; + width: 100%; + border: 3px solid; + border-color: #898989; + border-radius: 6px; + + margin-bottom: 30px; + padding: 10px; +} + +.button-search-book { + width: 150px; + height: 40px; + background-color: #00aeef; + border-color: #00aeef; +} + +.button-search-book span { + color: white; + font-size: 20px; +} \ No newline at end of file diff --git a/public/static/css/search_result.css b/public/static/css/search_result.css new file mode 100644 index 0000000000000000000000000000000000000000..18555d764d6b90143181d88e82be06d00772e312 --- /dev/null +++ b/public/static/css/search_result.css @@ -0,0 +1,75 @@ +.search-result-title { + display: flex; + flex-direction: row; + justify-content: space-between; +} + +.search-result-title h1, .search-result-title h2{ + align-self: flex-end; +} + +.search-result-title h1 { + font-size: 110px; + margin-bottom: 45px; + margin-top: 10px; +} + +.search-result-title h2 { + margin-bottom: 65px; + font-size: 30px; + color: #4c4c4c; +} + +.result { + margin-bottom: 20px; +} + +.result-img { + width: 25%; +} + +.result-content { + width: 70%; +} + +.result-title { + color: #f26600; + + margin-top: 0px; + margin-bottom: 0px; +} + +.result-author { + color: #4d4d4d; + + margin-top: 0px; + margin-bottom: 0px; +} + +.result-description { + color: #4c4c4c; + font-size: 20px; +} + +.result button { + width: 120px; + height: 35px; + background-color: #00aeef; + border-color: #00aeef; + border-radius: 5px; +} + +.result button span { + color: white; + font-size: 18px; +} + +.result-img { + padding-left: 20px; + padding-right: 20px; +} + +.result-img img { + width: 100%; + height: auto; +} \ No newline at end of file diff --git a/public/static/html/browse.html b/public/static/html/browse.html new file mode 100644 index 0000000000000000000000000000000000000000..312bfbee3a82cd5a2fa342780b29332394af7d01 --- /dev/null +++ b/public/static/html/browse.html @@ -0,0 +1,21 @@ +<link rel="stylesheet" href="/static/css/base.css"> +<link rel="stylesheet" href="/static/css/browse.css"> +<div class="content"> + <div class="title"> + <h1> + Search Book + </h1> + </div> + <input + type="text" + class="search-book" + name="search-book" + id="search-book" + placeholder="Input search terms..." + /> + <div class="justify-content-end"> + <button class="button-search-book" id="button-search-book"><span>Search</span></button> + </div> +</div> + +<script src="/static/js/browse.js"></script> \ No newline at end of file diff --git a/public/static/html/header.html b/public/static/html/header.html index 12ea60763c5d68b743cd8313ff64996c4ee37487..385e947255884e0776eacd18caed5d0755d27d42 100644 --- a/public/static/html/header.html +++ b/public/static/html/header.html @@ -1,3 +1,4 @@ +<meta name="viewport" content="width=device-width, initial-scale=1.0"> <link rel="stylesheet" href="/static/css/base.css"> <link rel="stylesheet" href="/static/css/header.css"> <nav> diff --git a/public/static/html/search_result.html b/public/static/html/search_result.html new file mode 100644 index 0000000000000000000000000000000000000000..0adff9a65c188ff83bd7c8765b542dd66af7ca9b --- /dev/null +++ b/public/static/html/search_result.html @@ -0,0 +1,12 @@ +<link rel="stylesheet" href="/static/css/base.css"> +<link rel="stylesheet" href="/static/css/search_result.css"> +<div class="content search-result"> + <div class="search-result-title row"> + <h1 class="title">Search Result</h1> + <h2 id="number_results"></h2> + </div> + <div id="results"> + </div> +</div> + +<script src="/static/js/result.js"></script> \ No newline at end of file diff --git a/public/static/img/contoh_buku.png b/public/static/img/contoh_buku.png new file mode 100644 index 0000000000000000000000000000000000000000..884a518a5827507962d3dcd239bd6f07f9a6c750 Binary files /dev/null and b/public/static/img/contoh_buku.png differ diff --git a/public/static/js/browse.js b/public/static/js/browse.js new file mode 100644 index 0000000000000000000000000000000000000000..7cd5bca1cd1dedb1cf12f46f9a54627fa9a318f3 --- /dev/null +++ b/public/static/js/browse.js @@ -0,0 +1,10 @@ +const doSearchBook = function() { + const terms = document.getElementById("search-book").value; + + window.location.replace(`http://localhost:4000/results/?title=${terms}`); +} + +window.onload = function(){ + let searchBookButton = document.getElementById("button-search-book"); + searchBookButton.onclick = doSearchBook; +} \ No newline at end of file diff --git a/public/static/js/login.js b/public/static/js/login.js index 9ba787e6d97044d812bff7735ec24792cdf6c2c4..0570521fef7e0e290c7a553c86739ca63a69df1f 100644 --- a/public/static/js/login.js +++ b/public/static/js/login.js @@ -9,7 +9,7 @@ const doLogin = function(){ doAjax('/auth/login/',"POST", payload, function(response){ console.log(response); - setCookie("Authorization", response.data.token, 300); + setCookie("Authorization", response.data.token, 30000); window.location.replace('http://localhost:4000/browse/'); }, function(response){ alert("Invalid username or password"); diff --git a/public/static/js/result.js b/public/static/js/result.js new file mode 100644 index 0000000000000000000000000000000000000000..f8feb86dbb3ba23ce29e61746eb5821fb2fc50a1 --- /dev/null +++ b/public/static/js/result.js @@ -0,0 +1,40 @@ +const getBookResult = function() { + let url_string = window.location.href; + let url = new URL(url_string); + let terms = url.searchParams.get("title"); + let results = document.getElementById("results"); + let number_results = document.getElementById("number_results"); + + doAjax(`/api/book/?title=${terms}`, "GET", null, function(response) { + if (response.data) { + number_results.innerHTML = `Found ${response.data.length} result(s)` + response.data.forEach(book => { + results.innerHTML += ( + `<div class="result row justify-content-between"> + <div class="result-img"> + <img + src="/static/img/contoh_buku.png" + /> + </div> + <div class="result-content"> + <h1 class="result-title">${book.title}</h1> + <h2 class="result-author">${book.author} - 4.3/5.0 (1 votes)</h2> + <p class="result-description"> + ${book.description} + </p> + <div class="justify-content-end"> + <button type="submit"><span>Detail</span></button> + </div> + </div> + </div>` + ) + }); + } else { + number_results.innerHTML = `Found 0 result(s)`; + } + }); +} + +window.onload = function() { + getBookResult(); +} \ No newline at end of file diff --git a/public/static/js/util.js b/public/static/js/util.js index 3061acc04672a6eb8530933ce6a8c845b69145ed..03fe828dcbfb35144a49a63ff526bcbb987e07d7 100644 --- a/public/static/js/util.js +++ b/public/static/js/util.js @@ -27,7 +27,8 @@ function doAjax(url, method, data, successCallback,failCallback) { req.open(method, url, true); req.setRequestHeader("Content-Type", "application/json"); req.onreadystatechange = function(){ - if (this.status==200){ + if (this.readyState == 4 && this.status==200){ + console.log('haha') successCallback(JSON.parse(req.responseText)); } else { console.log(req.responseText);