diff --git a/src/public/js/editCatalog.js b/src/public/js/editCatalog.js index 8bb3c98aeb14f26d90a962d33e8e394f399ee861..b3003d15ae6b55e88cd471ee6fcd51d530349e3e 100644 --- a/src/public/js/editCatalog.js +++ b/src/public/js/editCatalog.js @@ -12,10 +12,10 @@ function updateCatalog(form) { const xhttp = new XMLHttpRequest(); xhttp.open("POST", apiUrl, true); - xhttp.setRequestHeader("Content-Type", "multipart/form-data"); xhttp.onreadystatechange = function () { if (xhttp.readyState === 4) { + console.log(xhttp); if (xhttp.status === 200) { showToast("Success", "Catalog updated", "success"); setTimeout(() => { diff --git a/src/server/app/Controller/CatalogController.php b/src/server/app/Controller/CatalogController.php index 10360e6d24387fb9011f75d69bac0ab1752736d6..97d602b89c9f238c383a9f8abdcfa25b91c23a9f 100644 --- a/src/server/app/Controller/CatalogController.php +++ b/src/server/app/Controller/CatalogController.php @@ -12,6 +12,7 @@ require_once __DIR__ . '/../Repository/UserRepository.php'; require_once __DIR__ . '/../Repository/SessionRepository.php'; require_once __DIR__ . '/../Model/CatalogCreateRequest.php'; +require_once __DIR__ . '/../Model/catalog/CatalogUpdateRequest.php'; require_once __DIR__ . '/../Model/CatalogSearchRequest.php'; class CatalogController @@ -151,7 +152,7 @@ class CatalogController public function postEdit($uuid): void { - $request = new CatalogCreateRequest(); + $request = new CatalogUpdateRequest(); $request->title = $_POST['title']; $request->description = $_POST['description']; $request->category = $_POST['category']; @@ -218,17 +219,11 @@ class CatalogController throw new ValidationException("You are not authorized to update this catalog."); } - $json = file_get_contents('php://input'); - $data = json_decode($json); - - if ($data === null) { - throw new ValidationException("Invalid request body."); - } + $request = new CatalogUpdateRequest(); - $request = new CatalogCreateRequest(); - $request->title = $data->title; - $request->description = $data->description; - $request->category = $data->category; + $request->title = $_POST['title']; + $request->description = $_POST['description']; + $request->category = $_POST['category']; if (isset($_FILES['poster'])) { $request->poster = $_FILES['poster']; @@ -239,6 +234,13 @@ class CatalogController } $this->catalogService->update($uuid, $request); + http_response_code(200); + $response = [ + "status" => 200, + "message" => "Successfully update catalog", + ]; + + echo json_encode($response); } catch (ValidationException $exception) { http_response_code(400); $response = [ diff --git a/src/server/app/Model/CatalogCreateRequest.php b/src/server/app/Model/CatalogCreateRequest.php index 9ceb0f3fec592f76e970f195c11980c4414cf624..13ccdaaa0c8144b8a6bbf65bf64d56bee001b159 100644 --- a/src/server/app/Model/CatalogCreateRequest.php +++ b/src/server/app/Model/CatalogCreateRequest.php @@ -2,9 +2,9 @@ class CatalogCreateRequest { - public ?string $title = null; - public ?string $description = null; + public string $title; + public string $description; public $poster = null; public $trailer = null; - public ?string $category = null; + public string $category; } \ No newline at end of file diff --git a/src/server/app/Model/catalog/CatalogUpdateRequest.php b/src/server/app/Model/catalog/CatalogUpdateRequest.php new file mode 100644 index 0000000000000000000000000000000000000000..a7a004baca1b0d3ba1c1ebc3cfb8cc8839d1abb1 --- /dev/null +++ b/src/server/app/Model/catalog/CatalogUpdateRequest.php @@ -0,0 +1,10 @@ +<?php + +class CatalogUpdateRequest +{ + public string $title; + public string $description; + public $poster = null; + public $trailer = null; + public string $category; +} \ No newline at end of file diff --git a/src/server/app/Service/CatalogService.php b/src/server/app/Service/CatalogService.php index 5c1b3f3d28f03304405e4b64f940287a814d0f10..ac82f4f5ceed7e98a53eb9a3476231f8e0a743d8 100644 --- a/src/server/app/Service/CatalogService.php +++ b/src/server/app/Service/CatalogService.php @@ -6,6 +6,7 @@ require_once __DIR__ . '/../Utils/FileUploader.php'; require_once __DIR__ . '/../Utils/UUIDGenerator.php'; require_once __DIR__ . '/../Model/CatalogCreateRequest.php'; +require_once __DIR__ . '/../Model/catalog/CatalogUpdateRequest.php'; require_once __DIR__ . '/../Model/CatalogSearchRequest.php'; require_once __DIR__ . '/../Model/CatalogCreateResponse.php'; @@ -116,7 +117,7 @@ class CatalogService } } - public function update(string $uuid, CatalogCreateRequest $request) + public function update(string $uuid, CatalogUpdateRequest $request) { try { Database::beginTransaction(); @@ -152,6 +153,19 @@ class CatalogService } } + private function validateCatalogUpdateRequest(CatalogUpdateRequest $request) + { + if ( + $request->title == null || trim($request->title) == "" + ) { + throw new ValidationException("Title cannot be blank."); + } + + if ($request->category == null || trim($request->category) == "") { + throw new ValidationException("Category cannot be blank."); + } + } + public function search(CatalogSearchRequest $catalogSearchRequest): CatalogSearchResponse { $this->validateCatalogSearchRequest($catalogSearchRequest);