Skip to content

Commit

Permalink
Episode 13: Add Statuses
Browse files Browse the repository at this point in the history
  • Loading branch information
SarahSibert committed Apr 26, 2021
1 parent 8dd4a3f commit 30b2af6
Show file tree
Hide file tree
Showing 13 changed files with 234 additions and 10 deletions.
2 changes: 1 addition & 1 deletion app/Http/Controllers/IdeaController.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class IdeaController extends Controller
public function index()
{
return view('idea.index', [
'ideas' => Idea::with('user', 'category')
'ideas' => Idea::with('user', 'category', 'status')
->simplePaginate(Idea::PAGINATION_COUNT),
]);
}
Expand Down
85 changes: 85 additions & 0 deletions app/Http/Controllers/StatusController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

namespace App\Http\Controllers;

use App\Models\Status;
use Illuminate\Http\Request;

class StatusController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
//
}

/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
//
}

/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
//
}

/**
* Display the specified resource.
*
* @param \App\Models\Status $status
* @return \Illuminate\Http\Response
*/
public function show(Status $status)
{
//
}

/**
* Show the form for editing the specified resource.
*
* @param \App\Models\Status $status
* @return \Illuminate\Http\Response
*/
public function edit(Status $status)
{
//
}

/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\Models\Status $status
* @return \Illuminate\Http\Response
*/
public function update(Request $request, Status $status)
{
//
}

/**
* Remove the specified resource from storage.
*
* @param \App\Models\Status $status
* @return \Illuminate\Http\Response
*/
public function destroy(Status $status)
{
//
}
}
5 changes: 5 additions & 0 deletions app/Models/Idea.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,9 @@ public function category()
{
return $this->belongsTo(Category::class);
}

public function status()
{
return $this->belongsTo(Status::class);
}
}
29 changes: 29 additions & 0 deletions app/Models/Status.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace App\Models;

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

class Status extends Model
{
use HasFactory;

public function ideas()
{
return $this->hasMany(Idea::class);
}

public function getStatusClasses()
{
$allStatuses = [
'Open' => 'bg-blue-200',
'Considering' => 'bg-purple-500 text-white',
'In Progress' => 'bg-yellow-500 text-white',
'Implemented' => 'bg-green-500 text-white',
'Closed' => 'bg-gray-800 text-white',
];

return $allStatuses[$this->name];
}
}
1 change: 1 addition & 0 deletions database/factories/IdeaFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public function definition()
return [
'user_id' => User::factory(),
'category_id' => $this->faker->numberBetween(1, 4),
'status_id' => $this->faker->numberBetween(1, 5),
'title' => ucwords($this->faker->words(4, true)),
'description' => $this->faker->paragraph(5),
];
Expand Down
28 changes: 28 additions & 0 deletions database/factories/StatusFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Database\Factories;

use App\Models\Status;
use Illuminate\Database\Eloquent\Factories\Factory;

class StatusFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = Status::class;

/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
return [
'name' => $this->faker->words(2, true),
];
}
}
32 changes: 32 additions & 0 deletions database/migrations/2021_04_23_075452_create_statuses_table.php
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;

class CreateStatusesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('statuses', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('statuses');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public function up()
$table->id();
$table->foreignId('user_id')->constrained();
$table->foreignId('category_id')->constrained();
$table->foreignId('status_id')->constrained();
$table->string('title');
$table->string('slug')->nullable;
$table->text('description');
Expand Down
9 changes: 8 additions & 1 deletion database/seeders/DatabaseSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

namespace Database\Seeders;

use App\Models\Category;
use App\Models\Idea;
use App\Models\Status;
use App\Models\Category;
use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder
Expand All @@ -20,6 +21,12 @@ public function run()
Category::factory()->create(['name' => 'Category 3']);
Category::factory()->create(['name' => 'Category 4']);

Status::factory()->create(['name' => 'Open']);
Status::factory()->create(['name' => 'Considering']);
Status::factory()->create(['name' => 'In Progress']);
Status::factory()->create(['name' => 'Implemented']);
Status::factory()->create(['name' => 'Closed']);

Idea::factory(30)->create();
}
}
18 changes: 18 additions & 0 deletions database/seeders/StatusSeeder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;

class StatusSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
//
}
}
2 changes: 1 addition & 1 deletion resources/views/idea/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class="idea-container hover:shadow-md transition duration-150 ease-in bg-white r
<div
x-data="{ isOpen: false}"
class="mt-2 md:md-0 flex items-center space-x-2">
<button class="bg-gray-200 text-xs font-bold uppercase leading-none border border-gray-300 rounded-full text-center w-28 h-7 py-2 px-4">OPEN</button>
<button class="{{ $idea->status->getStatusClasses() }} text-xs font-bold uppercase leading-none rounded-full text-center w-28 h-7 py-2 px-4">{{$idea->status->name}}</button>
<button
@click="isOpen = !isOpen"
class="relative bg-gray-100 hover:bg-gray-200 transition duration-150 ease-in rounded-full h-7 py-2 px-3">
Expand Down
4 changes: 2 additions & 2 deletions resources/views/idea/show.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
x-data="{ isOpen: false}"
class="flex items-center space-x-2 mt-4 md:mt-0"
>
<div class="bg-gray-200 text-xs font-bold uppercase leading-none border border-gray-300 rounded-full text-center w-28 h-7 py-2 px-4">OPEN</div>
<div class="{{ $idea->status->getStatusClasses() }} text-xs font-bold uppercase leading-none rounded-full text-center w-28 h-7 py-2 px-4">{{$idea->status->name}}</div>
<button
@click="isOpen = !isOpen"
class="relative bg-gray-100 hover:bg-gray-200 transition duration-150 ease-in rounded-full h-7 py-2 px-3">
Expand Down Expand Up @@ -65,7 +65,7 @@ class="w-20 bg-gray-200 border border-gray-200 font-bold text-xxs uppercase roun
</div>
</div>
<div class="flex items-center space-x-2 hidden">
<div class="bg-gray-200 text-xs font-bold uppercase leading-none border border-gray-300 rounded-full text-center w-28 h-7 py-2 px-4">OPEN</div>
<div class="bg-gray-200 text-xs font-bold uppercase leading-none border border-gray-300 rounded-full text-center w-28 h-7 py-2 px-4">{{$idea->category->name}}</div>
<button class="relative bg-gray-100 hover:bg-gray-200 transition duration-150 ease-in rounded-full h-7 py-2 px-3">
<svg fill="currentColor" width="24" height="6"><path d="M2.97.061A2.969 2.969 0 000 3.031 2.968 2.968 0 002.97 6a2.97 2.97 0 100-5.94zm9.184 0a2.97 2.97 0 100 5.939 2.97 2.97 0 100-5.939zm8.877 0a2.97 2.97 0 10-.003 5.94A2.97 2.97 0 0021.03.06z"></path></svg>
<ul class="absolute w-44 text-left font-semibold bg-white shadow-lg rounded-xl py-3 ml-8">
Expand Down
28 changes: 23 additions & 5 deletions tests/Feature/ShowIdeasTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Tests\TestCase;
use App\Models\Idea;
use App\Models\Status;
use App\Models\Category;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;
Expand All @@ -18,15 +19,20 @@ public function test_list_of_ideas_shows_on_main_page()
$categoryOne = Category::factory()->create(['name' => 'Category 1']);
$categoryTwo = Category::factory()->create(['name' => 'Category 2']);

$statusOpen = Status::factory()->create(['name' => 'Open']);
$statusConsidering = Status::factory()->create(['name' => 'Considering']);

$ideaOne = Idea::factory()->create([
'title' => 'My First Idea',
'category_id' => $categoryOne->id,
'description' => 'Description of my First Idea'
'title' => 'My First Idea',
'category_id' => $categoryOne->id,
'status_id' => $statusOpen->id,
'description' => 'Description of my First Idea'
]);

$ideaTwo = Idea::factory()->create([
'title' => 'My Second Idea',
'category_id' => $categoryTwo->id,
'status_id' => $statusConsidering->id,
'description' => 'Description of my Second Idea'
]);

Expand All @@ -38,20 +44,25 @@ public function test_list_of_ideas_shows_on_main_page()
$response->assertSee($ideaOne->title);
$response->assertSee($ideaOne->description);
$response->assertSee($categoryOne->name);
$response->assertSee('<button class="bg-blue-200 text-xs font-bold uppercase leading-none rounded-full text-center w-28 h-7 py-2 px-4">Open</button>', false);

$response->assertSee($ideaTwo->title);
$response->assertSee($ideaTwo->description);
$response->assertSee($categoryTwo->name);
$response->assertSee('<button class="bg-purple-500 text-white text-xs font-bold uppercase leading-none rounded-full text-center w-28 h-7 py-2 px-4">Considering</button>', false);
}

/* @test */
public function test_single_idea_shows_correctly_on_show_apge()
public function test_single_idea_shows_correctly_on_show_page()
{
$categoryOne = Category::factory()->create(['name' => 'Category 1']);

$statusOpen = Status::factory()->create(['name' => 'Open']);

$idea = Idea::factory()->create([
'title' => 'My First Idea',
'category_id' => $categoryOne->id,
'status_id' => $statusOpen->id,
'title' => 'My First Idea',
'description' => 'Description of my First Idea'
]);

Expand All @@ -62,16 +73,19 @@ public function test_single_idea_shows_correctly_on_show_apge()
$response->assertSee($idea->title);
$response->assertSee($categoryOne->name);
$response->assertSee($idea->description);
$response->assertSee('<div class="bg-blue-200 text-xs font-bold uppercase leading-none rounded-full text-center w-28 h-7 py-2 px-4">Open</div>', false);
}

/** @test */
public function ideas_pagination_works()
{
$categoryOne = Category::factory()->create(['name' => 'Category 1']);

$statusOpen = Status::factory()->create(['name' => 'Open']);

Idea::factory(Idea::PAGINATION_COUNT + 1)->create([
'category_id' => $categoryOne->id,
'status_id' => $statusOpen->id,
]);

$ideaOne = Idea::find(1);
Expand All @@ -98,15 +112,19 @@ public function same_idea_title_different_slugs()
{
$categoryOne = Category::factory()->create(['name' => 'Category 1']);

$statusOpen = Status::factory()->create(['name' => 'Open']);

$ideaOne = Idea::factory()->create([
'category_id' => $categoryOne->id,
'title' => 'My First Idea',
'status_id' => $statusOpen->id,
'description' => 'Description for my first idea',
]);

$ideaTwo = Idea::factory()->create([
'category_id' => $categoryOne->id,
'title' => 'My First Idea',
'status_id' => $statusOpen->id,
'description' => 'Another Description for my first idea',
]);

Expand Down

0 comments on commit 30b2af6

Please sign in to comment.