Skip to content

Commit

Permalink
refactor the way taxonomy is synced
Browse files Browse the repository at this point in the history
  • Loading branch information
austintoddj committed Dec 27, 2020
1 parent 818e0a8 commit 19cb5d7
Showing 1 changed file with 38 additions and 57 deletions.
95 changes: 38 additions & 57 deletions src/Http/Controllers/PostController.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public function store(PostRequest $request, $id): JsonResponse
->with('tags', 'topic')
->find($id);

if (! $post) {
if (!$post) {
$post = new Post(['id' => $id]);
}

Expand All @@ -111,9 +111,42 @@ public function store(PostRequest $request, $id): JsonResponse

$post->save();

$post->tags()->sync($this->syncTags($request->input('tags', [])));
$tags = Tag::query()->get(['id', 'name', 'slug']);
$topics = Topic::query()->get(['id', 'name', 'slug']);

$post->topic()->sync($this->syncTopic($request->input('topic', [])));
$tagsToSync = collect($request->input('tags', []))->map(function ($item) use ($tags) {
$tag = $tags->firstWhere('slug', $item['slug']);

if (!$tag) {
$tag = Tag::create([
'id' => $id = Uuid::uuid4()->toString(),
'name' => $item['name'],
'slug' => $item['slug'],
'user_id' => request()->user('canvas')->id,
]);
}

return (string)$tag->id;
})->toArray();

$topicToSync = collect($request->input('topic', []))->map(function ($item) use ($topics) {
$topic = $topics->firstWhere('slug', $item['slug']);

if (!$topic) {
$topic = Topic::create([
'id' => $id = Uuid::uuid4()->toString(),
'name' => $item['name'],
'slug' => $item['slug'],
'user_id' => request()->user('canvas')->id,
]);
}

return (string)$topic->id;
})->toArray();

$post->tags()->sync($tagsToSync);

$post->topic()->sync($topicToSync);

return response()->json($post->refresh(), 201);
}
Expand All @@ -138,8 +171,8 @@ public function show($id): JsonResponse
if ($post) {
return response()->json([
'post' => $post,
'tags' => Tag::query()->get(['name', 'slug']), // TODO: Is this necessary anymore since we already join tags above?
'topics' => Topic::query()->get(['name', 'slug']), // TODO: Is this necessary anymore since we already join the topic above?
'tags' => Tag::query()->get(['name', 'slug']),
'topics' => Topic::query()->get(['name', 'slug']),
]);
} else {
return response()->json(null, 404);
Expand Down Expand Up @@ -167,56 +200,4 @@ public function destroy($id)

return response()->json(null, 204);
}

/**
* Sync given tags.
*
* @param array $incomingTags
* @return array
*/
protected function syncTags(array $incomingTags): array
{
$tags = Tag::query()->get(['id', 'name', 'slug']);

return collect($incomingTags)->map(function ($item) use ($tags) {
$tag = $tags->firstWhere('slug', $item['slug']);

if (! $tag) {
$tag = Tag::create([
'id' => $id = Uuid::uuid4()->toString(),
'name' => $item['name'],
'slug' => $item['slug'],
'user_id' => request()->user('canvas')->id,
]);
}

return (string) $tag->id;
})->toArray();
}

/**
* Sync a given topic.
*
* @param array $incomingTopic
* @return array
*/
protected function syncTopic(array $incomingTopic): array
{
$topics = Topic::query()->get(['id', 'name', 'slug']);

return collect($incomingTopic)->map(function ($item) use ($topics) {
$topic = $topics->firstWhere('slug', $item['slug']);

if (! $topic) {
$topic = Topic::create([
'id' => $id = Uuid::uuid4()->toString(),
'name' => $item['name'],
'slug' => $item['slug'],
'user_id' => request()->user('canvas')->id,
]);
}

return (string) $topic->id;
})->toArray();
}
}

0 comments on commit 19cb5d7

Please sign in to comment.