Cleanup and move some controller actions around. Fix navigation update and recipe deletion.

This commit is contained in:
Maxime Corteel 2020-02-02 14:33:16 +01:00
Родитель a786dc4b0d
Коммит 87aa6a302a
9 изменённых файлов: 120 добавлений и 157 удалений

Просмотреть файл

@ -9,26 +9,24 @@
*/
return [
'routes' => [
['name' => 'page#index', 'url' => '/', 'verb' => 'GET'],
['name' => 'page#home', 'url' => '/home', 'verb' => 'GET'],
['name' => 'page#search', 'url' => '/search/{query}', 'verb' => 'GET'],
['name' => 'page#tag', 'url' => '/tag/{tag}', 'verb' => 'GET'],
['name' => 'page#all', 'url' => '/all', 'verb' => 'GET'],
['name' => 'page#error', 'url' => '/error', 'verb' => 'GET'],
['name' => 'page#create', 'url' => '/recipes/create', 'verb' => 'GET'],
['name' => 'page#new', 'url' => '/recipes/create', 'verb' => 'POST'],
['name' => 'page#import', 'url' => '/import', 'verb' => 'POST'],
['name' => 'page#edit', 'url' => '/recipes/{id}/edit', 'verb' => 'GET', 'requirements' => ['id' => '\d+']],
['name' => 'page#update', 'url' => '/recipes/{id}/edit', 'verb' => 'PUT', 'requirements' => ['id' => '\d+']],
['name' => 'page#recipe', 'url' => '/recipes/{id}', 'verb' => 'GET', 'requirements' => ['id' => '\d+']],
['name' => 'recipe#index', 'url' => '/recipes', 'verb' => 'GET'],
['name' => 'recipe#add', 'url' => '/add', 'verb' => 'POST'],
['name' => 'recipe#delete', 'url' => '/recipes/{id}', 'verb' => 'DELETE', 'requirements' => ['id' => '\d+']],
['name' => 'recipe#update', 'url' => '/recipes/{id}', 'verb' => 'PUT', 'requirements' => ['id' => '\d+']],
['name' => 'recipe#create', 'url' => '/recipes', 'verb' => 'POST'],
// ['name' => 'recipe#get', 'url' => '/recipes/{id}', 'verb' => 'GET', 'requirements' => ['id' => '\d+']],
['name' => 'main#index', 'url' => '/', 'verb' => 'GET'],
['name' => 'main#home', 'url' => '/home', 'verb' => 'GET'],
['name' => 'main#tags', 'url' => '/tags', 'verb' => 'GET'],
['name' => 'main#search', 'url' => '/search/{query}', 'verb' => 'GET'],
['name' => 'main#tag', 'url' => '/tag/{tag}', 'verb' => 'GET'],
['name' => 'main#all', 'url' => '/all', 'verb' => 'GET'],
['name' => 'main#error', 'url' => '/error', 'verb' => 'GET'],
['name' => 'main#create', 'url' => '/recipes/create', 'verb' => 'GET'],
['name' => 'main#new', 'url' => '/recipes/create', 'verb' => 'POST'],
['name' => 'main#import', 'url' => '/import', 'verb' => 'POST'],
['name' => 'main#edit', 'url' => '/recipes/{id}/edit', 'verb' => 'GET', 'requirements' => ['id' => '\d+']],
['name' => 'main#update', 'url' => '/recipes/{id}/edit', 'verb' => 'PUT', 'requirements' => ['id' => '\d+']],
['name' => 'main#recipe', 'url' => '/recipes/{id}', 'verb' => 'GET', 'requirements' => ['id' => '\d+']],
['name' => 'recipe#image', 'url' => '/recipes/{id}/image', 'verb' => 'GET', 'requirements' => ['id' => '\d+']],
['name' => 'recipe#reindex', 'url' => '/reindex', 'verb' => 'POST'],
['name' => 'recipe#config', 'url' => '/config', 'verb' => 'POST'],
]
['name' => 'config#reindex', 'url' => '/reindex', 'verb' => 'POST'],
['name' => 'config#config', 'url' => '/config', 'verb' => 'POST'],
],
'resources' => [
'recipe' => ['url' => '/api/recipes']
]
];

Просмотреть файл

@ -240,7 +240,7 @@ var Content = function (cookbook) {
var id = e.currentTarget.dataset.id;
$.ajax({
url: cookbook._baseUrl + '/recipes/' + id,
url: cookbook._baseUrl + '/api/recipes/' + id,
method: 'DELETE',
})
.done(function(html) {
@ -490,26 +490,27 @@ var Nav = function (cookbook) {
*/
self.render = function () {
$.ajax({
url: cookbook._baseUrl + '/recipes?keywords=' + self.getKeywords(),
url: cookbook._baseUrl + '/tags',
method: 'GET',
})
.done(function(json) {
var html = json.map(function (recipeData) {
var recipeEntry = '<li>';
recipeEntry += '<a href="#'+recipeData.recipe_id+'">';
recipeEntry += '<img src="'+recipeData.image_url+'">';
recipeEntry += recipeData.name;
recipeEntry += '</a></li>';
return recipeEntry;
var html = '<li class="icon-category-organization"><a href="#all">' + t(appName, 'All recipes') + '</a></li>';
html += json.map(function(tag) {
var entry = '<li class="icon-category-files">';
entry += '<a href="#tag/' + encodeURIComponent(tag.name) + '">';
entry += '<span class="pull-right">' + tag.recipe_count + '</span>';
entry += tag.name;
entry += '</a></li>';
return entry;
}).join("\n");
$('#app-navigation #recipes').html(html);
$('#app-navigation #categories').html(html);
self.highlightActive();
})
.fail(function(e) {
alert(t(appName, 'Failed to fetch recipes'));
alert(t(appName, 'Failed to fetch tags'));
if(e && e instanceof Error) { throw e; }
});

Просмотреть файл

@ -0,0 +1,66 @@
<?php
namespace OCA\Cookbook\Controller;
use OCP\IConfig;
use OCP\IRequest;
use OCP\IDBConnection;
use OCP\Files\IRootFolder;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\DataDisplayResponse;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\Http\FileDisplayResponse;
use OCP\AppFramework\Controller;
use OCA\Cookbook\Service\RecipeService;
use OCP\IURLGenerator;
class ConfigController extends Controller
{
private $userId;
/**
* @var RecipeService
*/
private $service;
/**
* @var IURLGenerator
*/
private $urlGenerator;
public function __construct($AppName, IRequest $request, IURLGenerator $urlGenerator, RecipeService $recipeService)
{
parent::__construct($AppName, $request);
$this->service = $recipeService;
$this->urlGenerator = $urlGenerator;
}
/**
* @NoAdminRequired
* @NoCSRFRequired
*/
public function config()
{
if (isset($_POST['folder'])) {
$this->service->setUserFolderPath($_POST['folder']);
$this->service->rebuildSearchIndex();
}
if (isset($_POST['update_interval'])) {
$this->service->setSearchIndexUpdateInterval($_POST['update_interval']);
}
return new DataResponse('OK', Http::STATUS_OK);
}
/**
* @NoAdminRequired
* @NoCSRFRequired
*/
public function reindex()
{
$this->service->rebuildSearchIndex();
return new DataResponse('Search index rebuilt successfully', Http::STATUS_OK);
}
}

Просмотреть файл

@ -11,7 +11,7 @@ use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\Controller;
use OCA\Cookbook\Service\RecipeService;
class PageController extends Controller
class MainController extends Controller
{
protected $appName;
@ -45,6 +45,16 @@ class PageController extends Controller
return new TemplateResponse($this->appName, 'index', $view_data); // templates/index.php
}
/**
* @NoAdminRequired
* @NoCSRFRequired
*/
public function tags()
{
$tags = $this->service->getAllKeywordsInSearchIndex();
return new DataResponse($tags, 200, ['Content-Type' => 'application/json']);
}
/**
* @NoAdminRequired
* @NoCSRFRequired
@ -190,26 +200,6 @@ class PageController extends Controller
}
}
/**
* @NoAdminRequired
* @NoCSRFRequired
*/
public function import()
{
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('#recipes/' . $recipe_file->getParent()->getId() . '/edit');
} catch (\Exception $e) {
return new DataResponse($e->getMessage(), 502);
}
}
/**
* @NoAdminRequired
* @NoCSRFRequired

Просмотреть файл

@ -54,50 +54,13 @@ class RecipeController extends Controller
return new DataResponse($recipes, Http::STATUS_OK, ['Content-Type' => 'application/json']);
}
/**
* @NoAdminRequired
* @NoCSRFRequired
*/
public function config()
{
if (isset($_POST['folder'])) {
$this->service->setUserFolderPath($_POST['folder']);
$this->service->rebuildSearchIndex();
}
if (isset($_POST['update_interval'])) {
$this->service->setSearchIndexUpdateInterval($_POST['update_interval']);
}
return new DataResponse('OK', Http::STATUS_OK);
}
/**
* @NoAdminRequired
* @NoCSRFRequired
*/
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($e->getMessage(), 502);
}
}
/**
* @NoAdminRequired
* @NoCSRFRequired
* @param int $id
* @return DataResponse
*/
public function get($id)
public function show($id)
{
$json = $this->service->getRecipeById($id);
@ -150,7 +113,7 @@ class RecipeController extends Controller
* @param int $id
* @return DataResponse
*/
public function delete($id)
public function destroy($id)
{
try {
$this->service->deleteRecipe($id);
@ -160,17 +123,6 @@ class RecipeController extends Controller
}
}
/**
* @NoAdminRequired
* @NoCSRFRequired
*/
public function reindex()
{
$this->service->rebuildSearchIndex();
return new DataResponse('Search index rebuilt successfully', Http::STATUS_OK);
}
/**
* @NoAdminRequired
* @NoCSRFRequired

Просмотреть файл

@ -91,7 +91,7 @@ class RecipeDb {
$qb->select('k.name')
->selectAlias($qb->createFunction('COUNT(k.recipe_id)'), 'recipe_count')
->from('cookbook_keywords', 'k')
->where('user_id = :user')
->where('user_id = :user AND k.name != \'\'')
->groupBy('k.name')
->orderBy('k.name');
$qb->setParameter('user', $userId, TYPE::STRING);

Просмотреть файл

@ -1,31 +1,17 @@
<form id="editRecipeForm" action="#" method="<?php echo $_['id'] ? 'PUT' : 'POST' ?>">
<div id="controls">
<div class="breadcrumb">
<div class="crumb svg crumbmenu hidden">
<a class="icon-more menutoggle" aria-expanded="false"></a>
<div class="popovermenu menu-center menu">
<ul>
<li class="crumblist ui-droppable in-breadcrumb">
<a href="#">
<span class="icon-folder"></span>
<span><?php p($l->t('Home')); ?></span>
</a>
</li>
<li class="crumblist in-breadcrumb">
<a href="#<?php echo $_['id']; ?>">
<span class="icon-folder"></span>
<span><?php echo $_['id'] ? $_['name'] : p($l->t('New recipe')); ?></span>
</a>
</li>
</ul>
</div>
</div>
<div class="crumb svg crumbhome ui-droppable">
<a href="#" class="icon-home"><?php p($l->t('Home')); ?></a>
</div>
<div class="crumb svg">
<a href="#<?php echo $_['id']; ?>"><?php echo $_['id'] ? $_['name'] : p($l->t('New recipe')); ?></a>
<a href="#recipes/<?php echo $_['id']; ?>"><?php echo $_['id'] ? $_['name'] : p($l->t('New recipe')); ?></a>
</div>
<?php if($_['id']) { ?>
<div class="crumb svg">
<a href="javascript:;"><?php echo p($l->t('Edit')); ?></a>
</div>
<?php } ?>
</div>
<div class="actions">
<button type="submit">

Просмотреть файл

@ -1,24 +1,5 @@
<div id="controls">
<div class="breadcrumb">
<div class="crumb svg crumbmenu hidden">
<a class="icon-more menutoggle" aria-expanded="false"></a>
<div class="popovermenu menu-center menu">
<ul>
<li class="crumblist ui-droppable in-breadcrumb">
<a href="#">
<span class="icon-folder"></span>
<span><?php p($l->t('Home')); ?></span>
</a>
</li>
<li class="crumblist in-breadcrumb">
<a href="#recipes/<?php echo $_['id']; ?>">
<span class="icon-folder"></span>
<span><?php echo $_['name']; ?></span>
</a>
</li>
</ul>
</div>
</div>
<div class="crumb svg crumbhome ui-droppable">
<a href="#" class="icon-home"><?php p($l->t('Home')); ?></a>
</div>

Просмотреть файл

@ -11,16 +11,5 @@
</form>
<ul id="categories">
<li class="icon-category-organization"><a href="#all"><?php echo p($l->t('All recipes')); ?></a></li>
<?php foreach($_['all_keywords'] as $keyword) { ?>
<?php if(!isset($keyword['name']) || empty($keyword['name'])) { continue; } ?>
<li class="icon-category-files">
<a href="#tag/<?php echo urlencode($keyword['name']); ?>">
<strong class="pull-right"><?php echo $keyword['recipe_count']; ?></strong>
<?php echo $keyword['name']; ?>
</a>
</li>
<?php } ?>
</ul>