Implement the other functionality for awards #154

This commit is contained in:
Nabeel Shahzad
2018-03-17 00:18:03 -05:00
parent 45a22e26be
commit 31b9195a6e
5 changed files with 76 additions and 29 deletions

View File

@@ -16,9 +16,17 @@ class CreateAwardsTable extends Migration
Schema::create('awards', function (Blueprint $table) { Schema::create('awards', function (Blueprint $table) {
$table->increments('id'); $table->increments('id');
$table->string('title', 50); $table->string('title', 50);
$table->text('description', 50)->nullable(); $table->text('description')->nullable();
$table->string('image', 255)->nullable(); $table->text('image_url')->nullable();
# ref fields are expenses tied to some model object
# EG, the airports has an internal expense for gate costs
$table->string('ref_class')->nullable();
$table->string('ref_class_id', 36)->nullable();
$table->timestamps(); $table->timestamps();
$table->index(['ref_class', 'ref_class_id']);
}); });
} }

View File

@@ -2,6 +2,7 @@
namespace App\Interfaces; namespace App\Interfaces;
use App\Models\Award;
use App\Models\User; use App\Models\User;
use App\Models\UserAward; use App\Models\UserAward;
@@ -22,42 +23,50 @@ class AwardInterface
protected $award; protected $award;
/** /**
* AwardInterface constructor. * @var User
* @param $award
*/ */
public function __construct($award) protected $user;
/**
* AwardInterface constructor.
* @param Award $award
* @param User $user
*/
public function __construct(Award $award, User $user)
{ {
$this->award = $award; $this->award = $award;
$this->user = $user;
} }
/** /**
* Add the award to this user, if they don't already have it * Add the award to this user, if they don't already have it
* @param User $user * @return bool|UserAward
* @return bool
*/ */
public function addAward(User $user) public function addAward()
{ {
$w = [ $w = [
'user_id' => $user->id, 'user_id' => $this->user->id,
'award_id' => $this->award->id 'award_id' => $this->award->id
]; ];
$found = UserAward::where($w)->count('id'); $found = UserAward::where($w)->count('id');
if($found > 0) { if ($found > 0) {
return true; return true;
} }
// Associate this award to the user now // Associate this award to the user now
$award = new UserAward($w); $award = new UserAward($w);
$award->save(); $award->save();
return $award;
} }
/** /**
* Each award class just needs to award * Each award class just needs to return true or false
* @param User $user * if it should actually be awarded to a user
* @return mixed * @return boolean
*/ */
public function check(User $user) public function check()
{ {
return false; return false;
} }

View File

@@ -3,8 +3,10 @@
namespace App\Models; namespace App\Models;
/** /**
* Class Award * The Award model
* * @property mixed id
* @property mixed ref_class
* @property mixed|null ref_class_id
* @package Award\Models * @package Award\Models
*/ */
class Award extends BaseModel class Award extends BaseModel
@@ -14,25 +16,32 @@ class Award extends BaseModel
public $fillable = [ public $fillable = [
'title', 'title',
'description', 'description',
'image', 'image_url',
]; 'ref_class',
'ref_class_id',
protected $casts = [
]; ];
public static $rules = [ public static $rules = [
'title' => 'required', 'title' => 'required',
'description' => 'nullable',
'image_url' => 'nullable',
]; ];
/** /**
* any foreign keys * Get the referring object
*/ */
/* public function getReference()
public function subfleets() { {
return $this->belongsToMany(Subfleet::class, 'subfleet_fare') if (!$this->ref_class) {
->withPivot('price', 'cost', 'capacity'); return null;
} }
*/
try {
return new $this->ref_class;
# return $klass;
# return $klass->find($this->ref_class_id);
} catch (\Exception $e) {
return null;
}
}
} }

View File

@@ -147,6 +147,11 @@ class User extends Authenticatable
return $this->belongsTo(Airline::class, 'airline_id'); return $this->belongsTo(Airline::class, 'airline_id');
} }
public function awards()
{
return $this->hasMany(UserAward::class, 'user_id');
}
public function home_airport() public function home_airport()
{ {
return $this->belongsTo(Airport::class, 'home_airport_id'); return $this->belongsTo(Airport::class, 'home_airport_id');

View File

@@ -14,4 +14,20 @@ class UserAward extends BaseModel
'user_id', 'user_id',
'award_id' 'award_id'
]; ];
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function award()
{
return $this->belongsTo(Award::class, 'award_id');
}
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function user()
{
return $this->belongsTo(User::class, 'user_id');
}
} }