ActivityReward.php 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. namespace App\Http\Controllers\admin;
  3. use App\Constants\HttpStatus;
  4. use App\Http\Controllers\Controller;
  5. use App\Services\ActivityRewardService;
  6. use Illuminate\Http\JsonResponse;
  7. use Illuminate\Support\Facades\DB;
  8. use Illuminate\Validation\ValidationException;
  9. use Exception;
  10. use App\Models\ActivityReward as ActivityRewardModel;
  11. class ActivityReward extends Controller
  12. {
  13. public function index(): JsonResponse
  14. {
  15. try {
  16. $params = request()->validate([
  17. 'page' => ['required', 'integer', 'min:1'],
  18. 'limit' => ['required', 'integer', 'min:1'],
  19. 'title' => ['nullable', 'string'],
  20. 'type' => ['nullable', 'string'],
  21. ]);
  22. $page = request()->input('page', 1);
  23. $limit = request()->input('limit', 10);
  24. $where = ActivityRewardService::getWhere($params);
  25. $query = ActivityRewardModel::where($where);
  26. $count = $query->count();
  27. $list = $query->orderByDesc('id')
  28. ->forPage($page, $limit)->get()->toArray();
  29. $result = ['total' => $count, 'data' => $list];
  30. } catch (ValidationException $e) {
  31. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  32. } catch (Exception $e) {
  33. return $this->error($e->getCode(), $e->getMessage());
  34. }
  35. return $this->success($result);
  36. }
  37. public function store(): JsonResponse
  38. {
  39. DB::beginTransaction();
  40. try {
  41. $params = request()->validate([
  42. 'id' => ['nullable', 'integer', 'min:1'],
  43. 'title' => ['required', 'string', 'min:1', 'max:140'],
  44. 'sub_title' => ['required', 'string', 'min:1', 'max:140'],
  45. 'start_time' => ['required', 'date', 'date_format:Y-m-d'],
  46. 'end_time' => ['required', 'date', 'date_format:Y-m-d', 'after_or_equal:start_time'],
  47. 'detail_image' => ['required', 'url', 'regex:/\.(jpeg|jpg|png|webp)$/i'],
  48. 'status' => ['required', 'integer', 'min:0', 'max:1'],
  49. 'type' => ['required', 'string'],
  50. ]);
  51. ActivityRewardService::submit($params);
  52. DB::commit();
  53. } catch (ValidationException $e) {
  54. DB::rollBack();
  55. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  56. } catch (Exception $e) {
  57. DB::rollBack();
  58. return $this->error($e->getCode(), $e->getMessage());
  59. }
  60. return $this->success();
  61. }
  62. public function destroy(): JsonResponse
  63. {
  64. try {
  65. request()->validate([
  66. 'id' => ['required', 'integer', 'min:1'],
  67. ]);
  68. $id = request()->input('id');
  69. ActivityRewardService::deleteAll(['id' => $id]);
  70. DB::commit();
  71. } catch (ValidationException $e) {
  72. DB::rollBack();
  73. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  74. } catch (Exception $e) {
  75. DB::rollBack();
  76. return $this->error($e->getCode(), $e->getMessage());
  77. }
  78. return $this->success([], '删除成功');
  79. }
  80. }