Skip to content

Commit

Permalink
notification read
Browse files Browse the repository at this point in the history
  • Loading branch information
zhuzichuan committed Apr 5, 2018
1 parent a167a0d commit 91574a2
Show file tree
Hide file tree
Showing 7 changed files with 198 additions and 0 deletions.
30 changes: 30 additions & 0 deletions app/Http/Controllers/Api/NotificationsController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Transformers\NotificationTransformer;

class NotificationsController extends Controller
{
public function index()
{
$notifications = $this->user->notifications()->paginate(20);

return $this->response->paginator($notifications, new NotificationTransformer());
}
public function stats()
{
return $this->response->array([
'unread_count' => $this->user()->notification_count,
]);
}

public function read()
{
$this->user()->markAsRead();

return $this->response->noContent();
}

}
52 changes: 52 additions & 0 deletions app/Http/Controllers/Api/RepliesController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace App\Http\Controllers\Api;

use App\Models\Topic;
use App\Models\Reply;
use App\Models\User;
use App\Http\Requests\Api\ReplyRequest;
use App\Transformers\ReplyTransformer;
use Illuminate\Http\Request;


class RepliesController extends Controller
{
public function store(ReplyRequest $request, Topic $topic, Reply $reply)
{
$reply->content = $request->content;
$reply->topic_id = $topic->id;
$reply->user_id = $this->user()->id;
$reply->save();

return $this->response->item($reply, new ReplyTransformer())->setStatusCode(201);
}

public function destroy(Topic $topic, Reply $reply)
{
if ($reply->topic_id != $topic->id) {
return $this->response->errorBadRequest();

}

$this->authorize('destroy', $reply);
$reply->delete();

return $this->response->noContent();
}

public function index(Topic $topic)
{
$replies = $topic->replies()->paginate(20);

return $this->response->paginator($replies, new ReplyTransformer());
}

public function userIndex(User $user)
{
$replies = $user->replies()->paginate(20);

return $this->response->paginator($replies, new ReplyTransformer());
}

}
30 changes: 30 additions & 0 deletions app/Http/Requests/Api/FormRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace App\Http\Requests\Api;

use Dingo\Api\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 [
//
];
}
}
16 changes: 16 additions & 0 deletions app/Http/Requests/Api/ReplyRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace App\Http\Requests\Api;



class ReplyRequest extends FormRequest
{

public function rules()
{
return [
'content' => 'required|min:2',
];
}
}
20 changes: 20 additions & 0 deletions app/Transformers/NotificationTransformer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace App\Transformers;

use League\Fractal\TransformerAbstract;
use Illuminate\Notifications\DatabaseNotification;

class NotificationTransformer extends TransformerAbstract
{
public function transform(DatabaseNotification $notification)
{
return [
'id' => $notification->id,
'type' => $notification->type,
'data' => $notification->data,
'read_at' => $notification->read_at ? $notification->read_at->toDateTimeString() : null,
'created_at' => $notification->created_at->toDateTimeString(),
'updated_at' => $notification->updated_at->toDateTimeString(),
];
}
31 changes: 31 additions & 0 deletions app/Transformers/ReplyTransformer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace App\Transformers;

use App\Models\Reply;
use League\Fractal\TransformerAbstract;

class ReplyTransformer extends TransformerAbstract
{
protected $availableIncludes = ['user', 'topic'];
public function transform(Reply $reply)
{

return [
'id' => $reply->id,
'user_id' => (int) $reply->user_id,
'topic_id' => (int) $reply->topic_id,
'content' => $reply->content,
'created_at' => $reply->created_at->toDateTimeString(),
'updated_at' => $reply->updated_at->toDateTimeString(), ];
}

public function includeUser(Reply $reply)
{
return $this->item($reply->user, new UserTransformer());
}
public function includeTopic(Reply $reply)
{
return $this->item($reply->topic, new TopicTransformer());
}
}
19 changes: 19 additions & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@
->name('api.topics.show');
$api->get('users/{user}/topics', 'TopicController@userIndex')
->name('api.users.topics.index');
$api->get('topics/{topic}/replies', 'RepliesController@index')
->name('api.topics.replies,index');
$api->get('users/{user}/replies', 'RepliesController@userIndex')
->name('api.users.replies.index');
// 需要 token 验证的接口
$api->group(['middleware' => 'api.auth'], function($api) {
// 当前登录用户信息
Expand All @@ -66,6 +70,21 @@
->name('api.topics.upadte');
$api->delete('topics/{topic}', 'TopicController@destroy')
->name('api.topics.destroy');

$api->post('topics/{topic}/replies', 'RepliesController@store')
->name('api.topics.replies.store');

$api->delete('topics/{topic}/replies/{reply}', 'RepliesController@destroy')
->name('api.topics.replies.destroy');

$api->get('users/notifications', 'NotificationsController@index')
->name('api.user.notifications.index');

$api->get('users/notifications/stats', 'NotificationsController@stats')
->name('api.user.notifications.stats');

$api->patch('user/read/notifications', 'NotificationsController@read')
->name('api.user.notifications.read');
});
});

Expand Down

0 comments on commit 91574a2

Please sign in to comment.