diff --git a/appinfo/routes.php b/appinfo/routes.php index fec1b99a..1249347f 100644 --- a/appinfo/routes.php +++ b/appinfo/routes.php @@ -15,7 +15,7 @@ return [ ['name' => 'recipe#index', 'url' => '/recipes', 'verb' => 'GET'], ['name' => 'recipe#add', 'url' => '/add', 'verb' => 'POST'], ['name' => 'recipe#delete', 'url' => '/recipes/{id}', 'verb' => 'DELETE'], - ['name' => 'recipe#update', 'url' => '/recipes', 'verb' => 'PUT'], + ['name' => 'recipe#update', 'url' => '/recipes/{id}', 'verb' => 'PUT'], ['name' => 'recipe#get', 'url' => '/recipes/{id}', 'verb' => 'GET'], ['name' => 'recipe#image', 'url' => '/recipes/{id}/image', 'verb' => 'GET'], ['name' => 'recipe#reindex', 'url' => '/reindex', 'verb' => 'POST'], diff --git a/js/script.js b/js/script.js index bca597b0..34167eb6 100644 --- a/js/script.js +++ b/js/script.js @@ -27,14 +27,10 @@ Cookbook.prototype = { return deferred.promise(); }, update: function(id, recipeData) { - if(recipeData && id) { - recipeData += '&id=' + id; - } - var deferred = $.Deferred(); var self = this; $.ajax({ - url: this._baseUrl + '/recipes', + url: this._baseUrl + '/recipes/' + (id ? id : ''), method: 'PUT', data: recipeData }).done(function (response) { diff --git a/lib/Controller/RecipeController.php b/lib/Controller/RecipeController.php index 086f8cd5..34806580 100644 --- a/lib/Controller/RecipeController.php +++ b/lib/Controller/RecipeController.php @@ -1,4 +1,5 @@ userId = $UserId; @@ -36,29 +39,31 @@ class RecipeController extends Controller { * @NoAdminRequired * @NoCSRFRequired */ - public function index() { - if(empty($_GET['keywords'])){ + public function index() + { + if (empty($_GET['keywords'])) { $recipes = $this->service->getAllRecipesInSearchIndex(); - }else{ + } else { $recipes = $this->service->findRecipesInSearchIndex(isset($_GET['keywords']) ? $_GET['keywords'] : ''); } - foreach($recipes as $i => $recipe) { - $recipes[$i]['image_url'] = $this->urlGenerator->linkToRoute('cookbook.recipe.image', [ 'id' => $recipe['recipe_id'], 'size' => 'thumb' ]); + foreach ($recipes as $i => $recipe) { + $recipes[$i]['image_url'] = $this->urlGenerator->linkToRoute('cookbook.recipe.image', ['id' => $recipe['recipe_id'], 'size' => 'thumb']); } - return new DataResponse($recipes, Http::STATUS_OK, [ 'Content-Type' => 'application/json' ]); + return new DataResponse($recipes, Http::STATUS_OK, ['Content-Type' => 'application/json']); } /** * @NoAdminRequired * @NoCSRFRequired */ - public function config() { - if(isset($_POST['folder'])) { + public function config() + { + if (isset($_POST['folder'])) { $this->service->setUserFolderPath($_POST['folder']); $this->service->rebuildSearchIndex(); } - - if(isset($_POST['update_interval'])) { + + if (isset($_POST['update_interval'])) { $this->service->setSearchIndexUpdateInterval($_POST['update_interval']); } @@ -69,14 +74,17 @@ class RecipeController extends Controller { * @NoAdminRequired * @NoCSRFRequired */ - public function add() { - if(!isset($_POST['url'])) { return new DataResponse('Field "url" is required', 400); } + public function add() + { + if (!isset($_POST['url'])) { + return new DataResponse('Field "url" is required', 400); + } try { $recipe_file = $this->service->downloadRecipe($_POST['url']); $recipe_json = $this->service->parseRecipeFile($recipe_file); - return new DataResponse($recipe_json, Http::STATUS_OK, [ 'Content-Type' => 'application/json' ]); - } catch(\Exception $e) { + return new DataResponse($recipe_json, Http::STATUS_OK, ['Content-Type' => 'application/json']); + } catch (\Exception $e) { return new DataResponse($e->getMessage(), 502); } } @@ -87,13 +95,14 @@ class RecipeController extends Controller { * @param int $id * @return DataResponse */ - public function get($id) { + public function get($id) + { $json = $this->service->getRecipeById($id); - if(null === $json){ - return new DataResponse($id, Http::STATUS_NOT_FOUND, [ 'Content-Type' => 'application/json' ] ); + if (null === $json) { + return new DataResponse($id, Http::STATUS_NOT_FOUND, ['Content-Type' => 'application/json']); } - return new DataResponse($json, Http::STATUS_OK, [ 'Content-Type' => 'application/json' ]); + return new DataResponse($json, Http::STATUS_OK, ['Content-Type' => 'application/json']); } /** @@ -104,14 +113,14 @@ class RecipeController extends Controller { * * @return DataResponse */ - public function update() { - $data = []; - - parse_str(file_get_contents('php://input'), $data); - - $file = $this->service->addRecipe($data); + public function update($id) + { + $recipeData = array(); + parse_str(file_get_contents("php://input"), $recipeData); + $this->service->deleteRecipe($_GET['id']); + $file = $this->service->addRecipe($recipeData); - return new DataResponse($file->getParent()->getId(), Http::STATUS_OK, [ 'Content-Type' => 'application/json' ]); + return new DataResponse($file->getParent()->getId(), Http::STATUS_OK, ['Content-Type' => 'application/json']); } /** @@ -120,11 +129,12 @@ class RecipeController extends Controller { * @param int $id * @return DataResponse */ - public function delete($id) { + public function delete($id) + { try { $this->service->deleteRecipe($id); return new DataResponse('Recipe ' . $_GET['id'] . ' deleted successfully', Http::STATUS_OK); - } catch(\Exception $e) { + } catch (\Exception $e) { return new DataResponse($e->getMessage(), 502); } } @@ -133,7 +143,8 @@ class RecipeController extends Controller { * @NoAdminRequired * @NoCSRFRequired */ - public function reindex() { + public function reindex() + { $this->service->rebuildSearchIndex(); return new DataResponse('Search index rebuilt successfully', Http::STATUS_OK); @@ -145,14 +156,15 @@ class RecipeController extends Controller { * @param $id * @return DataResponse|FileDisplayResponse */ - public function image($id) { + public function image($id) + { $size = isset($_GET['size']) ? $_GET['size'] : null; try { $file = $this->service->getRecipeImageFileByFolderId($id, $size); - return new FileDisplayResponse($file, Http::STATUS_OK, [ 'Content-Type' => 'image/jpeg' ]); - } catch(\Exception $e) { + return new FileDisplayResponse($file, Http::STATUS_OK, ['Content-Type' => 'image/jpeg']); + } catch (\Exception $e) { return new DataResponse($e->getMessage(), Http::STATUS_NOT_FOUND); } }