Lae/app/Http/Controllers/Remote/Host/HostController.php

130 lines
2.9 KiB
PHP
Raw Normal View History

2022-08-16 10:44:16 +00:00
<?php
namespace App\Http\Controllers\Remote\Host;
2022-08-29 09:31:08 +00:00
use App\Models\Host;
use Illuminate\Support\Str;
2022-08-16 10:44:16 +00:00
use Illuminate\Http\Request;
2022-08-29 09:31:08 +00:00
use App\Http\Controllers\Controller;
2022-08-16 10:44:16 +00:00
class HostController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
//
2022-08-28 17:17:57 +00:00
// Host::all();
2022-08-16 10:44:16 +00:00
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
2022-08-28 17:17:57 +00:00
// 存储计费项目
2022-09-08 16:12:02 +00:00
$this->validate($request, [
2022-08-29 09:31:08 +00:00
'status' => 'required|in:running,stopped,error,suspended,pending',
'price' => 'required|numeric',
'user_id' => 'required|integer|exists:users,id',
]);
// 如果没有 name则随机
$name = $request->input('name', Str::random(10));
$data = [
'name' => $name,
'status' => $request->status,
'price' => $request->price,
'user_id' => $request->user_id,
'module_id' => auth('remote')->id()
];
$host = Host::create($data);
$host['host_id'] = $host->id;
return $this->created($host);
2022-08-16 10:44:16 +00:00
}
/**
* Display the specified resource.
*
* @param Host $host
2022-08-16 10:44:16 +00:00
* @return \Illuminate\Http\Response
*/
public function show(Host $host)
2022-08-16 10:44:16 +00:00
{
return $this->success($host);
2022-08-16 10:44:16 +00:00
//
// dd($host->cost());
2022-08-16 10:44:16 +00:00
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param Host $host
2022-08-16 10:44:16 +00:00
* @return \Illuminate\Http\Response
*/
public function update(Request $request, Host $host)
2022-08-16 10:44:16 +00:00
{
//
2022-09-08 16:12:02 +00:00
$this->validate($request, [
2022-08-29 09:31:08 +00:00
'status' => 'sometimes|in:running,stopped,error,suspended,pending',
// 'managed_price' => 'sometimes|numeric|nullable',
// 如果是立即扣费
2022-08-29 10:59:32 +00:00
'cost_once' => 'sometimes|numeric|nullable',
2022-10-07 05:15:08 +00:00
'cost_balance' => 'sometimes|numeric|nullable',
]);
// if has cost_once
if ($request->has('cost_once')) {
2022-10-07 05:15:08 +00:00
$host->cost($request->cost_once ?? 0, false);
return $this->updated();
}
if ($request->has('cost_balance')) {
$host->costBalance($request->cost_balance ?? 0);
2022-10-07 05:15:08 +00:00
return $this->updated();
}
2022-10-07 05:15:08 +00:00
$update = $request->except(['module_id', 'user_id']);
2022-08-29 10:59:32 +00:00
$host->update($update);
return $this->updated($host);
2022-08-16 10:44:16 +00:00
}
/**
* Remove the specified resource from storage.
*
2022-10-16 09:25:04 +00:00
* @param $host
2022-08-16 10:44:16 +00:00
* @return \Illuminate\Http\Response
*/
2022-10-16 09:25:04 +00:00
public function destroy($host)
2022-08-16 10:44:16 +00:00
{
2022-10-16 09:25:04 +00:00
$host = Host::where('id', $host)->first($host);
if ($host) {
$host->delete();
}
return $this->deleted($host);
2022-08-16 10:44:16 +00:00
}
}