Add back locations with new theme

This commit is contained in:
Dane Everitt 2017-03-16 18:09:02 -04:00
parent db072025d4
commit 05d2a6d370
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
7 changed files with 383 additions and 187 deletions

View file

@ -25,16 +25,12 @@
namespace Pterodactyl\Repositories;
use Validator;
use Pterodactyl\Models;
use Pterodactyl\Models\Location;
use Pterodactyl\Exceptions\DisplayException;
use Pterodactyl\Exceptions\DisplayValidationException;
class LocationRepository
{
public function __construct()
{
//
}
/**
* Creates a new location on the system.
*
@ -45,48 +41,63 @@ class LocationRepository
public function create(array $data)
{
$validator = Validator::make($data, [
'short' => 'required|regex:/^[\w.-]{1,20}$/i|unique:locations,short',
'long' => 'required|string|min:1|max:255',
'short' => 'required|string|between:1,60|unique:locations,short',
'long' => 'required|string|between:1,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());
throw new DisplayValidationException(json_encode($validator->errors()));
}
$location = Models\Location::create([
return Location::create([
'long' => $data['long'],
'short' => $data['short'],
]);
}
/**
* Modifies a location.
*
* @param int $id
* @param array $data
* @return \Pterodactyl\Models\Location
*
* @throws Pterodactyl\Exceptions\DisplayValidationException
*/
public function update($id, array $data)
{
$location = Location::findOrFail($id);
$validator = Validator::make($data, [
'short' => 'sometimes|required|string|between:1,60|unique:locations,short,' . $location->id,
'long' => 'sometimes|required|string|between:1,255',
]);
if ($validator->fails()) {
throw new DisplayValidationException(json_encode($validator->errors()));
}
$location->fill($data)->save();
return $location;
}
/**
* Modifies a location based on the fields passed in $data.
* @param int $id
* @param array $data
* @throws Pterodactyl\Exceptions\DisplayValidationException
* @return bool
* Deletes a location from the system.
*
* @param int $id
* @return void
*
* @throws Pterodactyl\Exceptions\DisplayException
*/
public function edit($id, array $data)
public function delete($id)
{
$location = Models\Location::findOrFail($id);
$location = Location::withCount('nodes')->findOrFail($id);
$validator = Validator::make($data, [
'short' => 'required|regex:/^[\w.-]{1,20}$/i|unique:locations,short,' . $location->id,
'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());
if ($location->nodes_count > 0) {
throw new DisplayException('Cannot delete a location that has nodes assigned to it.');
}
$location->fill($data);
return $location->save();
$location->delete();
}
}