Skip to content
This repository was archived by the owner on Mar 11, 2024. It is now read-only.
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 @@
1. How long did you spend to complete this test?

- 1 hour

2. What was easy?

- mostly yes

3. What was hard?

- just took a longer time creating a test for new post insert with foreight key
2 changes: 2 additions & 0 deletions app/Http/Controllers/Auth/AuthenticatedSessionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ public function store(LoginRequest $request)

$request->session()->regenerate();

session()->flash('success', "You're logged in!");

return redirect()->intended(RouteServiceProvider::HOME);
}

Expand Down
47 changes: 47 additions & 0 deletions app/Http/Controllers/PostController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace App\Http\Controllers;

use App\Http\Requests\PostCreateRequest;
use App\Models\Post;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Support\Facades\Auth;
use Illuminate\Database\QueryException;
use Illuminate\Support\Facades\Log;
use Image;

class PostController extends BaseController
{
const RESIZE_FILE_WIDTH = 400;
const RESIZE_FILE_HEIGHT = 300;

public function create(Request $request)
{
return view('home');
}

public function store(PostCreateRequest $request)
{
try {
$imagePath = "";
if ($request->hasFile('image')) {
$file = $request->file('image');
$imagePath = '/uploads/' . pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME) . '-' . date('mdhis') . '.' . $file->getClientOriginalExtension();
Image::make($request->file('image'))->resize($this::RESIZE_FILE_WIDTH, $this::RESIZE_FILE_HEIGHT)->save(public_path() . $imagePath);
}
$post = Post::create([
'user_id' => Auth::user()->id,
'name' => $request->name,
'description' => $request->description,
'image_path' => $imagePath
]);
} catch (QueryException $e) {
session()->flash('exception', "Error has occured.");
}

session()->flash('success', "Post #{$post->id} was successfully created");

return redirect(route('post.create'));
}
}
31 changes: 31 additions & 0 deletions app/Http/Requests/PostCreateRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?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',
'image' => 'image|mimes:jpg,png,jpeg,gif,svg'
];
}
}
23 changes: 23 additions & 0 deletions app/Models/Post.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
use HasFactory;

protected $fillable = [
'user_id',
'name',
'description',
'image_path'
];

public function user()
{
return $this->belongsTo(User::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.

25 changes: 25 additions & 0 deletions database/factories/PostFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Database\Factories;

use Illuminate\Database\Eloquent\Factories\Factory;

/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Post>
*/
class PostFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition()
{
return [
'user_id' => 1,
'name' => fake()->name(),
'description' => fake()->sentence()
];
}
}
2 changes: 1 addition & 1 deletion database/factories/UserFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function definition()
'name' => fake()->name(),
'email' => fake()->safeEmail(),
'email_verified_at' => now(),
'password' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password
'password' => bcrypt('password'),
'remember_token' => Str::random(10),
];
}
Expand Down
34 changes: 34 additions & 0 deletions database/migrations/2022_11_16_001950_create_posts_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?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->string('name')->nullable(false);
$table->text('description');
$table->foreignId('user_id')->constrained('users')->cascadeOnDelete();
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('posts');
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?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::table('posts', function (Blueprint $table) {
$table->string('image_path')->nullable()->after('description');
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('posts', function (Blueprint $table) {
$table->dropColumn('image_path');
});
}
};
11 changes: 5 additions & 6 deletions database/seeders/DatabaseSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,10 @@ 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([
'id' => 1,
'name' => 'Test User',
'email' => 'user@codinglabs.test'
]);
}
}
22 changes: 22 additions & 0 deletions database/seeders/PostSeeder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;

class PostSeeder extends Seeder
{
/**
* Seed the application's database.
*
* @return void
*/
public function run()
{
\App\Models\Post::factory()->create([
'user_id' => 1,
'name' => 'Post Title',
'description' => 'some description',
]);
}
}
Empty file added public/uploads/empty
Empty file.
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']) !!}>
3 changes: 3 additions & 0 deletions resources/views/components/textarea.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@props(['disabled' => false])

<textarea {{ $disabled ? 'disabled' : '' }} {!! $attributes->merge(['class' => 'rounded-md shadow-sm border-gray-300 focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50']) !!}></textarea>
Loading