From 3c525feee1e6816e579b0c45529c3f1770b46871 Mon Sep 17 00:00:00 2001 From: fthvgb1 Date: Mon, 1 Jan 2018 12:00:45 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=AF=9D=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Http/Controllers/TopicsController.php | 58 +++++++++++ app/Http/Requests/Request.php | 14 +++ app/Http/Requests/TopicRequest.php | 40 ++++++++ app/Models/Model.php | 19 ++++ app/Models/Topic.php | 8 ++ app/Observers/TopicObserver.php | 21 ++++ app/Observers/UserObserver.php | 21 ++++ app/Policies/Policy.php | 22 +++++ app/Policies/TopicPolicy.php | 20 ++++ app/Providers/AuthServiceProvider.php | 1 + database/factories/TopicFactory.php | 9 ++ .../2017_12_31_131710_create_topics_table.php | 30 ++++++ database/seeds/DatabaseSeeder.php | 1 + database/seeds/TopicsTableSeeder.php | 20 ++++ resources/views/common/error.blade.php | 10 ++ .../views/topics/create_and_edit.blade.php | 98 +++++++++++++++++++ resources/views/topics/index.blade.php | 83 ++++++++++++++++ resources/views/topics/show.blade.php | 64 ++++++++++++ routes/web.php | 3 +- 19 files changed, 541 insertions(+), 1 deletion(-) create mode 100644 app/Http/Controllers/TopicsController.php create mode 100644 app/Http/Requests/Request.php create mode 100644 app/Http/Requests/TopicRequest.php create mode 100644 app/Models/Model.php create mode 100644 app/Models/Topic.php create mode 100644 app/Observers/TopicObserver.php create mode 100644 app/Observers/UserObserver.php create mode 100644 app/Policies/Policy.php create mode 100644 app/Policies/TopicPolicy.php create mode 100644 database/factories/TopicFactory.php create mode 100644 database/migrations/2017_12_31_131710_create_topics_table.php create mode 100644 database/seeds/TopicsTableSeeder.php create mode 100644 resources/views/common/error.blade.php create mode 100644 resources/views/topics/create_and_edit.blade.php create mode 100644 resources/views/topics/index.blade.php create mode 100644 resources/views/topics/show.blade.php diff --git a/app/Http/Controllers/TopicsController.php b/app/Http/Controllers/TopicsController.php new file mode 100644 index 0000000..f7cb3e1 --- /dev/null +++ b/app/Http/Controllers/TopicsController.php @@ -0,0 +1,58 @@ +middleware('auth', ['except' => ['index', 'show']]); + } + + public function index() + { + $topics = Topic::paginate(); + return view('topics.index', compact('topics')); + } + + public function show(Topic $topic) + { + return view('topics.show', compact('topic')); + } + + public function create(Topic $topic) + { + return view('topics.create_and_edit', compact('topic')); + } + + public function store(TopicRequest $request) + { + $topic = Topic::create($request->all()); + return redirect()->route('topics.show', $topic->id)->with('message', 'Created successfully.'); + } + + public function edit(Topic $topic) + { + $this->authorize('update', $topic); + return view('topics.create_and_edit', compact('topic')); + } + + public function update(TopicRequest $request, Topic $topic) + { + $this->authorize('update', $topic); + $topic->update($request->all()); + + return redirect()->route('topics.show', $topic->id)->with('message', 'Updated successfully.'); + } + + public function destroy(Topic $topic) + { + $this->authorize('destroy', $topic); + $topic->delete(); + + return redirect()->route('topics.index')->with('message', 'Deleted successfully.'); + } +} \ No newline at end of file diff --git a/app/Http/Requests/Request.php b/app/Http/Requests/Request.php new file mode 100644 index 0000000..4980076 --- /dev/null +++ b/app/Http/Requests/Request.php @@ -0,0 +1,14 @@ +method()) { + // CREATE + case 'POST': + { + return [ + // CREATE ROLES + ]; + } + // UPDATE + case 'PUT': + case 'PATCH': + { + return [ + // UPDATE ROLES + ]; + } + case 'GET': + case 'DELETE': + default: + { + return []; + }; + } + } + + public function messages() + { + return [ + // Validation messages + ]; + } +} diff --git a/app/Models/Model.php b/app/Models/Model.php new file mode 100644 index 0000000..1a7eb54 --- /dev/null +++ b/app/Models/Model.php @@ -0,0 +1,19 @@ +orderBy('id', 'desc'); + } + + public function scopeOrdered($query) + { + return $query->orderBy('order', 'desc'); + } + +} diff --git a/app/Models/Topic.php b/app/Models/Topic.php new file mode 100644 index 0000000..7e9d8c6 --- /dev/null +++ b/app/Models/Topic.php @@ -0,0 +1,8 @@ +isSuperAdmin()) { + // return true; + // } + } +} diff --git a/app/Policies/TopicPolicy.php b/app/Policies/TopicPolicy.php new file mode 100644 index 0000000..2a95543 --- /dev/null +++ b/app/Policies/TopicPolicy.php @@ -0,0 +1,20 @@ +user_id == $user->id; + return true; + } + + public function destroy(User $user, Topic $topic) + { + return true; + } +} diff --git a/app/Providers/AuthServiceProvider.php b/app/Providers/AuthServiceProvider.php index 687c65c..5c148bb 100644 --- a/app/Providers/AuthServiceProvider.php +++ b/app/Providers/AuthServiceProvider.php @@ -14,6 +14,7 @@ class AuthServiceProvider extends ServiceProvider * @var array */ protected $policies = [ + \App\Models\Topic::class => \App\Policies\TopicPolicy::class, 'App\Model' => 'App\Policies\ModelPolicy', User::class => UserPolicy::class, ]; diff --git a/database/factories/TopicFactory.php b/database/factories/TopicFactory.php new file mode 100644 index 0000000..25f3e9a --- /dev/null +++ b/database/factories/TopicFactory.php @@ -0,0 +1,9 @@ +define(App\Models\Topic::class, function (Faker $faker) { + return [ + // 'name' => $faker->name, + ]; +}); diff --git a/database/migrations/2017_12_31_131710_create_topics_table.php b/database/migrations/2017_12_31_131710_create_topics_table.php new file mode 100644 index 0000000..1217347 --- /dev/null +++ b/database/migrations/2017_12_31_131710_create_topics_table.php @@ -0,0 +1,30 @@ +increments('id'); + $table->string('"title')->index(); + $table->text('body'); + $table->integer('user_id')->unsigned()->index(); + $table->integer('category_id')->unsigned()->index(); + $table->integer('reply_count')->unsigned()->default(0); + $table->integer('view_count')->unsigned()->default(0); + $table->integer('last_reply_user_id')->unsigned()->default(0); + $table->integer('order')->unsigned()->default(0); + $table->text('excerpt'); + $table->string('slug')->nullable(); + $table->timestamps(); + }); + } + + public function down() + { + Schema::drop('topics'); + } +} diff --git a/database/seeds/DatabaseSeeder.php b/database/seeds/DatabaseSeeder.php index e119db6..cacec49 100644 --- a/database/seeds/DatabaseSeeder.php +++ b/database/seeds/DatabaseSeeder.php @@ -12,5 +12,6 @@ class DatabaseSeeder extends Seeder public function run() { // $this->call(UsersTableSeeder::class); + $this->call(TopicsTableSeeder::class); } } diff --git a/database/seeds/TopicsTableSeeder.php b/database/seeds/TopicsTableSeeder.php new file mode 100644 index 0000000..8588842 --- /dev/null +++ b/database/seeds/TopicsTableSeeder.php @@ -0,0 +1,20 @@ +times(50)->make()->each(function ($topic, $index) { + if ($index == 0) { + // $topic->field = 'value'; + } + }); + + Topic::insert($topics->toArray()); + } + +} + diff --git a/resources/views/common/error.blade.php b/resources/views/common/error.blade.php new file mode 100644 index 0000000..166b234 --- /dev/null +++ b/resources/views/common/error.blade.php @@ -0,0 +1,10 @@ +@if (count($errors) > 0) +
+

There were some problems with your input.

+ +
+@endif \ No newline at end of file diff --git a/resources/views/topics/create_and_edit.blade.php b/resources/views/topics/create_and_edit.blade.php new file mode 100644 index 0000000..475a5cf --- /dev/null +++ b/resources/views/topics/create_and_edit.blade.php @@ -0,0 +1,98 @@ +@extends('layouts.app') + +@section('content') + +
+
+
+ +
+

+ Topic / + @if($topic->id) + Edit #{{$topic->id}} + @else + Create + @endif +

+
+ + @include('common.error') + +
+ @if($topic->id) +
+ + @else + + @endif + + + + +
+ + title ) }}"/> +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+ +
+ + Back +
+
+
+
+
+
+ +@endsection \ No newline at end of file diff --git a/resources/views/topics/index.blade.php b/resources/views/topics/index.blade.php new file mode 100644 index 0000000..27f0bc6 --- /dev/null +++ b/resources/views/topics/index.blade.php @@ -0,0 +1,83 @@ +@extends('layouts.app') + +@section('content') +
+
+
+
+

+ Topic + Create +

+
+ +
+ @if($topics->count()) + + + + + + + + + + + + + + + + + + + + @foreach($topics as $topic) + + + + + + + + + + + + + + + + + @endforeach + +
#"titleBodyUser_idCategory_idReply_countView_countLast_reply_user_idOrderExcerptSlugOPTIONS
{{$topic->id}}{{$topic->title}}{{$topic->body}}{{$topic->user_id}}{{$topic->category_id}}{{$topic->reply_count}}{{$topic->view_count}}{{$topic->last_reply_user_id}}{{$topic->order}}{{$topic->excerpt}}{{$topic->slug}} + + + + + + + + +
+ {{csrf_field()}} + + + +
+
+ {!! $topics->render() !!} + @else +

Empty!

+ @endif +
+
+
+
+ +@endsection \ No newline at end of file diff --git a/resources/views/topics/show.blade.php b/resources/views/topics/show.blade.php new file mode 100644 index 0000000..1223c35 --- /dev/null +++ b/resources/views/topics/show.blade.php @@ -0,0 +1,64 @@ +@extends('layouts.app') + +@section('content') + +
+
+
+
+

Topic / Show #{{ $topic->id }}

+
+ +
+
+
+
+ Back +
+ +
+
+ + +

+ {{ $topic->title }} +

+

+ {{ $topic->body }} +

+

+ {{ $topic->user_id }} +

+

+ {{ $topic->category_id }} +

+

+ {{ $topic->reply_count }} +

+

+ {{ $topic->view_count }} +

+

+ {{ $topic->last_reply_user_id }} +

+

+ {{ $topic->order }} +

+

+ {{ $topic->excerpt }} +

+

+ {{ $topic->slug }} +

+
+
+
+
+ +@endsection diff --git a/routes/web.php b/routes/web.php index 91a0480..13f833f 100644 --- a/routes/web.php +++ b/routes/web.php @@ -31,4 +31,5 @@ Route::post('register', 'Auth\RegisterController@register'); Route::get('password/reset', 'Auth\ForgotPasswordController@showLinkRequestForm')->name('password.request'); Route::post('password/email', 'Auth\ForgotPasswordController@sendResetLinkEmail')->name('password.email'); Route::get('password/reset/{token}', 'Auth\ResetPasswordController@showResetForm')->name('password.reset'); -Route::post('password/reset', 'Auth\ResetPasswordController@reset'); \ No newline at end of file +Route::post('password/reset', 'Auth\ResetPasswordController@reset'); +Route::resource('topics', 'TopicsController', ['only' => ['index', 'show', 'create', 'store', 'update', 'edit', 'destroy']]); \ No newline at end of file