|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace app\controller\Admin; |
| 4 | + |
| 5 | +use app\model\App; |
| 6 | +use app\model\Game; |
| 7 | +use app\util\Validate; |
| 8 | +use Illuminate\Database\Eloquent\ModelNotFoundException; |
| 9 | +use support\Request; |
| 10 | + |
| 11 | +class AppController |
| 12 | +{ |
| 13 | + static public $rules = [ |
| 14 | + 'id' => 'nullable|integer', |
| 15 | + 'game_id' => 'required|integer', |
| 16 | + 'name' => 'required', |
| 17 | + 'description' => 'nullable', |
| 18 | + 'data_path' => 'required|json', |
| 19 | + 'working_path' => 'required', |
| 20 | + 'images' => 'required|json', |
| 21 | + 'config' => 'required', |
| 22 | + 'startup' => 'required', |
| 23 | + 'skip_install' => 'required|boolean', |
| 24 | + 'install_image' => 'required_unless:skip_install,1', |
| 25 | + 'install_script' => 'required_unless:skip_install,1' |
| 26 | + ]; |
| 27 | + |
| 28 | + public function GetList(Request $request) |
| 29 | + { |
| 30 | + return json([ |
| 31 | + 'code' => 200, |
| 32 | + 'data' => App::select(['id', 'game_id', 'name', 'updated_at', 'created_at']) |
| 33 | + ->withCount('instances') |
| 34 | + ->withCount('versions') |
| 35 | + ->with('game:id,name') |
| 36 | + ->get() |
| 37 | + ]); |
| 38 | + } |
| 39 | + |
| 40 | + public function Create(Request $request) |
| 41 | + { |
| 42 | + try { |
| 43 | + $data = Validate::Input($request, self::$rules); |
| 44 | + Game::findOrFail($data['game_id']); |
| 45 | + App::create($data); |
| 46 | + |
| 47 | + return json(['code' => 200]); |
| 48 | + } catch (ModelNotFoundException $e) { |
| 49 | + return json(['code' => 400, 'msg' => '游戏不存在。'])->withStatus(400); |
| 50 | + } catch (\Throwable $th) { |
| 51 | + return json(['code' => $th->getCode() ?: 500, 'msg' => $th->getMessage()])->withStatus($th->getCode() ?: 500); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + public function GetDetail(Request $request, Int $appId) |
| 56 | + { |
| 57 | + try { |
| 58 | + return json([ |
| 59 | + 'code' => 200, |
| 60 | + 'attributes' => App::findOrFail($appId) |
| 61 | + ]); |
| 62 | + } catch (ModelNotFoundException $e) { |
| 63 | + return json(['code' => 400, 'msg' => '镜像不存在。'])->withStatus(400); |
| 64 | + } catch (\Throwable $th) { |
| 65 | + return json(['code' => $th->getCode() ?: 500, 'msg' => $th->getMessage()])->withStatus($th->getCode() ?: 500); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + public function Update(Request $request, int $appId) |
| 70 | + { |
| 71 | + try { |
| 72 | + $data = Validate::Input($request, self::$rules); |
| 73 | + Game::findOrFail($data['game_id']); |
| 74 | + App::findOrFail($appId)->fill($data)->save(); |
| 75 | + |
| 76 | + return json(['code' => 200]); |
| 77 | + } catch (ModelNotFoundException $e) { |
| 78 | + return json(['code' => 400, 'msg' => '游戏或镜像不存在。'])->withStatus(400); |
| 79 | + } catch (\Throwable $th) { |
| 80 | + return json(['code' => $th->getCode() ?: 500, 'msg' => $th->getMessage()])->withStatus($th->getCode() ?: 500); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + public function Delete(Request $request, int $appId) |
| 85 | + { |
| 86 | + try { |
| 87 | + $app = App::withCount(['instances'])->findOrFail($appId); |
| 88 | + if ($app->instances_count > 0) throw new \Exception('无法删除带有实例的镜像。', 400); |
| 89 | + $app->delete(); |
| 90 | + |
| 91 | + return json(['code' => 200]); |
| 92 | + } catch (ModelNotFoundException $e) { |
| 93 | + return json(['code' => 400, 'msg' => '镜像不存在。'])->withStatus(400); |
| 94 | + } catch (\Throwable $th) { |
| 95 | + return json(['code' => $th->getCode() ?: 500, 'msg' => $th->getMessage()])->withStatus($th->getCode() ?: 500); |
| 96 | + } |
| 97 | + } |
| 98 | +} |
0 commit comments