add location editing

This commit is contained in:
Dane Everitt 2016-01-16 22:57:28 -05:00
parent 21a95a5d0e
commit fb5533f107
4 changed files with 146 additions and 2 deletions

View file

@ -5,7 +5,11 @@ namespace Pterodactyl\Http\Controllers\Admin;
use DB; use DB;
use Pterodactyl\Models; use Pterodactyl\Models;
use Pterodactyl\Repositories\LocationRepository;
use Pterodactyl\Http\Controllers\Controller; use Pterodactyl\Http\Controllers\Controller;
use Pterodactyl\Exceptions\DisplayValidationException;
use Illuminate\Http\Request; use Illuminate\Http\Request;
class LocationsController extends Controller class LocationsController extends Controller
@ -51,4 +55,25 @@ class LocationsController extends Controller
return response('', 204); return response('', 204);
} }
public function patchLocation(Request $request, $id)
{
try {
$location = new LocationRepository;
$location->edit($id, $request->all());
return response('', 204);
} catch (DisplayValidationException $ex) {
return response()->json([
'error' => 'There was a validation error while processing this request. Location descriptions must be between 1 and 255 characters, and the location code must be between 1 and 10 characters with no spaces or special characters.'
], 422);
} catch (\Exception $ex) {
// This gets caught and processed into JSON anyways.
throw $ex;
}
}
public function postLocation(Request $request)
{
//
}
} }

View file

@ -219,6 +219,12 @@ class AdminRoutes {
$router->delete('/{id}', [ $router->delete('/{id}', [
'uses' => 'Admin\LocationsController@deleteLocation' 'uses' => 'Admin\LocationsController@deleteLocation'
]); ]);
$router->patch('/{id}', [
'uses' => 'Admin\LocationsController@patchLocation'
]);
$router->post('/', [
'uses' => 'Admin\LocationsController@postLocation'
]);
}); });
// API Routes // API Routes

View file

@ -0,0 +1,44 @@
<?php
namespace Pterodactyl\Repositories;
use Validator;
use Pterodactyl\Models;
use Pterodactyl\Exceptions\DisplayException;
use Pterodactyl\Exceptions\DisplayValidationException;
class LocationRepository
{
public function __construct()
{
//
}
public function edit($id, array $data)
{
$validator = Validator::make($data, [
'short' => 'regex:/^[a-z0-9_.-]{1,10}$/i',
'long' => '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 = Models\Location::findOrFail($id);
if (isset($data['short'])) {
$location->short = $data['short'];
}
if (isset($data['long'])) {
$location->long = $data['long'];
}
return $location->save();
}
}

View file

@ -29,7 +29,7 @@
<td>{{ $location->long }}</td> <td>{{ $location->long }}</td>
<td class="text-center">{{ $location->a_nodeCount }}</td> <td class="text-center">{{ $location->a_nodeCount }}</td>
<td class="text-center">{{ $location->a_serverCount }}</td> <td class="text-center">{{ $location->a_serverCount }}</td>
<td class="text-center"><a href="#edit"><i class="fa fa-wrench" data-action="edit" data-id="{{ $location->id }}" data-short="{{ $location->short }}" data-long="{{ $location->long }}"></i></a></td> <td class="text-center"><a href="#edit"><i class="fa fa-wrench" data-toggle="modal" data-target="#editModal" data-action="edit" data-id="{{ $location->id }}" data-short="{{ $location->short }}" data-long="{{ $location->long }}"></i></a></td>
<td class="text-center"><a href="#delete" class="text-danger" data-action="delete" data-id="{{ $location->id }}"><i class="fa fa-trash-o"></i></a></td> <td class="text-center"><a href="#delete" class="text-danger" data-action="delete" data-id="{{ $location->id }}"><i class="fa fa-trash-o"></i></a></td>
</tr> </tr>
@endforeach @endforeach
@ -46,9 +46,78 @@
</div> </div>
</div> </div>
</div> </div>
<div class="modal fade" id="editModal" 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" id="exampleModalLabel">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">
</div>
<div class="form-group">
<label for="location-long" class="control-label">Description:</label>
<input type="text" class="form-control" id="location-long">
</div>
</div>
<div class="modal-footer">
<input type="hidden" id="location-id">
<button type="button" class="btn btn-sm btn-default" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-sm btn-primary">Edit Location</button>
</div>
</form>
</div>
</div>
</div>
<script> <script>
$(document).ready(function () { $(document).ready(function () {
$('#sidebar_links').find("a[href='/admin/locations']").addClass('active'); $('#sidebar_links').find("a[href='/admin/locations']").addClass('active');
$('#editModal').on('show.bs.modal', function (event) {
var button = $(event.relatedTarget);
var short = button.data('short');
var long = button.data('long');
var id = button.data('id');
var modal = $(this);
modal.find('#location-id').val(id);
modal.find('#location-short').val(short);
modal.find('#location-long').val(long);
});
$('#editLocationForm').submit(function (event) {
event.preventDefault();
$.ajax({
method: 'PATCH',
url: '{{ route('admin.locations') }}/' + $('#location-id').val(),
headers: {
'X-CSRF-TOKEN': '{{ csrf_token() }}'
},
data: {
short: $('#location-short').val(),
long: $('#location-long').val()
}
}).done(function (data) {
swal({
type: 'success',
title: '',
text: 'Successfully updated location information.',
closeOnConfirm: false,
showLoaderOnConfirm: true
}, function () {
window.location = '{{ route('admin.locations') }}';
});
}).fail(function (jqXHR) {
console.error(jqXHR);
swal({
type: 'error',
title: 'Whoops!',
text: (typeof jqXHR.responseJSON.error !== 'undefined') ? jqXHR.responseJSON.error : 'An error occured while processing this request.'
});
});
});
$('[data-action="delete"]').click(function (event) { $('[data-action="delete"]').click(function (event) {
event.preventDefault(); event.preventDefault();
var self = $(this); var self = $(this);
@ -78,7 +147,7 @@ $(document).ready(function () {
swal({ swal({
type: 'error', type: 'error',
title: 'Whoops!', title: 'Whoops!',
text: (typeof jqXHR.responseJSON !== 'undefined') ? jqXHR.responseJSON.error : 'An error occured while processing this request.' text: (typeof jqXHR.responseJSON.error !== 'undefined') ? jqXHR.responseJSON.error : 'An error occured while processing this request.'
}); });
}); });
}); });