Lae/app/Http/Controllers/Modules/HostController.php

153 lines
3.5 KiB
PHP
Raw Normal View History

2022-08-16 10:44:16 +00:00
<?php
2022-11-16 05:22:41 +00:00
namespace App\Http\Controllers\Modules;
2022-08-16 10:44:16 +00:00
2022-11-06 11:28:22 +00:00
use App\Http\Controllers\Controller;
2022-08-29 09:31:08 +00:00
use App\Models\Host;
use App\Models\User;
2022-11-16 02:29:50 +00:00
use Illuminate\Http\JsonResponse;
2022-08-16 10:44:16 +00:00
use Illuminate\Http\Request;
2022-11-16 02:29:50 +00:00
use Illuminate\Http\Response;
2022-11-06 11:28:22 +00:00
use Illuminate\Support\Str;
2022-11-16 05:22:41 +00:00
use function auth;
2022-11-06 11:28:22 +00:00
// use Illuminate\Support\Facades\Log;
2022-08-16 10:44:16 +00:00
class HostController extends Controller
{
/**
* Display a listing of the resource.
*
2022-11-16 05:22:41 +00:00
* @return Response|null
2022-08-16 10:44:16 +00:00
*/
2022-11-16 05:22:41 +00:00
public function index(): ?Response
2022-08-16 10:44:16 +00:00
{
//
2022-08-28 17:17:57 +00:00
// Host::all();
2022-11-16 02:29:50 +00:00
2022-11-16 05:22:41 +00:00
return null;
2022-08-16 10:44:16 +00:00
}
/**
* Store a newly created resource in storage.
*
2022-11-16 02:29:50 +00:00
* @param Request $request
*
* @return Response|JsonResponse
2022-11-16 05:22:41 +00:00
* @throws \Illuminate\Validation\ValidationException
2022-08-16 10:44:16 +00:00
*/
2022-11-16 05:22:41 +00:00
public function store(Request $request): Response|JsonResponse
2022-08-16 10:44:16 +00:00
{
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',
]);
//
$user = User::findOrFail($request->user_id);
2022-10-28 14:34:23 +00:00
if ($user->balance < 1) {
return $this->error('此用户余额不足,无法开设计费项目。');
}
2022-08-29 09:31:08 +00:00
// 如果没有 name则随机
$name = $request->input('name', Str::random(10));
$data = [
'name' => $name,
'status' => $request->status,
'price' => $request->price,
2022-11-06 14:57:01 +00:00
'managed_price' => $request->managed_price ?? 0,
'user_id' => $user->id,
2022-11-06 14:57:01 +00:00
'module_id' => auth('module')->id()
2022-08-29 09:31:08 +00:00
];
$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-11-16 02:29:50 +00:00
*
* @return JsonResponse
2022-08-16 10:44:16 +00:00
*/
2022-11-16 05:22:41 +00:00
public function show(Host $host): JsonResponse
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.
*
2022-11-16 02:29:50 +00:00
* @param Request $request
* @param Host $host
2022-11-16 02:29:50 +00:00
*
* @return JsonResponse
2022-11-16 05:22:41 +00:00
* @throws \Illuminate\Validation\ValidationException
2022-08-16 10:44:16 +00:00
*/
2022-11-16 05:22:41 +00:00
public function update(Request $request, Host $host): JsonResponse
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-11-16 02:29:50 +00:00
*
* @return JsonResponse
2022-08-16 10:44:16 +00:00
*/
2022-11-16 05:22:41 +00:00
public function destroy($host): JsonResponse
2022-08-16 10:44:16 +00:00
{
2022-10-28 08:33:50 +00:00
if ($host instanceof Host) {
2022-10-16 09:25:04 +00:00
$host->delete();
2022-10-28 08:33:50 +00:00
} else {
2022-11-08 09:28:32 +00:00
$host = Host::findOrFail($host);
2022-10-28 08:33:50 +00:00
2022-11-16 05:22:41 +00:00
$host?->delete();
2022-10-16 09:25:04 +00:00
}
return $this->deleted($host);
2022-08-16 10:44:16 +00:00
}
}