Created API endpoints
This commit is contained in:
108
app/Http/Controllers/QuestionAnswerController.php
Normal file
108
app/Http/Controllers/QuestionAnswerController.php
Normal file
@@ -0,0 +1,108 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use App\Http\Resources\AnswerResource;
|
||||
use App\Transformers\AnswerTransformer;
|
||||
use App\Models\Answer;
|
||||
use App\Models\Question;
|
||||
use Dingo\Api\Routing\Helpers;
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
|
||||
class QuestionAnswerController extends Controller
|
||||
{
|
||||
use Helpers;
|
||||
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function index($questionId)
|
||||
{
|
||||
$question = Question::findOrFail($questionId);
|
||||
// return $this->response
|
||||
// ->item($question, new QuestionTransformer, ['key' => 'questions']);
|
||||
|
||||
$answer = $question->answers()->get();
|
||||
|
||||
return AnswerResource::collection($answer);
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for creating a new resource.
|
||||
*
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function create()
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function store(Request $request)
|
||||
{
|
||||
// return $request;
|
||||
$question = Question::find($request->question_id);
|
||||
if ($question == null) {
|
||||
throw new NotFoundHttpException("Question not found.");
|
||||
|
||||
}
|
||||
$answer = $question->answers()->create($request->all());
|
||||
|
||||
// return $this->response
|
||||
// ->item($answer, new AnswerTransformer, ['key' => 'answers']);
|
||||
return new AnswerResource($answer);
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the specified resource.
|
||||
*
|
||||
* @param int $id
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function show($id)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for editing the specified resource.
|
||||
*
|
||||
* @param int $id
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function edit($id)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @param int $id
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function update(Request $request, $id)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
*
|
||||
* @param int $id
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function destroy($id)
|
||||
{
|
||||
//
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user