|
2 | 2 |
|
3 | 3 | namespace Database\Seeders; |
4 | 4 |
|
| 5 | +use App\Models\Snippet; |
| 6 | +use App\Models\SnippetView; |
5 | 7 | use App\Models\User; |
6 | | -// use Illuminate\Database\Console\Seeds\WithoutModelEvents; |
7 | | -use Database\Seeders\SnippetSeeder; |
8 | 8 | use Illuminate\Database\Seeder; |
| 9 | +use Carbon\Carbon; |
| 10 | +use Illuminate\Support\Facades\Hash; |
| 11 | + |
9 | 12 |
|
10 | 13 | class DatabaseSeeder extends Seeder |
11 | 14 | { |
12 | 15 | /** |
13 | | - * Seed the application's database. |
| 16 | + * Run the database seeds. |
14 | 17 | */ |
15 | 18 | public function run(): void |
16 | 19 | { |
17 | | - // User::factory(10)->create(); |
18 | | - |
19 | | - // User::factory()->create([ |
20 | | - // 'name' => 'Test User', |
21 | | - // 'email' => 'test@example.com', |
22 | | - // ]); |
23 | | - $this->call([ |
24 | | - SnippetSeeder::class, |
| 20 | + // Create a test user if none exists |
| 21 | + $user = User::firstOrCreate( |
| 22 | + ['email' => 'test@example.com'], |
| 23 | + [ |
| 24 | + 'name' => 'Test User', |
| 25 | + 'password' => Hash::make('password'), |
| 26 | + 'email_verified_at' => now(), |
| 27 | + ] |
| 28 | + ); |
| 29 | + |
| 30 | + // Create a sample snippet |
| 31 | + $snippet = Snippet::create([ |
| 32 | + 'title' => 'Example Sorting Algorithm', |
| 33 | + 'description' => 'A simple sorting algorithm implementation for demonstration purposes', |
| 34 | + 'code' => "function bubbleSort(arr) {\n const n = arr.length;\n \n for (let i = 0; i < n; i++) {\n for (let j = 0; j < n - i - 1; j++) {\n if (arr[j] > arr[j + 1]) {\n // Swap elements\n [arr[j], arr[j + 1]] = [arr[j + 1], arr[j]];\n }\n }\n }\n \n return arr;\n}", |
| 35 | + 'language' => 'javascript', |
| 36 | + 'is_public' => true, |
| 37 | + 'user_id' => $user->id, |
| 38 | + 'views_count' => 0, |
| 39 | + 'favorites_count' => rand(5, 20), |
25 | 40 | ]); |
| 41 | + |
| 42 | + // Generate views for each month (January to current month) |
| 43 | + $this->generateMonthlyViews($snippet); |
| 44 | + |
| 45 | + $this->command->info('Test snippet created with ID: ' . $snippet->id); |
| 46 | + $this->command->info('Monthly views have been generated for the test snippet'); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Generate monthly views for a snippet |
| 51 | + */ |
| 52 | + private function generateMonthlyViews(Snippet $snippet): void |
| 53 | + { |
| 54 | + // Get current month and year |
| 55 | + $currentMonth = Carbon::now()->month; |
| 56 | + $currentYear = Carbon::now()->year; |
| 57 | + |
| 58 | + // Set up IP addresses for variety |
| 59 | + $ipAddresses = [ |
| 60 | + '192.168.1.1', '192.168.1.2', '192.168.1.3', |
| 61 | + '192.168.1.4', '192.168.1.5', '192.168.1.6', |
| 62 | + '172.16.0.1', '172.16.0.2', '172.16.0.3', |
| 63 | + '10.0.0.1', '10.0.0.2', '10.0.0.3' |
| 64 | + ]; |
| 65 | + |
| 66 | + $viewsCount = 0; |
| 67 | + |
| 68 | + // Generate views for each month from January to current month |
| 69 | + for ($month = 1; $month <= $currentMonth; $month++) { |
| 70 | + // Generate a random number of views for this month (increasing trend) |
| 71 | + $monthlyViews = rand(20, 50) + ($month * 15); // More views in later months |
| 72 | + |
| 73 | + // For each view in this month |
| 74 | + for ($i = 0; $i < $monthlyViews; $i++) { |
| 75 | + // Create a random date within the month |
| 76 | + $day = rand(1, Carbon::create($currentYear, $month)->daysInMonth); |
| 77 | + $hour = rand(0, 23); |
| 78 | + $minute = rand(0, 59); |
| 79 | + |
| 80 | + $viewDate = Carbon::create($currentYear, $month, $day, $hour, $minute); |
| 81 | + |
| 82 | + // Skip future dates |
| 83 | + if ($viewDate->isFuture()) { |
| 84 | + continue; |
| 85 | + } |
| 86 | + |
| 87 | + // Create the view record |
| 88 | + SnippetView::create([ |
| 89 | + 'snippet_id' => $snippet->id, |
| 90 | + 'viewed_at' => $viewDate, |
| 91 | + 'viewer_ip' => $ipAddresses[array_rand($ipAddresses)], |
| 92 | + 'created_at' => $viewDate, |
| 93 | + 'updated_at' => $viewDate, |
| 94 | + ]); |
| 95 | + |
| 96 | + $viewsCount++; |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + // Update the snippet's views_count |
| 101 | + $snippet->update(['views_count' => $viewsCount]); |
26 | 102 | } |
27 | 103 | } |
0 commit comments