Skip to content

Commit

Permalink
Merge branch 'quetzyg-improve-receivers-controller'
Browse files Browse the repository at this point in the history
  • Loading branch information
akazorg committed Oct 19, 2018
2 parents c353773 + f4f5f00 commit 63290a8
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 22 deletions.
37 changes: 15 additions & 22 deletions src/Http/Controllers/ReceiversController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Launcher\Mercurius\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Launcher\Mercurius\Repositories\UserRepository;

class ReceiversController extends Controller
{
Expand All @@ -19,36 +21,27 @@ public function __construct()
/**
* Search for receivers.
*
* @param Request $request
* @param Request $request
* @param UserRepository $userRepository
*
* @return \Illuminate\Http\Response|\Illuminate\Contracts\Routing\ResponseFactory
*/
public function search(Request $request)
public function search(Request $request, UserRepository $userRepository): Response
{
try {
$query = $request->input('q', '');

$result = [
if (($query = $request->input('q')) === null) {
return response([
'hits' => [],
'total' => 0,
];

if ($query == '') {
return response($result);
}
]);
}

$_usr = config('mercurius.models.user');
$_where = ['name', 'LIKE', '%'.$query.'%'];
$paginator = $userRepository->search($query);

$result['total'] = $_usr::where(...$_where)->count();
$result['hits'] = $_usr::select('id', 'name', 'avatar', 'is_online')
->where(...$_where)
->limit(6)
->get();
$result = [
'total' => $paginator->total(),
'hits' => $paginator->items(),
];

return response($result);
} catch (\Exception $e) {
return [$e->getMessage()];
}
return response($result);
}
}
29 changes: 29 additions & 0 deletions src/Repositories/UserRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Launcher\Mercurius\Repositories;

use Illuminate\Contracts\Pagination\LengthAwarePaginator;

class UserRepository
{
/**
* Perform a search.
*
* @param string $query
* @param int $limit
*
* @return LengthAwarePaginator
*/
public function search(string $query, int $limit = 6): LengthAwarePaginator
{
$userFqcn = config('mercurius.models.user');

return $userFqcn::where('name', 'LIKE', '%'.$query.'%')
->paginate($limit, [
'id',
'name',
'avatar',
'is_online',
]);
}
}

0 comments on commit 63290a8

Please sign in to comment.