mirror of
https://github.com/henrywhitaker3/Speedtest-Tracker.git
synced 2025-12-21 13:23:04 +01:00
Added settings table
This commit is contained in:
37
app/Helpers/SettingsHelper.php
Normal file
37
app/Helpers/SettingsHelper.php
Normal file
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace App\Helpers;
|
||||
|
||||
use App\Setting;
|
||||
|
||||
class SettingsHelper {
|
||||
public static function get(String $name)
|
||||
{
|
||||
$name = Setting::where('name', $name)->get();
|
||||
|
||||
if(sizeof($name) == 0) {
|
||||
return false;
|
||||
} else if(sizeof($name) == 1) {
|
||||
return $name[0];
|
||||
} else {
|
||||
return $name;
|
||||
}
|
||||
}
|
||||
|
||||
public static function set(String $name, String $value)
|
||||
{
|
||||
$setting = SettingsHelper::get($name);
|
||||
|
||||
if($setting !== false) {
|
||||
$setting->value = $value;
|
||||
$setting->save();
|
||||
} else {
|
||||
$setting = Setting::create([
|
||||
'name' => $name,
|
||||
'value' => $value,
|
||||
]);
|
||||
}
|
||||
|
||||
return $setting;
|
||||
}
|
||||
}
|
||||
@@ -11,7 +11,7 @@ class SpeedtestHelper {
|
||||
public static function runSpeedtest($output = false)
|
||||
{
|
||||
if($output === false) {
|
||||
$output = shell_exec('speedtest-cli --json');
|
||||
$output = SpeedtestHelper::output();
|
||||
}
|
||||
|
||||
try {
|
||||
@@ -31,6 +31,11 @@ class SpeedtestHelper {
|
||||
return (isset($test)) ? $test : false;
|
||||
}
|
||||
|
||||
public static function output()
|
||||
{
|
||||
return shell_exec('speedtest-cli --json');
|
||||
}
|
||||
|
||||
public static function latest()
|
||||
{
|
||||
$data = Speedtest::latest()->get();
|
||||
|
||||
46
app/Http/Controllers/SettingsController.php
Normal file
46
app/Http/Controllers/SettingsController.php
Normal file
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Helpers\SettingsHelper;
|
||||
use App\Setting;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
|
||||
class SettingsController extends Controller
|
||||
{
|
||||
public function index()
|
||||
{
|
||||
return Setting::get();
|
||||
}
|
||||
|
||||
public function get(Setting $setting)
|
||||
{
|
||||
return $setting;
|
||||
}
|
||||
|
||||
public function store(Request $request)
|
||||
{
|
||||
$rule = [
|
||||
'name' => [ 'required', 'string', 'min:1' ],
|
||||
];
|
||||
$validator = Validator::make($request->all(), $rule);
|
||||
if($validator->fails()) {
|
||||
return response()->json([
|
||||
'method' => 'Store a setting',
|
||||
'error' => $validator->errors()
|
||||
], 422);
|
||||
}
|
||||
|
||||
if(!isset($request->value)) {
|
||||
$request->value = '';
|
||||
}
|
||||
|
||||
$setting = SettingsHelper::set($request->name, $request->value);
|
||||
|
||||
return response()->json([
|
||||
'method' => 'Store a setting',
|
||||
'data' => $setting
|
||||
], 200);
|
||||
}
|
||||
}
|
||||
@@ -31,7 +31,7 @@ class SpeedtestJob implements ShouldQueue
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
$output = shell_exec('speedtest-cli --json');
|
||||
$output = SpeedtestHelper::output();
|
||||
$speedtest = SpeedtestHelper::runSpeedtest($output);
|
||||
event(new SpeedtestCompleteEvent($speedtest));
|
||||
return $speedtest;
|
||||
|
||||
19
app/Setting.php
Normal file
19
app/Setting.php
Normal file
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace App;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class Setting extends Model
|
||||
{
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $fillable = [
|
||||
'name', 'value'
|
||||
];
|
||||
|
||||
protected $table = 'settings';
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
use App\Helpers\SettingsHelper;
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class CreateSettingsTable extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::create('settings', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->string('name')->unique();
|
||||
$table->string('value');
|
||||
$table->timestamps();
|
||||
});
|
||||
|
||||
SettingsHelper::set('schedule', '0 * * * *');
|
||||
SettingsHelper::set('server', '');
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
Schema::dropIfExists('settings');
|
||||
}
|
||||
}
|
||||
@@ -53,3 +53,15 @@ Route::group([
|
||||
Route::get('move', 'UpdateController@moveUpdate')
|
||||
->name('update.move');
|
||||
});
|
||||
|
||||
Route::group([
|
||||
'middleware' => 'api',
|
||||
'prefix' => 'settings'
|
||||
], function () {
|
||||
Route::get('/', 'SettingsController@index')
|
||||
->name('settings.index');
|
||||
Route::put('/', 'SettingsController@store')
|
||||
->name('settings.store');
|
||||
Route::post('/', 'SettingsController@store')
|
||||
->name('settings.update');
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user