Skip to content

Commit

Permalink
added sweetalerts, auth policy, images to s3, error views, edit and d…
Browse files Browse the repository at this point in the history
…elete methods
  • Loading branch information
luckyguy73 committed Mar 19, 2017
1 parent c9781fc commit dd54c56
Show file tree
Hide file tree
Showing 44 changed files with 13,453 additions and 9,345 deletions.
3 changes: 2 additions & 1 deletion app/Http/Controllers/CommentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Post;
use App\Comment;
use Illuminate\Http\Request;
use Alert;

class CommentController extends Controller
{
Expand Down Expand Up @@ -44,7 +45,7 @@ public function store(Request $request, Post $post)
'user_id' => auth()->id(),
'post_id' => $post->id,
]);
session()->flash('success', 'Comment has been added');
Alert::success('Comment has been posted');
return back();
}

Expand Down
56 changes: 47 additions & 9 deletions app/Http/Controllers/PostController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
use App\Post;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Validation\Rule;
use Alert;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Storage;

class PostController extends Controller
{
Expand All @@ -31,6 +34,7 @@ public function index()
*/
public function create()
{
$this->authorize('create', Post::class);
return view('posts.create');
}

Expand All @@ -44,12 +48,12 @@ public function store(Request $request)
{
$request['title'] = strtolower(request('title'));
$this->validate($request, [
'title' => 'required|unique:posts|max:255',
'ingredients' => 'required|min:5',
'directions' => 'required|min:5',
'title' => 'required|unique:posts|max:100',
'ingredients' => 'required',
'directions' => 'required',
'image' => 'file|max:40000|mimes:jpeg,gif,png,svg,bmp',
]);
$path = $request->file('image')->store(auth()->id(), 's3');
$path = empty(request('image')) ? null : $request->file('image')->store(auth()->id(), 's3');
Post::create([
'title' => request('title'),
'ingredients' => request('ingredients'),
Expand All @@ -58,8 +62,8 @@ public function store(Request $request)
'user_id' => auth()->id(),
'image' => $path,
])->tags()->attach($request->tag);

session()->flash('success', 'Recipe has been published');
Alert::success('Recipe has been published');
// session()->flash('success', 'Recipe has been published');
return redirect()->route('posts.index');
}

Expand All @@ -82,7 +86,8 @@ public function show(Post $post)
*/
public function edit(Post $post)
{
//
$this->authorize('edit', $post);
return view('posts.edit', compact('post'));
}

/**
Expand All @@ -94,7 +99,37 @@ public function edit(Post $post)
*/
public function update(Request $request, Post $post)
{
//
$this->authorize('update', $post);
$request['title'] = strtolower(request('title'));
$this->validate($request, [
'title' => [
'required',
Rule::unique('posts')->ignore($post->id),
'max:100',
],
'ingredients' => 'required',
'directions' => 'required',
'image' => 'file|max:40000|mimes:jpeg,gif,png,svg,bmp',
]);
if (!empty(request('image'))) {
if(!is_null($post->image)) {
$file = $post->image;
Storage::disk('s3')->delete($file);
}
$path = $request->file('image')->store(auth()->id(), 's3');
$post->image = $path;
}
$post->title = request('title');
$post->ingredients = request('ingredients');
$post->directions = request('directions');
$post->slug = str_slug($request->title, '-');
$post->tags()->detach();
$post->tags()->attach($request->tag);
$post->save();

Alert::success('Recipe successfully updated');
// session()->flash('success', 'Recipe updates successful');
return redirect()->route('posts.index');
}

/**
Expand All @@ -105,6 +140,9 @@ public function update(Request $request, Post $post)
*/
public function destroy(Post $post)
{
//
$this->authorize('delete', $post);
$post->delete();
Alert::success('Recipe successfully deleted');
return redirect()->route('posts.index');
}
}
1 change: 1 addition & 0 deletions app/Http/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class Kernel extends HttpKernel
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
\UxWeb\SweetAlert\ConvertMessagesIntoSweatAlert::class,
],

'api' => [
Expand Down
72 changes: 72 additions & 0 deletions app/Policies/PostPolicy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

namespace App\Policies;

use App\Post;
use App\User;
use Illuminate\Support\Facades\Auth;
use Illuminate\Auth\Access\HandlesAuthorization;

class PostPolicy
{
use HandlesAuthorization;

/**
* Determine whether the user can view the post.
*
* @param \App\User $user
* @param \App\Post $post
* @return mixed
*/
public function view(User $user, Post $post)
{
//
}

/**
* Determine whether the user can create posts.
*
* @param \App\User $user
* @return mixed
*/
public function create(User $user)
{
return Auth::check();
}

/**
* Determine whether the user can update the post.
*
* @param \App\User $user
* @param \App\Post $post
* @return mixed
*/
public function edit(User $user, Post $post)
{
return $user->id === $post->user_id;
}

/**
* Determine whether the user can update the post.
*
* @param \App\User $user
* @param \App\Post $post
* @return mixed
*/
public function update(User $user, Post $post)
{
return $user->id === $post->user_id;
}

/**
* Determine whether the user can delete the post.
*
* @param \App\User $user
* @param \App\Post $post
* @return mixed
*/
public function delete(User $user, Post $post)
{
return $user->id === $post->user_id;
}
}
2 changes: 1 addition & 1 deletion app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class AppServiceProvider extends ServiceProvider
public function boot()
{
Schema::defaultStringLength(191);
view()->composer(['layouts.partials.sidebar', 'posts.create'], function($view) {
view()->composer(['layouts.partials.sidebar', 'posts.create', 'posts.edit'], function($view) {
$view->with('archives', \App\Post::archives());
$view->with('tags', \App\Tag::orderBy('name', 'asc')->get());
});
Expand Down
4 changes: 3 additions & 1 deletion app/Providers/AuthServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace App\Providers;

use App\Post;
use App\Policies\PostPolicy;
use Illuminate\Support\Facades\Gate;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;

Expand All @@ -13,7 +15,7 @@ class AuthServiceProvider extends ServiceProvider
* @var array
*/
protected $policies = [
'App\Model' => 'App\Policies\ModelPolicy',
Post::class => PostPolicy::class,
];

/**
Expand Down
37 changes: 0 additions & 37 deletions app/Providers/GoogleDriveServiceProvider.php

This file was deleted.

3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"php": ">=5.6.4",
"laravel/framework": "5.4.*",
"laravel/tinker": "~1.0",
"league/flysystem-aws-s3-v3": "^1.0"
"league/flysystem-aws-s3-v3": "^1.0",
"uxweb/sweet-alert": "^1.4"
},
"require-dev": {
"fzaninotto/faker": "~1.4",
Expand Down
53 changes: 52 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion config/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,12 @@
Illuminate\Translation\TranslationServiceProvider::class,
Illuminate\Validation\ValidationServiceProvider::class,
Illuminate\View\ViewServiceProvider::class,
App\Providers\GoogleDriveServiceProvider::class,

/*
* Package Service Providers...
*/
Laravel\Tinker\TinkerServiceProvider::class,
UxWeb\SweetAlert\SweetAlertServiceProvider::class,

/*
* Application Service Providers...
Expand Down Expand Up @@ -226,6 +226,7 @@
'URL' => Illuminate\Support\Facades\URL::class,
'Validator' => Illuminate\Support\Facades\Validator::class,
'View' => Illuminate\Support\Facades\View::class,
'Alert' => UxWeb\SweetAlert\SweetAlert::class,

],

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function up()
$table->string('slug');
$table->text('ingredients');
$table->text('directions');
$table->string('image');
$table->string('image')->nullable();
$table->timestamps();

$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function up()
*/
public function down()
{
Schema::dropIfExists('tags');
Schema::dropIfExists('post_tag');
Schema::dropIfExists('tags');
}
}
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,8 @@
"laravel-mix": "^0.8.3",
"lodash": "^4.17.4",
"vue": "^2.1.10"
},
"dependencies": {
"sweetalert": "^1.1.3"
}
}
Loading

0 comments on commit dd54c56

Please sign in to comment.