From 644f26fbfe09810209bcc7b3633defade58970af Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Sat, 16 Jan 2016 23:10:46 -0500 Subject: [PATCH] Add location creation --- .../Controllers/Admin/LocationsController.php | 19 ++++++++- app/Models/Location.php | 7 ++++ app/Repositories/LocationRepository.php | 37 +++++++++++++++++- .../views/admin/locations/index.blade.php | 39 ++++++++++++++++++- 4 files changed, 98 insertions(+), 4 deletions(-) diff --git a/app/Http/Controllers/Admin/LocationsController.php b/app/Http/Controllers/Admin/LocationsController.php index e914c9753..e4429682f 100644 --- a/app/Http/Controllers/Admin/LocationsController.php +++ b/app/Http/Controllers/Admin/LocationsController.php @@ -3,12 +3,14 @@ namespace Pterodactyl\Http\Controllers\Admin; use DB; +use Alert; use Pterodactyl\Models; use Pterodactyl\Repositories\LocationRepository; use Pterodactyl\Http\Controllers\Controller; use Pterodactyl\Exceptions\DisplayValidationException; +use Pterodactyl\Exceptions\DisplayException; use Illuminate\Http\Request; @@ -73,7 +75,22 @@ class LocationsController extends Controller public function postLocation(Request $request) { - // + try { + $location = new LocationRepository; + $id = $location->create($request->except([ + '_token' + ])); + Alert::success('New location successfully added.')->flash(); + return redirect()->route('admin.locations'); + } catch (DisplayValidationException $ex) { + return redirect()->route('admin.locations')->withErrors(json_decode($ex->getMessage()))->withInput(); + } catch (DisplayException $ex) { + Alert::danger($ex->getMessage())->flash(); + } catch (\Exception $ex) { + Log::error($ex); + Alert::danger('An unhandled exception occured while attempting to add this location. Please try again.')->flash(); + } + return redirect()->route('admin.locations')->withInput(); } } diff --git a/app/Models/Location.php b/app/Models/Location.php index 77867e4e9..a434b57cf 100644 --- a/app/Models/Location.php +++ b/app/Models/Location.php @@ -14,4 +14,11 @@ class Location extends Model */ protected $table = 'locations'; + /** + * Fields that are not mass assignable. + * + * @var array + */ + protected $guarded = ['id', 'created_at', 'updated_at']; + } diff --git a/app/Repositories/LocationRepository.php b/app/Repositories/LocationRepository.php index 3f4a16fb5..65c551b35 100644 --- a/app/Repositories/LocationRepository.php +++ b/app/Repositories/LocationRepository.php @@ -5,7 +5,6 @@ namespace Pterodactyl\Repositories; use Validator; use Pterodactyl\Models; -use Pterodactyl\Exceptions\DisplayException; use Pterodactyl\Exceptions\DisplayValidationException; class LocationRepository @@ -16,6 +15,42 @@ class LocationRepository // } + /** + * Creates a new location on the system. + * @param array $data + * @throws Pterodactyl\Exceptions\DisplayValidationException + * @return integer + */ + public function create(array $data) + { + $validator = Validator::make($data, [ + 'short' => 'required|regex:/^[a-z0-9_.-]{1,10}$/i|unique:locations,short', + 'long' => 'required|string|min:1|max:255' + ]); + + // Run validator, throw catchable and displayable exception if it fails. + // Exception includes a JSON result of failed validation rules. + if ($validator->fails()) { + throw new DisplayValidationException($validator->errors()); + } + + $location = new Models\Location; + $location->fill([ + 'long' => $data['long'], + 'short' => $data['short'] + ]); + $location->save(); + + return $location->id; + } + + /** + * Modifies a location based on the fields passed in $data. + * @param integer $id + * @param array $data + * @throws Pterodactyl\Exceptions\DisplayValidationException + * @return boolean + */ public function edit($id, array $data) { $validator = Validator::make($data, [ diff --git a/resources/views/admin/locations/index.blade.php b/resources/views/admin/locations/index.blade.php index 2ea3338b1..213af81c6 100644 --- a/resources/views/admin/locations/index.blade.php +++ b/resources/views/admin/locations/index.blade.php @@ -41,7 +41,7 @@
- +
@@ -51,17 +51,19 @@ +