Add/edit the award class in Admin #155

This commit is contained in:
Nabeel Shahzad
2018-03-17 12:17:38 -05:00
parent a21e2dd412
commit e9baf4acb5
14 changed files with 195 additions and 86 deletions

View File

@@ -7,7 +7,7 @@ use App\Models\Acars;
use App\Models\Airline;
use App\Models\Pirep;
use App\Models\User;
use App\Services\AwardsService;
use App\Services\AwardService;
use DB;
use PDO;
use Symfony\Component\Yaml\Yaml;
@@ -51,7 +51,7 @@ class DevCommands extends BaseCommand
*/
protected function listAwardClasses()
{
$awardSvc = app(AwardsService::class);
$awardSvc = app(AwardService::class);
$awards = $awardSvc->findAllAwardClasses();
$headers = ['Award Name', 'Class'];

View File

@@ -15,7 +15,7 @@ class CreateAwardsTable extends Migration
{
Schema::create('awards', function (Blueprint $table) {
$table->increments('id');
$table->string('title', 50);
$table->string('name');
$table->text('description')->nullable();
$table->text('image_url')->nullable();

View File

@@ -93,6 +93,16 @@ ranks:
auto_approve_manual: 1
auto_promote: 0
awards:
- id: 1
name: Pilot 50 flights
description: When a pilot has 50 flights, give this award
image_url:
ref_class: App\Awards\PilotFlightAwards
ref_class_params: 50
created_at: now
updated_at: now
news:
- id: 1
user_id: 1

View File

@@ -5,6 +5,7 @@ namespace App\Http\Controllers\Admin;
use App\Http\Requests\CreateAwardRequest;
use App\Http\Requests\UpdateAwardRequest;
use App\Repositories\AwardRepository;
use App\Services\AwardService;
use Illuminate\Http\Request;
use Flash;
use Prettus\Repository\Criteria\RequestCriteria;
@@ -13,11 +14,39 @@ use Response;
class AwardController extends BaseController
{
/** @var AwardRepository */
private $awardRepository;
private $awardRepository,
$awardSvc;
public function __construct(AwardRepository $awardRepo)
public function __construct(
AwardRepository $awardRepo,
AwardService $awardSvc
)
{
$this->awardRepository = $awardRepo;
$this->awardSvc = $awardSvc;
}
/**
* @return array
*/
protected function getAwardClassesAndDescriptions(): array
{
$awards = [
'' => '',
];
$descriptions = [];
$award_classes = $this->awardSvc->findAllAwardClasses();
foreach($award_classes as $class_ref => $award) {
$awards[$class_ref] = $award->name;
$descriptions[$class_ref] = $award->param_description;
}
return [
'awards' => $awards,
'descriptions' => $descriptions,
];
}
/**
@@ -42,7 +71,11 @@ class AwardController extends BaseController
*/
public function create()
{
return view('admin.awards.create');
$class_refs = $this->getAwardClassesAndDescriptions();
return view('admin.awards.create', [
'award_classes' => $class_refs['awards'],
'award_descriptions' => $class_refs['descriptions'],
]);
}
/**
@@ -91,8 +124,11 @@ class AwardController extends BaseController
return redirect(route('admin.awards.index'));
}
$class_refs = $this->getAwardClassesAndDescriptions();
return view('admin.awards.edit', [
'award' => $award,
'award_classes' => $class_refs['awards'],
'award_descriptions' => $class_refs['descriptions'],
]);
}

View File

@@ -25,6 +25,6 @@ class UpdateAwardRequest extends FormRequest
*/
public function rules()
{
return Fare::$rules;
return Award::$rules;
}
}

View File

@@ -14,7 +14,7 @@ class Award extends BaseModel
public $table = 'awards';
public $fillable = [
'title',
'name',
'description',
'image_url',
'ref_class',
@@ -22,9 +22,12 @@ class Award extends BaseModel
];
public static $rules = [
'title' => 'required',
'name' => 'required',
'description' => 'nullable',
'image_url' => 'nullable',
'ref_class' => 'required',
'ref_class_params' => 'nullable'
];
/**
@@ -41,8 +44,6 @@ class Award extends BaseModel
try {
return new $this->ref_class($award, $user);
# return $klass;
# return $klass->find($this->ref_class_id);
} catch (\Exception $e) {
return null;
}

View File

@@ -5,26 +5,32 @@ namespace App\Services;
use App\Support\ClassLoader;
use Module;
class AwardsService
class AwardService
{
/**
* Find any of the award classes
* @return \App\Interfaces\AwardInterface[]
*/
public function findAllAwardClasses()
{
$awards = [];
$formatted_awards = [];
# Find the awards in the app/Awards directory
$classes = ClassLoader::getClassesInPath(app_path('/Awards'));
$awards = array_merge($awards, $classes);
# Look throughout all the other modules
# Look throughout all the other modules, in the module/{MODULE}/Awards directory
foreach (Module::all() as $module) {
$path = $module->getExtraPath('Awards');
$classes = ClassLoader::getClassesInPath($path);
$awards = array_merge($awards, $classes);
}
return $awards;
foreach ($awards as $award) {
$formatted_awards[\get_class($award)] = $award;
}
return $formatted_awards;
}
}