Skip to content
Snippets Groups Projects
Commit 6e3ec817 authored by Angela Livia Arumsari's avatar Angela Livia Arumsari
Browse files

feat: add endpoint for polling film, modify add and update film

parent fc6efddb
Branches
Tags
1 merge request!2endpoint for polling and get image file, fix search issue, modify db
......@@ -40,6 +40,7 @@ class App
$this->router->addRoute('/register', RegisterController::class);
$this->router->addRoute('/films', FilmController::class);
$this->router->addRoute('/film-details', FilmController::class);
$this->router->addRoute('/film-polling', FilmController::class);
$this->router->addRoute('/search', FilmController::class);
$this->router->addRoute('/add-film', CreateFilmController::class);
$this->router->addRoute('/update-film', UpdateFilmController::class);
......
......@@ -27,21 +27,35 @@ class FilmController extends BaseController
protected function get($urlParams)
{
$uri = Request::getURL();
$page = (isset($_GET['page']) and (int) $_GET['page'] >= 1) ? $_GET['page'] : 1;
$word = $_GET['q'] ?? "";
$genre = $_GET['genre'] ?? 'all';
$released_year = $_GET['year'] ?? 'all';
$isDesc = $_GET['sort'] ?? "asc";
$order = $_GET['order'] ?? 'title';
$data = $this->service->searchAndFilter($word, $order, $isDesc, $genre, $released_year, $page);
$row_count = $this->service->countRowBySearchAndFilter($word, $genre, $released_year);
if ($uri == "/films" || $uri == '/search') {
$page = (isset($_GET['page']) and (int) $_GET['page'] >= 1) ? $_GET['page'] : 1;
$word = $_GET['q'] ?? "";
$genre = $_GET['genre'] ?? 'all';
$released_year = $_GET['year'] ?? 'all';
$isDesc = $_GET['sort'] ?? "asc";
$order = $_GET['order'] ?? 'title';
$data = $this->service->searchAndFilter($word, $order, $isDesc, $genre, $released_year, $page);
$row_count = $this->service->countRowBySearchAndFilter($word, $genre, $released_year);
if ($uri == "/films") {
$data['genres'] = $this->service->getAllCategoryValues('genre');
$data['released_years'] = $this->service->getAllCategoryValues('released_year');
$data['total_page'] = ceil($row_count / 10);
if ($uri == '/films') {
$data['genres'] = $this->service->getAllCategoryValues('genre');
$data['released_years'] = $this->service->getAllCategoryValues('released_year');
$data['total_page'] = ceil($row_count / 10);
parent::render($data, 'films', "layouts/base");
}
else {
$films = [];
foreach ($data['films'] as $film) {
$films[] = $film->toResponse();
}
$data['films'] = $films;
$data['total_page'] = ceil($row_count / 10);
response::send_json_response($data);
}
parent::render($data, 'films', "layouts/base");
} elseif ($uri == '/film-details') {
$data['film'] = $this->service->getById($_GET['film_id']);
if (isset($_SESSION['user_id'])) {
......@@ -51,16 +65,15 @@ class FilmController extends BaseController
}
parent::render($data, 'film-details', "layouts/base");
} else {
} else if ($uri == '/film-polling') {
$isInitialSync = $_GET['initial'] ?? 'no';
error_log($isInitialSync);
$data = $this->service->polling($isInitialSync);
$films = [];
foreach ($data['films'] as $film) {
foreach ($data as $film) {
$films[] = $film->toResponse();
}
$data['films'] = $films;
$data['total_page'] = ceil($row_count / 10);
response::send_json_response($data);
response::send_json_response($films);
}
}
......
......@@ -165,6 +165,9 @@ class UpdateFilmController extends BaseController
}
// Call service
$currentDateTime = new \DateTime('now', new \DateTimeZone('UTC'));
$formattedDateTime = $currentDateTime->format("Y-m-d H:i:s");
$data["last_updated"] = $formattedDateTime;
$filmModel = new FilmModel();
$filmModel->constructFromArray($data);
$response = $this->service->update($filmModel);
......
......@@ -29,6 +29,8 @@ class FilmService extends BaseService
public function add($image_path, $trailer_path, $title, $released_year, $director, $description, $cast, $genre)
{
$currentDateTime = new \DateTime('now', new \DateTimeZone('UTC'));
$formattedDateTime = $currentDateTime->format("Y-m-d H:i:s");
$film = new FilmModel();
$film
->set('image_path', $image_path)
......@@ -39,7 +41,8 @@ class FilmService extends BaseService
->set('trailer_path', $trailer_path)
->set('description', $description)
->set('cast', $cast)
->set('genre', $genre);
->set('genre', $genre)
->set('last_updated', $formattedDateTime);
$id = $this->repository->insert($film, array(
'image_path' => PDO::PARAM_STR,
......@@ -50,6 +53,7 @@ class FilmService extends BaseService
'description' => PDO::PARAM_STR,
'cast' => PDO::PARAM_STR,
'genre' => PDO::PARAM_STR,
'last_updated'=> PDO::PARAM_STR,
));
$response = $this->repository->getById($id);
......@@ -81,6 +85,7 @@ class FilmService extends BaseService
$arrParams['description'] = PDO::PARAM_STR;
$arrParams['cast'] = PDO::PARAM_STR;
$arrParams['genre'] = PDO::PARAM_STR;
$arrParams['last_updated'] = PDO::PARAM_STR;
return $this->repository->update($film, $arrParams);
}
......@@ -105,6 +110,17 @@ class FilmService extends BaseService
return $data;
}
public function polling($isInitialSync)
{
$response = $this->repository->getAllBySearchAndFilter(null, null, null, null, null, null, null, $isInitialSync);
$films = [];
foreach ($response as $resp) {
$film = new FilmModel();
$films[] = $film->constructFromArray($resp);
}
return $films;
}
public function getAllCategoryValues($category)
{
return $this->repository->getAllCategoryValues($category);
......
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