diff options
author | Marvin Borner | 2018-08-30 17:40:19 +0200 |
---|---|---|
committer | Marvin Borner | 2018-08-30 17:40:19 +0200 |
commit | e30618dd397dea1ca295feef537e01f97996e71c (patch) | |
tree | 64e912fcf2b21c9723b28d7f00c6d43eeccf552e | |
parent | 4df4ae5b8eb97d5557bbd4f839416abded27495d (diff) |
Finished/optimized getting of posts
-rw-r--r-- | api/Posts/Models/Post.php | 2 | ||||
-rw-r--r-- | api/Posts/Models/PostType.php | 2 | ||||
-rw-r--r-- | api/Posts/Repositories/PostRepository.php | 27 | ||||
-rw-r--r-- | api/Posts/Services/PostService.php | 5 |
4 files changed, 31 insertions, 5 deletions
diff --git a/api/Posts/Models/Post.php b/api/Posts/Models/Post.php index 9da1401..b907808 100644 --- a/api/Posts/Models/Post.php +++ b/api/Posts/Models/Post.php @@ -26,7 +26,7 @@ class Post extends Model public function post_type() { - return $this->belongsTo('Api\Posts\Models\PostType'); + return $this->belongsTo('Api\Posts\Models\PostType', 'post_types_id', 'id'); } public function media_post() diff --git a/api/Posts/Models/PostType.php b/api/Posts/Models/PostType.php index 3547a0a..de8d0f3 100644 --- a/api/Posts/Models/PostType.php +++ b/api/Posts/Models/PostType.php @@ -14,6 +14,6 @@ class PostType extends Model public function posts() { - return $this->hasMany('Api\Posts\Models\Post'); + return $this->hasMany('Api\Posts\Models\Post', 'post_types_id', 'id'); } } diff --git a/api/Posts/Repositories/PostRepository.php b/api/Posts/Repositories/PostRepository.php index 9954af9..1f035b7 100644 --- a/api/Posts/Repositories/PostRepository.php +++ b/api/Posts/Repositories/PostRepository.php @@ -12,6 +12,33 @@ class PostRepository extends Repository return new Post(); } + public function getJoined($options) + { + $query = Post::query()->with('user')->with('post_type'); + $this->applyResourceOptions($query, $options); + $posts = $query->get(); + $joinedPosts = []; + + foreach ($posts as $post) { + $postType = 'Api\Posts\Models\\' . $post["post_type"]["type"] . 'Post'; + $postTypeClass = new $postType(); + $post["post"] = $postTypeClass::query()->where('id', $post->id)->first(); + array_push($joinedPosts, $post); + } + return $joinedPosts; + } + + public function getJoinedById($postId) + { + $query = Post::query()->with('user')->with('post_type')->where('id', $postId); + $post = $query->first(); + + $postType = 'Api\Posts\Models\\' . $post["post_type"]["type"] . 'Post'; + $postTypeClass = new $postType(); + $post["post"] = $postTypeClass::query()->where('id', $post["id"])->first(); + return $post; + } + public function create(array $data) { $post = $this->getModel(); diff --git a/api/Posts/Services/PostService.php b/api/Posts/Services/PostService.php index 812f638..0232af2 100644 --- a/api/Posts/Services/PostService.php +++ b/api/Posts/Services/PostService.php @@ -6,7 +6,6 @@ use Api\Posts\Events\PostWasCreated; use Api\Posts\Events\PostWasDeleted; use Api\Posts\Events\PostWasUpdated; use Api\Posts\Exceptions\PostNotFoundException; -use Api\Posts\Models\Post; use Api\Posts\Repositories\PostRepository; use Illuminate\Auth\AuthManager; use Illuminate\Database\DatabaseManager; @@ -36,7 +35,7 @@ class PostService public function getAll($options = []) { - return $this->postRepository->get($options); + return $this->postRepository->getJoined($options); } public function getById($postId, array $options = []) @@ -77,7 +76,7 @@ class PostService private function getRequestedPost($postId) { - $post = Post::with('post_type')->with('user')->where('id', $postId)->get(); + $post = $this->postRepository->getJoinedById($postId); if (is_null($post)) { throw new PostNotFoundException(); |