-
Notifications
You must be signed in to change notification settings - Fork 17
Feature/andre floquet #37
base: main
Are you sure you want to change the base?
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| <?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 Image; | ||
|
|
||
|
|
||
| class PostController extends Controller | ||
| { | ||
| public function create(Request $request) | ||
| { | ||
| return view('home'); | ||
| } | ||
|
|
||
| public function store(PostCreateRequest $request) | ||
| { | ||
| $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 | ||
| ]); | ||
|
|
||
| if(isset($post)) { | ||
| session()->flash('success', "The post '{$post->name}' has been created!"); | ||
| } | ||
|
|
||
| return redirect(route('post.create')); | ||
| } | ||
| } | ||
| 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' | ||
| ]; | ||
| } | ||
| } |
| 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); | ||
| } | ||
| } |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| 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(); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? 😱
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. actually no, this looks correct.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
| $table->string('name')->unique(); | ||
| $table->text('description'); | ||
| $table->text('image'); | ||
| $table->timestamps(); | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Reverse the migrations. | ||
| * | ||
| * @return void | ||
| */ | ||
| public function down() | ||
| { | ||
| Schema::dropIfExists('posts'); | ||
| } | ||
| }; | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| <input {!! $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']) !!}> |
| 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> |
| 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" type="file" 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> | ||
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.
If the
Post::create()operation failed an exception would be thrown, so this if condition is unnecessary.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, I agree. Would be better in a try catch block
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.
changed for try catch block returning friendly user message