Skip to content
Snippets Groups Projects
Commit 788c7f56 authored by Kenneth Ezekiel's avatar Kenneth Ezekiel
Browse files

feat: user profile (update) and fix filmservice

parent 92980203
Branches
Tags
No related merge requests found
...@@ -128,6 +128,10 @@ li { ...@@ -128,6 +128,10 @@ li {
display: none; display: none;
} }
.logo {
font-weight: bolder;
}
@media screen and (max-width: 800px) { @media screen and (max-width: 800px) {
.mobile-view { .mobile-view {
display: flex; display: flex;
......
...@@ -7,6 +7,7 @@ use app\base\BaseController; ...@@ -7,6 +7,7 @@ use app\base\BaseController;
use app\controllers\CreateFilmController; use app\controllers\CreateFilmController;
use app\controllers\LoginController; use app\controllers\LoginController;
use app\controllers\MainController; use app\controllers\MainController;
use app\controllers\ProfileController;
use app\controllers\ReviewController; use app\controllers\ReviewController;
use app\controllers\RegisterController; use app\controllers\RegisterController;
use app\controllers\UpdateFilmController; use app\controllers\UpdateFilmController;
...@@ -36,5 +37,6 @@ class App ...@@ -36,5 +37,6 @@ class App
$this->router->addRoute('/register', RegisterController::class); $this->router->addRoute('/register', RegisterController::class);
$this->router->addRoute('/add-film', CreateFilmController::class); $this->router->addRoute('/add-film', CreateFilmController::class);
$this->router->addRoute('/update-film', UpdateFilmController::class); $this->router->addRoute('/update-film', UpdateFilmController::class);
$this->router->addRoute('/profile', ProfileController::class);
} }
} }
...@@ -48,8 +48,12 @@ abstract class BaseController ...@@ -48,8 +48,12 @@ abstract class BaseController
include_once __DIR__ . "/../../views/{$layout}.php"; include_once __DIR__ . "/../../views/{$layout}.php";
} }
protected static function redirect($url, $statusCode = 303) protected static function redirect($url, $data = [], $statusCode = 303)
{ {
header('Location: ' . $url, true, $statusCode); $params = "";
foreach ($data as $key => $value) {
$params .= "$key=$value&";
}
header('Location: ' . $url . "?" . $params, true, $statusCode);
} }
} }
...@@ -40,7 +40,7 @@ class CreateFilmController extends BaseController ...@@ -40,7 +40,7 @@ class CreateFilmController extends BaseController
{ {
if (!isset($_SESSION['role']) or $_SESSION['role'] != 'admin') { if (!isset($_SESSION['role']) or $_SESSION['role'] != 'admin') {
// TODO: make error controller // TODO: make error controller
parent::redirect("/error", 401); parent::redirect("/error", [], 401);
return; return;
} }
parent::render($urlParams, "create_film", "layouts/base"); parent::render($urlParams, "create_film", "layouts/base");
......
<?php
namespace app\controllers;
use app\base\BaseController;
use app\exceptions\BadRequestException;
use app\models\UserModel;
use app\Request;
use app\services\UserService;
use Exception;
class ProfileController extends BaseController
{
public function __construct()
{
parent::__construct(UserService::getInstance());
}
protected function get($urlParams)
{
$user = $this->service->getById($_SESSION['user_id']);
$data = [];
$data['email'] = $user->email;
$data['username'] = $user->username;
parent::render($data, "profile", "layouts/base");
}
protected function post($urlParams)
{
try {
$user = $this->service->getById($_SESSION['user_id']);
$old_pass = $user->password;
// Get data
$email = $_POST['email'];
$username = $_POST['username'];
$password = $_POST['password'] ? $_POST['password'] : $old_pass;
$confirm_password = $_POST['confirm-password'] ? $_POST['confirm-password'] : $old_pass;
if ($this->service->isEmailExist($email) and $user->email != $email) {
throw new BadRequestException("Email Already Exists!");
}
if ($this->service->isUsernameExist($username) and $user->username != $username) {
throw new BadRequestException("Username Already Exists!");
}
if ($password != $confirm_password) {
throw new BadRequestException("Password does not match!");
}
$user
->set('email', $email)
->set('username', $username)
->set('password', $_POST['password'] ? password_hash($password, PASSWORD_DEFAULT) : $password);
// Call service
$response = $this->service->update($user);
echo $response;
$msg = "";
$_SESSION['username'] = $username;
$msg = "Successfully updated profile!";
// Render response
parent::redirect("/", ["Msg" => $msg]);
} catch (Exception $e) {
$msg = $e->getMessage();
parent::render(["errorMsg" => $msg], "profile", "layouts/base");
}
}
}
...@@ -46,10 +46,17 @@ class UpdateFilmController extends BaseController ...@@ -46,10 +46,17 @@ class UpdateFilmController extends BaseController
return; return;
} }
$film_id = $urlParams['film-id']; $film_id = $urlParams['film_id'];
$film = $this->service->getById($film_id); $film = $this->service->getById($film_id);
$data = [];
parent::render($film, "update_film", "layouts/base"); $data['title'] = $film->title;
$data['released_year'] = $film->released_year;
$data['director'] = $film->director;
$data['description'] = $film->description;
$data['cast'] = $film->cast;
$data['genre'] = $film->genre;
parent::render($data, "update_film", "layouts/base");
} }
protected function post($urlParams) protected function post($urlParams)
...@@ -59,20 +66,22 @@ class UpdateFilmController extends BaseController ...@@ -59,20 +66,22 @@ class UpdateFilmController extends BaseController
return; return;
} }
try { try {
$film_id = $urlParams['film-id']; $film_id = $urlParams['film_id'];
$film = $this->service->getById($film_id); $film = $this->service->getById($film_id);
// Get data // Get data
$film['title'] = $_POST['title']; $data = [];
$film['released_year'] = $_POST['released-year']; $data['film_id'] = $film->film_id;
$film['director'] = $_POST['director']; $data['title'] = $_POST['title'];
$film['description'] = $_POST['description']; $data['released_year'] = $_POST['released-year'];
$film['cast'] = $_POST['cast']; $data['director'] = $_POST['director'];
$film['genre'] = $_POST['genre']; $data['description'] = $_POST['description'];
$data['cast'] = $_POST['cast'];
$data['genre'] = $_POST['genre'];
// Check if file is valid // Check if file is valid
if ($_FILES['image-path']['error'] == UPLOAD_ERR_NO_FILE) { if ($_FILES['image-path']['error'] == UPLOAD_ERR_NO_FILE) {
$image_path = $film['image_path']; $data['image_path'] = $film->image_path;
} else { } else {
if ($_FILES['image-path']['error'] == UPLOAD_ERR_OK) { if ($_FILES['image-path']['error'] == UPLOAD_ERR_OK) {
$image_tmp = $_FILES['image-path']['tmp_name']; $image_tmp = $_FILES['image-path']['tmp_name'];
...@@ -89,7 +98,7 @@ class UpdateFilmController extends BaseController ...@@ -89,7 +98,7 @@ class UpdateFilmController extends BaseController
} }
if ($_FILES['trailer-path']['error'] == UPLOAD_ERR_NO_FILE) { if ($_FILES['trailer-path']['error'] == UPLOAD_ERR_NO_FILE) {
$trailer_path = $film['trailer_path']; $data['trailer_path'] = $film->trailer_path;
} else { } else {
if ($_FILES['trailer-path']['error'] == UPLOAD_ERR_OK) { if ($_FILES['trailer-path']['error'] == UPLOAD_ERR_OK) {
$trailer_tmp = $_FILES['trailer-path']['tmp_name']; $trailer_tmp = $_FILES['trailer-path']['tmp_name'];
...@@ -107,15 +116,14 @@ class UpdateFilmController extends BaseController ...@@ -107,15 +116,14 @@ class UpdateFilmController extends BaseController
// Call service // Call service
$filmModel = new FilmModel(); $filmModel = new FilmModel();
$filmModel->constructFromArray($film); $filmModel->constructFromArray($data);
$response = $this->service->update($filmModel); $response = $this->service->update($filmModel);
if ($response) { if ($response) {
var_dump($response);
$msg = "Successfully updated film!"; $msg = "Successfully updated film!";
} }
// Render response // Render response
parent::render(["Msg" => $msg], "home", "layouts/base"); parent::redirect("/", ["Msg" => $msg]);
} catch (Exception $e) { } catch (Exception $e) {
$msg = $e->getMessage(); $msg = $e->getMessage();
parent::render(["errorMsg" => $msg], "create_film", "layouts/base"); parent::render(["errorMsg" => $msg], "create_film", "layouts/base");
......
...@@ -59,7 +59,15 @@ class FilmService extends BaseService ...@@ -59,7 +59,15 @@ class FilmService extends BaseService
public function getById($film_id) public function getById($film_id)
{ {
return $this->repository->getById($film_id); $film = $this->repository->getById($film_id);
if ($film) {
$filmModel = new filmModel();
$filmModel->constructFromArray($film);
return $filmModel;
}
return null;
} }
public function update($film) public function update($film)
......
...@@ -119,7 +119,11 @@ class UserService extends BaseService ...@@ -119,7 +119,11 @@ class UserService extends BaseService
{ {
// $user = (new UserModel())->set('nama', $nama)->set('username', $username)->set('email', $email)->set('password', password_hash($password, PASSWORD_DEFAULT)); // $user = (new UserModel())->set('nama', $nama)->set('username', $username)->set('email', $email)->set('password', password_hash($password, PASSWORD_DEFAULT));
$user = new UserModel(); $user = new UserModel();
$user->set('email', $email)->set('username', $username)->set('password', password_hash($password, PASSWORD_DEFAULT))->set('role', $role); $user
->set('email', $email)
->set('username', $username)
->set('password', password_hash($password, PASSWORD_DEFAULT))
->set('role', $role);
$id = $this->repository->insert($user, array( $id = $this->repository->insert($user, array(
'email' => PDO::PARAM_STR, 'email' => PDO::PARAM_STR,
...@@ -187,4 +191,15 @@ class UserService extends BaseService ...@@ -187,4 +191,15 @@ class UserService extends BaseService
return null; return null;
} }
public function update($user)
{
$arrParams = [];
$arrParams['user_id'] = PDO::PARAM_INT;
$arrParams['email'] = PDO::PARAM_STR;
$arrParams['username'] = PDO::PARAM_STR;
$arrParams['password'] = PDO::PARAM_STR;
$arrParams['role'] = PDO::PARAM_STR;
$this->repository->update($user, $arrParams);
}
} }
...@@ -10,6 +10,9 @@ ...@@ -10,6 +10,9 @@
<link rel="preconnect" href="https://fonts.googleapis.com"> <link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin> <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Open+Sans:ital,wght@0,300;0,400;0,500;0,600;0,700;0,800;1,300;1,400;1,500;1,600;1,700;1,800&display=swap" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=Open+Sans:ital,wght@0,300;0,400;0,500;0,600;0,700;0,800;1,300;1,400;1,500;1,600;1,700;1,800&display=swap" rel="stylesheet">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Open+Sans:ital,wght@0,300;0,400;0,500;0,600;0,700;0,800;1,300;1,400;1,500;1,600;1,700;1,800&family=Poppins:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;0,800;0,900;1,100;1,200;1,300;1,400;1,500;1,600;1,700;1,800;1,900&display=swap" rel="stylesheet">
<!-- <link rel="stylesheet" href="public/css/lib.css"> <!-- <link rel="stylesheet" href="public/css/lib.css">
<link rel="stylesheet" href="public/css/shared.css"> <link rel="stylesheet" href="public/css/shared.css">
<link rel="stylesheet" href="public/css/home.css"> --> <link rel="stylesheet" href="public/css/home.css"> -->
...@@ -27,7 +30,7 @@ ...@@ -27,7 +30,7 @@
<ul class='nav-links'> <ul class='nav-links'>
<div class='menu'> <div class='menu'>
<li class='menu-item'><a href='/'>Home</a></li> <li class='menu-item'><a href='/'>Home</a></li>
<li class='menu-item'><a href='/film'>Films</a></li> <li class='menu-item'><a href='/add-film'>Films</a></li>
<?php <?php
...@@ -42,11 +45,11 @@ ...@@ -42,11 +45,11 @@
} }
} else { } else {
$username = $_SESSION['username']; $username = $_SESSION['username'];
echo "<p class='profile'><a href='/'> <img src='/public/assets/person.svg'></img> <span>username</span></a></p>"; echo "<p class='profile'><a href='/profile'> <img src='/public/assets/person.svg'></img> <span>$username</span></a></p>";
echo "<button class='logout-button'><a href='/logout'>Logout</a></button>"; echo "<button class='logout-button'><a href='/logout'>Logout</a></button>";
} }
?> ?>
</div> </div>
</ul> </ul>
<!-- <div class='mobile-view'> <!-- <div class='mobile-view'>
......
<div class="form-container">
<h2 class="header-title">Profile</h2>
<p class="error-msg"><?php if (isset($errorMsg)) {
echo "$errorMsg";
} ?></p>
<form class="form" method="post" enctype="multipart/form-data">
<div class="form-group">
<label for="email">Email</label>
<br>
<input class="input" type="text" id="email" name="email" value="<?= $email ?>" required>
</div>
<div class="form-group">
<label for="username">Username</label>
<br>
<input class="input" type="text" id="username" name="username" value="<?= $username ?>" required>
</div>
<div class="form-group">
<label for="password">Password</label>
<br>
<input class="input" type="password" id="password" name="password">
</div>
<div class="form-group">
<label for="confirm-password">Confirm Password</label>
<br>
<input class="input" type="password" id="confirm-password" name="confirm-password">
</div>
<div class="form-group">
<button class="button" ctype="submit">Add</button>
</div>
</form>
</div>
\ No newline at end of file
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