diff --git a/app/Http/Controllers/TopicsController.php b/app/Http/Controllers/TopicsController.php new file mode 100644 index 000000000..f97eaafe4 --- /dev/null +++ b/app/Http/Controllers/TopicsController.php @@ -0,0 +1,60 @@ +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 000000000..a015f3d19 --- /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 000000000..1a7eb5418 --- /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 000000000..1a8890b0e --- /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 000000000..b67ba0efb --- /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/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index ee8ca5bcd..330b6b265 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -22,7 +22,10 @@ public function register() * @return void */ public function boot() - { + { + \App\Models\User::observe(\App\Observers\UserObserver::class); + \App\Models\Topic::observe(\App\Observers\TopicObserver::class); + // } } diff --git a/app/Providers/AuthServiceProvider.php b/app/Providers/AuthServiceProvider.php index 07b2d72ef..5a84ff44f 100644 --- a/app/Providers/AuthServiceProvider.php +++ b/app/Providers/AuthServiceProvider.php @@ -13,6 +13,7 @@ class AuthServiceProvider extends ServiceProvider * @var array */ protected $policies = [ + \App\Models\Topic::class => \App\Policies\TopicPolicy::class, // 'App\Model' => 'App\Policies\ModelPolicy', ]; diff --git a/database/factories/TopicFactory.php b/database/factories/TopicFactory.php new file mode 100644 index 000000000..25f3e9ac9 --- /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/2019_09_20_065528_create_topics_table.php b/database/migrations/2019_09_20_065528_create_topics_table.php new file mode 100644 index 000000000..eb8cd0b2b --- /dev/null +++ b/database/migrations/2019_09_20_065528_create_topics_table.php @@ -0,0 +1,30 @@ +increments('id'); + $table->string('title')->index(); + $table->text('body'); + $table->bigInteger('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')->nullable(); + $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 91cb6d1c2..72257df86 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 000000000..e629dee11 --- /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 000000000..cbc937e42 --- /dev/null +++ b/resources/views/common/error.blade.php @@ -0,0 +1,10 @@ +@if (count($errors) > 0) +
+
有错误发生:
+ +
+@endif 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 000000000..373483afb --- /dev/null +++ b/resources/views/topics/create_and_edit.blade.php @@ -0,0 +1,84 @@ +@extends('layouts.app') + +@section('content') + +
+
+
+ +
+

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

+
+ +
+ @if($topic->id) +
+ + @else + + @endif + + @include('common.error') + + + + +
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+ +
+ + <- Back +
+
+
+
+
+
+ +@endsection diff --git a/resources/views/topics/index.blade.php b/resources/views/topics/index.blade.php new file mode 100644 index 000000000..cb9690632 --- /dev/null +++ b/resources/views/topics/index.blade.php @@ -0,0 +1,61 @@ +@extends('layouts.app') + +@section('content') +
+
+
+
+

+ Topic + Create +

+
+ +
+ @if($topics->count()) + + + + + + + + + + + @foreach($topics as $topic) + + + + + + + + @endforeach + +
#Title Body User_id Category_id Reply_count View_count Last_reply_user_id Order Excerpt SlugOPTIONS
{{$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}} + + V + + + + E + + +
+ {{csrf_field()}} + + + +
+
+ {!! $topics->render() !!} + @else +

Empty!

+ @endif +
+
+
+
+ +@endsection diff --git a/resources/views/topics/show.blade.php b/resources/views/topics/show.blade.php new file mode 100644 index 000000000..425e48a3d --- /dev/null +++ b/resources/views/topics/show.blade.php @@ -0,0 +1,63 @@ +@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 c46483bcd..f944e4e6e 100644 --- a/routes/web.php +++ b/routes/web.php @@ -23,3 +23,5 @@ Route::post('email/resend', 'Auth\VerificationController@resend')->name('verification.resend'); Route::resource('users', 'UsersController', ['only' => ['show', 'update', 'edit']]); + +Route::resource('topics', 'TopicsController', ['only' => ['index', 'show', 'create', 'store', 'update', 'edit', 'destroy']]); \ No newline at end of file