]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/AttributeController.php
Added further attribute endpoints and added tests
[bookstack] / app / Http / Controllers / AttributeController.php
1 <?php namespace BookStack\Http\Controllers;
2
3 use BookStack\Repos\AttributeRepo;
4 use Illuminate\Http\Request;
5 use BookStack\Http\Requests;
6
7 class AttributeController extends Controller
8 {
9
10     protected $attributeRepo;
11
12     /**
13      * AttributeController constructor.
14      * @param $attributeRepo
15      */
16     public function __construct(AttributeRepo $attributeRepo)
17     {
18         $this->attributeRepo = $attributeRepo;
19     }
20
21     /**
22      * Get all the Attributes for a particular entity
23      * @param $entityType
24      * @param $entityId
25      */
26     public function getForEntity($entityType, $entityId)
27     {
28         $attributes = $this->attributeRepo->getForEntity($entityType, $entityId);
29         return response()->json($attributes);
30     }
31
32     /**
33      * Update the attributes for a particular entity.
34      * @param $entityType
35      * @param $entityId
36      * @param Request $request
37      * @return mixed
38      */
39     public function updateForEntity($entityType, $entityId, Request $request)
40     {
41
42         $this->validate($request, [
43             'attributes.*.name' => 'required|min:3|max:250',
44             'attributes.*.value' => 'max:250'
45         ]);
46
47         $entity = $this->attributeRepo->getEntity($entityType, $entityId, 'update');
48         if ($entity === null) return $this->jsonError("Entity not found", 404);
49
50         $inputAttributes = $request->input('attributes');
51         $attributes = $this->attributeRepo->saveAttributesToEntity($entity, $inputAttributes);
52         return response()->json($attributes);
53     }
54
55     /**
56      * Get attribute name suggestions from a given search term.
57      * @param Request $request
58      */
59     public function getNameSuggestions(Request $request)
60     {
61         $searchTerm = $request->get('search');
62         $suggestions = $this->attributeRepo->getNameSuggestions($searchTerm);
63         return response()->json($suggestions);
64     }
65
66
67 }