diff --git a/src/app/components/podcast/page.php b/src/app/components/podcast/page.php index af5c57c113ed9d0bd8feed01e6bd3db2ea15ad95..1c127e60e244cafef64c2fba7f66a789fa82e800 100644 --- a/src/app/components/podcast/page.php +++ b/src/app/components/podcast/page.php @@ -7,7 +7,7 @@ <link rel="stylesheet" type="text/css" href="<?= BASE_URL ?>/styles/globals.css"> <link rel="stylesheet" type="text/css" href="<?= BASE_URL ?>/styles/podcast/style.css"> - <script type="text/javascript" src="<?= BASE_URL ?>/javascript/podcast/script.js" defer> + <script type="module" src="<?= BASE_URL ?>/javascript/podcast/script.js" defer> </script> <title>Main Dashboard</title> </head> @@ -38,11 +38,13 @@ <p>Add To Library</p> </button> - <ul id="library-choices" class="hidden"> + <ul id="library-choices" class="hidden" data-id-podcast="<?=$this->data["podcast"]->id_podcast ?>"> <?php foreach ($this->data["libraries"] as $library) : ?> <!-- Ini sekarang pake li tapi ntar kalo mau diganti jadi button atau a bisa juga --> <li> - <p class="b3"><?= $library ?></p> + <p class="b3 playlist" + data-id="<?= $library->id_playlist?>"> + <?= $library->title ?></p> </li> <?php endforeach; ?> </ul> diff --git a/src/app/controllers/login/get_login.php b/src/app/controllers/login/get_login.php index f1f7fd41aa629ddb6506166a96347d54cd1ee8a9..44627b028f597b1a2d46150bb2d66b8a45b41171 100644 --- a/src/app/controllers/login/get_login.php +++ b/src/app/controllers/login/get_login.php @@ -10,7 +10,8 @@ class GetLoginController if(isset($_SESSION['user_id'])){ - header("Location: http://localhost:8080/public/home?user_id=" . $_SESSION['user_id']); + header("Location: " . BASE_URL . "/home?user_id=" . $_SESSION["user_id"]); + }else{ $data = []; $view = new LoginView($data); diff --git a/src/app/controllers/login/post_login.php b/src/app/controllers/login/post_login.php index 1d1dbb1a70c84628dc63aae0e065cf7eb2aed394..b063c411299f144c64efe2be5c6f90fba8f6a25b 100644 --- a/src/app/controllers/login/post_login.php +++ b/src/app/controllers/login/post_login.php @@ -4,8 +4,7 @@ class PostLoginController { public function call() { - require_once __DIR__ . "/../../views/login/login.php"; - require_once __DIR__ . "/../../models/user.php"; + // require_once __DIR__ . "/../../views/login/login.php"; session_start(); if(isset($_POST['username']) && isset($_POST['password'])){ @@ -15,7 +14,7 @@ class PostLoginController $model = new UserModel(); try{ - $user = $model->getUser($_POST['username']); + $user = $model->getUser($username); $user = json_decode(json_encode($user), true); if(!$user){ diff --git a/src/app/controllers/podcast/get_page.php b/src/app/controllers/podcast/get_page.php index 12bb94de4ff454bd2aa4b076e84e2b7208d1e561..3f82502c59c5d06caa59daa6d7e9aebf01bd2848 100644 --- a/src/app/controllers/podcast/get_page.php +++ b/src/app/controllers/podcast/get_page.php @@ -4,6 +4,7 @@ class GetPodcastPageController { public function call() { + session_start(); if (!isset($_SESSION["user_id"])) { http_response_code(403); @@ -24,14 +25,17 @@ class GetPodcastPageController $podcastModel = new PodcastModel(); $episodeModel = new EpisodeModel(); + $playlistModel = new PlaylistModel(); $podcast = $podcastModel->getById($idPodcast); $episodes = $episodeModel->getByPodcastId($idPodcast); + $playlists = $playlistModel->getUserPlaylist($_SESSION["user_id"]); + $data = [ "podcast" => $podcast, "episodes" => $episodes, - "libraries" => ["Contoh Library 1", "Contoh Library 2", "Contoh Library 3"] + "libraries" => $playlists, ]; $view = new PodcastPageView($data); diff --git a/src/app/controllers/podcast/post_page.php b/src/app/controllers/podcast/post_page.php new file mode 100644 index 0000000000000000000000000000000000000000..1a62717a81319e89024bd7aefc657ff802c15271 --- /dev/null +++ b/src/app/controllers/podcast/post_page.php @@ -0,0 +1,38 @@ +<?php + +class PostPodcastPageController +{ + public function call() + { + session_start(); + if(!isset($_SESSION["user_id"])){ + header("Location: " . BASE_URL . "/login"); + + return; + } + + $idPodcast = ""; + $idPlaylist = ""; + if(isset($_POST["id_playlist"]) && isset($_POST["id_podcast"])){ + $idPlaylist = $_POST["id_playlist"]; + $idPodcast = $_POST["id_podcast"]; + } + + $model = new PlaylistModel(); + + try{ + $model->addPodcastToPlaylist($idPlaylist, $idPodcast); + http_response_code(201); + exit; + + }catch(PDOException $e){ + // duplicate entries, violate integrity constraint (SQL code 23000) + if($e->getCode() === 23000){ + http_response_code(203); + }else{ // other type of error causing failure + http_response_code(200); + }; + exit; + } + } +} diff --git a/src/app/controllers/signup/get_signup.php b/src/app/controllers/signup/get_signup.php index 83adadb948f34900d76d4b25be1aae06a9ea124e..a680a69873413ce8b9ca1413b50e5b3011c5be9a 100644 --- a/src/app/controllers/signup/get_signup.php +++ b/src/app/controllers/signup/get_signup.php @@ -4,6 +4,8 @@ class GetSignupController { public function call() { + session_start(); + if(!isset($_SESSION['user_id'])){ require_once __DIR__ . "/../../views/signup/signup.php"; @@ -11,7 +13,7 @@ class GetSignupController $view = new SignupView($data); $view->render(); }else{ - header("Location: " . BASE_URL . "/home"); + header("Location: " . BASE_URL . "/home?user_id=" . $_SESSION["user_id"]); } } diff --git a/src/app/controllers/signup/post_signup.php b/src/app/controllers/signup/post_signup.php index cb5331b1ec84097f1f3e786ca89a5ff0ea274238..c9bafbd92257385eb0bc1add1db90306b4282a9c 100644 --- a/src/app/controllers/signup/post_signup.php +++ b/src/app/controllers/signup/post_signup.php @@ -5,7 +5,6 @@ class PostSignupController public function call() { require_once __DIR__ . "/../../views/signup/signup.php"; - require_once __DIR__ . "/../../models/user.php"; session_start(); if(!isset($_SESSION['user_id'])){ diff --git a/src/app/core/app.php b/src/app/core/app.php index 84324758f190492eb357f91e9147ebab99974514..f9ae5cd6309ef3b8936187ba6eab4e3a7f5df84c 100644 --- a/src/app/core/app.php +++ b/src/app/core/app.php @@ -28,6 +28,8 @@ class App $router->delete("public/dashboard/podcast", new DeletePodcastController()); $router->get("public/podcast", new GetPodcastPageController()); + $router->post("public/podcast", new PostPodcastPageController()); + $router->post("public/logout", new LogoutController()); diff --git a/src/app/init.php b/src/app/init.php index a984ef3b9c391d1bf4ebff7fd6a1e7c0ae97011d..7e17c3d2699de94ff7eec4a6b743b14fc846129d 100644 --- a/src/app/init.php +++ b/src/app/init.php @@ -22,6 +22,8 @@ require_once __DIR__ . "/controllers/dashboard/post_edit_podcast.php"; require_once __DIR__ . "/controllers/dashboard/delete_podcast.php"; require_once __DIR__ . "/controllers/podcast/get_page.php"; +require_once __DIR__ . "/controllers/podcast/post_page.php"; + require_once __DIR__ . "/controllers/logout/logout.php"; @@ -39,3 +41,4 @@ require_once __DIR__ . "/controllers/search/get_search.php"; require_once __DIR__ . "/models/podcast.php"; require_once __DIR__ . "/models/episode.php"; require_once __DIR__ . "/models/user.php"; +require_once __DIR__ . "/models/playlist.php"; diff --git a/src/app/models/playlist.php b/src/app/models/playlist.php index e8b6b0533abb955f4ae904be2dbe05fcc04581ad..ba55193a3ebd0d41d076c6eba58a88a65414e2be 100644 --- a/src/app/models/playlist.php +++ b/src/app/models/playlist.php @@ -37,4 +37,61 @@ class PlaylistModel return $playlistPodcast; } + public function addPlaylist($id_user, $title, $id_playlist) + { + $query = " + INSERT INTO playlist (id_playlist, title, id_user) + VALUES(:id_playlist, :title, :id_user) + "; + + $this->db->query($query); + $this->db->bind("id_user", $id_user); + $this->db->bind("title", $title); + $this->db->bind("id_playlist", $id_playlist); + + $this->db->execute(); + } + + public function addPodcastToPlaylist($id_playlist, $id_podcast) + { + $query = " + INSERT INTO podcast_x_playlist (id_playlist, id_podcast) + VALUES(:id_playlist, :id_podcast) + "; + + $this->db->query($query); + $this->db->bind("id_playlist", $id_playlist); + $this->db->bind("id_podcast", $id_podcast); + + $this->db->execute(); + + } + + public function deletePlaylist($id_playlist) + { + $query = " + DELETE FROM playlist + WHERE id_playlist = :id_playlist + "; + + $this->db->query($query); + $this->db->bind("id_playlist", $id_playlist); + + $this->db->execute(); + } + + public function removePodcastFromPlaylist($id_podcast, $id_playlist) + { + $query = " + DELETE FROM podcast_x_playlist + WHERE id_playlist = :id_playlist AND id_podcast = :id_podcast + "; + + $this->db->query($query); + $this->db->bind("id_playlist", $id_playlist); + $this->db->bind("id_podcast", $id_podcast); + + $this->db->execute(); + } + } diff --git a/src/app/models/podcast.php b/src/app/models/podcast.php index e77bc641d91dcaee11fa3cd5aa89de56660cac2a..ee193721999d951ed6222d120400ee809555baf9 100644 --- a/src/app/models/podcast.php +++ b/src/app/models/podcast.php @@ -139,18 +139,18 @@ class PodcastModel return $podcasts; } - public function getPodcast($id_podcast) { - $query = " - SELECT * FROM podcast - WHERE id_podcast = :id_podcast - "; - - $this->db->query($query); - $this->db->bind("id_podcast", $id_podcast); - $podcast = $this->db->fetchAll(); - - return $podcast; - } + // public function getPodcast($id_podcast) { + // $query = " + // SELECT * FROM podcast + // WHERE id_podcast = :id_podcast + // "; + + // $this->db->query($query); + // $this->db->bind("id_podcast", $id_podcast); + // $podcast = $this->db->fetchAll(); + + // return $podcast; + // } public function getById($idPodcast) { diff --git a/src/public/javascript/podcast/script.js b/src/public/javascript/podcast/script.js index b1dd6defd8e22f8a8f396a15231d0703892d4203..0d98d9eb18c1a93f8c2804557b7d170c134386a6 100644 --- a/src/public/javascript/podcast/script.js +++ b/src/public/javascript/podcast/script.js @@ -1,10 +1,13 @@ "use strict"; +import { showSuccessToast, showErrorToast, showInformationToast } from "../toast.mjs"; + // Get DOM elements const playButtonsEl = document.querySelectorAll(".play-button"); const addLibraryButtonEl = document.getElementById("add-library-btn") const libraryChoicesEl = document.getElementById("library-choices") const overlayEl = document.getElementById("overlay-library") +const playlists = document.querySelectorAll(".playlist"); // Handle play episode Array.from(playButtonsEl).forEach((el) => { @@ -31,3 +34,35 @@ overlayEl.addEventListener("click", (e) => { libraryChoicesEl.classList.toggle("hidden"); overlayEl.classList.toggle("hidden"); }); + + +if(playlists.length !== 0){ + playlists.forEach(function(playlist){ + playlist.addEventListener("click", (e) => { + e.preventDefault(); + + const data = new FormData(); + + const xhr = new XMLHttpRequest(); + + xhr.open("POST", "/public/podcast", true); + + data.append("id_playlist", playlist.dataset.id); + data.append("id_podcast", libraryChoicesEl.dataset.idPodcast); + + xhr.onload = function () { + if (xhr.readyState === XMLHttpRequest.DONE) { + if (xhr.status === 201) { + showSuccessToast("Podcast berhasil ditambahkan ke playlist!"); + }else if(xhr.status === 203){ + showInformationToast("Podcast sudah ada dalam playlist!") + } + else{ // status code 200, some other error + showErrorToast("Podcast gagal ditambahkan ke playlist!"); + } + } + } + xhr.send(data); + }) + }) +} \ No newline at end of file