1
0
mirror of https://github.com/BookStackApp/BookStack.git synced 2025-07-28 17:02:04 +03:00

Added further attribute endpoints and added tests

This commit is contained in:
Dan Brown
2016-05-07 14:29:43 +01:00
parent c99653f0f2
commit fcfb9470c9
14 changed files with 282 additions and 27 deletions

View File

@ -1,7 +1,7 @@
<?php namespace BookStack;
abstract class Entity extends Ownable
class Entity extends Ownable
{
/**
@ -200,11 +200,5 @@ abstract class Entity extends Ownable
return $search->orderBy('title_relevance', 'desc');
}
/**
* Get the url for this item.
* @return string
*/
abstract public function getUrl();
}

View File

@ -2,7 +2,6 @@
use BookStack\Repos\AttributeRepo;
use Illuminate\Http\Request;
use BookStack\Http\Requests;
class AttributeController extends Controller
@ -19,7 +18,6 @@ class AttributeController extends Controller
$this->attributeRepo = $attributeRepo;
}
/**
* Get all the Attributes for a particular entity
* @param $entityType
@ -27,6 +25,43 @@ class AttributeController extends Controller
*/
public function getForEntity($entityType, $entityId)
{
$attributes = $this->attributeRepo->getForEntity($entityType, $entityId);
return response()->json($attributes);
}
/**
* Update the attributes for a particular entity.
* @param $entityType
* @param $entityId
* @param Request $request
* @return mixed
*/
public function updateForEntity($entityType, $entityId, Request $request)
{
$this->validate($request, [
'attributes.*.name' => 'required|min:3|max:250',
'attributes.*.value' => 'max:250'
]);
$entity = $this->attributeRepo->getEntity($entityType, $entityId, 'update');
if ($entity === null) return $this->jsonError("Entity not found", 404);
$inputAttributes = $request->input('attributes');
$attributes = $this->attributeRepo->saveAttributesToEntity($entity, $inputAttributes);
return response()->json($attributes);
}
/**
* Get attribute name suggestions from a given search term.
* @param Request $request
*/
public function getNameSuggestions(Request $request)
{
$searchTerm = $request->get('search');
$suggestions = $this->attributeRepo->getNameSuggestions($searchTerm);
return response()->json($suggestions);
}
}

View File

@ -110,4 +110,15 @@ abstract class Controller extends BaseController
return true;
}
/**
* Send back a json error message.
* @param string $messageText
* @param int $statusCode
* @return mixed
*/
protected function jsonError($messageText = "", $statusCode = 500)
{
return response()->json(['message' => $messageText], $statusCode);
}
}

View File

@ -88,6 +88,8 @@ Route::group(['middleware' => 'auth'], function () {
// Attribute routes (AJAX)
Route::group(['prefix' => 'ajax/attributes'], function() {
Route::get('/get/{entityType}/{entityId}', 'AttributeController@getForEntity');
Route::get('/suggest', 'AttributeController@getNameSuggestions');
Route::post('/update/{entityType}/{entityId}', 'AttributeController@updateForEntity');
});
// Links

View File

@ -28,5 +28,76 @@ class AttributeRepo
$this->permissionService = $ps;
}
/**
* Get an entity instance of its particular type.
* @param $entityType
* @param $entityId
* @param string $action
*/
public function getEntity($entityType, $entityId, $action = 'view')
{
$entityInstance = $this->entity->getEntityInstance($entityType);
$searchQuery = $entityInstance->where('id', '=', $entityId)->with('attributes');
$searchQuery = $this->permissionService->enforceEntityRestrictions($searchQuery, $action);
return $searchQuery->first();
}
/**
* Get all attributes for a particular entity.
* @param string $entityType
* @param int $entityId
* @return mixed
*/
public function getForEntity($entityType, $entityId)
{
$entity = $this->getEntity($entityType, $entityId);
if ($entity === null) return collect();
return $entity->attributes;
}
/**
* Get attribute name suggestions from scanning existing attribute names.
* @param $searchTerm
* @return array
*/
public function getNameSuggestions($searchTerm)
{
if ($searchTerm === '') return [];
$query = $this->attribute->where('name', 'LIKE', $searchTerm . '%')->groupBy('name')->orderBy('name', 'desc');
$query = $this->permissionService->filterRestrictedEntityRelations($query, 'attributes', 'entity_id', 'entity_type');
return $query->get(['name'])->pluck('name');
}
/**
* Save an array of attributes to an entity
* @param Entity $entity
* @param array $attributes
* @return array|\Illuminate\Database\Eloquent\Collection
*/
public function saveAttributesToEntity(Entity $entity, $attributes = [])
{
$entity->attributes()->delete();
$newAttributes = [];
foreach ($attributes as $attribute) {
$newAttributes[] = $this->newInstanceFromInput($attribute);
}
return $entity->attributes()->saveMany($newAttributes);
}
/**
* Create a new Attribute instance from user input.
* @param $input
* @return static
*/
protected function newInstanceFromInput($input)
{
$name = trim($input['name']);
$value = isset($input['value']) ? trim($input['value']) : '';
// Any other modification or cleanup required can go here
$values = ['name' => $name, 'value' => $value];
return $this->attribute->newInstance($values);
}
}

View File

@ -582,6 +582,7 @@ class PageRepo extends EntityRepo
{
Activity::removeEntity($page);
$page->views()->delete();
$page->attributes()->delete();
$page->revisions()->delete();
$page->permissions()->delete();
$this->permissionService->deleteJointPermissionsForEntity($page);