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

View file

@ -41,7 +41,7 @@
<div class="well">
<div class="row">
<div class="col-md-12">
<button class="btn btn-sm btn-success" id="addNewLocation">Add New Location</button>
<button class="btn btn-sm btn-success" data-toggle="modal" data-target="#addModal">Add New Location</button>
</div>
</div>
</div>
@ -51,17 +51,19 @@
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
<h4 class="modal-title" id="exampleModalLabel">Editing Location</h4>
<h4 class="modal-title">Editing Location</h4>
</div>
<form action="{{ route('admin.locations') }}" method="POST" id="editLocationForm">
<div class="modal-body">
<div class="form-group">
<label for="location-short" class="control-label">Location Code:</label>
<input type="text" class="form-control" id="location-short">
<p class="text-muted"><small>This should be a short identifier for this location (e.g. <code>ny1</code>). This field is limited to a maximum of 10 characters from the following list: <code>a-zA-Z0-9_-.</code></small></p>
</div>
<div class="form-group">
<label for="location-long" class="control-label">Description:</label>
<input type="text" class="form-control" id="location-long">
<p class="text-muted"><small>This should be a longer description of the location for internal reference.</small></p>
</div>
</div>
<div class="modal-footer">
@ -73,6 +75,39 @@
</div>
</div>
</div>
<div class="modal fade" id="addModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
<h4 class="modal-title">Add New Location</h4>
</div>
<form action="{{ route('admin.locations') }}" method="POST" id="addLocationForm">
<div class="modal-body">
<div class="form-group">
<label for="short" class="control-label">Location Code:</label>
<div>
<input type="text" class="form-control" name="short" value="{{ old('short') }}">
<p class="text-muted"><small>This should be a short identifier for this location (e.g. <code>ny1</code>). This field is limited to a maximum of 10 characters from the following list: <code>a-zA-Z0-9_-.</code></small></p>
</div>
</div>
<div class="form-group">
<label for="long" class="control-label">Description:</label>
<div>
<input type="text" class="form-control" name="long" value="{{ old('long') }}">
<p class="text-muted"><small>This should be a longer description of the location for internal reference.</small></p>
</div>
</div>
</div>
<div class="modal-footer">
{!! csrf_field() !!}
<button type="button" class="btn btn-sm btn-default" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-sm btn-primary">Add Location</button>
</div>
</form>
</div>
</div>
</div>
<script>
$(document).ready(function () {
$('#sidebar_links').find("a[href='/admin/locations']").addClass('active');