説明なし
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

TriviaController.php 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. namespace App\Http\Controllers\Api\V1;
  3. use App\Http\Controllers\Controller;
  4. use Illuminate\Support\Facades\DB;
  5. use App\Model\Trivia;
  6. use App\Model\Points;
  7. use App\Model\TriviaAnswer;
  8. use App\Model\PointLog;
  9. use App\User;
  10. use Illuminate\Support\Facades\Auth;
  11. use Helper;
  12. use Request;
  13. use Validator;
  14. class TriviaController extends Controller
  15. {
  16. public function __construct()
  17. {
  18. DB::enableQueryLog();
  19. }
  20. public function getAll()
  21. {
  22. $query = Trivia::get();
  23. $query = array_map(function ($item) {
  24. $item['answer'] = TriviaAnswer::where('trivia_id', $item['id'])->get();
  25. return $item;
  26. }, $query->toArray());
  27. if($query){
  28. $result = $query;
  29. $res_status = true;
  30. $msg = 'Mendapatkan Data';
  31. $status_msg = $msg;
  32. return $this->resSuccess(null, $res_status, $msg, $status_msg, $result);
  33. }else {
  34. $res_status = false;
  35. $msg = 'Data tidak ditemukan';
  36. $status_msg = $msg;
  37. return $this->resSuccess(null, $res_status, $msg, $status_msg, null);
  38. }
  39. }
  40. public function getSingle($data_id)
  41. {
  42. $query = Trivia::where('id', Helper::hash($data_id, 'decode'))->get();
  43. $query = array_map(function ($item) {
  44. $item['answer'] = TriviaAnswer::where('trivia_id', $item['id'])->get();
  45. return $item;
  46. }, $query->toArray());
  47. if($query){
  48. $result = $query;
  49. $res_status = true;
  50. $msg = 'Mendapatkan Data';
  51. $status_msg = $msg;
  52. return $this->resSuccess(null, $res_status, $msg, $status_msg, $result);
  53. }else {
  54. $res_status = false;
  55. $msg = 'Data tidak ditemukan';
  56. $status_msg = $msg;
  57. return $this->resSuccess(null, $res_status, $msg, $status_msg, null);
  58. }
  59. }
  60. public function postSubmitAnswer()
  61. {
  62. $validator = Validator::make(Request::all(), [
  63. 'user_id' => ['required'],
  64. 'question_id' => ['required'],
  65. 'answer_id' => ['required'],
  66. ]);
  67. if(!$validator->fails()){
  68. $trivia_answer = TriviaAnswer::find(Helper::hash(Request::all()['answer_id'], 'decode'));
  69. if (($trivia_answer->trivia_id == Helper::hash(Request::all()['question_id'], 'decode')) && ($trivia_answer->is_right == 't')) {
  70. // Start Perhitungan
  71. $pointLog = new PointLog();
  72. $pointLog->user_id = Helper::hash(Request::all()['user_id'], 'decode');
  73. $pointLog->type_point = 'trivia';
  74. $pointLog->point = Points::where('type', 'trivia')->pluck('value')->first();
  75. $pointLog->save();
  76. // End Perhitungan
  77. $pointLog->user_id = Helper::hash($pointLog->user_id, 'encode');
  78. $pointLog->answer_id = Helper::hash($pointLog->answer_id, 'encode');
  79. $pointLog->question_id = Helper::hash($pointLog->question_id, 'encode');
  80. $pointLog->hash_id = Helper::hash($pointLog->id, 'encode');
  81. $result = $pointLog;
  82. $res_status = true;
  83. $msg = 'Jawaban Benar';
  84. $status_msg = $msg;
  85. return $this->resSuccess(null, $res_status, $msg, $status_msg, $result);
  86. }else{
  87. $res_status = false;
  88. $msg = 'Jawaban Salah';
  89. $status_msg = $msg;
  90. return $this->resSuccess(null, $res_status, $msg, $status_msg, null);
  91. }
  92. }else {
  93. $res_status = false;
  94. $msg = 'Jawaban Salah';
  95. $status_msg = $validator->errors();
  96. return $this->resSuccess(null, $res_status, $msg, $status_msg, null);
  97. }
  98. }
  99. public function resSuccess($param = null, $status = null, $msg = null, $status_msg = null, $result = null)
  100. {
  101. $response['response'] = array(
  102. 'status' => $status,
  103. 'message' => $msg,
  104. 'status_msg' => $status_msg,
  105. );
  106. $response['param'] = !empty($param) ? $param : '';
  107. $response['results'] = $result;
  108. return response()->json($response, 200);
  109. }
  110. }