Game.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. namespace App\Http\Controllers\admin;
  3. use App\Constants\HttpStatus;
  4. use App\Http\Controllers\Controller;
  5. use App\Services\GameService;
  6. use Illuminate\Support\Facades\DB;
  7. use Illuminate\Validation\ValidationException;
  8. use Exception;
  9. use App\Models\Game as GameModel;
  10. class Game extends Controller
  11. {
  12. /**
  13. * @api {get} /admin/game 游戏列表
  14. * @apiGroup 游戏管理
  15. * @apiUse result
  16. * @apiUse header
  17. * @apiVersion 1.0.0
  18. *
  19. * @apiParam {int} [page=1]
  20. * @apiParam {int} [limit=10]
  21. * @apiParam {string} [game_name] 游戏名称
  22. *
  23. * @apiSuccess (data) {Object} data
  24. * @apiSuccess (data) {int} data.total 数量
  25. * @apiSuccess (data) {Object[]} data.data 列表
  26. * @apiSuccess (data) {int} data.data.id
  27. * @apiSuccess (data) {int} data.data.game_name 游戏名称
  28. * @apiSuccess (data) {string} data.data.updated_at
  29. * @apiSuccess (data) {string} data.data.created_at
  30. */
  31. function index()
  32. {
  33. try {
  34. request()->validate([
  35. 'game_name' => ['nullable', 'string'],
  36. ]);
  37. $search = request()->all();
  38. $result = GameService::paginate($search);
  39. } catch (ValidationException $e) {
  40. return $this->error(HttpStatus::VALIDATION_FAILED, $e->validator->errors()->first());
  41. } catch (Exception $e) {
  42. return $this->error(intval($e->getCode()));
  43. }
  44. return $this->success($result);
  45. }
  46. /**
  47. * @api {post} /admin/game/update 修改
  48. * @apiDescription 更新或者新增都用这个接口
  49. * @apiGroup 游戏管理
  50. * @apiUse result
  51. * @apiUse header
  52. * @apiVersion 1.0.0
  53. *
  54. * @apiParam {int} id 要更新的ID
  55. * - 如果是新增,则id=0
  56. * @apiParam {string} game_name 游戏名称
  57. */
  58. public function update()
  59. {
  60. DB::beginTransaction();
  61. try {
  62. request()->validate([
  63. 'id' => ['required', 'integer', 'min:0', 'max:99999999'],
  64. 'game_name' => ['required', 'string', 'min:1', 'max:32'],
  65. ]);
  66. $id = request()->input('id', 0);
  67. $gameName = request()->input('game_name');
  68. $gameName = trim($gameName);
  69. if (GameModel::where('game_name', $gameName)->first()) {
  70. throw new Exception('游戏名称已存在', HttpStatus::CUSTOM_ERROR);
  71. }
  72. GameModel::updateOrCreate(
  73. ['id' => $id],
  74. ['game_name' => $gameName]
  75. );
  76. DB::commit();
  77. } catch (ValidationException $e) {
  78. DB::rollBack();
  79. return $this->error(HttpStatus::VALIDATION_FAILED, $e->validator->errors()->first());
  80. } catch (Exception $e) {
  81. DB::rollBack();
  82. return $this->error(intval($e->getCode()), $e->getMessage());
  83. }
  84. return $this->success();
  85. }
  86. /**
  87. * @api {post} /admin/game/delete 删除
  88. * @apiGroup 游戏管理
  89. * @apiUse result
  90. * @apiUse header
  91. * @apiVersion 1.0.0
  92. *
  93. * @apiParam {int} id 要删除的ID
  94. *
  95. */
  96. function destroy()
  97. {
  98. DB::beginTransaction();
  99. try {
  100. request()->validate([
  101. 'id' => ['required', 'integer', 'min:1', 'max:99999999'],
  102. ]);
  103. $id = request()->input('id', 0);
  104. $game = GameModel::where('id', $id)->first();
  105. if (!$game) throw new Exception("数据不存在", HttpStatus::CUSTOM_ERROR);
  106. $game->delete();
  107. DB::commit();
  108. } catch (ValidationException $e) {
  109. DB::rollBack();
  110. return $this->error(HttpStatus::VALIDATION_FAILED, $e->validator->errors()->first());
  111. } catch (Exception $e) {
  112. DB::rollBack();
  113. return $this->error(intval($e->getCode()));
  114. }
  115. return $this->success();
  116. }
  117. }