feat: add solved threads
This commit is contained in:
@@ -99,6 +99,7 @@ class PortalController extends Controller
|
||||
'id' => $thread->id,
|
||||
'title' => $thread->title,
|
||||
'body' => $thread->body,
|
||||
'solved' => (bool) $thread->solved,
|
||||
'forum' => "/api/forums/{$thread->forum_id}",
|
||||
'user_id' => $thread->user_id,
|
||||
'posts_count' => ($thread->posts_count ?? 0) + 1,
|
||||
|
||||
@@ -97,6 +97,36 @@ class ThreadController extends Controller
|
||||
return response()->json(null, 204);
|
||||
}
|
||||
|
||||
public function updateSolved(Request $request, Thread $thread): JsonResponse
|
||||
{
|
||||
$user = $request->user();
|
||||
if (!$user) {
|
||||
return response()->json(['message' => 'Unauthorized.'], 401);
|
||||
}
|
||||
|
||||
$isAdmin = $user->roles()->where('name', 'ROLE_ADMIN')->exists();
|
||||
if (!$isAdmin && $thread->user_id !== $user->id) {
|
||||
return response()->json(['message' => 'Not authorized to update solved status.'], 403);
|
||||
}
|
||||
|
||||
$data = $request->validate([
|
||||
'solved' => ['required', 'boolean'],
|
||||
]);
|
||||
|
||||
$thread->solved = $data['solved'];
|
||||
$thread->save();
|
||||
$thread->refresh();
|
||||
$thread->loadMissing([
|
||||
'user' => fn ($query) => $query
|
||||
->withCount(['posts', 'threads', 'thanksGiven', 'thanksReceived'])
|
||||
->with(['rank', 'roles']),
|
||||
'latestPost.user.rank',
|
||||
'latestPost.user.roles',
|
||||
])->loadCount('posts');
|
||||
|
||||
return response()->json($this->serializeThread($thread));
|
||||
}
|
||||
|
||||
private function parseIriId(?string $value): ?int
|
||||
{
|
||||
if (!$value) {
|
||||
@@ -120,6 +150,7 @@ class ThreadController extends Controller
|
||||
'id' => $thread->id,
|
||||
'title' => $thread->title,
|
||||
'body' => $thread->body,
|
||||
'solved' => (bool) $thread->solved,
|
||||
'forum' => "/api/forums/{$thread->forum_id}",
|
||||
'user_id' => $thread->user_id,
|
||||
'posts_count' => ($thread->posts_count ?? 0) + 1,
|
||||
|
||||
@@ -14,6 +14,7 @@ use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
* @property int|null $user_id
|
||||
* @property string $title
|
||||
* @property string $body
|
||||
* @property bool $solved
|
||||
* @property \Illuminate\Support\Carbon|null $created_at
|
||||
* @property \Illuminate\Support\Carbon|null $updated_at
|
||||
* @property-read \App\Models\Forum $forum
|
||||
@@ -41,6 +42,11 @@ class Thread extends Model
|
||||
'user_id',
|
||||
'title',
|
||||
'body',
|
||||
'solved',
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'solved' => 'bool',
|
||||
];
|
||||
|
||||
public function forum(): BelongsTo
|
||||
|
||||
Reference in New Issue
Block a user