123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- <?php
-
- namespace App\Http\Controllers\Api\V1;
-
- use App\Http\Controllers\Controller;
- use Illuminate\Support\Facades\DB;
- use App\Model\Trivia;
- use App\Model\Points;
- use App\Model\TriviaAnswer;
- use App\Model\PointLog;
- use App\User;
- use Illuminate\Support\Facades\Auth;
- use Helper;
- use Request;
- use Validator;
-
- class TriviaController extends Controller
- {
- public function __construct()
- {
- DB::enableQueryLog();
- }
-
- public function getAll()
- {
- $query = Trivia::get();
- $query = array_map(function ($item) {
- $item['answer'] = TriviaAnswer::where('trivia_id', $item['id'])->get();
- return $item;
- }, $query->toArray());
-
- if($query){
- $result = $query;
-
- $res_status = true;
- $msg = 'Mendapatkan Data';
- $status_msg = $msg;
-
- return $this->resSuccess(null, $res_status, $msg, $status_msg, $result);
-
- }else {
-
- $res_status = false;
- $msg = 'Data tidak ditemukan';
- $status_msg = $msg;
-
- return $this->resSuccess(null, $res_status, $msg, $status_msg, null);
- }
- }
-
- public function getSingle($data_id)
- {
- $query = Trivia::where('id', Helper::hash($data_id, 'decode'))->get();
- $query = array_map(function ($item) {
- $item['answer'] = TriviaAnswer::where('trivia_id', $item['id'])->get();
- return $item;
- }, $query->toArray());
-
- if($query){
- $result = $query;
-
- $res_status = true;
- $msg = 'Mendapatkan Data';
- $status_msg = $msg;
-
- return $this->resSuccess(null, $res_status, $msg, $status_msg, $result);
-
- }else {
-
- $res_status = false;
- $msg = 'Data tidak ditemukan';
- $status_msg = $msg;
-
- return $this->resSuccess(null, $res_status, $msg, $status_msg, null);
- }
- }
-
- public function postSubmitAnswer()
- {
- $validator = Validator::make(Request::all(), [
- 'user_id' => ['required'],
- 'question_id' => ['required'],
- 'answer_id' => ['required'],
- ]);
-
- if(!$validator->fails()){
- $trivia_answer = TriviaAnswer::find(Helper::hash(Request::all()['answer_id'], 'decode'));
- if (($trivia_answer->trivia_id == Helper::hash(Request::all()['question_id'], 'decode')) && ($trivia_answer->is_right == 't')) {
- // Start Perhitungan
- $pointLog = new PointLog();
- $pointLog->user_id = Helper::hash(Request::all()['user_id'], 'decode');
- $pointLog->type_point = 'trivia';
- $pointLog->point = Points::where('type', 'trivia')->pluck('value')->first();
- $pointLog->save();
- // End Perhitungan
-
- $pointLog->user_id = Helper::hash($pointLog->user_id, 'encode');
- $pointLog->answer_id = Helper::hash($pointLog->answer_id, 'encode');
- $pointLog->question_id = Helper::hash($pointLog->question_id, 'encode');
- $pointLog->hash_id = Helper::hash($pointLog->id, 'encode');
-
- $result = $pointLog;
- $res_status = true;
- $msg = 'Jawaban Benar';
- $status_msg = $msg;
-
- return $this->resSuccess(null, $res_status, $msg, $status_msg, $result);
- }else{
- $res_status = false;
- $msg = 'Jawaban Salah';
- $status_msg = $msg;
-
- return $this->resSuccess(null, $res_status, $msg, $status_msg, null);
- }
- }else {
-
- $res_status = false;
- $msg = 'Jawaban Salah';
- $status_msg = $validator->errors();
-
- return $this->resSuccess(null, $res_status, $msg, $status_msg, null);
- }
- }
-
- public function resSuccess($param = null, $status = null, $msg = null, $status_msg = null, $result = null)
- {
- $response['response'] = array(
- 'status' => $status,
- 'message' => $msg,
- 'status_msg' => $status_msg,
- );
- $response['param'] = !empty($param) ? $param : '';
-
- $response['results'] = $result;
-
- return response()->json($response, 200);
- }
-
- }
|