diff --git a/app/Http/Controllers/Api/ArticleController.php b/app/Http/Controllers/Api/ArticleController.php index 40154ed49..ddfebb694 100644 --- a/app/Http/Controllers/Api/ArticleController.php +++ b/app/Http/Controllers/Api/ArticleController.php @@ -2,6 +2,7 @@ namespace App\Http\Controllers\Api; +use Illuminate\Http\Request; use App\Http\Requests\ArticleRequest; use App\Repositories\ArticleRepository; @@ -21,9 +22,9 @@ public function __construct(ArticleRepository $article) * * @return \Illuminate\Http\JsonResponse */ - public function index() + public function index(Request $request) { - return $this->response->collection($this->article->page()); + return $this->response->collection($this->article->pageWithRequest($request)); } /** diff --git a/app/Http/Controllers/Api/CategoryController.php b/app/Http/Controllers/Api/CategoryController.php index 25c4f9aea..7642b8a53 100644 --- a/app/Http/Controllers/Api/CategoryController.php +++ b/app/Http/Controllers/Api/CategoryController.php @@ -22,9 +22,9 @@ public function __construct(CategoryRepository $category) * * @return \Illuminate\Http\JsonResponse */ - public function index() + public function index(Request $request) { - return $this->response->collection($this->category->page()); + return $this->response->collection($this->category->pageWithRequest($request)); } /** @@ -99,7 +99,7 @@ public function update(CategoryRequest $request, $id) * Remove the specified resource from storage. * * @param int $id - * + * * @return \Illuminate\Http\JsonResponse */ public function destroy($id) diff --git a/app/Http/Controllers/Api/CommentController.php b/app/Http/Controllers/Api/CommentController.php index 01389b983..df80c3325 100644 --- a/app/Http/Controllers/Api/CommentController.php +++ b/app/Http/Controllers/Api/CommentController.php @@ -24,9 +24,9 @@ public function __construct(CommentRepository $comment) * * @return \Illuminate\Http\JsonResponse */ - public function index() + public function index(Request $request) { - return $this->response->collection($this->comment->page()); + return $this->response->collection($this->comment->pageWithRequest($request)); } /** diff --git a/app/Http/Controllers/Api/DiscussionController.php b/app/Http/Controllers/Api/DiscussionController.php index 98fc1ce06..e944a4f02 100644 --- a/app/Http/Controllers/Api/DiscussionController.php +++ b/app/Http/Controllers/Api/DiscussionController.php @@ -22,9 +22,9 @@ public function __construct(DiscussionRepository $discussion) * * @return \Illuminate\Http\JsonResponse */ - public function index() + public function index(Request $request) { - return $this->response->collection($this->discussion->page(10, 'desc')); + return $this->response->collection($this->discussion->pageWithRequest($request)); } /** diff --git a/app/Http/Controllers/Api/LinkController.php b/app/Http/Controllers/Api/LinkController.php index dfbb9afe9..dea372b0d 100644 --- a/app/Http/Controllers/Api/LinkController.php +++ b/app/Http/Controllers/Api/LinkController.php @@ -20,15 +20,15 @@ public function __construct(LinkRepository $link) $this->manager = app('uploader'); } - + /** * Display a listing of the resource. * * @return \Illuminate\Http\JsonResponse */ - public function index() + public function index(Request $request) { - return $this->response->collection($this->link->page()); + return $this->response->collection($this->link->pageWithRequest($request)); } /** diff --git a/app/Http/Controllers/Api/TagController.php b/app/Http/Controllers/Api/TagController.php index d747bb0b7..23f44a8f3 100644 --- a/app/Http/Controllers/Api/TagController.php +++ b/app/Http/Controllers/Api/TagController.php @@ -22,9 +22,9 @@ public function __construct(TagRepository $tag) * * @return \Illuminate\Http\JsonResponse */ - public function index() + public function index(Request $request) { - return $this->response->collection($this->tag->page()); + return $this->response->collection($this->tag->pageWithRequest($request)); } /** diff --git a/app/Http/Controllers/Api/UserController.php b/app/Http/Controllers/Api/UserController.php index db16bb59a..c4630319e 100644 --- a/app/Http/Controllers/Api/UserController.php +++ b/app/Http/Controllers/Api/UserController.php @@ -23,9 +23,9 @@ public function __construct(UserRepository $user) * * @return \Illuminate\Http\JsonResponse */ - public function index() + public function index(Request $request) { - return $this->response->collection($this->user->page()); + return $this->response->collection($this->user->pageWithRequest($request)); } /** @@ -93,7 +93,7 @@ public function update(Request $request, $id) /** * Crop Avatar - * + * * @param Request $request * @return \Illuminate\Http\JsonResponse */ diff --git a/app/Http/Controllers/Api/VisitorController.php b/app/Http/Controllers/Api/VisitorController.php index 1b1178326..653725c5b 100644 --- a/app/Http/Controllers/Api/VisitorController.php +++ b/app/Http/Controllers/Api/VisitorController.php @@ -2,6 +2,7 @@ namespace App\Http\Controllers\Api; +use Illuminate\Http\Request; use App\Repositories\VisitorRepository; class VisitorController extends ApiController @@ -20,9 +21,9 @@ public function __construct(VisitorRepository $visitor) * * @return \Illuminate\Http\JsonResponse */ - public function index() + public function index(Request $request) { - return $this->response->collection($this->visitor->page()); + return $this->response->collection($this->visitor->pageWithRequest($request)); } } diff --git a/app/Repositories/ArticleRepository.php b/app/Repositories/ArticleRepository.php index 823635438..406f1d157 100644 --- a/app/Repositories/ArticleRepository.php +++ b/app/Repositories/ArticleRepository.php @@ -22,7 +22,30 @@ public function __construct(Article $article, VisitorRepository $visitor) /** * Get the page of articles without draft scope. - * + * + * @param Request $request + * @param integer $number + * @param string $sort + * @param string $sortColumn + * @return collection + */ + public function pageWithRequest($request, $number = 10, $sort = 'desc', $sortColumn = 'created_at') + { + $this->model = $this->checkAuthScope(); + + $keyword = $request->get('keyword'); + + return $this->model + ->when($keyword, function ($query) use ($keyword) { + $query->where('title', 'like', "%{$keyword}%") + ->orWhere('subtitle', 'like', "%{$keyword}%"); + }) + ->orderBy($sortColumn, $sort)->paginate($number); + } + + /** + * Get the page of articles without draft scope. + * * @param integer $number * @param string $sort * @param string $sortColumn @@ -37,7 +60,7 @@ public function page($number = 10, $sort = 'desc', $sortColumn = 'created_at') /** * Get the article record without draft scope. - * + * * @param int $id * @return mixed */ @@ -48,7 +71,7 @@ public function getById($id) /** * Update the article record without draft scope. - * + * * @param int $id * @param array $input * @return boolean @@ -82,7 +105,7 @@ public function getBySlug($slug) /** * Check the auth and the model without global scope when user is the admin. - * + * * @return Model */ public function checkAuthScope() @@ -107,7 +130,7 @@ public function syncTag(array $tags) /** * Search the articles by the keyword. - * + * * @param string $key * @return collection */ diff --git a/app/Repositories/CategoryRepository.php b/app/Repositories/CategoryRepository.php index 51986cd29..8899d3bc7 100644 --- a/app/Repositories/CategoryRepository.php +++ b/app/Repositories/CategoryRepository.php @@ -15,9 +15,29 @@ public function __construct(Category $category) $this->model = $category; } + /** + * Get number of the records. + * + * @param Request $request + * @param integer $number + * @param string $sort + * @param string $sortColumn + * @return collection + */ + public function pageWithRequest($request, $number = 10, $sort = 'desc', $sortColumn = 'created_at') + { + $keyword = $request->get('keyword'); + + return $this->model + ->when($keyword, function ($query) use ($keyword) { + $query->where('name', 'like', "%{$keyword}%"); + }) + ->orderBy($sortColumn, $sort)->paginate($number); + } + /** * Get record by the name. - * + * * @param string $name * @return collection */ diff --git a/app/Repositories/CommentRepository.php b/app/Repositories/CommentRepository.php index 63de42fda..79a538ab9 100644 --- a/app/Repositories/CommentRepository.php +++ b/app/Repositories/CommentRepository.php @@ -18,6 +18,28 @@ public function __construct(Comment $comment) $this->model = $comment; } + /** + * Get number of the records. + * + * @param Request $request + * @param integer $number + * @param string $sort + * @param string $sortColumn + * @return collection + */ + public function pageWithRequest($request, $number = 10, $sort = 'desc', $sortColumn = 'created_at') + { + $keyword = $request->get('keyword'); + + return $this->model + ->when($keyword, function ($query) use ($keyword) { + $query->whereHas('user', function ($query) use ($keyword) { + $query->where('name', 'like', "%{$keyword}%"); + }); + }) + ->orderBy($sortColumn, $sort)->paginate($number); + } + /** * Store a new record. * @@ -57,7 +79,7 @@ public function save($model, $input) /** * Get comments by the commentable_id and commentable_type - * + * * @param int $commentableId * @param string $commentableType * @return array @@ -71,10 +93,10 @@ public function getByCommentable($commentableId, $commentableType) /** * Toogle up vote and down vote by user. - * + * * @param int $id * @param boolean $isUpVote - * + * * @return boolean */ public function toggleVote($id, $isUpVote = true) @@ -94,11 +116,11 @@ public function toggleVote($id, $isUpVote = true) /** * Up vote or down vote item. - * + * * @param \App\User $user * @param \Illuminate\Database\Eloquent\Model $target * @param string $type - * + * * @return boolean */ public function upOrDownVote($user, $target, $type = 'up') diff --git a/app/Repositories/DiscussionRepository.php b/app/Repositories/DiscussionRepository.php index 9376d5c26..1bc0d7e14 100644 --- a/app/Repositories/DiscussionRepository.php +++ b/app/Repositories/DiscussionRepository.php @@ -16,6 +16,31 @@ public function __construct(Discussion $discussion) $this->model = $discussion; } + /** + * Get number of the records. + * + * @param Request $request + * @param integer $number + * @param string $sort + * @param string $sortColumn + * @return collection + */ + public function pageWithRequest($request, $number = 10, $sort = 'desc', $sortColumn = 'created_at') + { + $this->model = $this->checkAuthScope(); + + $keyword = $request->get('keyword'); + + return $this->model + ->when($keyword, function ($query) use ($keyword) { + $query->where('title', 'like', "%{$keyword}%") + ->orWhereHas('user', function ($query) use ($keyword) { + $query->where('name', 'like', "%{$keyword}%"); + }); + }) + ->orderBy($sortColumn, $sort)->paginate($number); + } + /** * Get number of the records. * @@ -33,7 +58,7 @@ public function page($number = 10, $sort = 'asc', $sortColumn = 'created_at') /** * Get the discussion record without draft scope. - * + * * @param int $id * @return mixed */ @@ -64,7 +89,7 @@ public function store($data) /** * Update a record by id. - * + * * @param int $id * @param array $data * @return boolean @@ -86,7 +111,7 @@ public function update(int $id, array $data) /** * Update a record by id without tag. - * + * * @param int $id * @param array $data * @return boolean @@ -102,7 +127,7 @@ public function updateWithoutTags(int $id, array $data) /** * Check the auth and the model without global scope when user is the admin. - * + * * @return Model */ public function checkAuthScope() diff --git a/app/Repositories/LinkRepository.php b/app/Repositories/LinkRepository.php index 5750fa0f6..43dda8db2 100644 --- a/app/Repositories/LinkRepository.php +++ b/app/Repositories/LinkRepository.php @@ -31,9 +31,31 @@ public function page($number = 10, $sort = 'desc', $sortColumn = 'created_at') return $this->model->orderBy($sortColumn, $sort)->paginate($number); } + /** + * Get number of the records. + * + * @param Request $request + * @param integer $number + * @param string $sort + * @param string $sortColumn + * @return collection + */ + public function pageWithRequest($request, $number = 10, $sort = 'desc', $sortColumn = 'created_at') + { + $this->model = $this->checkAuthScope(); + + $keyword = $request->get('keyword'); + + return $this->model + ->when($keyword, function ($query) use ($keyword) { + $query->where('name', 'like', "%{$keyword}%"); + }) + ->orderBy($sortColumn, $sort)->paginate($number); + } + /** * Get the article record without draft scope. - * + * * @param int $id * @return mixed */ @@ -44,7 +66,7 @@ public function getById($id) /** * Update the article record without draft scope. - * + * * @param int $id * @param array $input * @return boolean @@ -58,7 +80,7 @@ public function update($id, $input) /** * Check the auth and the model without global scope when user is the admin. - * + * * @return Model */ public function checkAuthScope() diff --git a/app/Repositories/TagRepository.php b/app/Repositories/TagRepository.php index c4a27ce6a..4f9f89046 100644 --- a/app/Repositories/TagRepository.php +++ b/app/Repositories/TagRepository.php @@ -15,9 +15,30 @@ public function __construct(Tag $tag) $this->model = $tag; } + /** + * Get number of the records. + * + * @param Request $request + * @param integer $number + * @param string $sort + * @param string $sortColumn + * @return collection + */ + public function pageWithRequest($request, $number = 10, $sort = 'desc', $sortColumn = 'created_at') + { + $keyword = $request->get('keyword'); + + return $this->model + ->when($keyword, function ($query) use ($keyword) { + $query->where('tag', 'like', "%{$keyword}%") + ->orWhere('title', 'like', "%{$keyword}%"); + }) + ->orderBy($sortColumn, $sort)->paginate($number); + } + /** * Get record by the name. - * + * * @param string $name * @return collection */ diff --git a/app/Repositories/UserRepository.php b/app/Repositories/UserRepository.php index 5747f22b1..0c29d6d85 100644 --- a/app/Repositories/UserRepository.php +++ b/app/Repositories/UserRepository.php @@ -41,7 +41,7 @@ public function getList() /** * Get the user by name. - * + * * @param string $name * @return mixed */ @@ -52,6 +52,27 @@ public function getByName($name) ->first(); } + /** + * Get number of the records + * + * @param Request $request + * @param int $number + * @param string $sort + * @param string $sortColumn + * @return Paginate + */ + public function pageWithRequest($request, $number = 10, $sort = 'desc', $sortColumn = 'created_at') + { + $keyword = $request->get('keyword'); + + return $this->model->withoutGlobalScope(StatusScope::class) + ->when($keyword, function ($query) use ($keyword) { + $query->where('name', 'like', "%{$keyword}%"); + }) + ->orderBy($sortColumn, $sort) + ->paginate($number); + } + /** * Get number of the records * @@ -67,7 +88,7 @@ public function page($number = 10, $sort = 'desc', $sortColumn = 'created_at') /** * Get the article record without draft scope. - * + * * @param int $id * @return mixed */ @@ -78,7 +99,7 @@ public function getById($id) /** * Update the article record without draft scope. - * + * * @param int $id * @param array $input * @return boolean @@ -92,7 +113,7 @@ public function update($id, $input) /** * Get user by the user github id. - * + * * @param int $githubId * @return mixed */ @@ -103,8 +124,8 @@ public function getByGithubId($githubId) /** * Change the user password. - * - * @param App\User $user + * + * @param App\User $user * @param string $password * @return boolean */ @@ -115,7 +136,7 @@ public function changePassword($user, $password) /** * Save the user avatar path. - * + * * @param int $id * @param string $photo * @return boolean diff --git a/app/Repositories/VisitorRepository.php b/app/Repositories/VisitorRepository.php index ca1157ecc..16cb90641 100644 --- a/app/Repositories/VisitorRepository.php +++ b/app/Repositories/VisitorRepository.php @@ -31,6 +31,29 @@ public function __construct(Visitor $visitor, IP $ip) $this->ip = $ip; } + /** + * Get number of the records. + * + * @param Request $request + * @param integer $number + * @param string $sort + * @param string $sortColumn + * @return collection + */ + public function pageWithRequest($request, $number = 10, $sort = 'desc', $sortColumn = 'created_at') + { + $keyword = $request->get('keyword'); + + return $this->model + ->when($keyword, function ($query) use ($keyword) { + $query->where('ip', $keyword) + ->orWhereHas('article', function ($query) use ($keyword) { + $query->where('title', 'like', "%{$keyword}%"); + }); + }) + ->orderBy($sortColumn, $sort)->paginate($number); + } + /** * Update or create the record of visitors table * @@ -73,7 +96,7 @@ public function hasArticleIp($article_id, $ip) /** * Get all the clicks. - * + * * @return int */ public function getAllClicks() diff --git a/resources/assets/js/dashboard/components/Table.vue b/resources/assets/js/dashboard/components/Table.vue index f30cf3c4e..def77924f 100644 --- a/resources/assets/js/dashboard/components/Table.vue +++ b/resources/assets/js/dashboard/components/Table.vue @@ -2,7 +2,13 @@