Skip to content

Commit 3141172

Browse files
committed
refactor: streamline comment deletion process and enhance database transaction handling
1 parent dde4ff5 commit 3141172

File tree

3 files changed

+40
-13
lines changed

3 files changed

+40
-13
lines changed

app/Controllers/Api/CommentController.php

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -122,27 +122,15 @@ public function destroy(string $id): JsonResponse
122122
}
123123

124124
$comment = $this->comment->getByOwnId(Auth::id(), $id);
125-
126125
if (!$comment->exist()) {
127126
return $this->json->errorNotFound();
128127
}
129128

130-
try {
131-
$status = DB::transaction(function (LikeContract $like) use ($comment): int {
132-
$like->deleteByCommentID($comment->uuid);
133-
$this->comment->deleteByParentID($comment->uuid);
134-
135-
return $comment->destroy();
136-
});
137-
138-
if ($status === 1) {
129+
if ($this->comment->deleteAllByParentID(Auth::id(), $comment->uuid)) {
139130
return $this->json->successStatusTrue();
140131
}
141132

142133
return $this->json->errorServer();
143-
} catch (Throwable) {
144-
return $this->json->errorServer();
145-
}
146134
}
147135

148136
#[UuidMiddleware]

app/Repositories/CommentContract.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ public function getAll(int $user_id, bool $is_admin, string $user_name, int $lim
1111
public function count(int $user_id): int;
1212
public function getByUuid(int $user_id, string $uuid): Model;
1313
public function getByOwnId(int $user_id, string $own_id): Model;
14+
public function deleteAllByParentID(int $userId, string $parentId): bool;
1415
public function deleteByParentID(string $uuid): int;
1516
public function countCommentByUserID(int $id): int;
1617
public function countPresenceByUserID(int $id): Model;

app/Repositories/CommentRepositories.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,44 @@ public function getByOwnId(int $user_id, string $own_id): Model
123123
->first();
124124
}
125125

126+
public function deleteAllByParentID(int $userId, string $parentId): bool
127+
{
128+
$commentUuids = [$parentId];
129+
$nextParents = [$parentId];
130+
131+
while (count($nextParents) > 0) {
132+
$comments = Comment::where('user_id', $userId)
133+
->whereIn('parent_id', $nextParents)
134+
->select('uuid')
135+
->get();
136+
137+
$newUuids = [];
138+
foreach ($comments as $comment) {
139+
$commentUuids[] = $comment->uuid;
140+
$newUuids[] = $comment->uuid;
141+
}
142+
143+
$nextParents = $newUuids;
144+
}
145+
146+
return DB::transaction(function () use ($commentUuids, $userId): bool {
147+
148+
Like::where('user_id', $userId)
149+
->whereIn('comment_id', $commentUuids)
150+
->delete();
151+
152+
$deletedComments = Comment::where('user_id', $userId)
153+
->whereIn('uuid', $commentUuids)
154+
->delete();
155+
156+
if (count($commentUuids) === $deletedComments) {
157+
return true;
158+
}
159+
160+
throw new Exception('Uncompleted deletion: comments=' . strval($deletedComments));
161+
});
162+
}
163+
126164
public function deleteByParentID(string $uuid): int
127165
{
128166
return Comment::where('parent_id', $uuid)->delete();

0 commit comments

Comments
 (0)