Sport.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use App\Constants\HttpStatus;
  4. use App\Http\Controllers\Controller;
  5. use App\Models\Sport as SportModel;
  6. use App\Services\FixtureService;
  7. use Illuminate\Http\JsonResponse;
  8. use Exception;
  9. use Illuminate\Validation\ValidationException;
  10. class Sport extends Controller
  11. {
  12. public function index(): JsonResponse
  13. {
  14. $date = request()->input('date', date('Y-m-d'));
  15. $start = strtotime($date);
  16. $end = strtotime("+1 day", $start);
  17. $list = SportModel::where("game_time", ">=", $start)
  18. ->where("game_time", "<", $end)
  19. ->get()->makeHidden(['id']);
  20. $count = SportModel::where("game_time", ">=", $start)
  21. ->where("game_time", "<", $end)->count();
  22. return $this->success(['list' => $list, 'count' => $count]);
  23. }
  24. public function odds(): JsonResponse
  25. {
  26. try {
  27. request()->validate([
  28. 'data_id' => ['required', 'string', 'min:1'],
  29. ]);
  30. $dataId = request()->input('data_id');
  31. $res = FixtureService::odds($dataId);
  32. } catch (ValidationException $e) {
  33. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  34. } catch (Exception $e) {
  35. return $this->error($e->getCode());
  36. }
  37. return $this->success($res);
  38. }
  39. public function oddsLive(): JsonResponse
  40. {
  41. try {
  42. request()->validate([
  43. 'data_id' => ['required', 'string', 'min:1'],
  44. ]);
  45. $dataId = request()->input('data_id');
  46. $res = FixtureService::oddsLive($dataId);
  47. } catch (ValidationException $e) {
  48. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  49. } catch (Exception $e) {
  50. return $this->error($e->getCode());
  51. }
  52. return $this->success($res);
  53. }
  54. }