guillermo- get-users [skip ci]

This commit is contained in:
ivan 2016-11-30 03:33:46 -03:00
parent 5c4eef12e5
commit 8ab6240385
6 changed files with 118 additions and 2 deletions

View File

@ -63,7 +63,8 @@ class CreateController extends Controller {
));
$author->sharedTicketList->add($ticket);
$author->tickets++;
$author->store();
$ticket->store();

View File

@ -8,6 +8,7 @@ include 'user/send-recover-password.php';
include 'user/edit-password.php';
include 'user/edit-email.php';
include 'user/get.php';
include 'user/get-users.php';
$userControllers = new ControllerGroup();
$userControllers->setGroupPath('/user');
@ -21,5 +22,6 @@ $userControllers->addController(new RecoverPasswordController);
$userControllers->addController(new EditPassword);
$userControllers->addController(new EditEmail);
$userControllers->addController(new GetUserController);
$userControllers->addController(new GetUsersController);
$userControllers->finalize();

View File

@ -0,0 +1,107 @@
<?php
use Respect\Validation\Validator as DataValidator;
class GetUsersController extends Controller {
const PATH = '/get-users';
public function validations() {
return[
'permission' => 'staff_2',
'requestData' => [
'page' => [
'validation' => DataValidator::numeric(),
'error' => ERRORS::INVALID_PAGE
],
'orderBy' => [
'validation' => DataValidator::in(['id','tickets']),
'error' => ERRORS::INVALID_ORDER
]
]
];
}
public function handler() {
$userList = $this->getUserList();
$userListArray = [];
foreach ($userList as $user) {
$userListArray[] = [
'id' => $user->id,
'name' => $user->name,
'tickets' => $user->tickets,
'email' => $user->email,
'signupDate' => $user->signupDate
];
}
Response::respondSuccess([
'users' => $userListArray,
'pages' => $this->getPagesQuantity(),
'page' => Controller::request('page'),
'orderBy' => Controller::request('orderBy'),
'desc' => Controller::request('desc'),
'search' => Controller::request('search')
]);
}
private function getUserList() {
$query = $this->getSearchQuery();
return User::find($query, [
'%' . Controller::request('search') . '%',
'%' . Controller::request('search') . '%',
Controller::request('search') . '%',
Controller::request('search') . '%'
]);
}
private function getPagesQuantity() {
$query = '';
if(Controller::request('search')) {
$query .= " (name LIKE ? OR email LIKE ? )";
}
$usersQuantity = User::count($query, [
'%' . Controller::request('search') . '%',
'%' . Controller::request('search') . '%'
]);
return ceil($usersQuantity / 10);
}
private function getSearchQuery() {
$query = '';
if(Controller::request('search')) {
$query .= " (name LIKE ? OR email LIKE ? )";
$query .= " ORDER BY CASE WHEN (name LIKE ? OR email LIKE ?)";
$query .= " THEN 1 ELSE 2 END ASC,";
} else {
$query .= " ORDER BY ";
}
$query .= $this->getOrderAndLimit();
return $query;
}
private function getOrderAndLimit() {
$query = '';
if(Controller::request('orderBy') === 'tickets') {
$query .= 'tickets';
} else {
$query .= 'id';
}
if(Controller::request('desc')) {
$query .= ' desc';
} else {
$query .= ' asc';
}
$query .= " LIMIT 10 OFFSET ". ((Controller::request('page')-1)*10);
return $query;
}
}

View File

@ -65,6 +65,8 @@ class SignUpController extends Controller {
$userInstance->setProperties([
'name' => $this->userName,
'signupDate' => Date::getCurrentDate(),
'tickets' => 0,
'email' => $this->userEmail,
'password' => Hashing::hashPassword($this->userPassword)
]);

View File

@ -22,4 +22,6 @@ class ERRORS {
const INVALID_PAGE = 'INVALID_PAGE';
const INVALID_QUERY = 'INVALID_QUERY';
const INVALID_TOPIC = 'INVALID_TOPIC';
const INVALID_SEARCH = 'INVALID_SEARCH';
const INVALID_ORDER = 'INVALID_ORDER';
}

View File

@ -15,8 +15,10 @@ class User extends DataStore {
'email',
'password',
'name',
'signupDate',
'tickets',
'sharedTicketList',
'verificationToken',
'verificationToken'
];
}