Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 23 additions & 18 deletions app/Http/Controllers/HomeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace App\Http\Controllers;

use App\Models\Article;
use App\Models\Reply;
use App\Models\Thread;
use App\Models\User;
use Illuminate\Support\Facades\Cache;
Expand All @@ -10,6 +12,15 @@ class HomeController extends Controller
{
public function show()
{
$communityMembers = Cache::remember('communityMembers', now()->addMinutes(10), function () {
return User::withCounts()
->hasActivity()
->inRandomOrder()
->take(100)
->get()
->chunk(20);
});

$totalUsers = Cache::remember('totalUsers', now()->addDay(), function () {
return number_format(User::count());
});
Expand All @@ -18,8 +29,8 @@ public function show()
return number_format(Thread::count());
});

$resolutionTime = Cache::remember('resolutionTime', now()->addDay(), function () {
return number_format(Thread::resolutionTime());
$totalReplies = Cache::remember('totalReplies', now()->addDay(), function () {
return number_format(Reply::count());
});

$latestThreads = Cache::remember('latestThreads', now()->addHour(), function () {
Expand All @@ -30,29 +41,23 @@ public function show()
->get();
});

$latestArticles = Cache::remember('latestArticles', now()->addHour(), function () {
return Article::published()
->trending()
->limit(4)
->get();
});

return view('home', [
'communityMembers' => $communityMembers,
'totalUsers' => $totalUsers,
'totalThreads' => $totalThreads,
'resolutionTime' => $resolutionTime,
'totalReplies' => $totalReplies,
'latestThreads' => $latestThreads,
'latestArticles' => $latestArticles,
]);
}

public function rules()
{
return view('rules');
}

public function terms()
{
return view('terms');
}

public function privacy()
{
return view('privacy');
}

public function pastebin(string $hash = '')
{
return redirect()->away("https://paste.laravel.io/$hash");
Expand Down
20 changes: 20 additions & 0 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,4 +249,24 @@ public function scopeMostSolutions(Builder $query)
->where('replyable_type', 'threads');
}])->orderBy('most_solutions', 'desc');
}

public function scopeWithCounts(Builder $query)
{
return $query->withCount([
'threadsRelation as threads_count',
'replyAble as replies_count',
'replyAble as solutions_count' => function (Builder $query) {
return $query->join('threads', 'threads.solution_reply_id', '=', 'replies.id')
->where('replyable_type', 'threads');
},
]);
}

public function scopeHasActivity(Builder $query)
{
return $query->where(function ($query) {
$query->has('threadsRelation')
->orHas('replyAble');
});
}
}
4 changes: 1 addition & 3 deletions database/factories/ArticleFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ class ArticleFactory extends Factory
public function definition()
{
return [
'author_id' => function () {
return User::factory()->create()->id;
},
'author_id' => User::factory(),
'title' => $this->faker->sentence,
'body' => $this->faker->paragraphs(3, true),
'slug' => $this->faker->unique()->slug,
Expand Down
4 changes: 2 additions & 2 deletions database/factories/ReplyFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ public function definition()
{
return [
'body' => $this->faker->text(),
'author_id' => $attributes['author_id'] ?? User::factory()->create()->id(),
'replyable_id' => $attributes['replyable_id'] ?? Thread::factory()->create()->id(),
'author_id' => User::factory(),
'replyable_id' => Thread::factory(),
'replyable_type' => Thread::TABLE,
];
}
Expand Down
2 changes: 1 addition & 1 deletion database/factories/ThreadFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function definition()
'subject' => $this->faker->text(20),
'body' => $this->faker->text,
'slug' => $this->faker->unique()->slug,
'author_id' => $attributes['author_id'] ?? User::factory()->create()->id(),
'author_id' => $attributes['author_id'] ?? User::factory(),
];
}

Expand Down
23 changes: 0 additions & 23 deletions database/seeders/ArticleSeeder.php

This file was deleted.

4 changes: 2 additions & 2 deletions database/seeders/DatabaseSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ public function run()
if (! App::environment('production')) {
$this->call(UserSeeder::class);
$this->call(TagSeeder::class);
$this->call(ThreadSeeder::class);
$this->call(ReplySeeder::class);
$this->call(NotificationSeeder::class);
$this->call(ArticleSeeder::class);
$this->call(LikeSeeder::class);
}
}
}
41 changes: 41 additions & 0 deletions database/seeders/LikeSeeder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Database\Seeders;

use App\Models\Article;
use App\Models\Reply;
use App\Models\Thread;
use App\Models\User;
use Illuminate\Database\Seeder;
use Illuminate\Foundation\Testing\WithFaker;

class LikeSeeder extends Seeder
{
use WithFaker;

public function run()
{
$users = User::all();
$articles = Article::all();
$replies = Reply::all();
$threads = Thread::all();

$articles->random(100)->each(function ($article) use ($users) {
foreach ($users->random(rand(1, 10)) as $user) {
$article->likedBy($user);
}
});

$replies->random(50)->each(function ($reply) use ($users) {
foreach ($users->random(rand(1, 10)) as $user) {
$reply->likedBy($user);
}
});

$threads->random(50)->each(function ($thread) use ($users) {
foreach ($users->random(rand(1, 10)) as $user) {
$thread->likedBy($user);
}
});
}
}
36 changes: 36 additions & 0 deletions database/seeders/ReplySeeder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Database\Seeders;

use App\Models\Reply;
use App\Models\Thread;
use App\Models\User;
use Illuminate\Database\Eloquent\Factories\Sequence;
use Illuminate\Database\Seeder;

class ReplySeeder extends Seeder
{
public function run()
{
$users = User::all();
$threads = Thread::all();

// Create 5 replies for each thread from random users.
$threads->each(function ($thread) use ($users) {
Reply::factory()
->count(5)
->state(new Sequence(
fn () => [
'author_id' => $users->random()->id,
'replyable_id' => $thread->id,
],
))
->create();
});

// Give 10 random threads a solution.
$threads->random(20)->each(function ($thread) {
$thread->markSolution($thread->repliesRelation()->get()->random());
});
}
}
28 changes: 24 additions & 4 deletions database/seeders/TagSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,40 @@

namespace Database\Seeders;

use App\Models\Article;
use App\Models\Tag;
use App\Models\Thread;
use Illuminate\Database\Seeder;

class TagSeeder extends Seeder
{
public function run()
{
$this->createTag('Installation', 'installation');
$this->createTag('Blade', 'blade');
$this->createTag('Cache', 'cache');
$tags = collect([
$this->createTag('Installation', 'installation'),
$this->createTag('Blade', 'blade'),
$this->createTag('Cache', 'cache'),
]);

Article::all()->each(function ($article) use ($tags) {
$article->syncTags(
$tags->random(rand(0, $tags->count()))
->pluck('id')
->toArray(),
);
});

Thread::all()->each(function ($article) use ($tags) {
$article->syncTags(
$tags->random(rand(0, $tags->count()))
->pluck('id')
->toArray(),
);
});
}

private function createTag(string $name, string $slug)
{
Tag::factory()->create(compact('name', 'slug'));
return Tag::factory()->create(compact('name', 'slug'));
}
}
19 changes: 0 additions & 19 deletions database/seeders/ThreadSeeder.php

This file was deleted.

18 changes: 18 additions & 0 deletions database/seeders/UserSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

namespace Database\Seeders;

use App\Models\Article;
use App\Models\Thread;
use App\Models\User;
use Illuminate\Database\Eloquent\Factories\Sequence;
use Illuminate\Database\Seeder;

class UserSeeder extends Seeder
Expand All @@ -17,5 +20,20 @@ public function run()
'password' => bcrypt('password'),
'type' => User::ADMIN,
]);

User::factory()
->count(100)
->has(Thread::factory()->count(2), 'threadsRelation')
->has(
Article::factory()
->count(2)
->state(
new Sequence(
['submitted_at' => now(), 'approved_at' => now()],
['submitted_at' => now()],
),
),
)
->create();
}
}
Loading