Skip to content

Commit

Permalink
+ model App\Eloquent\Model\LatestReplier
Browse files Browse the repository at this point in the history
+ add key `latestRepliers` in returned response @ `App\Http\Controllers\PostsQuery::query()`
@ be
  • Loading branch information
n0099 committed Aug 2, 2024
1 parent 2ff5a6c commit 17f07d7
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 10 deletions.
24 changes: 24 additions & 0 deletions be/app/Eloquent/Model/LatestReplier.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace App\Eloquent\Model;

use App\Eloquent\ModelHasPublicField;
use Illuminate\Database\Eloquent\Model;

class LatestReplier extends Model
{
use ModelHasPublicField;

protected $table = 'tbmc_latestReplier';

public function __construct(array $attributes = [])
{
parent::__construct($attributes);
$this->publicFields = [
'id',
'uid',
'createdAt',
'updatedAt'
];
}
}
2 changes: 1 addition & 1 deletion be/app/Eloquent/Model/Post/Thread.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function __construct(array $attributes = [])
'authorUid',
'postedAt',
'latestReplyPostedAt',
'latestReplierUid',
'latestReplierId',
'replyCount',
'viewCount',
'shareCount',
Expand Down
20 changes: 11 additions & 9 deletions be/app/Http/Controllers/PostsQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Http\Controllers;

use App\Eloquent\Model\LatestReplier;
use App\Helper;
use App\Http\PostsQuery\IndexQuery;
use App\Http\PostsQuery\ParamsValidator;
Expand All @@ -11,6 +12,7 @@
use Barryvdh\Debugbar\Facades\Debugbar;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;

class PostsQuery extends Controller
{
Expand Down Expand Up @@ -53,17 +55,16 @@ public function query(\Illuminate\Http\Request $request): array
Debugbar::stopMeasure('fillWithParentPost');

Debugbar::startMeasure('queryUsers');
$latestRepliers = LatestReplier::whereIn('id', $result['threads']->pluck('latestReplierId'))
->selectPublicFields()->get();
$whereCurrentFid = static fn (HasOne $q) => $q->where('fid', $result['fid']);
$users = User::whereIn(
'uid',
$result['threads']
->pluck('latestReplierUid')
->concat(array_map(
static fn (string $type) => $result[$type]->pluck('authorUid'),
Helper::POST_TYPES_PLURAL
))
->flatten()->filter() // remove NULLs from column latestReplierUid
->unique()->toArray()
collect($result)
->only(Helper::POST_TYPES_PLURAL)
->flatMap(static fn (Collection $posts) => $posts->pluck('authorUid'))
->concat($latestRepliers->pluck('uid'))
->filter()->unique()->toArray() // remove NULLs
)->with(['currentForumModerator' => $whereCurrentFid, 'currentAuthorExpGrade' => $whereCurrentFid])
->selectPublicFields()->get()->toArray();
Debugbar::stopMeasure('queryUsers');
Expand All @@ -76,7 +77,8 @@ public function query(\Illuminate\Http\Request $request): array
],
'forum' => Forum::fid($result['fid'])->selectPublicFields()->first()?->toArray(),
'threads' => $query->reOrderNestedPosts($query::nestPostsWithParent(...$result)),
'users' => $users
'users' => $users,
'latestRepliers' => $latestRepliers
];
}
}

0 comments on commit 17f07d7

Please sign in to comment.