Permalink
Browse files

建立 Todo

  • Loading branch information...
1 parent 9b53ecd commit 853c57dac7bf0a4d239123cd508037ed81372b6a Sam.IO.Hsiao committed Oct 15, 2016
@@ -0,0 +1,66 @@
+<?php
+
+declare(strict_types = 1);
+
+namespace App\Http\Controllers;
+
+use App\Http\Requests\StoreTaskRequest;
+use App\Services\TaskService;
+use App\Task;
+use Illuminate\Http\Response;
+
+class TasksController extends Controller
+{
+ /** @var TaskService */
+ private $taskService;
+
+ /**
+ * TasksController constructor.
+ * @param TaskService $taskService
+ */
+ public function __construct(TaskService $taskService)
+ {
+ $this->taskService = $taskService;
+ }
+
+ /**
+ * Display a listing of the resource.
+ * @return Response
+ */
+ public function index()
+ {
+ $data['tasks'] = $this->taskService->index();
+
+ return view('tasks', $data);
+ }
+
+ /**
+ * Store a newly created resource in storage.
+ * @param StoreTaskRequest $request
+ * @return Response
+ */
+ public function store(StoreTaskRequest $request)
+ {
+ $task = new Task();
+ $task->name = $request['name'];
+
+ $this->taskService->store($task);
+
+ return redirect('/todo');
+ }
+
+ /**
+ * Remove the specified resource from storage.
+ * @param int $id
+ * @return Response
+ */
+ public function destroy(int $id)
+ {
+ $task = new Task();
+ $task->id = $id;
+
+ $this->taskService->destroy($task);
+
+ return redirect('/todo');
+ }
+}
@@ -0,0 +1,30 @@
+<?php
+
+namespace App\Http\Requests;
+
+use Illuminate\Foundation\Http\FormRequest;
+
+class StoreTaskRequest 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
+ */
+ public function rules()
+ {
+ return [
+ 'name' => 'required|max:255',
+ ];
+ }
+}
@@ -0,0 +1,40 @@
+<?php
+
+declare(strict_types = 1);
+
+namespace App\Repositories;
+
+use App\Task;
+use Illuminate\Database\Eloquent\Collection;
+
+class TaskRepository
+{
+ /**
+ * @return Collection
+ */
+ public function index() : Collection
+ {
+ return Task::orderBy('created_at', 'asc')
+ ->get();
+ }
+
+ /**
+ * @param Task $task
+ * @return Task
+ */
+ public function store(Task $task) : Task
+ {
+ $task->save();
+
+ return $task;
+ }
+
+ /**
+ * @param Task $task
+ */
+ public function destroy(Task $task)
+ {
+ Task::findOrFail($task->id)
+ ->delete();
+ }
+}
@@ -0,0 +1,49 @@
+<?php
+
+declare(strict_types = 1);
+
+namespace App\Services;
+
+use App\Repositories\TaskRepository;
+use App\Task;
+use Illuminate\Database\Eloquent\Collection;
+
+class TaskService
+{
+ /** @var TaskRepository */
+ private $taskRepository;
+
+ /**
+ * TaskService constructor.
+ * @param TaskRepository $taskRepository
+ */
+ public function __construct(TaskRepository $taskRepository)
+ {
+ $this->taskRepository = $taskRepository;
+ }
+
+ /**
+ * @return Collection
+ */
+ public function index() : Collection
+ {
+ return $this->taskRepository->index();
+ }
+
+ /**
+ * @param Task $task
+ * @return Task
+ */
+ public function store(Task $task)
+ {
+ $this->taskRepository->store($task);
+ }
+
+ /**
+ * @param Task $task
+ */
+ public function destroy(Task $task)
+ {
+ $this->taskRepository->destroy($task);
+ }
+}
View
@@ -0,0 +1,25 @@
+<?php
+
+namespace App;
+
+use Illuminate\Database\Eloquent\Model;
+
+/**
+ * App\Task
+ *
+ * @property integer $id
+ * @property string $name
+ * @property \Carbon\Carbon $created_at
+ * @property \Carbon\Carbon $updated_at
+ * @method static \Illuminate\Database\Query\Builder|\App\Task whereId($value)
+ * @method static \Illuminate\Database\Query\Builder|\App\Task whereName($value)
+ * @method static \Illuminate\Database\Query\Builder|\App\Task whereCreatedAt($value)
+ * @method static \Illuminate\Database\Query\Builder|\App\Task whereUpdatedAt($value)
+ * @mixin \Eloquent
+ */
+class Task extends Model
+{
+ protected $fillable = [
+ 'name'
+ ];
+}
View
@@ -2,15 +2,29 @@
namespace App;
-use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
+use Illuminate\Notifications\Notifiable;
/**
* App\User
*
* @property-read \Illuminate\Notifications\DatabaseNotificationCollection|\Illuminate\Notifications\DatabaseNotification[] $notifications
* @property-read \Illuminate\Notifications\DatabaseNotificationCollection|\Illuminate\Notifications\DatabaseNotification[] $unreadNotifications
* @mixin \Eloquent
+ * @property integer $id
+ * @property string $name
+ * @property string $email
+ * @property string $password
+ * @property string $remember_token
+ * @property \Carbon\Carbon $created_at
+ * @property \Carbon\Carbon $updated_at
+ * @method static \Illuminate\Database\Query\Builder|\App\User whereId($value)
+ * @method static \Illuminate\Database\Query\Builder|\App\User whereName($value)
+ * @method static \Illuminate\Database\Query\Builder|\App\User whereEmail($value)
+ * @method static \Illuminate\Database\Query\Builder|\App\User wherePassword($value)
+ * @method static \Illuminate\Database\Query\Builder|\App\User whereRememberToken($value)
+ * @method static \Illuminate\Database\Query\Builder|\App\User whereCreatedAt($value)
+ * @method static \Illuminate\Database\Query\Builder|\App\User whereUpdatedAt($value)
*/
class User extends Authenticatable
{
Binary file not shown.
@@ -11,13 +11,8 @@
|
*/
-$factory->define(App\User::class, function (Faker\Generator $faker) {
- static $password;
-
+$factory->define(App\Task::class, function (Faker\Generator $faker) {
return [
- 'name' => $faker->name,
- 'email' => $faker->unique()->safeEmail,
- 'password' => $password ?: $password = bcrypt('secret'),
- 'remember_token' => str_random(10),
+ 'name' => $faker->title,
];
});
@@ -0,0 +1,31 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+
+class CreateTasksTable extends Migration
+{
+ /**
+ * Run the migrations.
+ *
+ * @return void
+ */
+ public function up()
+ {
+ Schema::create('tasks', function (Blueprint $table) {
+ $table->increments('id');
+ $table->string('name');
+ $table->timestamps();
+ });
+ }
+
+ /**
+ * Reverse the migrations.
+ *
+ * @return void
+ */
+ public function down()
+ {
+ Schema::drop('tasks');
+ }
+}
@@ -0,0 +1,14 @@
+@if (count($errors) > 0)
+ <!-- Form Error List -->
+ <div class="alert alert-danger">
+ <strong>Whoops! Something went wrong!</strong>
+
+ <br><br>
+
+ <ul>
+ @foreach ($errors->all() as $error)
+ <li>{{ $error }}</li>
+ @endforeach
+ </ul>
+ </div>
+@endif
@@ -0,0 +1,48 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+ <meta charset="utf-8">
+ <meta http-equiv="X-UA-Compatible" content="IE=edge">
+ <meta name="viewport" content="width=device-width, initial-scale=1">
+
+ <title>Laravel Quickstart - Basic</title>
+
+ <!-- Fonts -->
+ <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.4.0/css/font-awesome.min.css" rel='stylesheet'
+ type='text/css'>
+ <link href="https://fonts.googleapis.com/css?family=Lato:100,300,400,700" rel='stylesheet' type='text/css'>
+
+ <!-- Styles -->
+ <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">
+{{-- <link href="{{ elixir('css/app.css') }}" rel="stylesheet"> --}}
+
+<!-- jQuery -->
+ <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
+
+ <style>
+ body {
+ font-family: 'Lato';
+ }
+
+ .fa-btn {
+ margin-right: 6px;
+ }
+ </style>
+</head>
+<body id="app-layout">
+<nav class="navbar navbar-default">
+ <div class="container">
+ <div class="navbar-header">
+
+ <!-- Branding Image -->
+ <a class="navbar-brand" href="{{ url('/') }}">
+ Task List
+ </a>
+ </div>
+
+ </div>
+</nav>
+
+@yield('content')
+</body>
+</html>
Oops, something went wrong.

0 comments on commit 853c57d

Please sign in to comment.