Dan Brown

Made single entity updates more efficent

...@@ -730,6 +730,7 @@ class EntityRepo ...@@ -730,6 +730,7 @@ class EntityRepo
730 if ($chapter) $page->chapter_id = $chapter->id; 730 if ($chapter) $page->chapter_id = $chapter->id;
731 731
732 $book->pages()->save($page); 732 $book->pages()->save($page);
733 + $page = $this->page->find($page->id);
733 $this->permissionService->buildJointPermissionsForEntity($page); 734 $this->permissionService->buildJointPermissionsForEntity($page);
734 return $page; 735 return $page;
735 } 736 }
......
...@@ -150,13 +150,22 @@ class PermissionService ...@@ -150,13 +150,22 @@ class PermissionService
150 $roles = $this->role->with('permissions')->get()->all(); 150 $roles = $this->role->with('permissions')->get()->all();
151 151
152 // Chunk through all books 152 // Chunk through all books
153 - $this->book->newQuery()->select(['id', 'restricted', 'created_by'])->with(['chapters' => function($query) { 153 + $this->bookFetchQuery()->chunk(5, function ($books) use ($roles) {
154 + $this->buildJointPermissionsForBooks($books, $roles);
155 + });
156 + }
157 +
158 + /**
159 + * Get a query for fetching a book with it's children.
160 + * @return QueryBuilder
161 + */
162 + protected function bookFetchQuery()
163 + {
164 + return $this->book->newQuery()->select(['id', 'restricted', 'created_by'])->with(['chapters' => function($query) {
154 $query->select(['id', 'restricted', 'created_by', 'book_id']); 165 $query->select(['id', 'restricted', 'created_by', 'book_id']);
155 }, 'pages' => function($query) { 166 }, 'pages' => function($query) {
156 $query->select(['id', 'restricted', 'created_by', 'book_id', 'chapter_id']); 167 $query->select(['id', 'restricted', 'created_by', 'book_id', 'chapter_id']);
157 - }])->chunk(5, function ($books) use ($roles) { 168 + }]);
158 - $this->buildJointPermissionsForBooks($books, $roles);
159 - });
160 } 169 }
161 170
162 /** 171 /**
...@@ -188,14 +197,22 @@ class PermissionService ...@@ -188,14 +197,22 @@ class PermissionService
188 */ 197 */
189 public function buildJointPermissionsForEntity(Entity $entity) 198 public function buildJointPermissionsForEntity(Entity $entity)
190 { 199 {
191 - $roles = $this->role->newQuery()->get(); 200 + $entities = [$entity];
192 - $book = ($entity->isA('book')) ? $entity : $entity->book; 201 + if ($entity->isA('book')) {
193 - $book = $this->book->newQuery()->select(['id', 'restricted', 'created_by'])->with(['chapters' => function($query) { 202 + $books = $this->bookFetchQuery()->where('id', '=', $entity->id)->get();
194 - $query->select(['id', 'restricted', 'created_by', 'book_id']); 203 + $this->buildJointPermissionsForBooks($books, $this->role->newQuery()->get(), true);
195 - }, 'pages' => function($query) { 204 + return;
196 - $query->select(['id', 'restricted', 'created_by', 'book_id', 'chapter_id']); 205 + }
197 - }])->where('id', '=', $book->id)->get(); 206 +
198 - $this->buildJointPermissionsForBooks($book, $roles, true); 207 + $entities[] = $entity->book;
208 + if ($entity->isA('page') && $entity->chapter_id) $entities[] = $entity->chapter;
209 + if ($entity->isA('chapter')) {
210 + foreach ($entity->pages as $page) {
211 + $entities[] = $page;
212 + }
213 + }
214 + $this->deleteManyJointPermissionsForEntities($entities);
215 + $this->buildJointPermissionsForEntities(collect($entities));
199 } 216 }
200 217
201 /** 218 /**
...@@ -204,7 +221,7 @@ class PermissionService ...@@ -204,7 +221,7 @@ class PermissionService
204 */ 221 */
205 public function buildJointPermissionsForEntities(Collection $entities) 222 public function buildJointPermissionsForEntities(Collection $entities)
206 { 223 {
207 - $roles = $this->role->get(); 224 + $roles = $this->role->newQuery()->get();
208 $this->deleteManyJointPermissionsForEntities($entities->all()); 225 $this->deleteManyJointPermissionsForEntities($entities->all());
209 $this->createManyJointPermissions($entities, $roles); 226 $this->createManyJointPermissions($entities, $roles);
210 } 227 }
...@@ -219,11 +236,7 @@ class PermissionService ...@@ -219,11 +236,7 @@ class PermissionService
219 $this->deleteManyJointPermissionsForRoles($roles); 236 $this->deleteManyJointPermissionsForRoles($roles);
220 237
221 // Chunk through all books 238 // Chunk through all books
222 - $this->book->newQuery()->select(['id', 'restricted', 'created_by'])->with(['chapters' => function($query) { 239 + $this->bookFetchQuery()->chunk(5, function ($books) use ($roles) {
223 - $query->select(['id', 'restricted', 'created_by', 'book_id']);
224 - }, 'pages' => function($query) {
225 - $query->select(['id', 'restricted', 'created_by', 'book_id', 'chapter_id']);
226 - }])->chunk(5, function ($books) use ($roles) {
227 $this->buildJointPermissionsForBooks($books, $roles); 240 $this->buildJointPermissionsForBooks($books, $roles);
228 }); 241 });
229 } 242 }
......