Skip to content
This repository was archived by the owner on Mar 11, 2024. It is now read-only.

Feature/andre floquet #37

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
11 changes: 11 additions & 0 deletions README.md
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.
63 changes: 63 additions & 0 deletions app/Http/Controllers/PostController.php
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'));
}
}
}
32 changes: 32 additions & 0 deletions app/Http/Requests/PostCreateRequest.php
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'
];
}
}
28 changes: 28 additions & 0 deletions app/Models/Post.php
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);
}
}
5 changes: 5 additions & 0 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,9 @@ class User extends Authenticatable
protected $casts = [
'email_verified_at' => 'datetime',
];

public function posts()
{
return $this->hasMany(Post::class);
}
}
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"require": {
"php": "^8.0.2",
"guzzlehttp/guzzle": "^7.2",
"intervention/image": "^2.7",
"laravel/framework": "^9.19",
"laravel/sanctum": "^3.0",
"laravel/tinker": "^2.7"
Expand Down
86 changes: 85 additions & 1 deletion composer.lock

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

35 changes: 35 additions & 0 deletions database/migrations/2022_09_02_104041_create_posts_table.php
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();
Copy link
Member

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? 😱

Copy link
Member

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.

Copy link
Author

@andrefloquet andrefloquet Sep 4, 2022

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

$table->string('name')->unique();
$table->text('description');
$table->text('image');
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('posts');
}
};
10 changes: 4 additions & 6 deletions database/seeders/DatabaseSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,9 @@ class DatabaseSeeder extends Seeder
*/
public function run()
{
// \App\Models\User::factory(10)->create();

// \App\Models\User::factory()->create([
// 'name' => 'Test User',
// 'email' => 'user@codinglabs.test',
// ]);
\App\Models\User::factory()->create([
'email' => 'test@test.com',
'password' => bcrypt('secret')
]);
}
}
2 changes: 1 addition & 1 deletion package-lock.json

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

1 change: 1 addition & 0 deletions public/images/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
!.gitignore
Binary file added public/images/default_job_image_1662296372.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/facebook-profile_1662296317.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 14 additions & 0 deletions resources/views/components/input-file.blade.php
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']) !!}>
12 changes: 12 additions & 0 deletions resources/views/components/textarea.blade.php
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>
51 changes: 51 additions & 0 deletions resources/views/home.blade.php
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>
Loading