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

@ -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);
}
}