Skip to content

Commit

Permalink
生成话题骨架
Browse files Browse the repository at this point in the history
  • Loading branch information
summerblue committed Sep 19, 2019
1 parent 3535254 commit 3eaa3ee
Show file tree
Hide file tree
Showing 20 changed files with 511 additions and 1 deletion.
60 changes: 60 additions & 0 deletions app/Http/Controllers/TopicsController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace App\Http\Controllers;

use App\Models\Topic;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Requests\TopicRequest;

class TopicsController extends Controller
{
public function __construct()
{
$this->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.');
}
}
14 changes: 14 additions & 0 deletions app/Http/Requests/Request.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class Request extends FormRequest
{
public function authorize()
{
// Using policy for Authorization
return true;
}
}
41 changes: 41 additions & 0 deletions app/Http/Requests/TopicRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace App\Http\Requests;

class TopicRequest extends Request
{
public function rules()
{
switch($this->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
];
}
}
19 changes: 19 additions & 0 deletions app/Models/Model.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model as EloquentModel;

class Model extends EloquentModel
{
public function scopeRecent($query)
{
return $query->orderBy('id', 'desc');
}

public function scopeOrdered($query)
{
return $query->orderBy('order', 'desc');
}

}
8 changes: 8 additions & 0 deletions app/Models/Topic.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace App\Models;

class Topic extends Model
{
protected $fillable = ['title', 'body', 'user_id', 'category_id', 'reply_count', 'view_count', 'last_reply_user_id', 'order', 'excerpt', 'slug'];
}
21 changes: 21 additions & 0 deletions app/Observers/TopicObserver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace App\Observers;

use App\Models\Topic;

// creating, created, updating, updated, saving,
// saved, deleting, deleted, restoring, restored

class TopicObserver
{
public function creating(Topic $topic)
{
//
}

public function updating(Topic $topic)
{
//
}
}
21 changes: 21 additions & 0 deletions app/Observers/UserObserver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace App\Observers;

use App\Models\User;

// creating, created, updating, updated, saving,
// saved, deleting, deleted, restoring, restored

class UserObserver
{
public function creating(User $user)
{
//
}

public function updating(User $user)
{
//
}
}
22 changes: 22 additions & 0 deletions app/Policies/Policy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace App\Policies;

use Illuminate\Auth\Access\HandlesAuthorization;

class Policy
{
use HandlesAuthorization;

public function __construct()
{
//
}

public function before($user, $ability)
{
// if ($user->isSuperAdmin()) {
// return true;
// }
}
}
20 changes: 20 additions & 0 deletions app/Policies/TopicPolicy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace App\Policies;

use App\Models\User;
use App\Models\Topic;

class TopicPolicy extends Policy
{
public function update(User $user, Topic $topic)
{
// return $topic->user_id == $user->id;
return true;
}

public function destroy(User $user, Topic $topic)
{
return true;
}
}
5 changes: 4 additions & 1 deletion app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

//
}
}
1 change: 1 addition & 0 deletions app/Providers/AuthServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
];

Expand Down
9 changes: 9 additions & 0 deletions database/factories/TopicFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

use Faker\Generator as Faker;

$factory->define(App\Models\Topic::class, function (Faker $faker) {
return [
// 'name' => $faker->name,
];
});
30 changes: 30 additions & 0 deletions database/migrations/2019_09_20_065528_create_topics_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

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

class CreateTopicsTable extends Migration
{
public function up()
{
Schema::create('topics', function(Blueprint $table) {
$table->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');
}
}
1 change: 1 addition & 0 deletions database/seeds/DatabaseSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ class DatabaseSeeder extends Seeder
public function run()
{
// $this->call(UsersTableSeeder::class);
$this->call(TopicsTableSeeder::class);
}
}
20 changes: 20 additions & 0 deletions database/seeds/TopicsTableSeeder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

use Illuminate\Database\Seeder;
use App\Models\Topic;

class TopicsTableSeeder extends Seeder
{
public function run()
{
$topics = factory(Topic::class)->times(50)->make()->each(function ($topic, $index) {
if ($index == 0) {
// $topic->field = 'value';
}
});

Topic::insert($topics->toArray());
}

}

10 changes: 10 additions & 0 deletions resources/views/common/error.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
@if (count($errors) > 0)
<div class="alert alert-danger">
<div class="mt-2"><b>有错误发生:</b></div>
<ul class="mt-2 mb-2">
@foreach ($errors->all() as $error)
<li><i class="glyphicon glyphicon-remove"></i> {{ $error }}</li>
@endforeach
</ul>
</div>
@endif
Loading

0 comments on commit 3eaa3ee

Please sign in to comment.