phpvms/app/Repositories/UserRepository.php

85 lines
1.7 KiB
PHP
Raw Normal View History

2017-11-30 08:01:07 +08:00
<?php
2017-11-30 08:01:07 +08:00
namespace App\Repositories;
use App\Interfaces\Repository;
2017-12-31 10:40:32 +08:00
use App\Models\Enums\UserState;
2018-02-21 12:33:09 +08:00
use App\Models\User;
2017-12-23 05:11:27 +08:00
use App\Repositories\Criteria\WhereCriteria;
2018-02-21 12:33:09 +08:00
use Illuminate\Http\Request;
2017-11-30 08:01:07 +08:00
/**
* Class UserRepository
*/
class UserRepository extends Repository
2017-11-30 08:01:07 +08:00
{
protected $fieldSearchable = [
'name' => 'like',
'email' => 'like',
'home_airport_id',
'curr_airport_id',
2018-08-27 00:40:04 +08:00
'state',
2017-11-30 08:01:07 +08:00
];
/**
* @return string
*/
2017-11-30 08:01:07 +08:00
public function model()
{
return User::class;
}
/**
* Number of PIREPs that are pending
2018-08-27 00:40:04 +08:00
*
* @return mixed
*/
public function getPendingCount()
{
$where = [
2017-12-31 10:40:32 +08:00
'state' => UserState::PENDING,
];
$users = $this->orderBy('created_at', 'desc')
->findWhere($where, ['id'])
->count();
return $users;
}
2017-12-23 05:11:27 +08:00
/**
* Create the search criteria and return this with the stuff pushed
2018-08-27 00:40:04 +08:00
*
2017-12-23 05:11:27 +08:00
* @param Request $request
* @param bool $only_active
2018-08-27 00:40:04 +08:00
*
2017-12-23 05:11:27 +08:00
* @throws \Prettus\Repository\Exceptions\RepositoryException
2018-08-27 00:40:04 +08:00
*
* @return $this
2017-12-23 05:11:27 +08:00
*/
public function searchCriteria(Request $request, bool $only_active = true)
{
$where = [];
if ($only_active) {
2017-12-31 10:40:32 +08:00
$where['state'] = UserState::ACTIVE;
2017-12-23 05:11:27 +08:00
}
if ($request->filled('name')) {
$where['name'] = $request->name;
}
if ($request->filled('email')) {
$where['email'] = $request->email;
}
2018-01-04 22:14:39 +08:00
if ($request->filled('state')) {
$where['state'] = $request->state;
}
2017-12-23 05:11:27 +08:00
$this->pushCriteria(new WhereCriteria($request, $where));
2017-12-23 05:11:27 +08:00
return $this;
}
2017-11-30 08:01:07 +08:00
}