ActivityReward.php 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 App\Services\RoleService;
  7. use Illuminate\Http\JsonResponse;
  8. use Illuminate\Http\Request;
  9. use Illuminate\Support\Facades\DB;
  10. use Illuminate\Validation\ValidationException;
  11. use Exception;
  12. use App\Models\ActivityReward as ActivityRewardModel;
  13. class ActivityReward extends Controller
  14. {
  15. public function index(): JsonResponse
  16. {
  17. try {
  18. $params = request()->validate([
  19. 'page' => ['required', 'integer', 'min:1'],
  20. 'limit' => ['required', 'integer', 'min:1'],
  21. 'title' => ['nullable', 'string'],
  22. ]);
  23. $page = request()->input('page', 1);
  24. $limit = request()->input('limit', 10);
  25. $where = ActivityRewardService::getWhere($params);
  26. $query = ActivityRewardModel::where($where);
  27. $count = $query->count();
  28. $list = $query->orderByDesc('id')
  29. ->forPage($page, $limit)->get()->toArray();
  30. $result = ['total' => $count, 'data' => $list];
  31. } catch (ValidationException $e) {
  32. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  33. } catch (Exception $e) {
  34. return $this->error($e->getCode(), $e->getMessage());
  35. }
  36. return $this->success($result);
  37. }
  38. public function store()
  39. {
  40. DB::beginTransaction();
  41. try {
  42. $params = request()->validate([
  43. 'id' => ['nullable', 'integer', 'min:1'],
  44. 'title' => ['required', 'string', 'min:1', 'max:140'],
  45. 'sub_title' => ['required', 'string', 'min:1', 'max:140'],
  46. 'start_time' => ['required', 'date', 'date_format:Y-m-d'],
  47. 'end_time' => ['required', 'date', 'date_format:Y-m-d'],
  48. 'detail_image' => ['required', 'url', 'regex:/\.(jpeg|jpg|png|webp)$/i'],
  49. 'status' => ['required', 'integer', 'min:0', 'max:1'],
  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()
  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. }