Created API endpoints

This commit is contained in:
Gregorio Chiko Putra 2017-12-20 16:45:20 +07:00
parent ad11643854
commit 554c74481b
53 changed files with 4637 additions and 9 deletions

View File

@ -0,0 +1,95 @@
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Answer;
use App\Transformers\AnswerTransformer;
use App\Http\Resources\AnswerResource;
use Dingo\Api\Routing\Helpers;
class AnswerController extends Controller
{
use Helpers;
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$answers = Answer::all();
return AnswerResource::collection($answers);
}
/**
* 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)
{
//
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
$answer = Answer::findOrFail($id);
return $this->response
->item($answer, new AnswerTransformer, ['key' => 'answers']);
}
/**
* 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)
{
//
}
}

View File

@ -0,0 +1,95 @@
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Category;
use App\Http\Resources\CategoryResource;
use App\Transformers\CategoryTransformer;
use Dingo\Api\Routing\Helpers;
class CategoryController extends Controller
{
use Helpers;
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$categories = Category::all();
return CategoryResource::collection($categories);
}
/**
* 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)
{
//
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
$category = Category::findOrFail($id);
return $this->response
->item($category, new CategoryTransformer, ['key' => 'categories']);
}
/**
* 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)
{
//
}
}

View 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)
{
//
}
}

View File

@ -0,0 +1,92 @@
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Dingo\Api\Routing\Helpers;
use App\Models\Question;
use App\Transformers\QuestionTransformer;
class QuestionController extends Controller
{
use Helpers;
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
//
}
/**
* 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)
{
//
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
$question = Question::findOrFail($id);
return $this->response
->item($question, new QuestionTransformer, ['key' => 'questions']);
}
/**
* 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)
{
//
}
}

View File

@ -0,0 +1,97 @@
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Questionnaire;
use Dingo\Api\Routing\Helpers;
use App\Transformers\QuestionnaireTransformer;
use App\Http\Resources\QuestionnaireResource;
class QuestionnaireController extends Controller
{
use Helpers;
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$questionnaires = Questionnaire::all();
// return $this->response
// ->collection($questionnaires, new QuestionnaireResource, ['key' => 'questionnaires']);
return QuestionnaireResource::collection($questionnaires);
}
/**
* 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)
{
//
}
/**
* Display the specified resource.
*
* @param \App\Models\Questionnaire $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
$questionnaire = Questionnaire::findOrFail($id);
return $this->response
->item($questionnaire, new QuestionnaireTransformer, ['key' => 'questionnaires']);
}
/**
* Show the form for editing the specified resource.
*
* @param \App\Models\Questionnaire $id
* @return \Illuminate\Http\Response
*/
public function edit(Questionnaire $id)
{
//
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\Models\Questionnaire $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, Questionnaire $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* @param \App\Models\Questionnaire $id
* @return \Illuminate\Http\Response
*/
public function destroy(Questionnaire $id)
{
//
}
}

View File

@ -0,0 +1,96 @@
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Questionnaire;
use Dingo\Api\Routing\Helpers;
use App\Transformers\SectionTransformer;
use App\Http\Resources\SectionResource;
class QuestionnaireSectionController extends Controller
{
use Helpers;
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index($questionnaire)
{
$sections = Questionnaire::find($questionnaire)
->sections()->get();
// return $this->response
// ->paginator($sections, new SectionTransformer, ['key' => 'sections']);
return SectionResource::collection($sections);
}
/**
* 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)
{
//
}
/**
* Display the specified resource.
*
* @param \App\Model\Questionnaire $questionnaire
* @return \Illuminate\Http\Response
*/
public function show(Questionnaire $questionnaire, Section $section)
{
//
echo "You've requested section with questionnaire id:" . $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)
{
//
}
}

View File

@ -0,0 +1,95 @@
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Respondent;
use App\Http\Resources\RespondentResource;
use App\Transformers\RespondentTransformer;
use Dingo\Api\Routing\Helpers;
class RespondentController extends Controller
{
use Helpers;
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$respondents = Respondent::all();
return RespondentResource::collection($respondents);
}
/**
* 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)
{
//
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
$respondent = Respondent::findOrFail($id);
return $this->response
->item($respondent, new RespondentTransformer, ['key' => 'respondents']);
}
/**
* 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)
{
//
}
}

View File

@ -0,0 +1,92 @@
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Section;
use App\Transformers\SectionTransformer;
use Dingo\Api\Routing\Helpers;
class SectionController extends Controller
{
use Helpers;
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
//
}
/**
* 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)
{
//
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
$section = Section::findOrFail($id);
return $this->response
->item($section, new SectionTransformer, ['key' => 'sections']);
}
/**
* 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)
{
//
}
}

View File

@ -0,0 +1,96 @@
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Dingo\Api\Routing\Helpers;
use App\Models\Section;
use App\Models\Question;
use App\Transformers\QuestionTransformer;
class SectionQuestionController extends Controller
{
use Helpers;
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index($section)
{
$question = Section::find($section)
->questions()->paginate(1);
return $this->response
->paginator($question, new QuestionTransformer, ['key' => 'questions']);
// return QuestionTransformer::collection($questions);
}
/**
* 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)
{
//
}
/**
* 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)
{
//
}
}

View File

@ -0,0 +1,95 @@
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Dingo\Api\Routing\Helpers;
use App\Models\User;
use App\Transformers\UserTransformer;
use App\Http\Resources\UserResource;
class UserController extends Controller
{
use Helpers;
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$users = User::all();
return UserResource::collection($users);
}
/**
* 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)
{
//
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
$user = User::findOrFail($id);
return $this->response
->item($user, new UserTransformer, ['key' => 'users']);
}
/**
* 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)
{
//
}
}

View File

@ -19,6 +19,7 @@ class Kernel extends HttpKernel
\App\Http\Middleware\TrimStrings::class,
\Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
\App\Http\Middleware\TrustProxies::class,
\App\Http\Middleware\ModifyHeadersMiddleware::class,
];
/**

View File

@ -0,0 +1,23 @@
<?php
namespace App\Http\Middleware;
use Closure;
class ModifyHeadersMiddleware
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
$response = $next($request);
$response->header('Access-Control-Allow-Origin', '*');
$response->header('Access-Control-Allow-Headers', 'Origin, Content-Type');
return $response;
}
}

View File

@ -0,0 +1,30 @@
<?php
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\Resource;
class AnswerResource extends Resource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request
* @return array
*/
public function toArray($request)
{
return [
'id' => $this->id,
'choice' => $this->questionchoice,
'text' => $this->text,
'links' => [
'self' => route('answers.show', ['answers' => $this->id]),
'relatedTo' => [
'question' => route('questions.show', ['questions' => $this->question_id]),
'respondent' => route('respondents.show', ['respondents' => $this->respondent_id])
]
]
];
}
}

View File

@ -0,0 +1,27 @@
<?php
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\Resource;
class CategoryResource extends Resource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request
* @return array
*/
public function toArray($request)
{
// return parent::toArray($request);
return [
'id' => $this->id,
'name' => $this->name,
'code' => $this->code,
'links' => [
'self' => route('categories.show', ['categories' => $this->id])
]
];
}
}

View File

@ -0,0 +1,29 @@
<?php
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\Resource;
class QuestionResource extends Resource
{
/**
* Transform the resource collection into an array.
*
* @param \Illuminate\Http\Request
* @return array
*/
public function toArray($request)
{
// return parent::toArray($request);
return [
'id' => $this->id,
'text' => $this->text,
'number' => $this->number,
'description' => $this->description,
'section' => new SectionResource($this->section),
'links' => [
'self' => route('questions.show', ['questions' => $this->id])
]
];
}
}

View File

@ -0,0 +1,28 @@
<?php
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\Resource;
class QuestionchoiceResource extends Resource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request
* @return array
*/
public function toArray($request)
{
return [
'id' => $this->id,
'number' => $this->number,
'text' => $this->text,
'fillable' => $this->fillable,
'description' => $this->description,
'links' => [
'relatedTo' => route('questions.show', ['questions' => $this->question_id])
]
];
}
}

View File

@ -0,0 +1,28 @@
<?php
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\Resource;
class QuestionnaireResource extends Resource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request
* @return array
*/
public function toArray($request)
{
// return parent::toArray($request);
return [
'id' => $this->id,
'category' => new CategoryResource($this->category),
'title' => $this->title,
'description' => $this->description,
'links' => [
'self' => route('questionnaires.show', ['questionnaires' => $this->id])
]
];
}
}

View File

@ -0,0 +1,27 @@
<?php
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\Resource;
class RespondentResource extends Resource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request
* @return array
*/
public function toArray($request)
{
return [
'id' => $this->id,
'name' => $this->name,
'occupation' => $this->occupation,
'gender' => $this->gender,
'links' => [
'self' => route('respondents.show', ['respondents' => $this->id])
]
];
}
}

View File

@ -0,0 +1,27 @@
<?php
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\Resource;
class SectionResource extends Resource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request
* @return array
*/
public function toArray($request)
{
// return parent::toArray($request);
return [
'id' => $this->id,
'title' => $this->title,
'description' => $this->description,
'links' => [
'self' => route('sections.show', ['sections' => $this->id])
]
];
}
}

View File

@ -0,0 +1,25 @@
<?php
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\Resource;
class UserResource extends Resource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request
* @return array
*/
public function toArray($request)
{
// return parent::toArray($request);
return [
'id' => $this->id,
'username' => $this->username,
'name' => $this->name,
'email' => $this->email
];
}
}

45
app/Models/Answer.php Normal file
View File

@ -0,0 +1,45 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Answer extends Model
{
use SoftDeletes;
/**
* The attributes that should be mutated to dates.
*
* @var array
*/
protected $dates = [
'updated_at',
'created_at',
'deleted_at'
];
protected $fillable = [
'choice',
'text',
'respondent_id',
'question_id',
'questionchoice_id'
];
public function question()
{
return $this->belongsTo(Question::class);
}
public function questionchoice()
{
return $this->belongsTo(Questionchoice::class);
}
public function respondent()
{
return $this->belongsTo(Respondent::class);
}
}

27
app/Models/Category.php Normal file
View File

@ -0,0 +1,27 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Category extends Model
{
use SoftDeletes;
/**
* The attributes that should be mutated to dates.
*
* @var array
*/
protected $dates = [
'updated_at',
'created_at',
'deleted_at'
];
public function creator()
{
return $this->belongsTo(User::class, 'creator_id');
}
}

42
app/Models/Question.php Normal file
View File

@ -0,0 +1,42 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Question extends Model
{
use SoftDeletes;
/**
* The attributes that should be mutated to dates.
*
* @var array
*/
protected $dates = [
'updated_at',
'created_at',
'deleted_at'
];
public function section()
{
return $this->belongsTo(Section::class);
}
public function choices()
{
return $this->hasMany(Questionchoice::class);
}
public function creator()
{
return $this->belongsTo(User::class);
}
public function answers()
{
return $this->hasMany(Answer::class);
}
}

View File

@ -0,0 +1,27 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Questionchoice extends Model
{
use SoftDeletes;
/**
* The attributes that should be mutated to dates.
*
* @var array
*/
protected $dates = [
'updated_at',
'created_at',
'deleted_at'
];
public function question()
{
return $this->belongsTo(Question::class);
}
}

View File

@ -0,0 +1,45 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Questionnaire extends Model
{
use SoftDeletes;
/**
* The attributes that should be mutated to dates.
*
* @var array
*/
protected $dates = [
'updated_at',
'released_at',
'created_at',
'closed_at',
'reviewed_at',
'deleted_at'
];
public function sections()
{
return $this->hasMany(Section::class);
}
public function questions()
{
return $this->hasManyThrough(Question::class, Section::class);
}
public function category()
{
return $this->belongsTo(Category::class);
}
public function creator()
{
return $this->belongsTo(User::class);
}
}

27
app/Models/Respondent.php Normal file
View File

@ -0,0 +1,27 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Respondent extends Model
{
use SoftDeletes;
/**
* The attributes that should be mutated to dates.
*
* @var array
*/
protected $dates = [
'updated_at',
'created_at',
'deleted_at'
];
public function category()
{
return $this->belongsTo(Category::class);
}
}

42
app/Models/Section.php Normal file
View File

@ -0,0 +1,42 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Section extends Model
{
use SoftDeletes;
/**
* The attributes that should be mutated to dates.
*
* @var array
*/
protected $dates = [
'updated_at',
'created_at',
'deleted_at'
];
public function questionnaire()
{
return $this->belongsTo(Questionnaire::class);
}
public function questions()
{
return $this->hasMany(Question::class);
}
public function choices()
{
return $this->hasManyThrough(Choice::class, Question::class);
}
public function creator()
{
return $this->belongsTo(User::class);
}
}

View File

@ -1,14 +1,30 @@
<?php
namespace App;
namespace App\Models;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Database\Eloquent\SoftDeletes;
class User extends Authenticatable
{
use SoftDeletes;
/**
* The attributes that should be mutated to dates.
*
* @var array
*/
protected $dates = [
'updated_at',
'created_at',
'deleted_at'
];
use Notifiable;
protected $table = "users";
/**
* The attributes that are mass assignable.
*

View File

@ -0,0 +1,35 @@
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class JSONApiServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*
* @return void
*/
public function boot()
{
//
}
/**
* Register the application services.
*
* @return void
*/
public function register()
{
$this->app['Dingo\Api\Transformer\Factory']->setAdapter(function ($app) {
$baseUrl = 'http://api.questionnaire.dev/v1';
$manager = new \League\Fractal\Manager;
$manager->setSerializer(new \League\Fractal\Serializer\JsonApiSerializer($baseUrl));
return new \Dingo\Api\Transformer\Adapter\Fractal($manager, 'include', ',');
});
}
}

View File

@ -0,0 +1,30 @@
<?php
namespace App\Transformers;
use App\Models\Answer as Model;
use League\Fractal\TransformerAbstract;
use App\Http\Resources\QuestionResource;
use App\Http\Resources\RespondentResource;
use App\Http\Resources\QuestionchoiceResource;
// use App\Http\Resources\UserResource;
class AnswerTransformer extends TransformerAbstract
{
/**
* @param Model $model
* @return array
*/
public function transform(Model $model)
{
return [
'id' => $model->id,
'text' => $model->text,
'question' => new QuestionResource($model->question),
'respondent' => new RespondentResource($model->respondent),
'choice' => new QuestionchoiceResource($model->questionchoice),
'createdAt' => $model->created_at->toIso8601String(),
'updatedAt' => $model->updated_at->toIso8601String()
];
}
}

View File

@ -0,0 +1,29 @@
<?php
namespace App\Transformers;
use App\Models\Category as Model;
use League\Fractal\TransformerAbstract;
use App\Http\Resources\UserResource;
class CategoryTransformer extends TransformerAbstract
{
/**
* @param Model $model
* @return array
*/
public function transform(Model $model)
{
return [
'id' => $model->id,
'name' => $model->name,
'code' => $model->code,
'createdAt' => $model->created_at->toIso8601String(),
'updatedAt' => $model->updated_at->toIso8601String(),
'creator' => new UserResource($model->creator),
'links' => [
'relatedTo' => route('categories.index')
]
];
}
}

View File

@ -0,0 +1,35 @@
<?php
namespace App\Transformers;
use App\Models\Question as Model;
use League\Fractal\TransformerAbstract;
use App\Http\Resources\SectionResource;
use App\Http\Resources\UserResource;
class QuestionTransformer extends TransformerAbstract
{
/**
* @param Model $model
* @return array
*/
public function transform(Model $model)
{
return [
'createdAt' => $model->created_at->toIso8601String(),
'updatedAt' => $model->updated_at->toIso8601String(),
'id' => $model->id,
'text' => $model->text,
'description' => $model->description,
'section' => new SectionResource($model->section),
'number' => $model->number,
'expectedLength' => $model->expected_length,
'questionType' => $model->question_type,
'choices' => $model->choices,
'creator' => new UserResource($model->creator),
'links' => [
'relatedTo' => route('questions.index')
]
];
}
}

View File

@ -0,0 +1,24 @@
<?php
namespace App\Transformers;
use App\Models\Questionchoice as Model;
use League\Fractal\TransformerAbstract;
class QuestionchoiceTransformer extends TransformerAbstract
{
/**
* @param Model $model
* @return array
*/
public function transform(Model $model)
{
return [
'id' => $model->id,
'number' => $model->number,
'text' => $model->text,
'fillable' => $model->fillable,
'description' => $model->description
];
}
}

View File

@ -0,0 +1,38 @@
<?php
namespace App\Transformers;
use App\Models\Questionnaire as Model;
use App\Models\User;
use App\Models\Category;
use League\Fractal\TransformerAbstract;
use App\Http\Resources\CategoryResource;
use App\Http\Resources\SectionResource;
use App\Http\Resources\UserResource;
class QuestionnaireTransformer extends TransformerAbstract
{
/**
* @param Model $model
* @return array
*/
public function transform(Model $model)
{
return [
'createdAt' => $model->created_at->toIso8601String(),
'updatedAt' => $model->updated_at->toIso8601String(),
'id' => $model->id,
'category' => new CategoryResource($model->category),
'title' => $model->title,
'description' => $model->description,
'releasedAt' => $model->released_at,
'closedAt' => $model->closed_at,
'reviewedAt' => $model->reviewed_at,
'sections' => SectionResource::collection($model->sections, ['key' => 'sections']),
'creator' => $model->creator,
'links' => [
'relatedTo' => route('questionnaires.index')
]
];
}
}

View File

@ -0,0 +1,30 @@
<?php
namespace App\Transformers;
use App\Models\Respondent as Model;
use League\Fractal\TransformerAbstract;
use App\Http\Resources\CategoryResource;
class RespondentTransformer extends TransformerAbstract
{
/**
* @param Model $model
* @return array
*/
public function transform(Model $model)
{
return [
'id' => $model->id,
'name' => $model->name,
'occupation' => $model->occupation,
'gender' => $model->gender,
'birthdate' => $model->birthdate,
'email' => $model->email,
'handphone' => $model->handphone,
'category' => new CategoryResource($model->category),
'createdAt' => $model->created_at->toIso8601String(),
'updatedAt' => $model->updated_at->toIso8601String()
];
}
}

View File

@ -0,0 +1,33 @@
<?php
namespace App\Transformers;
use App\Models\Section as Model;
use League\Fractal\TransformerAbstract;
use App\Http\Resources\QuestionnaireResource;
use App\Http\Resources\QuestionResource;
use App\Http\Resources\UserResource;
class SectionTransformer extends TransformerAbstract
{
/**
* @param Model $model
* @return array
*/
public function transform(Model $model)
{
return [
'id' => $model->id,
'title' => $model->title,
'description' => $model->description,
'questionnaire' => new QuestionnaireResource($model->questionnaire),
'questions' => QuestionResource::collection($model->questions()->orderBy('number')->get()),
'createdAt' => $model->created_at->toIso8601String(),
'updatedAt' => $model->updated_at->toIso8601String(),
'creator' => new UserResource($model->creator),
'links' => [
'relatedTo' => route('sections.index')
]
];
}
}

View File

@ -0,0 +1,25 @@
<?php
namespace App\Transformers;
use App\Models\User as Model;
use League\Fractal\TransformerAbstract;
class UserTransformer extends TransformerAbstract
{
/**
* @param Model $model
* @return array
*/
public function transform(Model $model)
{
return [
'createdAt' => $model->created_at->toIso8601String(),
'updatedAt' => $model->updated_at->toIso8601String(),
'id' => $model->id,
'username' => $model->username,
'name' => $model->name,
'email' => $model->email
];
}
}

233
config/api.php Normal file
View File

@ -0,0 +1,233 @@
<?php
return [
/*
|--------------------------------------------------------------------------
| Standards Tree
|--------------------------------------------------------------------------
|
| Versioning an API with Dingo revolves around content negotiation and
| custom MIME types. A custom type will belong to one of three
| standards trees, the Vendor tree (vnd), the Personal tree
| (prs), and the Unregistered tree (x).
|
| By default the Unregistered tree (x) is used, however, should you wish
| to you can register your type with the IANA. For more details:
| https://tools.ietf.org/html/rfc6838
|
*/
'standardsTree' => env('API_STANDARDS_TREE', 'x'),
/*
|--------------------------------------------------------------------------
| API Subtype
|--------------------------------------------------------------------------
|
| Your subtype will follow the standards tree you use when used in the
| "Accept" header to negotiate the content type and version.
|
| For example: Accept: application/x.SUBTYPE.v1+json
|
*/
'subtype' => env('API_SUBTYPE', ''),
/*
|--------------------------------------------------------------------------
| Default API Version
|--------------------------------------------------------------------------
|
| This is the default version when strict mode is disabled and your API
| is accessed via a web browser. It's also used as the default version
| when generating your APIs documentation.
|
*/
'version' => env('API_VERSION', 'v1'),
/*
|--------------------------------------------------------------------------
| Default API Prefix
|--------------------------------------------------------------------------
|
| A default prefix to use for your API routes so you don't have to
| specify it for each group.
|
*/
'prefix' => env('API_PREFIX', "v1"),
/*
|--------------------------------------------------------------------------
| Default API Domain
|--------------------------------------------------------------------------
|
| A default domain to use for your API routes so you don't have to
| specify it for each group.
|
*/
'domain' => env('API_DOMAIN', null),
/*
|--------------------------------------------------------------------------
| Name
|--------------------------------------------------------------------------
|
| When documenting your API using the API Blueprint syntax you can
| configure a default name to avoid having to manually specify
| one when using the command.
|
*/
'name' => env('API_NAME', null),
/*
|--------------------------------------------------------------------------
| Conditional Requests
|--------------------------------------------------------------------------
|
| Globally enable conditional requests so that an ETag header is added to
| any successful response. Subsequent requests will perform a check and
| will return a 304 Not Modified. This can also be enabled or disabled
| on certain groups or routes.
|
*/
'conditionalRequest' => env('API_CONDITIONAL_REQUEST', true),
/*
|--------------------------------------------------------------------------
| Strict Mode
|--------------------------------------------------------------------------
|
| Enabling strict mode will require clients to send a valid Accept header
| with every request. This also voids the default API version, meaning
| your API will not be browsable via a web browser.
|
*/
'strict' => env('API_STRICT', false),
/*
|--------------------------------------------------------------------------
| Debug Mode
|--------------------------------------------------------------------------
|
| Enabling debug mode will result in error responses caused by thrown
| exceptions to have a "debug" key that will be populated with
| more detailed information on the exception.
|
*/
'debug' => env('API_DEBUG', false),
/*
|--------------------------------------------------------------------------
| Generic Error Format
|--------------------------------------------------------------------------
|
| When some HTTP exceptions are not caught and dealt with the API will
| generate a generic error response in the format provided. Any
| keys that aren't replaced with corresponding values will be
| removed from the final response.
|
*/
'errorFormat' => [
'message' => ':message',
'errors' => ':errors',
'code' => ':code',
'status_code' => ':status_code',
'debug' => ':debug',
],
/*
|--------------------------------------------------------------------------
| API Middleware
|--------------------------------------------------------------------------
|
| Middleware that will be applied globally to all API requests.
|
*/
'middleware' => [
],
/*
|--------------------------------------------------------------------------
| Authentication Providers
|--------------------------------------------------------------------------
|
| The authentication providers that should be used when attempting to
| authenticate an incoming API request.
|
*/
'auth' => [
],
/*
|--------------------------------------------------------------------------
| Throttling / Rate Limiting
|--------------------------------------------------------------------------
|
| Consumers of your API can be limited to the amount of requests they can
| make. You can create your own throttles or simply change the default
| throttles.
|
*/
'throttling' => [
],
/*
|--------------------------------------------------------------------------
| Response Transformer
|--------------------------------------------------------------------------
|
| Responses can be transformed so that they are easier to format. By
| default a Fractal transformer will be used to transform any
| responses prior to formatting. You can easily replace
| this with your own transformer.
|
*/
'transformer' => env('API_TRANSFORMER', Dingo\Api\Transformer\Adapter\Fractal::class),
/*
|--------------------------------------------------------------------------
| Response Formats
|--------------------------------------------------------------------------
|
| Responses can be returned in multiple formats by registering different
| response formatters. You can also customize an existing response
| formatter with a number of options to configure its output.
|
*/
'defaultFormat' => env('API_DEFAULT_FORMAT', 'json'),
'formats' => [
'json' => Dingo\Api\Http\Response\Format\Json::class,
],
'formatsOptions' => [
'json' => [
'pretty_print' => env('API_JSON_FORMAT_PRETTY_PRINT_ENABLED', false),
'indent_style' => env('API_JSON_FORMAT_INDENT_STYLE', 'space'),
'indent_size' => env('API_JSON_FORMAT_INDENT_SIZE', 2),
],
],
];

View File

@ -177,6 +177,8 @@ return [
App\Providers\EventServiceProvider::class,
App\Providers\RouteServiceProvider::class,
App\Providers\JSONApiServiceProvider::class,
],
/*

View File

@ -15,11 +15,19 @@ class CreateUsersTable extends Migration
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->rememberToken();
$table->string('username', 60);
$table->string('name', 100);
$table->string('email', 100)
->unique();
$table->string('password', 41);
$table->timestamps();
$table->softDeletes();
});
}

View File

@ -0,0 +1,48 @@
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateCategoriesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('categories', function (Blueprint $table) {
$table->increments('id');
$table->string('code', 10);
$table->string('name', 100);
$table->integer('creator_id')
->unsigned()
->nullable();
$table->timestamps();
$table->softDeletes();
$table->foreign('creator_id')
->references('id')->on('users')
->onDeletes('restrict')
->onUpdate('cascade');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('categories');
}
}

View File

@ -0,0 +1,75 @@
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateQuestionnairesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('questionnaires', function (Blueprint $table) {
$table->increments('id');
$table->string('title', 100);
$table->text('description')
->nullable();
$table->integer('creator_id')
->unsigned();
$table->integer('category_id')
->unsigned()
->nullable();
$table->integer('reviewer_id')
->unsigned()
->nullable();
$table->timestamps();
$table->timestamp('released_at')
->nullable();
$table->timestamp('closed_at')
->nullable();
$table->timestamp('reviewed_at')
->nullable();
$table->softDeletes();
$table->foreign('creator_id')
->references('id')->on('users')
->onDelete('restrict')
->onUpdate('cascade');
$table->foreign('category_id')
->references('id')->on('categories')
->onDelete('restrict')
->onUpdate('cascade');
$table->foreign('reviewer_id')
->references('id')->on('users')
->onDelete('restrict')
->onUpdate('cascade');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('questionnaires');
}
}

View File

@ -0,0 +1,56 @@
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateSectionsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('sections', function (Blueprint $table) {
$table->increments('id');
$table->integer('questionnaire_id')
->unsigned();
$table->integer('creator_id')
->unsigned();
$table->string('title', 100);
$table->text('description')
->nullable();
$table->timestamps();
$table->softDeletes();
$table->foreign('questionnaire_id')
->references('id')->on('questionnaires')
->onDelete('restrict')
->onUpdate('cascade');
$table->foreign('creator_id')
->references('id')->on('users')
->onDelete('restrict')
->onUpdate('cascade');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('sections');
}
}

View File

@ -0,0 +1,70 @@
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateQuestionsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('questions', function (Blueprint $table) {
$table->increments('id');
$table->integer('section_id')
->unsigned();
$table->integer('creator_id')
->unsigned();
$table->integer('number');
$table->string('text', 300);
$table->text('description')
->nullable();
$table->integer('expected_length')
->nullable();
$table->enum('question_type', [
'text',
'choice',
'multichoice',
'numeric',
'bool'
])
->default('text');
$table->timestamps();
$table->softDeletes();
$table->foreign('section_id')
->references('id')->on('sections')
->onDelete('restrict')
->onUpdate('cascade');
$table->foreign('creator_id')
->references('id')->on('users')
->onDelete('restrict')
->onUpdate('cascade');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('questions');
}
}

View File

@ -0,0 +1,62 @@
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateQuestionchoicesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('questionchoices', function (Blueprint $table) {
$table->increments('id');
$table->integer('question_id')
->unsigned();
$table->integer('creator_id')
->unsigned()
->nullable();
$table->integer('number');
$table->string('text', 300);
$table->text('description')
->nullable();
$table->boolean('fillable')
->nullable();
$table->timestamps();
$table->softDeletes();
$table->foreign('question_id')
->references('id')->on('questions')
->onDelete('restrict')
->onUpdate('cascade');
$table->foreign('creator_id')
->references('id')->on('users')
->onDelete('restrict')
->onUpdate('cascade');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('questionchoices');
}
}

View File

@ -0,0 +1,57 @@
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateRespondentsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('respondents', function (Blueprint $table) {
$table->increments('id');
$table->string('name', 100);
$table->string('handphone', 100);
$table->string('email', 100)
->unique();
$table->enum('gender', ['m', 'f']);
$table->string('occupation', 100);
$table->date('birthdate');
$table->integer('category_id')
->unsigned()
->nullable();
$table->timestamps();
$table->softDeletes();
$table->foreign('category_id')
->references('id')->on('categories')
->onDeletes('restrict')
->onUpdate('cascade');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('respondents');
}
}

View File

@ -0,0 +1,64 @@
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateAnswersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('answers', function (Blueprint $table) {
$table->increments('id');
$table->integer('respondent_id')
->unsigned()
->nullable();
$table->integer('question_id')
->unsigned()
->nullable();
$table->text('text');
$table->integer('questionchoice_id')
->unsigned()
->nullable();
$table->timestamps();
$table->softDeletes();
$table->foreign('respondent_id')
->references('id')->on('respondents')
->onDeletes('restrict')
->onUpdate('cascade');
$table->foreign('question_id')
->references('id')->on('questions')
->onDeletes('restrict')
->onUpdate('cascade');
$table->foreign('questionchoice_id')
->references('id')->on('questionchoices')
->onDeletes('restrict')
->onUpdate('cascade');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('answers');
}
}

View File

@ -11,6 +11,8 @@ class DatabaseSeeder extends Seeder
*/
public function run()
{
// $this->call(UsersTableSeeder::class);
$this->call(UsersTableSeeder::class);
$this->call(QuestionnairesSeeder::class);
$this->call(RespondentSeeder::class);
}
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,25 @@
<?php
use Illuminate\Database\Seeder;
use App\Models\Respondent;
class RespondentSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
Respondent::create([
'name' => 'respondent1',
'handphone' => '0123456789',
'email' => 'respondent1@company.com',
'gender' => 'm',
'occupation' => 'staff',
'birthdate' => '2017/01/01',
'category_id' => '1'
]);
}
}

View File

@ -0,0 +1,22 @@
<?php
use Illuminate\Database\Seeder;
use App\Models\User;
class UsersTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
User::create([
'username' => 'user1',
'name' => 'User 1',
'email' => 'user1@company.com',
'password' => 'user1'
]);
}
}

View File

@ -13,6 +13,26 @@ use Illuminate\Http\Request;
|
*/
Route::middleware('auth:api')->get('/user', function (Request $request) {
return $request->user();
// Route::middleware('auth:api')->get('/user', function (Request $request) {
// return $request->user();
// });
$api = app('Dingo\Api\Routing\Router');
// $api->version('v1', function ($api) {
// $api->get('questionnaires', function($id) {
// echo $id;
// });
// });
$api->version('v1', function ($api) {
$api->resource('questionnaires', 'App\Http\Controllers\QuestionnaireController');
$api->resource('questionnaires/{questionnaire}/sections', 'App\Http\Controllers\QuestionnaireSectionController');
$api->resource('sections', 'App\Http\Controllers\SectionController');
$api->resource('sections/{section}/questions', 'App\Http\Controllers\SectionQuestionController');
$api->resource('questions', 'App\Http\Controllers\QuestionController');
$api->resource('questions/{question}/answers', 'App\Http\Controllers\QuestionAnswerController');
$api->resource('categories', 'App\Http\Controllers\CategoryController');
$api->resource('answers', 'App\Http\Controllers\AnswerController');
$api->resource('respondents', 'App\Http\Controllers\RespondentController');
$api->resource('users', 'App\Http\Controllers\UserController');
});

View File

@ -14,7 +14,7 @@ class ViewingQuestionnaireTest extends TestCase
*/
public function testICanViewListOfQuestionnaire()
{
$response = $this->get('/v0/questionnaire');
$response = $this->get('/v1/questionnaires');
$response->assertStatus(200);
}