-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a167a0d
commit 91574a2
Showing
7 changed files
with
198 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 [ | ||
// | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(), | ||
]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters