Add location creation

This commit is contained in:
Dane Everitt 2016-01-16 23:10:46 -05:00
parent fb5533f107
commit 644f26fbfe
4 changed files with 98 additions and 4 deletions

View file

@ -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();
}
}

View file

@ -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'];
}

View file

@ -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, [