laravel-learn-bbs/app/Http/Controllers/Api/TopicsController.php

70 lines
2.0 KiB
PHP
Raw Permalink Normal View History

2018-06-10 10:04:03 +00:00
<?php
namespace App\Http\Controllers\Api;
use App\Http\Requests\Api\TopicRequest;
use App\Models\Topic;
2018-06-10 11:28:01 +00:00
use App\Models\User;
2018-06-10 10:04:03 +00:00
use App\Transformers\TopicTransformer;
2018-06-10 11:28:01 +00:00
use Illuminate\Http\Request;
2018-06-10 10:04:03 +00:00
class TopicsController extends Controller
{
public function store(TopicRequest $request, Topic $topic)
{
$topic->fill($request->all());
$topic->user_id = $this->user()->id;
$topic->save();
return $this->response->item($topic, new TopicTransformer())->setStatusCode(201);
}
2018-06-10 10:35:53 +00:00
/**
* @param TopicRequest $request
* @param Topic $topic
* @return \Dingo\Api\Http\Response
* @throws \Illuminate\Auth\Access\AuthorizationException
*/
public function update(TopicRequest $request, Topic $topic)
{
$this->authorize('update', $topic);
$topic->update($request->all());
return $this->response->item($topic, new TopicTransformer());
}
2018-06-10 11:28:01 +00:00
public function index(Request $request)
{
$query = Topic::query();
if ($category_id = $request->get('category_id', 0)) {
$query->where(['category_id', $category_id]);
}
$order = $request->get('order', 'recent');
$query->withOrder($query, $order);
$topics = $query->paginate(20);
return $this->response->paginator($topics, new TopicTransformer());
}
public function userIndex(User $user)
{
$topics = $user->topics()->recent()->paginate(20);
return $this->response->paginator($topics, new TopicTransformer());
}
2018-06-10 11:32:12 +00:00
public function show(Topic $topic)
{
return $this->response->item($topic, new TopicTransformer());
}
2018-06-10 11:28:01 +00:00
/**
* @param Topic $topic
* @return \Dingo\Api\Http\Response
* @throws \Illuminate\Auth\Access\AuthorizationException
* @throws \Throwable
*/
public function destroy(Topic $topic)
{
$this->authorize('update', $topic);
$topic->delete();
return $this->response->noContent();
}
2018-06-10 10:04:03 +00:00
}