Skip to content

Commit

Permalink
Added beheer front-end to manage intro packages.
Browse files Browse the repository at this point in the history
  • Loading branch information
marijnvanderhorst committed Nov 24, 2019
1 parent ba57566 commit 4e5cab0
Show file tree
Hide file tree
Showing 17 changed files with 1,004 additions and 400 deletions.
155 changes: 155 additions & 0 deletions app/Http/Controllers/IntroPackageController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
<?php

namespace App\Http\Controllers;

use App\IntroPackage;
use App\repositories\RepositorieFactory;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Session;
use Illuminate\Validation\ValidationException as ValidationExceptionAlias;

class IntroPackageController extends Controller
{
private $_introPackageRepository;
private $_applicationFormRepository;


/**
* IntroPackageController constructor.
*/
public function __construct(RepositorieFactory $repositoryFactory)
{
$this->_introPackageRepository = $repositoryFactory->getRepositorie(RepositorieFactory::$INTROPACKAGEREPOKEY);
$this->_applicationFormRepository = $repositoryFactory->getRepositorie(RepositorieFactory::$APPLICATIONFORMREPOKEY);

$this->middleware('auth');
$this->middleware('authorize:'
. \Config::get('constants.Content_administrator') .','
. \Config::get('constants.Activity_administrator')
);
}

/**
* Validate request input
*
* @param Request $request
* @throws ValidationExceptionAlias
*/
private function validateInput(Request $request){
$this->validate($request, [
'NL_text' => 'required',
'EN_text' => 'required',
'deadline' => 'required|date',
'application_form_id' => 'required|integer',
]);
}

/**
* Display a listing of intro packages.
*
* @return Response
*/
public function index()
{
$packages = $this->_introPackageRepository->all();

return view('beheer.intro.packages.index', [
'packages' => $packages,
]);
}

/**
* Show the form for creating a new intro package.
*
* @return Response
*/
public function create()
{
$applicationForms = $this->_applicationFormRepository->all(array("name","id"));

return view('beheer.intro.packages.create_edit', [
'package' => null,
'applicationForms' => $applicationForms,
]);
}

/**
* Store a newly created intro package in storage.
*
* @param Request $request
* @return Response
* @throws ValidationExceptionAlias
*/
public function store(Request $request)
{
$this->validateInput($request);
$this->_introPackageRepository->create($request->all());

Session::flash("message", trans('intro.packageAdded'));

return redirect()->route('beheer.intro.packages.index');
}

/**
* Display the specified intro package.
*
* @param IntroPackage $package
* @return Response
*/
public function show(IntroPackage $package)
{
return view('beheer.intro.packages.show', [
'package' => $package,
]);
}

/**
* Show the form for editing the specified intro package.
*
* @param IntroPackage $package
* @return Response
*/
public function edit(IntroPackage $package)
{
$applicationForms = $this->_applicationFormRepository->all(array("name","id"));

return view('beheer.intro.packages.create_edit', [
'package' => $package,
'applicationForms' => $applicationForms,
]);
}

/**
* Update the specified intro package in storage.
*
* @param Request $request
* @param IntroPackage $package
* @return Response
* @throws ValidationExceptionAlias
*/
public function update(Request $request, IntroPackage $package)
{
$this->validateInput($request);
$this->_introPackageRepository->update($package->id, $request->all());

Session::flash("message", trans('intro.packageEdited'));

return redirect()->route('beheer.intro.packages.index');
}

/**
* Remove the specified intro package from storage.
*
* @param IntroPackage $package
* @return Response
*/
public function destroy(IntroPackage $package)
{
$this->_introPackageRepository->delete($package->id);

Session::flash("message", trans('intro.packageDeleted'));

return redirect()->route('beheer.intro.packages.index');
}
}
28 changes: 28 additions & 0 deletions app/IntroPackage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class IntroPackage extends Model
{
protected $casts = [
'deadline' => 'date',
];

protected $fillable = [
'name',
'deadline',
'application_form_id',
];

public function packageName()
{
return $this->hasOne(Text::class, 'id', 'name');
}

public function applicationForm()
{
return $this->belongsTo(ApplicationForm::class);
}
}
65 changes: 65 additions & 0 deletions app/repositories/IntroPackageRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

namespace App\repositories;


use App\IntroPackage;
use Carbon\Carbon;

class IntroPackageRepository implements IRepository
{
private $_textRepository;

public function __construct(TextRepository $textRepository)
{
$this->_textRepository = $textRepository;
}

public function create(array $data)
{
$text = $this->_textRepository->create($data);

$data['deadline'] = Carbon::createFromFormat('d-m-Y', $data['deadline']);

$package = new IntroPackage($data);
$package->name = $text->id;
$package->save();

return $package;
}

public function update($id, array $data)
{
$package = $this->find($id);

$data['deadline'] = Carbon::createFromFormat('d-m-Y', $data['deadline']);

$package->update($data);
$this->_textRepository->update($package->name, $data);

return $package;
}

public function delete($id)
{
$package = $this->find($id);
$package->delete();
$this->_textRepository->delete($package->name);

}

public function find($id, $columns = array('*'))
{
return $this->findBy('id', $id, $columns)->first();
}

public function findBy($field, $value, $columns = array('*'))
{
return IntroPackage::where($field, '=', $value)->get($columns);
}

public function all($columns = array('*'))
{
return IntroPackage::all($columns);
}
}
2 changes: 2 additions & 0 deletions app/repositories/RepositorieFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class RepositorieFactory{
public static $BOOKREPOKEY = "BookRepository";
public static $PHOTOALBUMREPOKEY = "PhotoAlbum";
public static $PHOTOREPOKEY = "Photo";
public static $INTROPACKAGEREPOKEY = "IntroPackage";


public function __construct() {
Expand All @@ -52,6 +53,7 @@ public function __construct() {
RepositorieFactory::$BOOKREPOKEY => new BookRepository($textRepository),
RepositorieFactory::$PHOTOALBUMREPOKEY => new PhotoAlbumRepository(),
RepositorieFactory::$PHOTOREPOKEY => new PhotoRepository(),
RepositorieFactory::$INTROPACKAGEREPOKEY => new IntroPackageRepository($textRepository),
];
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateIntroPackagesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('intro_packages', function (Blueprint $table) {
$table->increments('id');

$table->integer('name')->unsigned();
$table->date('deadline');
$table->integer('application_form_id')->unsigned();

$table->timestamps();

$table->foreign('name')->references('id')->on('texts');
$table->foreign('application_form_id')->references('id')->on('application_forms');
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('intro_packages');
}
}
Loading

0 comments on commit 4e5cab0

Please sign in to comment.