回复crd

This commit is contained in:
fthvgb1 2018-06-10 22:37:37 +08:00
parent 22501cc89a
commit 200fbdfc0a
12 changed files with 171 additions and 53 deletions

View File

@ -0,0 +1,50 @@
<?php
namespace App\Http\Controllers\Api;
use App\Http\Requests\Api\ReplyRequest;
use App\Models\Reply;
use App\Models\Topic;
use App\Models\User;
use App\Transformers\ReplyTransformer;
class RepliesController extends Controller
{
public function store(ReplyRequest $request, Topic $topic, Reply $reply)
{
$reply->content = $request->get('content');
$reply->topic_id = $topic->id;
$reply->user_id = $this->user()->id;
$reply->save();
return $this->response->item($reply, new ReplyTransformer())->setStatusCode(201);
}
/**
* @param Reply $reply
* @return \Dingo\Api\Http\Response
* @throws \Illuminate\Auth\Access\AuthorizationException
* @throws \Throwable
*/
public function destroy(Reply $reply)
{
$this->authorize('destroy', $reply);
$reply->delete();
return $this->response->noContent();
}
/**
* @param Topic $topic
* @return \Dingo\Api\Http\Response
*/
public function index(Topic $topic)
{
$replies = $topic->replies()->recent()->paginate(20);
return $this->response->paginator($replies, new ReplyTransformer());
}
public function userIndex(User $user)
{
$replies = $user->replies()->recent()->paginate(20);
return $this->response->paginator($replies, new ReplyTransformer());
}
}

View File

@ -2,7 +2,6 @@
namespace App\Http\Requests\Api; namespace App\Http\Requests\Api;
use Illuminate\Foundation\Http\FormRequest;
class AuthorizationRequest extends FormRequest class AuthorizationRequest extends FormRequest
{ {

View File

@ -2,19 +2,9 @@
namespace App\Http\Requests\Api; namespace App\Http\Requests\Api;
use Illuminate\Foundation\Http\FormRequest;
class CaptchaRequest extends FormRequest class CaptchaRequest extends FormRequest
{ {
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/** /**
* Get the validation rules that apply to the request. * Get the validation rules that apply to the request.

View File

@ -0,0 +1,30 @@
<?php
namespace App\Http\Requests\Api;
use Illuminate\Foundation\Http\FormRequest as BaseFormRequest;
class FormRequest extends BaseFormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
//
];
}
}

View File

@ -2,19 +2,9 @@
namespace App\Http\Requests\Api; namespace App\Http\Requests\Api;
use Illuminate\Foundation\Http\FormRequest;
class ImageRequest extends FormRequest class ImageRequest extends FormRequest
{ {
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/** /**
* Get the validation rules that apply to the request. * Get the validation rules that apply to the request.

View File

@ -0,0 +1,33 @@
<?php
namespace App\Http\Requests\Api;
/**
* Class ReplyRequest
* @property string content
* @package App\Http\Requests\Api
*/
class ReplyRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'content' => 'required|min:2',
];
}
}

View File

@ -2,19 +2,8 @@
namespace App\Http\Requests\Api; namespace App\Http\Requests\Api;
use Illuminate\Foundation\Http\FormRequest;
class SocialAuthorizationRequest extends FormRequest class SocialAuthorizationRequest extends FormRequest
{ {
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/** /**
* Get the validation rules that apply to the request. * Get the validation rules that apply to the request.

View File

@ -2,7 +2,6 @@
namespace App\Http\Requests\Api; namespace App\Http\Requests\Api;
use Illuminate\Foundation\Http\FormRequest;
/** /**
* Class TopicRequest * Class TopicRequest
@ -13,15 +12,6 @@ use Illuminate\Foundation\Http\FormRequest;
*/ */
class TopicRequest extends FormRequest class TopicRequest extends FormRequest
{ {
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
public function rules() public function rules()
{ {

View File

@ -2,19 +2,9 @@
namespace App\Http\Requests\Api; namespace App\Http\Requests\Api;
use Illuminate\Foundation\Http\FormRequest;
class VerificationCodeRequest extends FormRequest class VerificationCodeRequest extends FormRequest
{ {
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/** /**
* Get the validation rules that apply to the request. * Get the validation rules that apply to the request.

View File

@ -2,6 +2,17 @@
namespace App\Models; namespace App\Models;
/**
* Class Reply
* @property int id
* @property string content
* @property string created_at
* @property string updated_at
* @property int user_id
* @property int topic_id
* @property User user
* @package App\Models
*/
class Reply extends Model class Reply extends Model
{ {
protected $fillable = ['content']; protected $fillable = ['content'];

View File

@ -0,0 +1,36 @@
<?php
/**
* Created by PhpStorm.
* User: xing
* Date: 2018/6/10
* Time: 21:52
*/
namespace App\Transformers;
use App\Models\Reply;
use Carbon\Carbon;
use League\Fractal\TransformerAbstract;
class ReplyTransformer extends TransformerAbstract
{
protected $availableIncludes = ['user'];
public function transform(Reply $reply)
{
return [
'id' => (int)$reply->id,
'user_id' => (int)$reply->user_id,
'topic_id' => (int)$reply->topic_id,
'content' => $reply->content,
'created_at' => $reply->created_at instanceof Carbon ? $reply->created_at->toDateTimeString() : $reply->created_at,
'updated_at' => $reply->updated_at instanceof Carbon ? $reply->updated_at->toDateTimeString() : $reply->updated_at,
];
}
public function includeUser(Reply $reply)
{
return $this->item($reply->user, new UserTransformer());
}
}

View File

@ -76,7 +76,17 @@ $api->version('v1', [
$api->patch('topics/{topic}', 'TopicsController@update')->name('api.topics.update'); $api->patch('topics/{topic}', 'TopicsController@update')->name('api.topics.update');
//删除话题 //删除话题
$api->delete('topics/{topic}', 'TopicsController@destroy')->name('api.topics.destroy'); $api->delete('topics/{topic}', 'TopicsController@destroy')->name('api.topics.destroy');
// 发布回复
$api->post('topics/{topic}/replies', 'RepliesController@store')
->name('api.topics.replies.store');
//删除回复
$api->delete('replies/{reply}', 'RepliesController@destroy')->name('api.replies.destroy');
// 话题回复列表
$api->get('topics/{topic}/replies', 'RepliesController@index')
->name('api.topics.replies.index');
//某个用户的回复列表
$api->get('users/{user}/replies', 'RepliesController@userIndex')
->name('api.users.replies.index');
}); });
}); });