PageRenderTime 41ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/app/Http/Controllers/QuestionsController.php

https://gitlab.com/daniel.setreus/quiz-app-api
PHP | 106 lines | 84 code | 20 blank | 2 comment | 25 complexity | 63b01d90788f655252d67b563bee3c7b MD5 | raw file
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use App\Http\Requests;
  5. use Illuminate\Http\JsonResponse;
  6. use DB;
  7. use App\Http\Requests\CreateQuestionsRequest;
  8. use App\Quiz;
  9. use App\Question;
  10. use App\Choice;
  11. class QuestionsController extends ApiBaseController
  12. {
  13. public function create($customer, $project, $quiz, CreateQuestionsRequest $request) {
  14. $quizObj = $this->getQuizObject($customer, $project, $quiz);
  15. if(!$quizObj)
  16. return $this->response->errorNotFound('Endpoint error: Check the URL');
  17. DB::beginTransaction();
  18. try {
  19. $question = new Question($request->all());
  20. $question->save();
  21. $quizObj->questions()->attach(array($question->id));
  22. if(isset($request->choices)) {
  23. $choices = json_decode($request->choices, true);
  24. if(!is_array($choices))
  25. throw new \Exception("Choices is not an array", 1);
  26. foreach($choices as $choice) {
  27. if(!array_key_exists('title', $choice) || !array_key_exists('isCorrect', $choice))
  28. throw new \Exception("Atleast one choise is missing value for title or isCorrect", 1);
  29. if(!is_bool($choice['isCorrect']))
  30. throw new \Exception("Value for isCorrect must be of type boolean");
  31. $c = new Choice($choice);
  32. $question->choices()->save($c);
  33. }
  34. }
  35. } catch (\Exception $e) {
  36. DB::rollback();
  37. return new JsonResponse(
  38. array(
  39. 'error' => 'Error saving question',
  40. 'message' => $e->getMessage(),
  41. 'request' => $request->all()
  42. ), 400);
  43. }
  44. DB::commit();
  45. return response()->json($question->toArray());
  46. }
  47. public function listQuestions($customer, $project, $quiz) {
  48. $quizObj = $this->getQuizObject($customer, $project, $quiz);
  49. if(!$quizObj)
  50. return $this->response->errorNotFound('Endpoint error: Check the URL');
  51. return response()->json($quizObj->questions->toArray());
  52. }
  53. public function viewQuestion($customer, $project, $quiz, $question) {
  54. $question = Question::whereHas('quiz', function($query) use($quiz) {
  55. if(is_numeric($quiz)) $key = 'quizzes.id';
  56. else $key = 'slug';
  57. $query->where($key, '=', $quiz);
  58. })
  59. ->whereHas('quiz.project', function($query) use($project) {
  60. if(is_numeric($project)) $key = 'projects.id';
  61. else $key = 'slug';
  62. $query->where($key, '=', $project);
  63. })
  64. ->whereHas('quiz.project.customer', function($query) use($customer) {
  65. if(is_numeric($customer)) $key = 'customers.id';
  66. else $key = 'slug';
  67. $query->where($key, '=', $customer);
  68. })
  69. ->with('choices')->find($question);
  70. if(!$question)
  71. return $this->response->errorNotFound('Endpoint error: Check the URL');
  72. return response()->json($question->toArray());
  73. }
  74. private function getQuizObject($customer, $project, $quiz) {
  75. $quizObj = Quiz::with('project.customer', 'questions.choices')->where('id', '=', $quiz)->orWhere('slug', '=', $quiz)->first();
  76. if(!$quizObj)
  77. return false;
  78. // Test if project match
  79. if(!($quizObj->project->id == $project || $quizObj->project->slug == $project))
  80. return false;
  81. // Test if customer match
  82. if(!($quizObj->project->customer->id == $customer || $quizObj->project->customer->slug == $customer))
  83. return false;
  84. return $quizObj;
  85. }
  86. }