Skip to content

Commit

Permalink
Add chapter-4
Browse files Browse the repository at this point in the history
  • Loading branch information
suzukalight committed Jul 23, 2020
1 parent f68ad38 commit 80d7372
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 2 deletions.
5 changes: 4 additions & 1 deletion backend/app/Folder.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,8 @@

class Folder extends Model
{
//
public function tasks()
{
return $this->hasMany('App\Task');
}
}
10 changes: 9 additions & 1 deletion backend/app/Http/Controllers/TaskController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,24 @@
namespace App\Http\Controllers;

use App\Folder;
use App\Task;
use Illuminate\Http\Request;

class TaskController extends Controller
{
public function index()
public function index(int $id)
{
$folders = Folder::all();

$current_folder = Folder::find($id);

// $tasks = Task::where('folder_id', $current_folder->id)->get();
$tasks = $current_folder->tasks()->get();

return view('tasks/index', [
'folders' => $folders,
'current_folder_id' => $current_folder->id,
'tasks' => $tasks,
]);
}
}
42 changes: 42 additions & 0 deletions backend/app/Task.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace App;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;

class Task extends Model
{
const STATUS = [
1 => ['label' => '未着手', 'class' => 'label-danger'],
2 => ['label' => '着手中', 'class' => 'label-info'],
3 => ['label' => '完了', 'class' => ''],
];

public function getStatusLabelAttribute()
{
$status = $this->attributes['status'];

if (!isset(self::STATUS[$status])) {
return '';
}

return self::STATUS[$status]['label'];
}

public function getStatusClassAttribute()
{
$status = $this->attributes['status'];

if (!isset(self::STATUS[$status])) {
return '';
}

return self::STATUS[$status]['class'];
}

public function getFormattedDueDateAttribute()
{
return Carbon::createFromFormat('Y-m-d', $this->attributes['due_date'])->format('Y/m/d');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateTasksTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('tasks', function (Blueprint $table) {
$table->increments('id');
$table->integer('folder_id')->unsigned();
$table->string('title', 100);
$table->date('due_date');
$table->integer('status')->default(1);
$table->timestamps();

$table->foreign('folder_id')->references('id')->on('folders');
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('tasks');
}
}
27 changes: 27 additions & 0 deletions backend/database/seeds/TasksTableSeeder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Carbon\Carbon;

class TasksTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
foreach (range(1, 3) as $num) {
DB::table('tasks')->insert([
'folder_id' => 1,
'title' => "サンプルタスク {$num}",
'status' => $num,
'due_date' => Carbon::now()->addDay($num),
'created_at' => Carbon::now(),
'updated_at' => Carbon::now(),
]);
}
}
}
32 changes: 32 additions & 0 deletions backend/resources/views/tasks/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,38 @@
</div>
<div class="column col-md-8">
<!-- ここにタスクが表示される -->
<div class="panel panel-default">
<div class="panel-heading">タスク</div>
<div class="panel-body">
<div class="text-right">
<a href="#" class="btn btn-default btn-block">
タスクを追加する
</a>
</div>
</div>
<table class="table">
<thead>
<tr>
<th>タイトル</th>
<th>状態</th>
<th>期限</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach($tasks as $task)
<tr>
<td>{{ $task->title }}</td>
<td>
<span class="label {{ $task->status_class }}">{{ $task->status_label }}</span>
</td>
<td>{{ $task->formatted_due_date }}</td>
<td><a href="#">編集</a></td>
</tr>
@endforeach
</tbody>
</table>
</div>
</div>
</div>
</div>
Expand Down

0 comments on commit 80d7372

Please sign in to comment.