ActivityReward.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. 'lang' => ['required', 'string'],
  51. ]);
  52. ActivityRewardService::submit($params);
  53. DB::commit();
  54. } catch (ValidationException $e) {
  55. DB::rollBack();
  56. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  57. } catch (Exception $e) {
  58. DB::rollBack();
  59. return $this->error($e->getCode(), $e->getMessage());
  60. }
  61. return $this->success();
  62. }
  63. public function destroy(): JsonResponse
  64. {
  65. try {
  66. request()->validate([
  67. 'id' => ['required', 'integer', 'min:1'],
  68. ]);
  69. $id = request()->input('id');
  70. ActivityRewardService::deleteAll(['id' => $id]);
  71. DB::commit();
  72. } catch (ValidationException $e) {
  73. DB::rollBack();
  74. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  75. } catch (Exception $e) {
  76. DB::rollBack();
  77. return $this->error($e->getCode(), $e->getMessage());
  78. }
  79. return $this->success([], '删除成功');
  80. }
  81. }