Skip to content

Commit

Permalink
fix tag bug, again
Browse files Browse the repository at this point in the history
  • Loading branch information
fr0tt committed Aug 21, 2022
1 parent 89a10d1 commit 27bd8a0
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 27 deletions.
3 changes: 3 additions & 0 deletions app/Http/Controllers/PostController.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,13 +140,16 @@ public function store(Request $request)
->max('order') + 1;

$post = Post::create($attributes);

if ($info['type'] === Post::POST_TYPE_LINK) {
$this->service->saveImage($info['image_path'], $post);
}
if (isset($validatedData['tags'])) {
$this->service->saveTags($post->id, $validatedData['tags']);
}

$post->tags = $post->tags()->get();

return response()->json(['data' => $post], Response::HTTP_CREATED);
}

Expand Down
21 changes: 15 additions & 6 deletions app/Services/PostService.php
Original file line number Diff line number Diff line change
Expand Up @@ -233,14 +233,23 @@ public function getDominantColor(string $base_url)
return $hex;
}

public function saveTags(int $post_id, array $tags)
public function saveTags(int $post_id, array $tag_ids)
{
foreach ($tags as $tag_id) {
PostTag::firstOrCreate([
'post_id' => $post_id,
'tag_id' => $tag_id
]);
$old_tags_obj = PostTag::select('tag_id')->where('post_id', $post_id)->get();
$old_tags = [];
foreach ($old_tags_obj as $old_tag) {
$old_tags[] = $old_tag->tag_id;
}

foreach ($tag_ids as $tag_id) {
if (!in_array($tag_id, $old_tags)) {
PostTag::create([
'post_id' => $post_id,
'tag_id' => $tag_id
]);
}
}
PostTag::whereNotIn('tag_id', $tag_ids)->where('post_id', $post_id)->delete();
}

public function saveImage($image_path, Post $post)
Expand Down
2 changes: 1 addition & 1 deletion public/js/app.js

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions public/js/app.js.LICENSE.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
* @license MIT
*/

/*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */

/**!
* Sortable 1.10.2
* @author RubaXa <trash@rubaxa.org>
Expand Down
2 changes: 1 addition & 1 deletion public/service-worker.js

Large diffs are not rendered by default.

46 changes: 27 additions & 19 deletions resources/js/components/pages/Post.vue
Original file line number Diff line number Diff line change
Expand Up @@ -201,13 +201,13 @@ export default {
this.editor.destroy()
},
methods: {
save() {
async save() {
let content = this.editor.getHTML()
if (content === '' || this.currentCollection === null) {
return
}
let tags = this.saveTags()
let tags = await this.saveTags()
const matches = content.match(/^<p>(?<content>.(?:(?!<p>)(?!<\/p>).)*)<\/p>$/)
if (matches !== null) {
Expand Down Expand Up @@ -251,7 +251,7 @@ export default {
event.preventDefault()
this.save()
},
saveTags() {
async saveTags() {
let newTags = []
let existingTags = []
if (!this.tags) {
Expand All @@ -264,23 +264,31 @@ export default {
existingTags.push(tag)
}
})
if (newTags.length > 0) {
axios
.post('/api/tags', {
tags: newTags,
})
.then((response) => {
existingTags = existingTags.concat(response.data.data)
})
.catch(() => {
this.$store.dispatch('notification/setNotification', {
type: 'error',
title: 'Error',
description: 'Tag(s) could not be created.',
return this.combineTags(existingTags, newTags)
},
combineTags(existingTags, newTags) {
return new Promise((resolve) => {
if (newTags.length > 0) {
axios
.post('/api/tags', {
tags: newTags,
})
})
}
return existingTags.map((tag) => tag.id)
.then((response) => {
existingTags = existingTags.concat(response.data.data)
resolve(existingTags.map((tag) => tag.id))
})
.catch(() => {
this.$store.dispatch('notification/setNotification', {
type: 'error',
title: 'Error',
description: 'Tag(s) could not be created.',
})
resolve(existingTags.map((tag) => tag.id))
})
} else {
resolve(existingTags.map((tag) => tag.id))
}
})
},
delete() {
this.$store.dispatch('post/deletePost', this.id)
Expand Down
30 changes: 30 additions & 0 deletions tests/PostTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,36 @@ public function testUpdatePost()
$this->assertEquals($new_content, $data->content);
}

public function testUpdatePostWithTag()
{
$user = factory(User::class)->create();
$collection = factory(Collection::class)->create();
$tag1 = factory(Tag::class)->create();
$tag2 = factory(Tag::class)->create();

$content = 'This is a very basic example.';

$this->actingAs($user)->json('POST', 'api/posts', [
'content' => $content,
'collection_id' => $collection->id,
'tags' => [$tag1->id]
]);

$this->assertEquals(201, $this->response->status());
$data = $this->response->getData()->data;
$this->assertEquals($tag1->id, $data->tags[0]->id);

$new_content = $content . '<p>++</p>';

$this->actingAs($user)->json('PATCH', 'api/posts/' . $data->id, [
'tags' => [$tag2->id]
]);

$this->assertEquals(200, $this->response->status());
$data = $this->response->getData()->data;
$this->assertEquals($tag2->id, $data->tags[0]->id);
}

public function testUpdatePostOrder()
{
$user = factory(User::class)->create();
Expand Down

0 comments on commit 27bd8a0

Please sign in to comment.