2017-06-29 08:56:10 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Repositories;
|
|
|
|
|
2019-07-16 03:44:31 +08:00
|
|
|
use App\Contracts\Repository;
|
2017-12-23 04:48:15 +08:00
|
|
|
use App\Models\Enums\PirepState;
|
2017-06-29 08:56:10 +08:00
|
|
|
use App\Models\Pirep;
|
2017-12-02 00:44:35 +08:00
|
|
|
use App\Models\User;
|
2017-06-29 08:56:10 +08:00
|
|
|
|
2018-03-20 09:50:40 +08:00
|
|
|
/**
|
|
|
|
* Class PirepRepository
|
|
|
|
*/
|
|
|
|
class PirepRepository extends Repository
|
2017-06-29 08:56:10 +08:00
|
|
|
{
|
|
|
|
protected $fieldSearchable = [
|
2017-12-03 14:48:33 +08:00
|
|
|
'user_id',
|
|
|
|
'status',
|
2017-12-23 04:48:15 +08:00
|
|
|
'state',
|
2017-06-29 08:56:10 +08:00
|
|
|
];
|
|
|
|
|
2018-03-20 09:50:40 +08:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2017-06-29 08:56:10 +08:00
|
|
|
public function model()
|
|
|
|
{
|
|
|
|
return Pirep::class;
|
|
|
|
}
|
2017-12-02 00:44:35 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all the pending reports in order. Returns the Pirep
|
|
|
|
* model but you still need to call ->all() or ->paginate()
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2017-12-02 00:44:35 +08:00
|
|
|
* @param User|null $user
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2017-12-02 00:44:35 +08:00
|
|
|
* @return Pirep
|
|
|
|
*/
|
2018-03-20 09:50:40 +08:00
|
|
|
public function getPending(User $user = null)
|
2017-12-02 00:44:35 +08:00
|
|
|
{
|
|
|
|
$where = [];
|
2018-03-20 09:50:40 +08:00
|
|
|
if ($user !== null) {
|
2017-12-02 00:44:35 +08:00
|
|
|
$where['user_id'] = $user->id;
|
|
|
|
}
|
|
|
|
|
|
|
|
$pireps = $this->orderBy('created_at', 'desc')->findWhere($where)->all();
|
2018-03-20 09:50:40 +08:00
|
|
|
|
2017-12-02 00:44:35 +08:00
|
|
|
return $pireps;
|
|
|
|
}
|
2017-12-04 00:55:01 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Number of PIREPs that are pending
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2017-12-04 00:55:01 +08:00
|
|
|
* @param User|null $user
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2017-12-04 00:55:01 +08:00
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function getPendingCount(User $user = null)
|
|
|
|
{
|
2017-12-23 04:48:15 +08:00
|
|
|
$where = [
|
|
|
|
'state' => PirepState::PENDING,
|
|
|
|
];
|
|
|
|
|
2017-12-04 00:55:01 +08:00
|
|
|
if ($user !== null) {
|
|
|
|
$where['user_id'] = $user->id;
|
|
|
|
}
|
|
|
|
|
2018-01-09 06:22:26 +08:00
|
|
|
$pireps = $this->orderBy('created_at', 'desc')
|
2018-03-20 09:50:40 +08:00
|
|
|
->findWhere($where, ['id'])
|
|
|
|
->count();
|
|
|
|
|
2017-12-04 00:55:01 +08:00
|
|
|
return $pireps;
|
|
|
|
}
|
2017-06-29 08:56:10 +08:00
|
|
|
}
|