发布话题
This commit is contained in:
parent
b908987973
commit
ef1a29cd40
14
app/Http/Controllers/Api/CategoriesController.php
Normal file
14
app/Http/Controllers/Api/CategoriesController.php
Normal file
|
@ -0,0 +1,14 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api;
|
||||
|
||||
use App\Models\Category;
|
||||
use App\Transformers\CategoryTransformer;
|
||||
|
||||
class CategoriesController extends Controller
|
||||
{
|
||||
public function index()
|
||||
{
|
||||
return $this->response->collection(Category::all(), new CategoryTransformer());
|
||||
}
|
||||
}
|
18
app/Http/Controllers/Api/TopicsController.php
Normal file
18
app/Http/Controllers/Api/TopicsController.php
Normal file
|
@ -0,0 +1,18 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api;
|
||||
|
||||
use App\Http\Requests\Api\TopicRequest;
|
||||
use App\Models\Topic;
|
||||
use App\Transformers\TopicTransformer;
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
43
app/Http/Requests/Api/TopicRequest.php
Normal file
43
app/Http/Requests/Api/TopicRequest.php
Normal file
|
@ -0,0 +1,43 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Requests\Api;
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
/**
|
||||
* Class TopicRequest
|
||||
* @property string title
|
||||
* @property string body
|
||||
* @property int category_id
|
||||
* @package App\Http\Requests\Api
|
||||
*/
|
||||
class TopicRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function authorize()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public function rules()
|
||||
{
|
||||
return [
|
||||
'title' => 'required|string',
|
||||
'body' => 'required|string',
|
||||
'category_id' => 'required|exists:categories,id',
|
||||
];
|
||||
}
|
||||
|
||||
public function attributes()
|
||||
{
|
||||
return [
|
||||
'title' => '标题',
|
||||
'body' => '话题内容',
|
||||
'category_id' => '分类',
|
||||
];
|
||||
}
|
||||
}
|
|
@ -2,6 +2,13 @@
|
|||
|
||||
namespace App\Models;
|
||||
|
||||
/**
|
||||
* Class Category
|
||||
* @property int $id
|
||||
* @property string $name
|
||||
* @property string $description
|
||||
* @package App\Models
|
||||
*/
|
||||
class Category extends Model
|
||||
{
|
||||
protected $guarded = ['id'];
|
||||
|
|
|
@ -2,6 +2,22 @@
|
|||
|
||||
namespace App\Models;
|
||||
|
||||
/**
|
||||
* Class Topic
|
||||
* @property int id
|
||||
* @property string title
|
||||
* @property int category_id
|
||||
* @property int user_id
|
||||
* @property int reply_count
|
||||
* @property int view_count
|
||||
* @property int last_reply_user_id
|
||||
* @property string body
|
||||
* @property string slug
|
||||
* @property string excerpt
|
||||
* @property string created_at
|
||||
* @property string updated_at
|
||||
* @package App\Models
|
||||
*/
|
||||
class Topic extends Model
|
||||
{
|
||||
protected $fillable = ['title', 'category_id', 'body', 'excerpt', 'slug'];
|
||||
|
|
26
app/Transformers/CategoryTransformer.php
Normal file
26
app/Transformers/CategoryTransformer.php
Normal file
|
@ -0,0 +1,26 @@
|
|||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: xing
|
||||
* Date: 2018/6/10
|
||||
* Time: 17:31
|
||||
*/
|
||||
|
||||
namespace App\Transformers;
|
||||
|
||||
|
||||
use App\Models\Category;
|
||||
use League\Fractal\TransformerAbstract;
|
||||
|
||||
class CategoryTransformer extends TransformerAbstract
|
||||
{
|
||||
public function transform(Category $category)
|
||||
{
|
||||
return [
|
||||
'id' => $category->id,
|
||||
'name' => $category->name,
|
||||
'description' => $category->description,
|
||||
];
|
||||
}
|
||||
|
||||
}
|
35
app/Transformers/TopicTransformer.php
Normal file
35
app/Transformers/TopicTransformer.php
Normal file
|
@ -0,0 +1,35 @@
|
|||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: xing
|
||||
* Date: 2018/6/10
|
||||
* Time: 17:46
|
||||
*/
|
||||
|
||||
namespace App\Transformers;
|
||||
|
||||
|
||||
use App\Models\Topic;
|
||||
use Carbon\Carbon;
|
||||
use League\Fractal\TransformerAbstract;
|
||||
|
||||
class TopicTransformer extends TransformerAbstract
|
||||
{
|
||||
public function transform(Topic $topic)
|
||||
{
|
||||
return [
|
||||
'id' => $topic->id,
|
||||
'title' => $topic->title,
|
||||
'body' => $topic->body,
|
||||
'user_id' => (int)$topic->user_id,
|
||||
'category_id' => (int)$topic->category_id,
|
||||
'reply_count' => (int)$topic->reply_count,
|
||||
'view_count' => (int)$topic->view_count,
|
||||
'last_reply_user_id' => (int)$topic->last_reply_user_id,
|
||||
'excerpt' => $topic->excerpt,
|
||||
'slug' => $topic->slug,
|
||||
'created_at' => $topic->created_at instanceof Carbon ? $topic->created_at->toDateTimeString() : $topic->created_at,
|
||||
'updated_at' => $topic->updated_at instanceof Carbon ? $topic->updated_at->toDateTimeString() : $topic->updated_at,
|
||||
];
|
||||
}
|
||||
}
|
|
@ -47,6 +47,10 @@ $api->version('v1', [
|
|||
// 删除token
|
||||
$api->delete('authorizations/current', 'AuthorizationsController@destroy')
|
||||
->name('api.authorizations.destroy');
|
||||
|
||||
// 游客可以访问的接口
|
||||
$api->get('categories', 'CategoriesController@index')
|
||||
->name('api.categories.index');
|
||||
});
|
||||
|
||||
// 需要 token 验证的接口
|
||||
|
@ -60,6 +64,8 @@ $api->version('v1', [
|
|||
// 编辑登录用户信息
|
||||
$api->patch('user', 'UsersController@update')
|
||||
->name('api.user.update');
|
||||
//发布话题
|
||||
$api->post('topics', 'TopicsController@store')->name('api.topics.store');
|
||||
});
|
||||
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue
Block a user