Add additional settings; Use pilot id length from setting

This commit is contained in:
Nabeel Shahzad
2017-12-12 12:43:58 -06:00
parent 695900a008
commit e05976a982
12 changed files with 82 additions and 45 deletions

View File

@@ -4,7 +4,7 @@ namespace App\Models;
use Eloquent as Model;
use App\Models\Traits\Uuids;
use App\Models\Traits\HashId;
/**
* Class Flight
@@ -13,7 +13,7 @@ use App\Models\Traits\Uuids;
*/
class Flight extends Model
{
use Uuids;
use HashId;
public $table = 'flights';
public $incrementing = false;

View File

@@ -3,7 +3,7 @@
namespace App\Models;
use Eloquent as Model;
use App\Models\Traits\Uuids;
use App\Models\Traits\HashId;
use Illuminate\Database\Eloquent\SoftDeletes;
/**
@@ -13,7 +13,7 @@ use Illuminate\Database\Eloquent\SoftDeletes;
*/
class Pirep extends Model
{
use Uuids;
use HashId;
use SoftDeletes;
public $table = 'pireps';

View File

@@ -5,21 +5,21 @@ namespace App\Models\Traits;
use Hashids\Hashids;
trait Uuids
trait HashId
{
protected static function boot()
{
parent::boot();
static::creating(function ($model) {
static::creating(function ($model)
{
$key = $model->getKeyName();
if (empty($model->{$key})) {
$hashids = new Hashids('', 10);
$mt = str_replace('.', '', microtime(true));
$id = $hashids->encode($mt);
$model->{$key} = $id;
}
});

View File

@@ -73,19 +73,16 @@ class User extends Authenticatable
*
* @var array
*/
protected $hidden
= [
'password',
'remember_token',
];
protected $hidden = [
'password',
'remember_token',
];
protected $casts
= [
'flights' => 'integer',
'flight_time' => 'integer',
'balance' => 'double',
'timezone' => 'integer',
];
protected $casts = [
'flights' => 'integer',
'flight_time' => 'integer',
'balance' => 'double',
];
/**
* Validation rules
@@ -106,9 +103,10 @@ class User extends Authenticatable
return $key;
}
public function pilot_id()
public function getPilotIdAttribute($value)
{
return $this->airline->icao.str_pad($this->id, 3, '0', STR_PAD_LEFT);
$length = setting('pilots.id_length');
return $this->airline->icao . str_pad($this->id, $length, '0', STR_PAD_LEFT);
}
public function gravatar()

View File

@@ -2,9 +2,11 @@
namespace App\Repositories;
use Illuminate\Support\Carbon;
use Prettus\Repository\Contracts\CacheableInterface;
use App\Models\Setting;
use App\Repositories\Traits\CacheableRepository;
use Prettus\Repository\Contracts\CacheableInterface;
class SettingRepository extends BaseRepository implements CacheableInterface
{
@@ -16,17 +18,44 @@ class SettingRepository extends BaseRepository implements CacheableInterface
}
/**
* Get a setting, reading it from the cache
* @param array $key
* @return mixed|void
* Get a setting, reading it from the cache possibly
* @param string $key
* @return mixed
*/
public function get($key)
public function retrieve($key)
{
$key = strtolower($key);
$setting = $this->findWhere(['key' => $key], ['type', 'value'])->first();
if(!$setting) {
return null;
}
# cast some types
switch($setting->type) {
case 'bool':
case 'boolean':
return (bool) $setting->value;
break;
case 'date':
return Carbon::parse($setting->value);
break;
case 'int':
case 'integer':
return (int) $setting->value;
break;
default:
return $setting->value;
}
}
public function set($key, $value)
public function store($key, $value)
{
$setting = $this->findWhere(['key' => $key], ['id'])->first();
if (!$setting) {
return null;
}
$this->update(['value' => $value], $setting->id);
}
}

View File

@@ -8,7 +8,7 @@ if (!function_exists('setting')) {
{
$settingRepo = app('setting');
if($value === null) {
return $settingRepo->get($key);
return $settingRepo->retrieve($key);
} else {
$settingRepo->set($key, $value);
}