diff --git a/src/Http/Controllers/PostController.php b/src/Http/Controllers/PostController.php index bef9333aa..fbdb02880 100644 --- a/src/Http/Controllers/PostController.php +++ b/src/Http/Controllers/PostController.php @@ -87,11 +87,11 @@ public function store(StorePostRequest $request, $id): JsonResponse { $data = $request->validated(); - if ($request->user('canvas')->isAdmin) { - $post = Post::with('tags', 'topic')->find($id); - } else { - $post = Post::with('tags', 'topic')->where('user_id', $request->user('canvas')->id)->find($id); - } + $post = Post::when($request->user('canvas')->isContributor, function ($query) { + return $query->where('user_id', request()->user('canvas')->id); + }, function ($query) { + return $query; + })->with('tags', 'topic')->find($id); if (! $post) { $post = new Post(['id' => $id]); @@ -99,7 +99,7 @@ public function store(StorePostRequest $request, $id): JsonResponse $post->fill($data); - $post->user_id = $request->user('canvas')->id; + $post->user_id = $post->user_id ?? request()->user('canvas')->id; $post->save(); diff --git a/src/Http/Controllers/TagController.php b/src/Http/Controllers/TagController.php index c6a095657..965607fd3 100644 --- a/src/Http/Controllers/TagController.php +++ b/src/Http/Controllers/TagController.php @@ -64,7 +64,7 @@ public function store(StoreTagRequest $request, $id): JsonResponse $tag->fill($data); - $tag->user_id = request()->user('canvas')->id; + $tag->user_id = $tag->user_id ?? request()->user('canvas')->id; $tag->save(); diff --git a/src/Http/Controllers/TopicController.php b/src/Http/Controllers/TopicController.php index c6785f552..79ca29d37 100644 --- a/src/Http/Controllers/TopicController.php +++ b/src/Http/Controllers/TopicController.php @@ -50,25 +50,25 @@ public function store(StoreTopicRequest $request, $id): JsonResponse { $data = $request->validated(); - $tag = Topic::find($id); + $topic = Topic::find($id); - if (! $tag) { - if ($tag = Topic::onlyTrashed()->firstWhere('slug', $data['slug'])) { - $tag->restore(); + if (! $topic) { + if ($topic = Topic::onlyTrashed()->firstWhere('slug', $data['slug'])) { + $topic->restore(); - return response()->json($tag->refresh(), 201); + return response()->json($topic->refresh(), 201); } else { - $tag = new Topic(['id' => $id]); + $topic = new Topic(['id' => $id]); } } - $tag->fill($data); + $topic->fill($data); - $tag->user_id = request()->user('canvas')->id; + $topic->user_id = $topic->user_id ?? request()->user('canvas')->id; - $tag->save(); + $topic->save(); - return response()->json($tag->refresh(), 201); + return response()->json($topic->refresh(), 201); } /**