mirror of
https://github.com/BookStackApp/BookStack.git
synced 2025-05-22 14:49:59 +08:00
Added further attribute endpoints and added tests
This commit is contained in:
@ -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);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user