Skip to content

Commit

Permalink
Add user statuses
Browse files Browse the repository at this point in the history
  • Loading branch information
summerblue committed Dec 18, 2016
1 parent 4b988f1 commit aaa5525
Show file tree
Hide file tree
Showing 22 changed files with 342 additions and 18 deletions.
11 changes: 10 additions & 1 deletion app/Http/Controllers/StaticPagesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,20 @@
use App\Http\Requests;
use App\Http\Controllers\Controller;

use App\Models\Status;
use Auth;

class StaticPagesController extends Controller
{

public function home()
{
return view('static_pages/home');
$feed_items = [];
if (Auth::check()) {
$feed_items = Auth::user()->feed()->paginate(30);
}

return view('static_pages/home', compact('feed_items'));
}

public function help()
Expand Down
42 changes: 42 additions & 0 deletions app/Http/Controllers/StatusesController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use App\Models\Status;
use Auth;

class StatusesController extends Controller
{
public function __construct()
{
$this->middleware('auth', [
'only' => ['store', 'destroy']
]);
}

public function store(Request $request)
{
$this->validate($request, [
'content' => 'required|max:140'
]);

Auth::user()->statuses()->create([
'content' => $request->content
]);
return redirect()->back();
}

public function destroy($id)
{
$status = Status::findOrFail($id);
$this->authorize('destroy', $status);
$status->delete();
session()->flash('success', '微博已被成功删除!');
return redirect()->back();
}
}
5 changes: 4 additions & 1 deletion app/Http/Controllers/UsersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ public function create()
public function show($id)
{
$user = User::findOrFail($id);
return view('users.show', compact('user'));
$statuses = $user->statuses()
->orderBy('created_at', 'desc')
->paginate(30);
return view('users.show', compact('user', 'statuses'));
}

public function store(Request $request)
Expand Down
2 changes: 2 additions & 0 deletions app/Http/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@
post('password/email', 'Auth\PasswordController@postEmail')->name('password.reset');
get('password/reset/{token}', 'Auth\PasswordController@getReset')->name('password.edit');
post('password/reset', 'Auth\PasswordController@postReset')->name('password.update');

resource('statuses', 'StatusesController', ['only' => ['store', 'destroy']]);
15 changes: 15 additions & 0 deletions app/Models/Status.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Status extends Model
{
protected $fillable = ['content'];

public function user()
{
return $this->belongsTo(User::class);
}
}
11 changes: 11 additions & 0 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,15 @@ public function setPasswordAttribute($password)
{
$this->attributes['password'] = bcrypt($password);
}

public function statuses()
{
return $this->hasMany(Status::class);
}

public function feed()
{
return $this->statuses()
->orderBy('created_at', 'desc');
}
}
17 changes: 17 additions & 0 deletions app/Policies/StatusPolicy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace App\Policies;

use Illuminate\Auth\Access\HandlesAuthorization;
use App\Models\User;
use App\Models\Status;

class StatusPolicy
{
use HandlesAuthorization;

public function destroy(User $user, Status $status)
{
return $user->id === $status->user_id;
}
}
3 changes: 2 additions & 1 deletion app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Carbon\Carbon;

class AppServiceProvider extends ServiceProvider
{
Expand All @@ -13,7 +14,7 @@ class AppServiceProvider extends ServiceProvider
*/
public function boot()
{
//
Carbon::setLocale('zh');
}

/**
Expand Down
3 changes: 3 additions & 0 deletions app/Providers/AuthServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;

use App\Models\User;
use App\Models\Status;
use App\Policies\UserPolicy;
use App\Policies\StatusPolicy;

class AuthServiceProvider extends ServiceProvider
{
Expand All @@ -18,6 +20,7 @@ class AuthServiceProvider extends ServiceProvider
protected $policies = [
'App\Model' => 'App\Policies\ModelPolicy',
User::class => UserPolicy::class,
Status::class => StatusPolicy::class,
];

/**
Expand Down
4 changes: 2 additions & 2 deletions config/mail.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
|
*/

'host' => env('MAIL_HOST', 'smtp.gmail.com'),
'host' => env('MAIL_HOST', 'smtp.mailgun.org'),

/*
|--------------------------------------------------------------------------
Expand All @@ -54,7 +54,7 @@
|
*/

'from' => ['address' => 'aufree@estgroupe.com', 'name' => 'Aufree'],
'from' => ['address' => null, 'name' => null],

/*
|--------------------------------------------------------------------------
Expand Down
9 changes: 9 additions & 0 deletions database/factories/ModelFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,12 @@
'updated_at' => $date_time,
];
});

$factory->define(App\Models\Status::class, function (Faker\Generator $faker) {
$date_time = $faker->date . ' ' . $faker->time;
return [
'content' => $faker->text(),
'created_at' => $date_time,
'updated_at' => $date_time,
];
});
33 changes: 33 additions & 0 deletions database/migrations/2016_12_18_011525_create_statuses_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

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

class CreateStatusesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('statuses', function (Blueprint $table) {
$table->increments('id');
$table->text('content');
$table->integer('user_id')->index();
$table->index(['created_at']);
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('statuses');
}
}
1 change: 1 addition & 0 deletions database/seeds/DatabaseSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public function run()
Model::unguard();

$this->call('UsersTableSeeder');
$this->call('StatusesTableSeeder');

Model::reguard();
}
Expand Down
25 changes: 25 additions & 0 deletions database/seeds/StatusesTableSeeder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

use Illuminate\Database\Seeder;
use App\Models\User;
use App\Models\Status;

class StatusesTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
$user_ids = ['1','2','3'];
$faker = app(Faker\Generator::class);

$statuses = factory(Status::class)->times(100)->make()->each(function ($status) use ($faker, $user_ids) {
$status->user_id = $faker->randomElement($user_ids);
});

Status::insert($statuses->toArray());
}
}
38 changes: 38 additions & 0 deletions public/css/app.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion public/css/app.css.map

Large diffs are not rendered by default.

53 changes: 53 additions & 0 deletions resources/assets/sass/app.scss
Original file line number Diff line number Diff line change
Expand Up @@ -205,3 +205,56 @@ input {
position: relative;
right: 0;
}

/* statuses */

.statuses {
list-style: none;
padding: 0;
margin-top: 20px;
li {
padding: 10px 0;
border-top: 1px solid #e8e8e8;
position: relative;
}
.user {
margin-top: 5em;
padding-top: 0;
}
.content {
display: block;
margin-left: 60px;
word-break: break-word;
img {
display: block;
padding: 5px 0;
}
}
.timestamp {
color: $gray-light;
display: block;
margin-left: 60px;
}
.gravatar {
margin-right: 10px;
margin-top: 5px;
}
form {
button.status-delete-btn {
position: absolute;
right: 0;
top: 10px;
}
}
}

aside {
textarea {
height: 100px;
margin-bottom: 5px;
}
}

.status_form {
margin-top: 20px;
}
8 changes: 8 additions & 0 deletions resources/views/shared/feed.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
@if (count($feed_items))
<ol class="statuses">
@foreach ($feed_items as $status)
@include('statuses._status', ['user' => $status->user])
@endforeach
{!! $feed_items->render() !!}
</ol>
@endif
8 changes: 8 additions & 0 deletions resources/views/shared/status_form.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<form action="{{ route('statuses.store') }}" method="POST">
@include('shared.errors')
{{ csrf_field() }}
<textarea class="form-control" rows="3" placeholder="聊聊新鲜事儿..." name="content">
{{ old('content') }}
</textarea>
<button type="submit" class="btn btn-primary pull-right">发布</button>
</form>
Loading

0 comments on commit aaa5525

Please sign in to comment.