validate([ 'game_name' => ['nullable', 'string'], ]); $search = request()->all(); $result = GameService::paginate($search); } catch (ValidationException $e) { return $this->error(HttpStatus::VALIDATION_FAILED, $e->validator->errors()->first()); } catch (Exception $e) { return $this->error(intval($e->getCode())); } return $this->success($result); } /** * @api {post} /admin/game/update 修改 * @apiDescription 更新或者新增都用这个接口 * @apiGroup 游戏管理 * @apiUse result * @apiUse header * @apiVersion 1.0.0 * * @apiParam {int} id 要更新的ID * - 如果是新增,则id=0 * @apiParam {string} game_name 游戏名称 */ public function update() { DB::beginTransaction(); try { request()->validate([ 'id' => ['required', 'integer', 'min:0', 'max:99999999'], 'game_name' => ['required', 'string', 'min:1', 'max:32'], ]); $id = request()->input('id', 0); $gameName = request()->input('game_name'); $gameName = trim($gameName); if (GameModel::where('game_name', $gameName)->first()) { throw new Exception('游戏名称已存在', HttpStatus::CUSTOM_ERROR); } GameModel::updateOrCreate( ['id' => $id], ['game_name' => $gameName] ); DB::commit(); } catch (ValidationException $e) { DB::rollBack(); return $this->error(HttpStatus::VALIDATION_FAILED, $e->validator->errors()->first()); } catch (Exception $e) { DB::rollBack(); return $this->error(intval($e->getCode()), $e->getMessage()); } return $this->success(); } /** * @api {post} /admin/game/delete 删除 * @apiGroup 游戏管理 * @apiUse result * @apiUse header * @apiVersion 1.0.0 * * @apiParam {int} id 要删除的ID * */ function destroy() { DB::beginTransaction(); try { request()->validate([ 'id' => ['required', 'integer', 'min:1', 'max:99999999'], ]); $id = request()->input('id', 0); $game = GameModel::where('id', $id)->first(); if (!$game) throw new Exception("数据不存在", HttpStatus::CUSTOM_ERROR); $game->delete(); DB::commit(); } catch (ValidationException $e) { DB::rollBack(); return $this->error(HttpStatus::VALIDATION_FAILED, $e->validator->errors()->first()); } catch (Exception $e) { DB::rollBack(); return $this->error(intval($e->getCode())); } return $this->success(); } }