Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/keeping an inventory #3

Merged
merged 6 commits into from Jan 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 17 additions & 0 deletions app/Http/Controllers/Inventory/IndexController.php
@@ -0,0 +1,17 @@
<?php

declare(strict_types = 1);

namespace App\Http\Controllers\Inventory;

use App\Http\Resources\InventoryResource;
use App\Models\Inventory;
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;

class IndexController
{
public function __invoke(): AnonymousResourceCollection
{
return InventoryResource::collection(Inventory::all());
}
}
23 changes: 23 additions & 0 deletions app/Http/Controllers/Inventory/StoreController.php
@@ -0,0 +1,23 @@
<?php

declare(strict_types = 1);

namespace App\Http\Controllers\Inventory;

use App\Http\Requests\InventoryStoreRequest;
use App\Models\Inventory;
use Illuminate\Contracts\Routing\ResponseFactory;
use Illuminate\Http\Response;

class StoreController
{
public function __invoke(InventoryStoreRequest $request): Response|ResponseFactory
{
Inventory::create([
'name' => $request->get(key: 'name'),
]);

return response(content: 'Inventory item created', status: 201);
}
}

32 changes: 32 additions & 0 deletions app/Http/Requests/InventoryStoreRequest.php
@@ -0,0 +1,32 @@
<?php

declare(strict_types = 1);

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class InventoryStoreRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize(): bool
{
return true;
}

/**
* Get the validation rules that apply to the request.
*
* @return array<string, mixed>
*/
public function rules(): array
{
return [
'name' => 'required',
];
}
}
24 changes: 24 additions & 0 deletions app/Http/Resources/InventoryResource.php
@@ -0,0 +1,24 @@
<?php

declare(strict_types = 1);

namespace App\Http\Resources;

use App\Models\Inventory;
use Illuminate\Http\Resources\Json\JsonResource;

/**
* @mixin Inventory
*/
class InventoryResource extends JsonResource
{
/**
* @return array<string, string>
*/
public function toArray($request): array
{
return [
'name' => $this->name,
];
}
}
20 changes: 20 additions & 0 deletions app/Models/Inventory.php
@@ -0,0 +1,20 @@
<?php

declare(strict_types = 1);

namespace App\Models;

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

/**
* @property string $name
*/
class Inventory extends Model
{
use HasFactory;

protected $fillable = [
'name',
];
}
25 changes: 25 additions & 0 deletions database/factories/InventoryFactory.php
@@ -0,0 +1,25 @@
<?php

declare(strict_types = 1);

namespace Database\Factories;

use Illuminate\Database\Eloquent\Factories\Factory;

/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Inventory>
*/
class InventoryFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition()
{
return [
'name' => $this->faker->sentence,
];
}
}
32 changes: 32 additions & 0 deletions database/migrations/2023_01_18_142313_create_inventories_table.php
@@ -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::create('inventories', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('inventories');
}
};
16 changes: 5 additions & 11 deletions routes/web.php
@@ -1,22 +1,16 @@
<?php

use App\Http\Controllers\Inventory\IndexController;
use App\Http\Controllers\Inventory\StoreController;
use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/', function () {
return view('welcome');
});

Route::get('inventory', IndexController::class)->name('inventory.index');
Route::post('inventory', StoreController::class)->name('inventory.store');

Route::middleware([
'auth:sanctum',
config('jetstream.auth_session'),
Expand Down
21 changes: 0 additions & 21 deletions tests/Feature/ExampleTest.php

This file was deleted.

25 changes: 25 additions & 0 deletions tests/Feature/Http/Controllers/Inventory/IndexControllerTest.php
@@ -0,0 +1,25 @@
<?php

declare(strict_types = 1);

use App\Models\Inventory;

test('the inventory list can be returned', function () {
[$itemA, $itemB, $itemC] = Inventory::factory()->count(3)->create();

$response = $this->get(route(name: 'inventory.index'));

$response->assertStatus(200)
->assertExactJson(['data' => [
[
'name' => $itemA->name,
],
[
'name' => $itemB->name,
],
[
'name' => $itemC->name,
],
]]);
});

22 changes: 22 additions & 0 deletions tests/Feature/Http/Controllers/Inventory/StoreControllerTest.php
@@ -0,0 +1,22 @@
<?php

declare(strict_types = 1);

use App\Models\Inventory;

test('inventory items can be created', function () {
$response = $this->postJson(route(name: 'inventory.store'), [
'name' => 'My Special Item',
]);

$response->assertStatus(201);

$this->assertDatabaseHas(Inventory::class, [
'name' => 'My Special Item',
]);
});

test('inventory items require a name', function () {
$this->postJson(route(name: 'inventory.store'))
->assertJsonValidationErrorFor('name');
});