Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions app/Events/PostCreated.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace App\Events;

use App\Post;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class PostCreated
{
use Dispatchable, SerializesModels;

public Post $post;

/**
* Create a new event instance.
*
* @param Post $post
*/
public function __construct(Post $post)
{
$this->post = $post;
}
}
21 changes: 15 additions & 6 deletions app/Http/Controllers/PostsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

namespace App\Http\Controllers;

use App\Mail\PostCreated;
use App\Notifications\PostCreated;
use App\Notifications\PostDeleted;
use App\Notifications\PostEdited;
use App\Post;
use App\Post_tag;
use App\Tag;
use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;

class PostsController extends Controller
{
Expand All @@ -25,8 +27,7 @@ public function index()

public function userPosts()
{
// $posts = Post::where('owner_id', auth()->id())->with('tags')->latest()->get();
$posts = Auth()->user()->posts()->with('tags')->latest()->get();
$posts = auth()->user()->posts()->with('tags')->latest()->get();
return view('/posts.index', compact('posts'));
}

Expand Down Expand Up @@ -57,7 +58,8 @@ public function store(Request $request)

$post = Post::create($attr);

Mail::to($post->owner->email)->send(new PostCreated($post));
sendMailNotifyToAdmin(new PostCreated($post));
flash( 'Post created successfully');

return redirect('/');
}
Expand Down Expand Up @@ -108,12 +110,19 @@ public function update(Request $request, Post $post)
};
}

sendMailNotifyToAdmin(new PostEdited($post));
flash( 'Post edited successfully');

return back();
}

public function destroy(Post $post)
{
$post->delete();
return redirect('/admin/posts');

sendMailNotifyToAdmin(new PostDeleted($post));
flash( 'Post delete successfully');

return redirect('/posts');
}
}
16 changes: 16 additions & 0 deletions app/Http/Controllers/StaticPagesController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class StaticPagesController extends Controller
{
public function contactsIndex() {
return view('static.contacts');
}

public function aboutIndex() {
return view('static.about');
}
}
24 changes: 24 additions & 0 deletions app/Listeners/SendPostCreateNotification.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace App\Listeners;

use App\Events\PostCreated;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Support\Facades\Mail;

class SendPostCreateNotification
{
/**
* Handle the event.
*
* @param PostCreated $event
* @return void
*/
public function handle(PostCreated $event)
{
Mail::to($event->post->owner->email)->send(
new \App\Mail\PostCreated($event->post)
);
}
}
61 changes: 61 additions & 0 deletions app/Notifications/PostCreated.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace App\Notifications;

use App\Post;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;

class PostCreated extends Notification
{
use Queueable;

public $post;

/**
* Create a new notification instance.
*
* @param Post $post
*/
public function __construct(Post $post)
{
$this->post = $post;
}

/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return ['mail'];
}

/**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
* @return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
return (new MailMessage)->markdown('mail.post-created', ['post' => $this->post]);
}

/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
* @return array
*/
public function toArray($notifiable)
{
return [
//
];
}
}
61 changes: 61 additions & 0 deletions app/Notifications/PostDeleted.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace App\Notifications;

use App\Post;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;

class PostDeleted extends Notification
{
use Queueable;

public $post;

/**
* Create a new notification instance.
*
* @param Post $post
*/
public function __construct(Post $post)
{
$this->post = $post;
}

/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return ['mail'];
}

/**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
* @return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
return (new MailMessage)->markdown('mail.post-deleted', ['post' => $this->post]);
}

/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
* @return array
*/
public function toArray($notifiable)
{
return [
//
];
}
}
61 changes: 61 additions & 0 deletions app/Notifications/PostEdited.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace App\Notifications;

use App\Post;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;

class PostEdited extends Notification
{
use Queueable;

public $post;

/**
* Create a new notification instance.
*
* @param Post $post
*/
public function __construct(Post $post)
{
$this->post = $post;
}

/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return ['mail'];
}

/**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
* @return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
return (new MailMessage)->markdown('mail.post-edited', ['post' => $this->post]);
}

/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
* @return array
*/
public function toArray($notifiable)
{
return [
//
];
}
}
5 changes: 5 additions & 0 deletions app/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App;

use App\Events\PostCreated;
use Illuminate\Database\Eloquent\Model;

class Post extends Model
Expand All @@ -10,6 +11,10 @@ class Post extends Model

protected $guarded = [];

// protected $dispatchesEvents = [
// 'created' => PostCreated::class,
// ];

public function tags()
{
return $this->belongsToMany(Tag::class);
Expand Down
5 changes: 5 additions & 0 deletions app/Providers/EventServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace App\Providers;

use App\Events\PostCreated;
use App\Listeners\SendPostCreateNotification;
use Illuminate\Auth\Events\Registered;
use Illuminate\Auth\Listeners\SendEmailVerificationNotification;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
Expand All @@ -18,6 +20,9 @@ class EventServiceProvider extends ServiceProvider
Registered::class => [
SendEmailVerificationNotification::class,
],
PostCreated::class => [
SendPostCreateNotification::class,
],
];

/**
Expand Down
14 changes: 14 additions & 0 deletions app/helpers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

function flash($message, $type = 'success') {
session()->flash('message', $message);
session()->flash('message_type', $type);
}

function sendMailNotifyToAdmin($mailView) {
$admin = \App\User::where('email', env('ADMIN_EMAIL_FOR_NOTIFICATIONS'))->first();

if ($admin) {
$admin->notify($mailView);
}
}
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@
"classmap": [
"database/seeds",
"database/factories"
],
"files": [
"app/helpers.php"
]
},
"autoload-dev": {
Expand Down
Loading