This repository was archived by the owner on Mar 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
Feature/andre floquet #37
Open
andrefloquet
wants to merge
4
commits into
codinglabsau:main
Choose a base branch
from
andrefloquet:feature/andre-floquet
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,11 @@ | ||
## How long did you spend to complete this test? | ||
|
||
I took about 7 hours | ||
|
||
## What was easy? | ||
|
||
All steps, exept steps 5 and bonus | ||
|
||
## What was hard? | ||
|
||
Nothing too hard, but took most time on view, validation and upload file. |
This file contains hidden or 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,63 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers; | ||
|
||
use Illuminate\Http\Request; | ||
use App\Http\Requests\PostCreateRequest; | ||
use App\Models\Post; | ||
use Illuminate\Support\Facades\Auth; | ||
use Illuminate\Support\Facades\File; | ||
use Illuminate\Database\QueryException; | ||
use Image; | ||
|
||
|
||
class PostController extends Controller | ||
{ | ||
public function create(Request $request) | ||
{ | ||
return view('home'); | ||
} | ||
|
||
public function store(PostCreateRequest $request) | ||
{ | ||
try { | ||
|
||
$imgPath = ""; | ||
|
||
if ($request->hasFile('image')) { | ||
// Create directory images if doesn't exist | ||
File::makeDirectory(public_path().'/images', 0755, true, true); | ||
// Avoid file names duplicated | ||
$fileNameWithExt = $request->file('image')->getClientOriginalName(); | ||
// Just file name | ||
$fileName = pathinfo($fileNameWithExt, PATHINFO_FILENAME); | ||
// Just file extension | ||
$extension = $request->file('image')->extension(); | ||
// Name to Store | ||
$fileNameToStore = $fileName.'_'.time().'.'.$extension; | ||
//Full Path | ||
$imgPath = public_path().'/images/'.$fileNameToStore; | ||
//resize and store | ||
Image::make($request->file('image'))->resize(400, 300)->save($imgPath); | ||
} | ||
|
||
$post = Post::create([ | ||
'user_id' => Auth::user()->id, | ||
'name' => trim($request->name), | ||
'description' => trim($request->description), | ||
'image' => $imgPath | ||
]); | ||
|
||
session()->flash('success', "The post '{$post->name}' has been created!"); | ||
|
||
} catch(QueryException $exception) { | ||
session()->flash('exception', "A database error occurred when creating a new post. | ||
Please refresh the page and try again. | ||
If the problem persists, contact the support."); | ||
} catch(\Exception $exception) { | ||
session()->flash('exception', "Please refresh the page and try again. If the problem persists, contact the support."); | ||
} finally { | ||
return redirect(route('post.create')); | ||
} | ||
} | ||
} |
This file contains hidden or 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,32 @@ | ||
<?php | ||
|
||
namespace App\Http\Requests; | ||
|
||
use Illuminate\Foundation\Http\FormRequest; | ||
|
||
class PostCreateRequest 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<string, mixed> | ||
*/ | ||
public function rules() | ||
{ | ||
return [ | ||
'name' => 'required|max:255|unique:posts,name', | ||
'description' => 'required', | ||
'image' => 'required|image|mimes:jpg,png,jpeg,gif,svg|max:2048' | ||
]; | ||
} | ||
} |
This file contains hidden or 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,28 @@ | ||
<?php | ||
|
||
namespace App\Models; | ||
|
||
use Illuminate\Database\Eloquent\Factories\HasFactory; | ||
use Illuminate\Database\Eloquent\Model; | ||
|
||
class Post extends Model | ||
{ | ||
use HasFactory; | ||
|
||
/** | ||
* The attributes that are mass assignable. | ||
* | ||
* @var array<int, string> | ||
*/ | ||
protected $fillable = [ | ||
'user_id', | ||
'name', | ||
'description', | ||
'image' | ||
]; | ||
|
||
public function user() | ||
{ | ||
return $this->belongsTo(User::class); | ||
} | ||
} |
This file contains hidden or 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
This file contains hidden or 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
35 changes: 35 additions & 0 deletions
35
database/migrations/2022_09_02_104041_create_posts_table.php
This file contains hidden or 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,35 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
return new class extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function up() | ||
{ | ||
Schema::create('posts', function (Blueprint $table) { | ||
$table->id(); | ||
$table->foreignId('user_id')->constrained()->cascadeOnDelete(); | ||
$table->string('name')->unique(); | ||
$table->text('description'); | ||
$table->text('image'); | ||
$table->timestamps(); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function down() | ||
{ | ||
Schema::dropIfExists('posts'); | ||
} | ||
}; |
This file contains hidden or 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 @@ | ||
!.gitignore |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or 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,14 @@ | ||
<input type="file" {!! $attributes->merge(['class' => 'block | ||
w-full | ||
px-3 | ||
py-1.5 | ||
text-base | ||
font-normal | ||
text-gray-700 | ||
bg-gray-100 bg-clip-padding | ||
border border-solid border-gray-300 | ||
rounded | ||
transition | ||
ease-in-out | ||
m-0 | ||
focus:text-gray-700 focus:bg-white focus:border-blue-600 focus:outline-none']) !!}> |
This file contains hidden or 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,12 @@ | ||
<textarea | ||
{!! $attributes->merge(['class' => 'mt-1 | ||
block | ||
w-full | ||
rounded-md | ||
border-gray-300 | ||
shadow-sm | ||
focus:border-indigo-300 | ||
focus:ring | ||
focus:ring-indigo-200 | ||
focus:ring-opacity-50']) !!} | ||
></textarea> |
This file contains hidden or 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,51 @@ | ||
<x-app-layout> | ||
<x-slot name="header"> | ||
<h2 class="font-semibold text-xl text-gray-800 leading-tight"> | ||
{{ __('Create a New Post') }} | ||
</h2> | ||
</x-slot> | ||
<div class="py-12"> | ||
<div class="max-w-7xl mx-auto sm:px-6 lg:px-8"> | ||
<div class="bg-white overflow-hidden shadow-sm sm:rounded-lg"> | ||
<div class="p-6 bg-white border-b border-gray-200"> | ||
<form method="POST" action="{{ route('post.store') }}" enctype="multipart/form-data"> | ||
@csrf | ||
<div class="mt-4 max-w-md mx-auto"> | ||
<div class="grid grid-cols-1 gap-6 mt-"> | ||
|
||
<div> | ||
<x-label for="name" :value="__('Name')" /> | ||
|
||
<x-input id="name" class="block mt-1 w-full" type="text" name="name" required autofocus /> | ||
</div> | ||
|
||
<div> | ||
<x-label for="description" :value="__('Description')" /> | ||
|
||
<x-textarea id="description" class="block mt-1 w-full" name="description" rows="5" required /> | ||
</div> | ||
|
||
<div> | ||
<x-label for="image" :value="__('Upload Image')" /> | ||
|
||
<x-input-file id="image" class="block mt-1 w-full" name="image" /> | ||
|
||
</div> | ||
|
||
<div class="flex items-center justify-end"> | ||
|
||
<x-button class="max-w-fit"> | ||
|
||
{{ __('Create Post') }} | ||
|
||
</x-button> | ||
|
||
</div> | ||
</div> | ||
</div> | ||
</form> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
</x-app-layout> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this means if the post is deleted, the user is also deleted? 😱
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
actually no, this looks correct.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah, the opposite. If the user is deleted, the posts from the user are deleted too