Skip to content

Commit

Permalink
Add search feature for table in dashboard
Browse files Browse the repository at this point in the history
  • Loading branch information
jcc committed May 3, 2018
1 parent 7ec1b28 commit dd46da2
Show file tree
Hide file tree
Showing 18 changed files with 289 additions and 60 deletions.
5 changes: 3 additions & 2 deletions app/Http/Controllers/Api/ArticleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Http\Requests\ArticleRequest;
use App\Repositories\ArticleRepository;

Expand All @@ -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));
}

/**
Expand Down
6 changes: 3 additions & 3 deletions app/Http/Controllers/Api/CategoryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

/**
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions app/Http/Controllers/Api/CommentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

/**
Expand Down
4 changes: 2 additions & 2 deletions app/Http/Controllers/Api/DiscussionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

/**
Expand Down
6 changes: 3 additions & 3 deletions app/Http/Controllers/Api/LinkController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

/**
Expand Down
4 changes: 2 additions & 2 deletions app/Http/Controllers/Api/TagController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

/**
Expand Down
6 changes: 3 additions & 3 deletions app/Http/Controllers/Api/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

/**
Expand Down Expand Up @@ -93,7 +93,7 @@ public function update(Request $request, $id)

/**
* Crop Avatar
*
*
* @param Request $request
* @return \Illuminate\Http\JsonResponse
*/
Expand Down
5 changes: 3 additions & 2 deletions app/Http/Controllers/Api/VisitorController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Repositories\VisitorRepository;

class VisitorController extends ApiController
Expand All @@ -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));
}

}
33 changes: 28 additions & 5 deletions app/Repositories/ArticleRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
*/
Expand All @@ -48,7 +71,7 @@ public function getById($id)

/**
* Update the article record without draft scope.
*
*
* @param int $id
* @param array $input
* @return boolean
Expand Down Expand Up @@ -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()
Expand All @@ -107,7 +130,7 @@ public function syncTag(array $tags)

/**
* Search the articles by the keyword.
*
*
* @param string $key
* @return collection
*/
Expand Down
22 changes: 21 additions & 1 deletion app/Repositories/CategoryRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
32 changes: 27 additions & 5 deletions app/Repositories/CommentRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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')
Expand Down
33 changes: 29 additions & 4 deletions app/Repositories/DiscussionRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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
*/
Expand Down Expand Up @@ -64,7 +89,7 @@ public function store($data)

/**
* Update a record by id.
*
*
* @param int $id
* @param array $data
* @return boolean
Expand All @@ -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
Expand All @@ -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()
Expand Down
Loading

0 comments on commit dd46da2

Please sign in to comment.