Skip to content

Commit 21ab4ed

Browse files
AllanKoderCopilot
andauthored
Tags (#26)
* Tag Pushes * Apply automatic changes * Update resources/js/Components/Form/TagSelector.vue Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update tests/RequestFactories/ResourceEdit/StoreResourceEditFactory.php Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update database/factories/ComputerScienceResourceFactory.php Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Apply automatic changes --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent e4696d8 commit 21ab4ed

14 files changed

Lines changed: 87 additions & 30 deletions

app/Http/Controllers/CommentController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace App\Http\Controllers;
44

5-
use App\Http\Requests\Comment\StoreCommentRequest;
5+
use App\Http\Requests\StoreCommentRequest;
66
use App\Http\Resources\CommentResource;
77
use App\Http\Resources\UserResource;
88
use App\Services\CommentService;

app/Http/Controllers/ComputerScienceResourceController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace App\Http\Controllers;
44

55
use App\Events\TagFrequencyChanged;
6-
use App\Http\Requests\ComputerScienceResource\StoreResourceRequest;
6+
use App\Http\Requests\StoreResourceRequest;
77
use App\Models\ComputerScienceResource;
88
use App\Models\NewsPost;
99
use App\Models\ResourceEdits;

app/Http/Controllers/ResourceEditsController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace App\Http\Controllers;
44

55
use App\Events\TagFrequencyChanged;
6-
use App\Http\Requests\ResourceEdit\StoreResourceEdit;
6+
use App\Http\Requests\StoreResourceEdit;
77
use App\Models\ComputerScienceResource;
88
use App\Models\ResourceEdits;
99
use App\Services\DataNormalizationService;

app/Http/Controllers/ResourceReviewController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace App\Http\Controllers;
44

5-
use App\Http\Requests\ResourceReview\StoreResourceReview;
5+
use App\Http\Requests\StoreResourceReview;
66
use App\Models\ComputerScienceResource;
77
use App\Models\ResourceReview;
88
use Illuminate\Support\Facades\Auth;

app/Http/Requests/Comment/StoreCommentRequest.php renamed to app/Http/Requests/StoreCommentRequest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace App\Http\Requests\Comment;
3+
namespace App\Http\Requests;
44

55
use App\Services\ModelResolverService;
66
use Illuminate\Foundation\Http\FormRequest;

app/Http/Requests/ResourceEdit/StoreResourceEdit.php renamed to app/Http/Requests/StoreResourceEdit.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace App\Http\Requests\ResourceEdit;
3+
namespace App\Http\Requests;
44

55
use Illuminate\Foundation\Http\FormRequest;
66
use Illuminate\Support\Facades\Auth;
@@ -35,13 +35,13 @@ public function rules(): array
3535
'proposed_changes.page_url' => ['nullable', 'string', 'url:http,https', 'max:255'],
3636
'proposed_changes.difficulty' => ['nullable', 'string', Rule::in(config('computerScienceResource.difficulties'))],
3737
'proposed_changes.pricing' => ['nullable', 'string', Rule::in(config('computerScienceResource.pricings'))],
38-
'proposed_changes.topic_tags' => ['nullable', 'array', 'min:2'],
39-
'proposed_changes.topic_tags.*' => ['required', 'distinct', 'string', 'max:50'],
4038
'proposed_changes.image_file' => ['nullable', 'image', 'max:500'], // 500 kilobytes
39+
'proposed_changes.topic_tags' => ['nullable', 'array', 'min:2'],
40+
'proposed_changes.topic_tags.*' => ['required', 'distinct', 'string', 'max:50', 'regex:'.config('computerScienceResource.tags_regex')],
4141
'proposed_changes.general_tags' => ['nullable', 'array'],
42-
'proposed_changes.general_tags.*' => ['required', 'distinct', 'string', 'max:50'],
42+
'proposed_changes.general_tags.*' => ['required', 'distinct', 'string', 'max:50', 'regex:'.config('computerScienceResource.tags_regex')],
4343
'proposed_changes.programming_language_tags' => ['nullable', 'array'],
44-
'proposed_changes.programming_language_tags.*' => ['required', 'distinct', 'string', 'max:50'],
44+
'proposed_changes.programming_language_tags.*' => ['required', 'distinct', 'string', 'max:50', 'regex:'.config('computerScienceResource.tags_regex')],
4545
];
4646
}
4747
}

app/Http/Requests/ComputerScienceResource/StoreResourceRequest.php renamed to app/Http/Requests/StoreResourceRequest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace App\Http\Requests\ComputerScienceResource;
3+
namespace App\Http\Requests;
44

55
use Illuminate\Foundation\Http\FormRequest;
66
use Illuminate\Support\Facades\Auth;
@@ -32,14 +32,14 @@ public function rules(): array
3232
'difficulty' => ['required', 'string', Rule::in(config('computerScienceResource.difficulties'))],
3333
'pricing' => ['required', 'string', Rule::in(config('computerScienceResource.pricings'))],
3434
'topic_tags' => ['required', 'array', 'min:2'],
35-
'topic_tags.*' => ['required', 'distinct', 'string', 'max:50'],
35+
'topic_tags.*' => ['required', 'distinct', 'string', 'max:50', 'regex:'.config('computerScienceResource.tags_regex')],
3636

3737
// Optional, can just be omitted
3838
'image_file' => ['nullable', 'image', 'max:500'], // 500 kiloBytes
3939
'general_tags' => ['array'],
40-
'general_tags.*' => ['required', 'distinct', 'string', 'max:50'],
40+
'general_tags.*' => ['required', 'distinct', 'string', 'max:50', 'regex:'.config('computerScienceResource.tags_regex')],
4141
'programming_language_tags' => ['array'],
42-
'programming_language_tags.*' => ['required', 'distinct', 'string', 'max:50'],
42+
'programming_language_tags.*' => ['required', 'distinct', 'string', 'max:50', 'regex:'.config('computerScienceResource.tags_regex')],
4343
];
4444
}
4545
}

app/Http/Requests/ResourceReview/StoreResourceReview.php renamed to app/Http/Requests/StoreResourceReview.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace App\Http\Requests\ResourceReview;
3+
namespace App\Http\Requests;
44

55
use Illuminate\Foundation\Http\FormRequest;
66
use Illuminate\Support\Facades\Auth;

config/computerScienceResource.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@
44
'platforms' => ['book', 'podcast', 'youtube_channel', 'blog', 'website', 'organization', 'service', 'bootcamp', 'newsletter', 'workshop', 'course', 'forum', 'mobile_app', 'desktop_app', 'magazine'],
55
'difficulties' => ['any', 'beginner', 'industry_simple', 'industry_standard', 'industry_professional', 'academic'],
66
'pricings' => ['free', 'paid', 'freemium', 'premium'],
7+
'tags_regex' => '/^[a-z0-9-]+$/',
78
];

database/factories/ComputerScienceResourceFactory.php

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,30 @@ public function configure(): Factory
5656
return $this->afterCreating(function (ComputerScienceResource $resource) {
5757
$fakerTags = ['tag1', 'tag2', 'tag3', 'tag4', 'tag5', fake()->word(), fake()->word()];
5858

59-
$resource->topic_tags = $this->topicTags ?? fake()->randomElements($fakerTags, fake()->numberBetween(3, count($fakerTags)));
60-
$resource->programming_language_tags = $this->programmingLanguageTags ?? fake()->randomElements($fakerTags);
61-
$resource->general_tags = $this->generalTags ?? fake()->randomElements($fakerTags);
59+
// Sanitize all tags before assigning
60+
$topicTags = $this->topicTags ?? fake()->randomElements($fakerTags, fake()->numberBetween(3, count($fakerTags)));
61+
$topicTags = array_map([$this, 'sanitizeTag'], $topicTags);
62+
63+
$programmingLanguageTags = $this->programmingLanguageTags ?? fake()->randomElements($fakerTags);
64+
$programmingLanguageTags = array_map([$this, 'sanitizeTag'], $programmingLanguageTags);
65+
66+
$generalTags = $this->generalTags ?? fake()->randomElements($fakerTags);
67+
$generalTags = array_map([$this, 'sanitizeTag'], $generalTags);
68+
69+
$resource->topic_tags = $topicTags;
70+
$resource->programming_language_tags = $programmingLanguageTags;
71+
$resource->general_tags = $generalTags;
6272

6373
TagFrequencyChanged::dispatch(null, $resource->tagCounter());
6474
});
6575
}
76+
77+
private function sanitizeTag(string $tag): string
78+
{
79+
// Lowercase, replace spaces with -, remove invalid chars
80+
$tag = strtolower(str_replace(' ', '-', $tag));
81+
82+
// Remove any character not a-z, 0-9, or hyphen
83+
return preg_replace('/[^a-z0-9-]/', '', $tag);
84+
}
6685
}

0 commit comments

Comments
 (0)