File tree 4 files changed +77
-2
lines changed
4 files changed +77
-2
lines changed Original file line number Diff line number Diff line change @@ -26,6 +26,11 @@ class ContestJudge extends Model
26
26
protected $ primaryKey = ':composite ' ;
27
27
protected $ primaryKeys = ['user_id ' , 'contest_id ' ];
28
28
29
+ public function contest (): BelongsTo
30
+ {
31
+ return $ this ->belongsTo (Contest::class, 'contest_id ' );
32
+ }
33
+
29
34
public function user (): BelongsTo
30
35
{
31
36
return $ this ->belongsTo (User::class, 'user_id ' );
Original file line number Diff line number Diff line change 8
8
namespace Database \Factories ;
9
9
10
10
use App \Models \Contest ;
11
+ use App \Models \ContestEntry ;
12
+ use App \Models \ContestJudge ;
13
+ use App \Models \ContestScoringCategory ;
11
14
use Carbon \Carbon ;
12
15
13
16
class ContestFactory extends Factory
@@ -72,4 +75,17 @@ public function voting(): static
72
75
'voting_ends_at ' => fn () => Carbon::now ()->addMonths (1 ),
73
76
]);
74
77
}
78
+
79
+ public function withEntries (int $ count = 2 )
80
+ {
81
+ \Log::debug ('withEntries ' );
82
+ return $ this ->has (ContestEntry::factory ()->count ($ count ), 'entries ' );
83
+ }
84
+
85
+ public function withJudges (int $ jugdeCount = 2 , $ categoryCount = 2 ): static
86
+ {
87
+ return $ this ->judged ()
88
+ ->has (ContestJudge::factory ()->withVotes ()->count ($ jugdeCount ))
89
+ ->has (ContestScoringCategory::factory ()->count ($ categoryCount ), 'scoringCategories ' );
90
+ }
75
91
}
Original file line number Diff line number Diff line change 7
7
8
8
namespace Database \Factories ;
9
9
10
+ use App \Models \Contest ;
11
+ use App \Models \ContestEntry ;
10
12
use App \Models \ContestJudge ;
13
+ use App \Models \User ;
11
14
12
15
class ContestJudgeFactory extends Factory
13
16
{
14
17
protected $ model = ContestJudge::class;
15
18
16
19
public function definition (): array
17
20
{
18
- // pivot table...
19
- return [];
21
+ return [
22
+ 'contest_id ' => Contest::factory ()->judged (),
23
+ 'user_id ' => User::factory (),
24
+ ];
25
+ }
26
+
27
+ public function withVotes (): static
28
+ {
29
+ \Log::debug ('withvotes ' );
30
+ return $ this ->afterCreating (function (ContestJudge $ contestJudge ) {
31
+ $ contest = $ contestJudge ->contest ;
32
+ $ categories = $ contest ->scoringCategories ;
33
+
34
+ foreach ($ contest ->entries as $ entry ) {
35
+ $ vote = $ entry ->judgeVotes ()->create ([
36
+ 'comment ' => $ this ->faker ->sentence (),
37
+ 'user_id ' => $ contestJudge ->user_id ,
38
+ ]);
39
+
40
+ foreach ($ categories as $ category ) {
41
+ $ vote ->scores ()->create ([
42
+ 'contest_scoring_category_id ' => $ category ->getKey (),
43
+ 'value ' => rand (1 , $ category ->max_value ),
44
+ ]);
45
+ }
46
+ }
47
+ });
20
48
}
21
49
}
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ // Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the GNU Affero General Public License v3.0.
4
+ // See the LICENCE file in the repository root for full licence text.
5
+
6
+ declare (strict_types=1 );
7
+
8
+ namespace Database \Factories ;
9
+
10
+ use App \Models \Contest ;
11
+ use App \Models \ContestScoringCategory ;
12
+
13
+ class ContestScoringCategoryFactory extends Factory
14
+ {
15
+ protected $ model = ContestScoringCategory::class;
16
+
17
+ public function definition (): array
18
+ {
19
+ return [
20
+ 'contest_id ' => Contest::factory (),
21
+ 'description ' => $ this ->faker ->sentence (),
22
+ 'max_value ' => 10 ,
23
+ 'name ' => $ this ->faker ->word (),
24
+ ];
25
+ }
26
+ }
You can’t perform that action at this time.
0 commit comments