Skip to content

Commit aaa5525

Browse files
committed
Add user statuses
1 parent 4b988f1 commit aaa5525

22 files changed

+342
-18
lines changed

app/Http/Controllers/StaticPagesController.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,20 @@
77
use App\Http\Requests;
88
use App\Http\Controllers\Controller;
99

10+
use App\Models\Status;
11+
use Auth;
12+
1013
class StaticPagesController extends Controller
1114
{
15+
1216
public function home()
1317
{
14-
return view('static_pages/home');
18+
$feed_items = [];
19+
if (Auth::check()) {
20+
$feed_items = Auth::user()->feed()->paginate(30);
21+
}
22+
23+
return view('static_pages/home', compact('feed_items'));
1524
}
1625

1726
public function help()
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace App\Http\Controllers;
4+
5+
use Illuminate\Http\Request;
6+
7+
use App\Http\Requests;
8+
use App\Http\Controllers\Controller;
9+
10+
use App\Models\Status;
11+
use Auth;
12+
13+
class StatusesController extends Controller
14+
{
15+
public function __construct()
16+
{
17+
$this->middleware('auth', [
18+
'only' => ['store', 'destroy']
19+
]);
20+
}
21+
22+
public function store(Request $request)
23+
{
24+
$this->validate($request, [
25+
'content' => 'required|max:140'
26+
]);
27+
28+
Auth::user()->statuses()->create([
29+
'content' => $request->content
30+
]);
31+
return redirect()->back();
32+
}
33+
34+
public function destroy($id)
35+
{
36+
$status = Status::findOrFail($id);
37+
$this->authorize('destroy', $status);
38+
$status->delete();
39+
session()->flash('success', '微博已被成功删除!');
40+
return redirect()->back();
41+
}
42+
}

app/Http/Controllers/UsersController.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@ public function create()
3939
public function show($id)
4040
{
4141
$user = User::findOrFail($id);
42-
return view('users.show', compact('user'));
42+
$statuses = $user->statuses()
43+
->orderBy('created_at', 'desc')
44+
->paginate(30);
45+
return view('users.show', compact('user', 'statuses'));
4346
}
4447

4548
public function store(Request $request)

app/Http/routes.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,5 @@
1717
post('password/email', 'Auth\PasswordController@postEmail')->name('password.reset');
1818
get('password/reset/{token}', 'Auth\PasswordController@getReset')->name('password.edit');
1919
post('password/reset', 'Auth\PasswordController@postReset')->name('password.update');
20+
21+
resource('statuses', 'StatusesController', ['only' => ['store', 'destroy']]);

app/Models/Status.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace App\Models;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
7+
class Status extends Model
8+
{
9+
protected $fillable = ['content'];
10+
11+
public function user()
12+
{
13+
return $this->belongsTo(User::class);
14+
}
15+
}

app/Models/User.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,15 @@ public function setPasswordAttribute($password)
5656
{
5757
$this->attributes['password'] = bcrypt($password);
5858
}
59+
60+
public function statuses()
61+
{
62+
return $this->hasMany(Status::class);
63+
}
64+
65+
public function feed()
66+
{
67+
return $this->statuses()
68+
->orderBy('created_at', 'desc');
69+
}
5970
}

app/Policies/StatusPolicy.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace App\Policies;
4+
5+
use Illuminate\Auth\Access\HandlesAuthorization;
6+
use App\Models\User;
7+
use App\Models\Status;
8+
9+
class StatusPolicy
10+
{
11+
use HandlesAuthorization;
12+
13+
public function destroy(User $user, Status $status)
14+
{
15+
return $user->id === $status->user_id;
16+
}
17+
}

app/Providers/AppServiceProvider.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace App\Providers;
44

55
use Illuminate\Support\ServiceProvider;
6+
use Carbon\Carbon;
67

78
class AppServiceProvider extends ServiceProvider
89
{
@@ -13,7 +14,7 @@ class AppServiceProvider extends ServiceProvider
1314
*/
1415
public function boot()
1516
{
16-
//
17+
Carbon::setLocale('zh');
1718
}
1819

1920
/**

app/Providers/AuthServiceProvider.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
77

88
use App\Models\User;
9+
use App\Models\Status;
910
use App\Policies\UserPolicy;
11+
use App\Policies\StatusPolicy;
1012

1113
class AuthServiceProvider extends ServiceProvider
1214
{
@@ -18,6 +20,7 @@ class AuthServiceProvider extends ServiceProvider
1820
protected $policies = [
1921
'App\Model' => 'App\Policies\ModelPolicy',
2022
User::class => UserPolicy::class,
23+
Status::class => StatusPolicy::class,
2124
];
2225

2326
/**

config/mail.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
|
2929
*/
3030

31-
'host' => env('MAIL_HOST', 'smtp.gmail.com'),
31+
'host' => env('MAIL_HOST', 'smtp.mailgun.org'),
3232

3333
/*
3434
|--------------------------------------------------------------------------
@@ -54,7 +54,7 @@
5454
|
5555
*/
5656

57-
'from' => ['address' => '[email protected]', 'name' => 'Aufree'],
57+
'from' => ['address' => null, 'name' => null],
5858

5959
/*
6060
|--------------------------------------------------------------------------

database/factories/ModelFactory.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,12 @@
2424
'updated_at' => $date_time,
2525
];
2626
});
27+
28+
$factory->define(App\Models\Status::class, function (Faker\Generator $faker) {
29+
$date_time = $faker->date . ' ' . $faker->time;
30+
return [
31+
'content' => $faker->text(),
32+
'created_at' => $date_time,
33+
'updated_at' => $date_time,
34+
];
35+
});
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
use Illuminate\Database\Schema\Blueprint;
4+
use Illuminate\Database\Migrations\Migration;
5+
6+
class CreateStatusesTable extends Migration
7+
{
8+
/**
9+
* Run the migrations.
10+
*
11+
* @return void
12+
*/
13+
public function up()
14+
{
15+
Schema::create('statuses', function (Blueprint $table) {
16+
$table->increments('id');
17+
$table->text('content');
18+
$table->integer('user_id')->index();
19+
$table->index(['created_at']);
20+
$table->timestamps();
21+
});
22+
}
23+
24+
/**
25+
* Reverse the migrations.
26+
*
27+
* @return void
28+
*/
29+
public function down()
30+
{
31+
Schema::drop('statuses');
32+
}
33+
}

database/seeds/DatabaseSeeder.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public function run()
1515
Model::unguard();
1616

1717
$this->call('UsersTableSeeder');
18+
$this->call('StatusesTableSeeder');
1819

1920
Model::reguard();
2021
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
use Illuminate\Database\Seeder;
4+
use App\Models\User;
5+
use App\Models\Status;
6+
7+
class StatusesTableSeeder extends Seeder
8+
{
9+
/**
10+
* Run the database seeds.
11+
*
12+
* @return void
13+
*/
14+
public function run()
15+
{
16+
$user_ids = ['1','2','3'];
17+
$faker = app(Faker\Generator::class);
18+
19+
$statuses = factory(Status::class)->times(100)->make()->each(function ($status) use ($faker, $user_ids) {
20+
$status->user_id = $faker->randomElement($user_ids);
21+
});
22+
23+
Status::insert($statuses->toArray());
24+
}
25+
}

public/css/app.css

Lines changed: 38 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

public/css/app.css.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

resources/assets/sass/app.scss

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,3 +205,56 @@ input {
205205
position: relative;
206206
right: 0;
207207
}
208+
209+
/* statuses */
210+
211+
.statuses {
212+
list-style: none;
213+
padding: 0;
214+
margin-top: 20px;
215+
li {
216+
padding: 10px 0;
217+
border-top: 1px solid #e8e8e8;
218+
position: relative;
219+
}
220+
.user {
221+
margin-top: 5em;
222+
padding-top: 0;
223+
}
224+
.content {
225+
display: block;
226+
margin-left: 60px;
227+
word-break: break-word;
228+
img {
229+
display: block;
230+
padding: 5px 0;
231+
}
232+
}
233+
.timestamp {
234+
color: $gray-light;
235+
display: block;
236+
margin-left: 60px;
237+
}
238+
.gravatar {
239+
margin-right: 10px;
240+
margin-top: 5px;
241+
}
242+
form {
243+
button.status-delete-btn {
244+
position: absolute;
245+
right: 0;
246+
top: 10px;
247+
}
248+
}
249+
}
250+
251+
aside {
252+
textarea {
253+
height: 100px;
254+
margin-bottom: 5px;
255+
}
256+
}
257+
258+
.status_form {
259+
margin-top: 20px;
260+
}

resources/views/shared/feed.blade.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
@if (count($feed_items))
2+
<ol class="statuses">
3+
@foreach ($feed_items as $status)
4+
@include('statuses._status', ['user' => $status->user])
5+
@endforeach
6+
{!! $feed_items->render() !!}
7+
</ol>
8+
@endif
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<form action="{{ route('statuses.store') }}" method="POST">
2+
@include('shared.errors')
3+
{{ csrf_field() }}
4+
<textarea class="form-control" rows="3" placeholder="聊聊新鲜事儿..." name="content">
5+
{{ old('content') }}
6+
</textarea>
7+
<button type="submit" class="btn btn-primary pull-right">发布</button>
8+
</form>

0 commit comments

Comments
 (0)