From 319be38fa9558d742a599839cca739e3f81082b6 Mon Sep 17 00:00:00 2001 From: cp6 Date: Sun, 29 May 2022 11:40:13 +1000 Subject: [PATCH 01/12] Added dashboard currency setting migration Added dashboard currency setting migration --- ...05_29_105255_add_dash_currency_setting.php | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 database/migrations/2022_05_29_105255_add_dash_currency_setting.php diff --git a/database/migrations/2022_05_29_105255_add_dash_currency_setting.php b/database/migrations/2022_05_29_105255_add_dash_currency_setting.php new file mode 100644 index 0000000..866a498 --- /dev/null +++ b/database/migrations/2022_05_29_105255_add_dash_currency_setting.php @@ -0,0 +1,19 @@ +char('dashboard_currency', 3)->default('USD'); + }); + } + + public function down() + { + } +} From ff280e6c6217b30fe005d1ad1857002bb21ade28 Mon Sep 17 00:00:00 2001 From: cp6 Date: Sun, 29 May 2022 16:46:24 +1000 Subject: [PATCH 02/12] Added clockwork to git ignore Added clockwork to git ignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 40af839..1619c13 100644 --- a/.gitignore +++ b/.gitignore @@ -56,3 +56,4 @@ fabric.properties # modules.xml # .idea/misc.xml # *.ipr +storage/clockwork/ From 73f2d8f07f151b139b1a21a812f3017af48c6ebf Mon Sep 17 00:00:00 2001 From: cp6 Date: Sun, 29 May 2022 16:46:55 +1000 Subject: [PATCH 03/12] Added dashboard currency setting Added dashboard currency setting --- app/Http/Controllers/HomeController.php | 3 ++- app/Http/Controllers/SettingsController.php | 6 +++-- app/Models/Home.php | 29 ++++++++++++--------- app/Models/Pricing.php | 21 +++++++++++++++ app/Models/Settings.php | 1 + resources/views/home.blade.php | 8 +++--- resources/views/settings/index.blade.php | 7 +++++ 7 files changed, 56 insertions(+), 19 deletions(-) diff --git a/app/Http/Controllers/HomeController.php b/app/Http/Controllers/HomeController.php index a1f0ce9..d5d5bfc 100644 --- a/app/Http/Controllers/HomeController.php +++ b/app/Http/Controllers/HomeController.php @@ -65,7 +65,8 @@ class HomeController extends Controller 'due_soon' => $due_soon, 'newest' => $recently_added, 'execution_time' => number_format($p->getTimeTaken(), 2), - 'servers_summary' => $server_summary + 'servers_summary' => $server_summary, + 'currency' => Session::get('dashboard_currency') ); return view('home', compact('information')); diff --git a/app/Http/Controllers/SettingsController.php b/app/Http/Controllers/SettingsController.php index 6bc444c..cac7965 100644 --- a/app/Http/Controllers/SettingsController.php +++ b/app/Http/Controllers/SettingsController.php @@ -33,7 +33,8 @@ class SettingsController extends Controller 'default_currency' => 'required', 'default_server_os' => 'required', 'due_soon_amount' => 'required|integer|between:0,12', - 'recently_added_amount' => 'required|integer|between:0,12' + 'recently_added_amount' => 'required|integer|between:0,12', + 'currency' => 'required|string|size:3' ]); DB::table('settings') @@ -51,7 +52,8 @@ class SettingsController extends Controller 'default_currency' => $request->default_currency, 'default_server_os' => $request->default_server_os, 'due_soon_amount' => $request->due_soon_amount, - 'recently_added_amount' => $request->recently_added_amount + 'recently_added_amount' => $request->recently_added_amount, + 'dashboard_currency' => $request->currency, ]); Settings::setSettingsToSession($settings); diff --git a/app/Models/Home.php b/app/Models/Home.php index 30586ce..baeddd5 100644 --- a/app/Models/Home.php +++ b/app/Models/Home.php @@ -119,24 +119,29 @@ class Home extends Model $total_cost_weekly = $total_cost_pm = $inactive_count = 0; foreach ($pricing as $price) { if ($price['active'] === 1) { + if (Session::get('dashboard_currency') !== 'USD') { + $the_price = Pricing::convertFromUSD($price['as_usd'], Session::get('dashboard_currency')); + } else { + $the_price = $price['as_usd']; + } if ($price['term'] === 1) {//1 month - $total_cost_weekly += ($price['as_usd'] / 4); - $total_cost_pm += $price['as_usd']; + $total_cost_weekly += ($the_price / 4); + $total_cost_pm += $the_price; } elseif ($price['term'] === 2) {//3 months - $total_cost_weekly += ($price['as_usd'] / 12); - $total_cost_pm += ($price['as_usd'] / 3); + $total_cost_weekly += ($the_price / 12); + $total_cost_pm += ($the_price / 3); } elseif ($price['term'] === 3) {// 6 month - $total_cost_weekly += ($price['as_usd'] / 24); - $total_cost_pm += ($price['as_usd'] / 6); + $total_cost_weekly += ($the_price / 24); + $total_cost_pm += ($the_price / 6); } elseif ($price['term'] === 4) {// 1 year - $total_cost_weekly += ($price['as_usd'] / 48); - $total_cost_pm += ($price['as_usd'] / 12); + $total_cost_weekly += ($the_price / 48); + $total_cost_pm += ($the_price / 12); } elseif ($price['term'] === 5) {//2 years - $total_cost_weekly += ($price['as_usd'] / 96); - $total_cost_pm += ($price['as_usd'] / 24); + $total_cost_weekly += ($the_price / 96); + $total_cost_pm += ($the_price / 24); } elseif ($price['term'] === 6) {//3 years - $total_cost_weekly += ($price['as_usd'] / 144); - $total_cost_pm += ($price['as_usd'] / 36); + $total_cost_weekly += ($the_price / 144); + $total_cost_pm += ($the_price / 36); } } else { $inactive_count++; diff --git a/app/Models/Pricing.php b/app/Models/Pricing.php index e021362..0a99d9c 100644 --- a/app/Models/Pricing.php +++ b/app/Models/Pricing.php @@ -14,6 +14,27 @@ class Pricing extends Model protected $fillable = ['service_id', 'service_type', 'currency', 'price', 'term', 'as_usd', 'usd_per_month', 'next_due_date']; + public static function convertFromUSD(string $amount, string $convert_to): float + {//Code rates update from an API?? + if ($convert_to === 'AUD') { + return (1.39 * $amount); + } elseif ($convert_to === "USD") { + return $amount; + } elseif ($convert_to === "GBP") { + return (0.79 * $amount); + } elseif ($convert_to === "EUR") { + return (0.93 * $amount); + } elseif ($convert_to === "NZD") { + return (1.53 * $amount); + } elseif ($convert_to === "JPY") { + return (127.12 * $amount); + } elseif ($convert_to === "CAD") { + return (1.27 * $amount); + } else { + return $amount; + } + } + public function convertToUSD(string $amount, string $convert_from): float { if ($convert_from === 'AUD') { diff --git a/app/Models/Settings.php b/app/Models/Settings.php index 1f584b4..a5625c7 100644 --- a/app/Models/Settings.php +++ b/app/Models/Settings.php @@ -40,6 +40,7 @@ class Settings extends Model Session::put('default_server_os', $settings[0]->default_server_os ?? 1); Session::put('due_soon_amount', $settings[0]->due_soon_amount ?? 6); Session::put('recently_added_amount', $settings[0]->recently_added_amount ?? 6); + Session::put('dashboard_currency', $settings[0]->dashboard_currency ?? 'USD'); Session::save(); } diff --git a/resources/views/home.blade.php b/resources/views/home.blade.php index 5f21cf3..a772331 100644 --- a/resources/views/home.blade.php +++ b/resources/views/home.blade.php @@ -71,7 +71,7 @@
-

{{$information['total_cost_weekly']}} USD

+

{{$information['total_cost_weekly']}} {{$information['currency']}}

Weekly cost

@@ -81,7 +81,7 @@
-

{{$information['total_cost_monthly']}} USD +

{{$information['total_cost_monthly']}} {{$information['currency']}}

Monthly cost

@@ -92,7 +92,7 @@
-

{{$information['total_cost_yearly']}} USD

+

{{$information['total_cost_yearly']}} {{$information['currency']}}

Yearly cost

@@ -102,7 +102,7 @@
-

{{$information['total_cost_2_yearly']}} USD +

{{$information['total_cost_2_yearly']}} {{$information['currency']}}

2 yearly cost

diff --git a/resources/views/settings/index.blade.php b/resources/views/settings/index.blade.php index 5ca3e47..716b6b1 100644 --- a/resources/views/settings/index.blade.php +++ b/resources/views/settings/index.blade.php @@ -214,6 +214,13 @@
+
+
+ + {{$setting[0]->dashboard_currency}} + +
+
Update settings From f72eec01f7ece4531629302e210c3ee06dd10a24 Mon Sep 17 00:00:00 2001 From: cp6 Date: Sun, 29 May 2022 16:50:15 +1000 Subject: [PATCH 04/12] Extended settings cache expiration length Extended settings cache expiration length --- app/Models/Settings.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Models/Settings.php b/app/Models/Settings.php index a5625c7..300f66b 100644 --- a/app/Models/Settings.php +++ b/app/Models/Settings.php @@ -18,7 +18,7 @@ class Settings extends Model public static function getSettings() { - return Cache::remember('settings', now()->addMinute(1), function () { + return Cache::remember('settings', now()->addWeek(1), function () { return DB::table('settings') ->where('id', '=', 1) ->get(); From 8e5a1f62c3e0c8bcf3e5b8eb211cfa79ca47ab96 Mon Sep 17 00:00:00 2001 From: cp6 Date: Sun, 29 May 2022 16:56:13 +1000 Subject: [PATCH 05/12] Added home page pricing break down func to cache Added home page pricing break down func to cache --- app/Http/Controllers/SettingsController.php | 1 + app/Models/Home.php | 72 +++++++++++---------- 2 files changed, 38 insertions(+), 35 deletions(-) diff --git a/app/Http/Controllers/SettingsController.php b/app/Http/Controllers/SettingsController.php index cac7965..c474492 100644 --- a/app/Http/Controllers/SettingsController.php +++ b/app/Http/Controllers/SettingsController.php @@ -60,6 +60,7 @@ class SettingsController extends Controller Cache::forget('due_soon');//Main page due_soon cache Cache::forget('recently_added');//Main page recently_added cache + Cache::forget('pricing_breakdown');//Main page pricing breakdown Cache::forget('settings');//Main page settings cache diff --git a/app/Models/Home.php b/app/Models/Home.php index baeddd5..dd0011c 100644 --- a/app/Models/Home.php +++ b/app/Models/Home.php @@ -116,45 +116,47 @@ class Home extends Model { $pricing = json_decode($all_pricing, true); - $total_cost_weekly = $total_cost_pm = $inactive_count = 0; - foreach ($pricing as $price) { - if ($price['active'] === 1) { - if (Session::get('dashboard_currency') !== 'USD') { - $the_price = Pricing::convertFromUSD($price['as_usd'], Session::get('dashboard_currency')); + return Cache::remember('pricing_breakdown', now()->addWeek(1), function () use ($pricing) { + $total_cost_weekly = $total_cost_pm = $inactive_count = 0; + foreach ($pricing as $price) { + if ($price['active'] === 1) { + if (Session::get('dashboard_currency') !== 'USD') { + $the_price = Pricing::convertFromUSD($price['as_usd'], Session::get('dashboard_currency')); + } else { + $the_price = $price['as_usd']; + } + if ($price['term'] === 1) {//1 month + $total_cost_weekly += ($the_price / 4); + $total_cost_pm += $the_price; + } elseif ($price['term'] === 2) {//3 months + $total_cost_weekly += ($the_price / 12); + $total_cost_pm += ($the_price / 3); + } elseif ($price['term'] === 3) {// 6 month + $total_cost_weekly += ($the_price / 24); + $total_cost_pm += ($the_price / 6); + } elseif ($price['term'] === 4) {// 1 year + $total_cost_weekly += ($the_price / 48); + $total_cost_pm += ($the_price / 12); + } elseif ($price['term'] === 5) {//2 years + $total_cost_weekly += ($the_price / 96); + $total_cost_pm += ($the_price / 24); + } elseif ($price['term'] === 6) {//3 years + $total_cost_weekly += ($the_price / 144); + $total_cost_pm += ($the_price / 36); + } } else { - $the_price = $price['as_usd']; + $inactive_count++; } - if ($price['term'] === 1) {//1 month - $total_cost_weekly += ($the_price / 4); - $total_cost_pm += $the_price; - } elseif ($price['term'] === 2) {//3 months - $total_cost_weekly += ($the_price / 12); - $total_cost_pm += ($the_price / 3); - } elseif ($price['term'] === 3) {// 6 month - $total_cost_weekly += ($the_price / 24); - $total_cost_pm += ($the_price / 6); - } elseif ($price['term'] === 4) {// 1 year - $total_cost_weekly += ($the_price / 48); - $total_cost_pm += ($the_price / 12); - } elseif ($price['term'] === 5) {//2 years - $total_cost_weekly += ($the_price / 96); - $total_cost_pm += ($the_price / 24); - } elseif ($price['term'] === 6) {//3 years - $total_cost_weekly += ($the_price / 144); - $total_cost_pm += ($the_price / 36); - } - } else { - $inactive_count++; } - } - $total_cost_yearly = ($total_cost_pm * 12); + $total_cost_yearly = ($total_cost_pm * 12); - return array( - 'total_cost_weekly' => $total_cost_weekly, - 'total_cost_montly' => $total_cost_pm, - 'total_cost_yearly' => $total_cost_yearly, - 'inactive_count' => $inactive_count, - ); + return array( + 'total_cost_weekly' => $total_cost_weekly, + 'total_cost_montly' => $total_cost_pm, + 'total_cost_yearly' => $total_cost_yearly, + 'inactive_count' => $inactive_count, + ); + }); } public static function doServicesCount($services_count): array From 025d206618be8b61923107df29c65206e92bd0d8 Mon Sep 17 00:00:00 2001 From: cp6 Date: Sun, 29 May 2022 17:02:49 +1000 Subject: [PATCH 06/12] Updated doServicesCount func to be cached Updated doServicesCount func to be cached --- app/Models/Home.php | 56 +++++++++++++++++++++++-------------------- app/Models/Server.php | 2 ++ 2 files changed, 32 insertions(+), 26 deletions(-) diff --git a/app/Models/Home.php b/app/Models/Home.php index dd0011c..f530cf4 100644 --- a/app/Models/Home.php +++ b/app/Models/Home.php @@ -19,6 +19,8 @@ class Home extends Model Cache::forget('due_soon');//Main page due_soon cache Cache::forget('recently_added');//Main page recently_added cache Cache::forget('all_pricing');//All the pricing + Cache::forget('services_count_all'); + Cache::forget('pricing_breakdown'); } public static function servicesCount() @@ -161,36 +163,38 @@ class Home extends Model public static function doServicesCount($services_count): array { - $servers_count = $domains_count = $shared_count = $reseller_count = $other_count = $seedbox_count = $total_services = 0; - $services_count = json_decode($services_count, true); - foreach ($services_count as $sc) { - $total_services += $sc['amount']; - if ($sc['service_type'] === 1) { - $servers_count = $sc['amount']; - } else if ($sc['service_type'] === 2) { - $shared_count = $sc['amount']; - } else if ($sc['service_type'] === 3) { - $reseller_count = $sc['amount']; - } else if ($sc['service_type'] === 4) { - $domains_count = $sc['amount']; - } else if ($sc['service_type'] === 5) { - $other_count = $sc['amount']; - } else if ($sc['service_type'] === 6) { - $seedbox_count = $sc['amount']; + return Cache::remember('services_count_all', now()->addWeek(1), function () use ($services_count) { + $servers_count = $domains_count = $shared_count = $reseller_count = $other_count = $seedbox_count = $total_services = 0; + foreach ($services_count as $sc) { + $total_services += $sc['amount']; + if ($sc['service_type'] === 1) { + $servers_count = $sc['amount']; + } else if ($sc['service_type'] === 2) { + $shared_count = $sc['amount']; + } else if ($sc['service_type'] === 3) { + $reseller_count = $sc['amount']; + } else if ($sc['service_type'] === 4) { + $domains_count = $sc['amount']; + } else if ($sc['service_type'] === 5) { + $other_count = $sc['amount']; + } else if ($sc['service_type'] === 6) { + $seedbox_count = $sc['amount']; + } } - } - return array( - 'servers' => $servers_count, - 'shared' => $shared_count, - 'reseller' => $reseller_count, - 'domains' => $domains_count, - 'other' => $other_count, - 'seedbox' => $seedbox_count, - 'total' => $total_services - ); + return array( + 'servers' => $servers_count, + 'shared' => $shared_count, + 'reseller' => $reseller_count, + 'domains' => $domains_count, + 'other' => $other_count, + 'seedbox' => $seedbox_count, + 'total' => $total_services + ); + }); + } diff --git a/app/Models/Server.php b/app/Models/Server.php index d807bd5..4e4bc11 100644 --- a/app/Models/Server.php +++ b/app/Models/Server.php @@ -164,6 +164,8 @@ class Server extends Model Cache::forget('servers_summary');//servers summary cache Cache::forget('public_server_data');//public servers Cache::forget('all_pricing');//All pricing + Cache::forget('services_count_all'); + Cache::forget('pricing_breakdown'); } public static function serverSpecificCacheForget(string $server_id): void From 529376a12dccd31fdb7c0b46b3dab39306fca112 Mon Sep 17 00:00:00 2001 From: cp6 Date: Sun, 29 May 2022 17:10:46 +1000 Subject: [PATCH 07/12] Updated settings page layout Updated settings page layout --- resources/views/settings/index.blade.php | 121 ++++++++++------------- 1 file changed, 54 insertions(+), 67 deletions(-) diff --git a/resources/views/settings/index.blade.php b/resources/views/settings/index.blade.php index 716b6b1..b1ac953 100644 --- a/resources/views/settings/index.blade.php +++ b/resources/views/settings/index.blade.php @@ -18,7 +18,7 @@ @csrf @method('PUT')
-
+
Use dark mode
@@ -32,9 +32,7 @@
-
-
-
+
Show versions footer
@@ -50,7 +48,45 @@
-
+
+ + Default server OS + default_server_os + {{$setting[0]->default_server_os}} + +
+
+ + Default currency + default_currency + {{$setting[0]->default_currency}} + +
+
+
+
+ + Due soon amount to show + due_soon_amount + 1 + 0 + 12 + {{$setting[0]->due_soon_amount}} + +
+
+ + Recently added amount to show + recently_added_amount + 1 + 0 + 12 + {{$setting[0]->recently_added_amount}} + +
+
+
+
Show servers to public
@@ -64,10 +100,16 @@
+
+ + Home page currency + {{$setting[0]->dashboard_currency}} + +
-

Only if show_servers_public is YES do these apply:

+

Only if Show servers to public is YES do these apply:

-
+
Show servers IP's
@@ -81,9 +123,7 @@
-
-
-
+
Show servers hostname
@@ -101,7 +141,7 @@
-
+
Show servers provider
@@ -117,9 +157,7 @@
-
-
-
+
Show servers location
@@ -137,7 +175,7 @@
-
+
Show servers price
@@ -153,9 +191,7 @@
-
-
-
+
Show servers YABs
@@ -172,55 +208,6 @@
-
-
- - Default server OS - default_server_os - {{$setting[0]->default_server_os}} - -
-
-
-
- - Default currency - default_currency - {{$setting[0]->default_currency}} - -
-
-
-
- - Due soon amount to show - due_soon_amount - 1 - 0 - 12 - {{$setting[0]->due_soon_amount}} - -
-
-
-
- - Recently added amount to show - recently_added_amount - 1 - 0 - 12 - {{$setting[0]->recently_added_amount}} - -
-
-
-
- - {{$setting[0]->dashboard_currency}} - -
-
Update settings From 227677a62a9a9ba6b8be8ff2343a1292eda3f54e Mon Sep 17 00:00:00 2001 From: cp6 Date: Sun, 19 Jun 2022 14:56:19 +1000 Subject: [PATCH 08/12] Added POST API to create server Added POST request to API to create a server --- README.md | 42 +++++++++++ app/Http/Controllers/ApiController.php | 97 +++++++++++++++++++++++++- routes/api.php | 4 ++ 3 files changed, 141 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 1b307f7..939bc8f 100644 --- a/README.md +++ b/README.md @@ -162,6 +162,48 @@ All API requests must be appended with `api/` e.g `mydomain.com/api/servers/gYk8 `shared/{id}` +**POST requests** + +Create a server + +`/servers` + +Body content template + +```json +{ +"active": 1, +"show_public": 0, +"hostname": "test.domain.com", +"ns1": "ns1", +"ns2": "ns2", +"server_type": 1, +"os_id": 2, +"provider_id": 10, +"location_id": 15, +"ssh_port": 22, +"bandwidth": 2000, +"ram": 2024, +"ram_type": "MB", +"ram_as_mb": 2024, +"disk": 30, +"disk_type": "GB", +"disk_as_gb": 30, +"cpu": 2, +"has_yabs": 0, +"was_promo": 1, +"ip1": "127.0.0.1", +"ip2": null, +"owned_since": "2022-01-01", +"currency": "USD", +"price": 4.00, +"payment_term": 1, +"as_usd": 4.00, +"usd_per_month": 4.00, +"next_due_date": "2022-02-01" +} +``` + ## Notes **Public viewable listings** diff --git a/app/Http/Controllers/ApiController.php b/app/Http/Controllers/ApiController.php index ced080c..342ac00 100644 --- a/app/Http/Controllers/ApiController.php +++ b/app/Http/Controllers/ApiController.php @@ -13,6 +13,8 @@ use App\Models\Shared; use DataTables; use Illuminate\Http\Request; use Illuminate\Support\Facades\DB; +use Illuminate\Support\Facades\Validator; +use Illuminate\Support\Str; class ApiController extends Controller { @@ -23,7 +25,7 @@ class ApiController extends Controller ->join('providers as pr', 's.provider_id', '=', 'pr.id') ->join('locations as l', 's.location_id', '=', 'l.id') ->join('os as o', 's.os_id', '=', 'o.id') - ->get(['s.*', 'p.id as price_id', 'p.currency', 'p.price', 'p.term', 'p.as_usd', 'p.usd_per_month', 'p.next_due_date', 'pr.name as provider', 'l.name as location','o.name as os'])->toJson(JSON_PRETTY_PRINT); + ->get(['s.*', 'p.id as price_id', 'p.currency', 'p.price', 'p.term', 'p.as_usd', 'p.usd_per_month', 'p.next_due_date', 'pr.name as provider', 'l.name as location', 'o.name as os'])->toJson(JSON_PRETTY_PRINT); return response($servers, 200); } @@ -36,7 +38,7 @@ class ApiController extends Controller ->join('locations as l', 's.location_id', '=', 'l.id') ->join('os as o', 's.os_id', '=', 'o.id') ->where('s.id', '=', $id) - ->get(['s.*', 'p.id as price_id', 'p.currency', 'p.price', 'p.term', 'p.as_usd', 'p.usd_per_month', 'p.next_due_date', 'pr.name as provider', 'l.name as location','o.name as os']); + ->get(['s.*', 'p.id as price_id', 'p.currency', 'p.price', 'p.term', 'p.as_usd', 'p.usd_per_month', 'p.next_due_date', 'pr.name as provider', 'l.name as location', 'o.name as os']); $yabs = DB::table('yabs') ->where('yabs.server_id', '=', $id) @@ -316,4 +318,95 @@ class ApiController extends Controller return response(array('ip' => null), 200); } + protected function storeServer(Request $request) + { + $rules = array( + 'hostname' => 'min:3', + 'server_type' => 'required|integer', + 'os_id' => 'required|integer', + 'provider_id' => 'required|integer', + 'location_id' => 'required|integer', + 'ssh_port' => 'required|integer', + 'ram' => 'required|integer', + 'ram_as_mb' => 'required|integer', + 'disk' => 'required|integer', + 'disk_as_gb' => 'required|integer', + 'cpu' => 'required|integer', + 'bandwidth' => 'required|integer', + 'was_promo' => 'required|integer', + 'active' => 'required|integer', + 'show_public' => 'required|integer', + 'owned_since' => 'required|date', + 'ram_type' => 'required|string|size:2', + 'disk_type' => 'required|string|size:2', + 'currency' => 'required|string|size:3', + 'price' => 'required|numeric', + 'payment_term' => 'required|integer', + 'next_due_date' => 'date', + ); + + $messages = array( + 'required' => ':attribute is required', + 'min' => ':attribute must be longer than 3', + 'integer' => ':attribute must be an integer', + 'string' => ':attribute must be a string', + 'size' => ':attribute must be exactly :size characters', + 'numeric' => ':attribute must be a float', + 'date' => ':attribute must be a date Y-m-d', + ); + + $validator = Validator::make($request->all(), $rules, $messages); + + if ($validator->fails()) { + return response()->json(['result' => 'fail', 'messages' => $validator->messages()], 400); + } + + $server_id = Str::random(8); + + $pricing = new Pricing(); + + $as_usd = $pricing->convertToUSD($request->price, $request->currency); + + $pricing->insertPricing(1, $server_id, $request->currency, $request->price, $request->payment_term, $as_usd, $request->next_due_date); + + if (!is_null($request->ip1)) { + IPs::insertIP($server_id, $request->ip1); + } + + if (!is_null($request->ip2)) { + IPs::insertIP($server_id, $request->ip2); + } + + $insert = Server::create([ + 'id' => $server_id, + 'hostname' => $request->hostname, + 'server_type' => $request->server_type, + 'os_id' => $request->os_id, + 'ssh_port' => $request->ssh_port, + 'provider_id' => $request->provider_id, + 'location_id' => $request->location_id, + 'ram' => $request->ram, + 'ram_type' => $request->ram_type, + 'ram_as_mb' => ($request->ram_type === 'MB') ? $request->ram : ($request->ram * 1024), + 'disk' => $request->disk, + 'disk_type' => $request->disk_type, + 'disk_as_gb' => ($request->disk_type === 'GB') ? $request->disk : ($request->disk * 1024), + 'owned_since' => $request->owned_since, + 'ns1' => $request->ns1, + 'ns2' => $request->ns2, + 'bandwidth' => $request->bandwidth, + 'cpu' => $request->cpu, + 'was_promo' => $request->was_promo, + 'show_public' => (isset($request->show_public)) ? 1 : 0 + ]); + + Server::serverRelatedCacheForget(); + + if ($insert) { + return response()->json(array('result' => 'success', 'server_id' => $server_id), 200); + } + + return response()->json(array('result' => 'fail', 'request' => $request->post()), 500); + } + } diff --git a/routes/api.php b/routes/api.php index 71f4515..d860d70 100644 --- a/routes/api.php +++ b/routes/api.php @@ -28,6 +28,10 @@ Route::middleware('auth:api')->get('domains/{id}', 'App\Http\Controllers\ApiCont Route::middleware('auth:api')->get('servers', 'App\Http\Controllers\ApiController@getAllServers'); Route::middleware('auth:api')->get('servers/{id}', 'App\Http\Controllers\ApiController@getServer'); +Route::middleware('auth:api')->post('servers', 'App\Http\Controllers\ApiController@storeServer'); +Route::middleware('auth:api')->put('servers/{id}', 'App\Http\Controllers\ApiController@updateServer'); +Route::middleware('auth:api')->delete('servers/{id}', 'App\Http\Controllers\ApiController@destroyServer'); + Route::middleware('auth:api')->get('IPs/', 'App\Http\Controllers\ApiController@getAllIPs'); Route::middleware('auth:api')->get('IPs/{id}', 'App\Http\Controllers\ApiController@getIP'); From 546f0ba565a895bc5bef78b91798e044f4ceb302 Mon Sep 17 00:00:00 2001 From: cp6 Date: Sun, 19 Jun 2022 15:30:32 +1000 Subject: [PATCH 09/12] Added YABs version v2022-06-11 Added YABs version v2022-06-11 Cleaned up version validation check --- app/Process.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/app/Process.php b/app/Process.php index c0b6706..9b040e5 100644 --- a/app/Process.php +++ b/app/Process.php @@ -171,6 +171,8 @@ class Process { $file_name = 'tempYabs.txt'; + $allowed_versions = ['v2021-12-28', 'v2022-02-18', 'v2022-04-30', 'v2022-05-06', 'v2022-06-11']; + Storage::disk('local')->put($file_name, $data_from_form); $file = Storage::disk('local')->get($file_name); @@ -192,8 +194,8 @@ class Process } $version_array = explode(' ', preg_replace('!\s+!', ' ', $this->trimRemoveR($array[2]))); - if ($version_array[1] === 'v2021-12-28' || $version_array[1] === 'v2022-02-18' || $version_array[1] === 'v2022-04-30' || $version_array[1] === 'v2022-05-06') {//YABs version - if ($version_array[1] === 'v2022-05-06') { + if (in_array($version_array[1], $allowed_versions, true)) {//YABs version is allowed + if ($version_array[1] === 'v2022-05-06' || $version_array[1] === 'v2022-06-11') {//These versions added in more responses $cpu = $this->trimRemoveR(str_replace(':', '', strstr($array[11], ': '))); $cpu_spec = explode(' ', strstr($array[12], ': '));//: 2 @ 3792.872 MHz $ram_line = $this->trimRemoveR(str_replace(':', '', strstr($array[15], ': '))); @@ -250,7 +252,7 @@ class Process ); if (isset($array[40])) { - if ($version_array[1] === 'v2022-05-06') { + if ($version_array[1] === 'v2022-05-06' || $version_array[1] === 'v2022-06-11') { if ($array[43] === "Geekbench 5 Benchmark Test:\r") { //No ipv6 //Has short ipv4 network speed testing (-r) From b34780546b2d301e67fc5a6409758c32eaa4e2c2 Mon Sep 17 00:00:00 2001 From: cp6 Date: Sun, 19 Jun 2022 16:30:27 +1000 Subject: [PATCH 10/12] Added destroy and update server to API Added destroy and update server to API --- app/Http/Controllers/ApiController.php | 78 ++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) diff --git a/app/Http/Controllers/ApiController.php b/app/Http/Controllers/ApiController.php index 342ac00..e36218e 100644 --- a/app/Http/Controllers/ApiController.php +++ b/app/Http/Controllers/ApiController.php @@ -336,6 +336,8 @@ class ApiController extends Controller 'was_promo' => 'required|integer', 'active' => 'required|integer', 'show_public' => 'required|integer', + 'ip1' => 'ip', + 'ip2' => 'ip', 'owned_since' => 'required|date', 'ram_type' => 'required|string|size:2', 'disk_type' => 'required|string|size:2', @@ -352,6 +354,7 @@ class ApiController extends Controller 'string' => ':attribute must be a string', 'size' => ':attribute must be exactly :size characters', 'numeric' => ':attribute must be a float', + 'ip' => ':attribute must be a valid IP address', 'date' => ':attribute must be a date Y-m-d', ); @@ -409,4 +412,79 @@ class ApiController extends Controller return response()->json(array('result' => 'fail', 'request' => $request->post()), 500); } + public function destroyServer(Request $request) + { + $items = Server::find($request->id); + + (!is_null($items)) ? $result = $items->delete() : $result = false; + + $p = new Pricing(); + $p->deletePricing($request->id); + + Labels::deleteLabelsAssignedTo($request->id); + IPs::deleteIPsAssignedTo($request->id); + Server::serverRelatedCacheForget(); + + if ($result) { + return response()->json(array('result' => 'success'), 200); + } + + return response()->json(array('result' => 'fail'), 500); + } + + public function updateServer(Request $request, Server $server) + { + $rules = array( + 'hostname' => 'string|min:3', + 'server_type' => 'integer', + 'os_id' => 'integer', + 'provider_id' => 'integer', + 'location_id' => 'integer', + 'ssh_port' => 'integer', + 'ram' => 'integer', + 'ram_as_mb' => 'integer', + 'disk' => 'integer', + 'disk_as_gb' => 'integer', + 'cpu' => 'integer', + 'bandwidth' => 'integer', + 'was_promo' => 'integer', + 'active' => 'integer', + 'show_public' => 'integer', + 'owned_since' => 'date', + 'ram_type' => 'string|size:2', + 'disk_type' => 'string|size:2', + 'currency' => 'string|size:3', + 'price' => 'numeric', + 'payment_term' => 'integer', + 'next_due_date' => 'date', + ); + + $messages = array( + 'required' => ':attribute is required', + 'min' => ':attribute must be longer than 3', + 'integer' => ':attribute must be an integer', + 'string' => ':attribute must be a string', + 'size' => ':attribute must be exactly :size characters', + 'numeric' => ':attribute must be a float', + 'date' => ':attribute must be a date Y-m-d', + ); + + $validator = Validator::make($request->all(), $rules, $messages); + + if ($validator->fails()) { + return response()->json(['result' => 'fail', 'messages' => $validator->messages()], 400); + } + + $server_update = Server::where('id', $request->id)->update(request()->all()); + + Server::serverRelatedCacheForget(); + Server::serverSpecificCacheForget($request->id); + + if ($server_update) { + return response()->json(array('result' => 'success', 'server_id' => $request->id), 200); + } + + return response()->json(array('result' => 'fail', 'request' => $request->post()), 500); + } + } From b9c756c8d5bba142b07b4a6902b63c5123c2ede3 Mon Sep 17 00:00:00 2001 From: cp6 Date: Sun, 19 Jun 2022 21:22:23 +1000 Subject: [PATCH 11/12] Added pricing update with API Added pricing update with API --- app/Http/Controllers/ApiController.php | 46 +++++++++++++++++++++++++- routes/api.php | 1 + 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/app/Http/Controllers/ApiController.php b/app/Http/Controllers/ApiController.php index e36218e..53662a0 100644 --- a/app/Http/Controllers/ApiController.php +++ b/app/Http/Controllers/ApiController.php @@ -12,6 +12,7 @@ use App\Models\Server; use App\Models\Shared; use DataTables; use Illuminate\Http\Request; +use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Validator; use Illuminate\Support\Str; @@ -432,7 +433,7 @@ class ApiController extends Controller return response()->json(array('result' => 'fail'), 500); } - public function updateServer(Request $request, Server $server) + public function updateServer(Request $request) { $rules = array( 'hostname' => 'string|min:3', @@ -487,4 +488,47 @@ class ApiController extends Controller return response()->json(array('result' => 'fail', 'request' => $request->post()), 500); } + public function updatePricing(Request $request) + { + $rules = array( + 'price' => 'required|numeric', + 'currency' => 'required|string|size:3', + 'term' => 'required|integer', + 'active' => 'integer', + 'next_due_date' => 'date', + ); + + $messages = array( + 'required' => ':attribute is required', + 'integer' => ':attribute must be an integer', + 'string' => ':attribute must be a string', + 'size' => ':attribute must be exactly :size characters', + 'numeric' => ':attribute must be a float', + 'date' => ':attribute must be a date Y-m-d', + ); + + $validator = Validator::make($request->all(), $rules, $messages); + + if ($validator->fails()) { + return response()->json(['result' => 'fail', 'messages' => $validator->messages()], 400); + } + + $pricing = new Pricing(); + + $request->as_usd = $pricing->convertToUSD($request->price, $request->currency); + + $request->usd_per_month = $pricing->costAsPerMonth($request->as_usd, $request->term); + + $price_update = Pricing::where('id', $request->id)->update(request()->all()); + + Cache::forget("all_pricing"); + Server::serverRelatedCacheForget(); + + if ($price_update) { + return response()->json(array('result' => 'success', 'server_id' => $request->id), 200); + } + + return response()->json(array('result' => 'fail', 'request' => $request->post()), 500); + } + } diff --git a/routes/api.php b/routes/api.php index d860d70..78df219 100644 --- a/routes/api.php +++ b/routes/api.php @@ -52,6 +52,7 @@ Route::middleware('auth:api')->get('os/{id}', 'App\Http\Controllers\ApiControlle Route::middleware('auth:api')->get('pricing/', 'App\Http\Controllers\ApiController@getAllPricing'); Route::middleware('auth:api')->get('pricing/{id}', 'App\Http\Controllers\ApiController@getPricing'); +Route::middleware('auth:api')->put('pricing/{id}', 'App\Http\Controllers\ApiController@updatePricing'); Route::middleware('auth:api')->get('providers/', 'App\Http\Controllers\ApiController@getAllProviders'); Route::middleware('auth:api')->get('providers/{id}', 'App\Http\Controllers\ApiController@getProvider'); From c926dfb179751bbf97573acb5fca619a14bc4077 Mon Sep 17 00:00:00 2001 From: cp6 Date: Sun, 19 Jun 2022 21:27:34 +1000 Subject: [PATCH 12/12] Updated readme for 2.1.1 Updated readme for 2.1.1 --- README.md | 78 +++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 62 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 939bc8f..b3d1c7a 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ Despite what the name infers this self hosted web app isn't just for storing idl a [YABs](https://github.com/masonr/yet-another-bench-script) output you can get disk & network speed values along with GeekBench 5 scores to do easier comparing and sorting. -[![Generic badge](https://img.shields.io/badge/version-2.1.0-blue.svg)](https://shields.io/) [![Generic badge](https://img.shields.io/badge/Laravel-9.0-red.svg)](https://shields.io/) [![Generic badge](https://img.shields.io/badge/PHP-8.1-purple.svg)](https://shields.io/) [![Generic badge](https://img.shields.io/badge/Bootstrap-5.1-pink.svg)](https://shields.io/) +[![Generic badge](https://img.shields.io/badge/version-2.1.1-blue.svg)](https://shields.io/) [![Generic badge](https://img.shields.io/badge/Laravel-9.0-red.svg)](https://shields.io/) [![Generic badge](https://img.shields.io/badge/PHP-8.1-purple.svg)](https://shields.io/) [![Generic badge](https://img.shields.io/badge/Bootstrap-5.1-pink.svg)](https://shields.io/) @@ -19,25 +19,16 @@ GeekBench 5 scores to do easier comparing and sorting. [Cloud Five Limited](https://cloud-v.net/) for providing the hosting for demo installation. -## 2.1.0 changes: +## 2.1.1 changes (19th June 2022): -* Added Seedbox CRUD -* Added dark mode (settings option. Bootstrap-Night https://vinorodrigues.github.io/bootstrap-dark-5/) -* Added some foreign keys for certain tables -* Added functions for IP and label assignments -* Added functions to forget (clear) cache, preventing chunks of duplicate code -* Added VMware to server virt select dropdown options -* Added Kharkiv and Sao Paulo to locations seeder -* Updated Controllers with DB calls and logic moved to relevant Model -* Updated YABs inserts for version v2022-05-06 -* Updated DB calls to use caching -* Updated Home blade info cards to be col-6 instead of 12 when on mobile -* Updated home page view links on recently added -* Fixed YABs insert error not displaying +* Added compatability for YABs version v2022-06-11 +* Added Create, Update and Delete servers with API +* Added Update pricing with API +* Updated YABs compatible versions check ## Requires -* PHP 8 (8.1 recommended) +* PHP 8.1 ## Features @@ -204,6 +195,61 @@ Body content template } ``` +**PUT requests** + +Update a server + +`/servers/ID` + +Body content template + +```json +{ +"active": 1, +"show_public": 0, +"hostname": "test.domain.com", +"ns1": "ns1", +"ns2": "ns2", +"server_type": 1, +"os_id": 2, +"provider_id": 10, +"location_id": 15, +"ssh_port": 22, +"bandwidth": 2000, +"ram": 2024, +"ram_type": "MB", +"ram_as_mb": 2024, +"disk": 30, +"disk_type": "GB", +"disk_as_gb": 30, +"cpu": 2, +"has_yabs": 0, +"was_promo": 1, +"owned_since": "2022-01-01" +} +``` + +Update pricing + +`/pricing/ID` + +Body content template + +```json +{ + "price": 10.50, + "currency": "USD", + "term" : 1 +} +``` + +**DELETE requests** + +Delete a server + +`/servers/ID` + + ## Notes **Public viewable listings**