Skip to content

Commit da6b7fd

Browse files
authored
Tag Changes (#43)
* Tag Changes * Apply automatic changes
1 parent f9fe36b commit da6b7fd

23 files changed

Lines changed: 157 additions & 87 deletions
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
namespace App\Console\Commands;
4+
5+
use App\Models\ComputerScienceResource;
6+
use Illuminate\Console\Command;
7+
use Illuminate\Support\Facades\DB;
8+
9+
class UpdateTagFrequencies extends Command
10+
{
11+
/**
12+
* The name and signature of the console command.
13+
*/
14+
protected $signature = 'tags:update-frequencies';
15+
16+
/**
17+
* The console command description.
18+
*/
19+
protected $description = 'Recalculate and update the tag frequencies for all tag types.';
20+
21+
/**
22+
* Execute the console command.
23+
*/
24+
public function handle(): int
25+
{
26+
$this->info('Starting tag frequency update...');
27+
28+
$tagTypes = [
29+
'programming_languages_tags',
30+
'topics_tags',
31+
'general_tags',
32+
];
33+
34+
foreach ($tagTypes as $type) {
35+
$this->updateType($type);
36+
}
37+
38+
$this->info('✅ Tag frequency update complete!');
39+
40+
return self::SUCCESS;
41+
}
42+
43+
/**
44+
* Update frequencies for a specific tag type.
45+
*/
46+
protected function updateType(string $type): void
47+
{
48+
$this->line("Processing {$type}...");
49+
50+
// Flatten all tags for this type across all resources
51+
$tags = ComputerScienceResource::all()
52+
->flatMap(fn ($r) => $r->{$type})
53+
->filter()
54+
->map(fn ($tag) => trim($tag))
55+
->toArray();
56+
57+
// Count occurrences
58+
$frequencies = array_count_values($tags);
59+
60+
foreach ($frequencies as $tag => $count) {
61+
DB::table('tag_frequencies')->updateOrInsert(
62+
['tag' => $tag, 'type' => $type],
63+
['count' => $count]
64+
);
65+
}
66+
67+
$this->line('Updated '.count($frequencies)." entries for {$type}.");
68+
}
69+
}

app/Filament/Admin/Resources/ComputerScienceResource.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,12 @@ public static function form(Form $form): Form
5757
->columns(2)
5858
->required(),
5959

60-
Forms\Components\TagsInput::make('topic_tags')
60+
Forms\Components\TagsInput::make('topics_tags')
6161
->label('Topic Tags')
6262
->disabled()
6363
->helperText('These are computed from relationships'),
6464

65-
Forms\Components\TagsInput::make('programming_language_tags')
65+
Forms\Components\TagsInput::make('programming_languages_tags')
6666
->label('Programming Language Tags')
6767
->disabled()
6868
->helperText('These are computed from relationships'),
@@ -94,16 +94,16 @@ public static function table(Table $table): Table
9494
TextColumn::make('name')->searchable()
9595
->description(fn (ModelsComputerScienceResource $resource): string => $resource->description)->wrap(),
9696
TextColumn::make('page_url')->searchable()->wrap()->copyable(),
97-
TextColumn::make('topic_tags')
97+
TextColumn::make('topics_tags')
9898
->label('topics_tags')
9999
->badge()
100100
->color('primary')
101-
->getStateUsing(fn ($record) => $record->topic_tags),
102-
TextColumn::make('programming_language_tags')
101+
->getStateUsing(fn ($record) => $record->topics_tags),
102+
TextColumn::make('programming_languages_tags')
103103
->label('Languages')
104104
->badge()
105105
->color('info')
106-
->getStateUsing(fn ($record) => $record->programming_language_tags),
106+
->getStateUsing(fn ($record) => $record->programming_languages_tags),
107107
TextColumn::make('general_tags')
108108
->label('General Tags')
109109
->badge()

app/Http/Controllers/ResourceEditsController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public function merge(ResourceEditsService $editsService, ResourceEdits $resourc
123123

124124
$resource->save();
125125

126-
$proposedTagFields = ['topic_tags', 'programming_language_tags', 'general_tags'];
126+
$proposedTagFields = ['topics_tags', 'programming_languages_tags', 'general_tags'];
127127
foreach ($proposedTagFields as $field) {
128128
if (array_key_exists($field, $changes)) {
129129
$resource->$field = $changes[$field];

app/Http/Requests/StoreResourceEditRequest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,14 @@ public function rules(): array
3636
'proposed_changes.difficulties' => ['nullable', 'array', 'min:1'],
3737
'proposed_changes.difficulties.*' => ['required', 'distinct', 'string', Rule::in(config('computerScienceResource.difficulties'))],
3838
'proposed_changes.pricing' => ['nullable', 'string', Rule::in(config('computerScienceResource.pricings'))],
39-
'proposed_changes.topic_tags' => ['nullable', 'array', 'min:2'],
40-
'proposed_changes.topic_tags.*' => ['required', 'distinct', 'string', 'max:50', 'regex:'.config('computerScienceResource.tags_regex')],
39+
'proposed_changes.topics_tags' => ['nullable', 'array', 'min:2'],
40+
'proposed_changes.topics_tags.*' => ['required', 'distinct', 'string', 'max:50', 'regex:'.config('computerScienceResource.tags_regex')],
4141

4242
'proposed_changes.image_file' => ['nullable', 'image', 'max:500'], // 500 kilobytes
4343
'proposed_changes.general_tags' => ['nullable', 'array'],
4444
'proposed_changes.general_tags.*' => ['required', 'distinct', 'string', 'max:50', 'regex:'.config('computerScienceResource.tags_regex')],
45-
'proposed_changes.programming_language_tags' => ['nullable', 'array'],
46-
'proposed_changes.programming_language_tags.*' => ['required', 'distinct', 'string', 'max:50', 'regex:'.config('computerScienceResource.tags_regex')],
45+
'proposed_changes.programming_languages_tags' => ['nullable', 'array'],
46+
'proposed_changes.programming_languages_tags.*' => ['required', 'distinct', 'string', 'max:50', 'regex:'.config('computerScienceResource.tags_regex')],
4747
];
4848
}
4949
}

app/Http/Requests/StoreResourceRequest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,15 @@ public function rules(): array
3232
'difficulties' => ['required', 'array', 'min:1'],
3333
'difficulties.*' => ['required', 'distinct', 'string', Rule::in(config('computerScienceResource.difficulties'))],
3434
'pricing' => ['required', 'string', Rule::in(config('computerScienceResource.pricings'))],
35-
'topic_tags' => ['required', 'array', 'min:2'],
36-
'topic_tags.*' => ['required', 'distinct', 'string', 'max:50', 'regex:'.config('computerScienceResource.tags_regex')],
35+
'topics_tags' => ['required', 'array', 'min:2'],
36+
'topics_tags.*' => ['required', 'distinct', 'string', 'max:50', 'regex:'.config('computerScienceResource.tags_regex')],
3737

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

app/Http/Resources/ComputerScienceResourceResource.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ public function toArray(Request $request): array
2222
'platforms' => $this->platforms,
2323
'difficulties' => $this->difficulties,
2424
'pricing' => $this->pricing,
25-
'topic_tags' => $this->topic_tags,
26-
'programming_language_tags' => $this->programming_language_tags,
25+
'topics_tags' => $this->topics_tags,
26+
'programming_languages_tags' => $this->programming_languages_tags,
2727
'general_tags' => $this->general_tags,
2828
];
2929
}

app/Models/ComputerScienceResource.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
use Spatie\Tags\HasTags;
2424

2525
/**
26-
* @property array $topic_tags
27-
* @property array $programming_language_tags
26+
* @property array $topics_tags
27+
* @property array $programming_languages_tags
2828
* @property array $general_tags
2929
*/
3030
#[ObservedBy([ComputerScienceResourceObserver::class])]
@@ -54,9 +54,9 @@ class ComputerScienceResource extends Model
5454

5555
protected $table = 'computer_science_resources';
5656

57-
protected $guarded = ['topic_tags', 'programming_language_tags', 'general_tags'];
57+
protected $guarded = ['topics_tags', 'programming_languages_tags', 'general_tags'];
5858

59-
protected $appends = ['topic_tags', 'programming_language_tags', 'general_tags', 'vote_score', 'user_vote', 'comments_count', 'image_url'];
59+
protected $appends = ['topics_tags', 'programming_languages_tags', 'general_tags', 'vote_score', 'user_vote', 'comments_count', 'image_url'];
6060

6161
public function getActivitylogOptions(): LogOptions
6262
{
@@ -140,12 +140,12 @@ protected function difficulties(): Attribute
140140
/**
141141
* Accessor to get topic tags.
142142
*/
143-
protected function topicTags(): Attribute
143+
protected function topicsTags(): Attribute
144144
{
145145
return Attribute::make(
146146
get: fn () => $this->tagsWithType('topics_tags')->pluck('name')->toArray(),
147147
set: function (array $value) {
148-
$old_value = $this->topic_tags;
148+
$old_value = $this->topics_tags;
149149
$this->syncTagsWithType($value, 'topics_tags');
150150
TagFrequencyChanged::dispatch('topics_tags', $old_value, $value);
151151

@@ -157,12 +157,12 @@ protected function topicTags(): Attribute
157157
/**
158158
* Accessor to get programming language tags.
159159
*/
160-
protected function programmingLanguageTags(): Attribute
160+
protected function programmingLanguagesTags(): Attribute
161161
{
162162
return Attribute::make(
163163
get: fn () => $this->tagsWithType('programming_languages_tags')->pluck('name')->toArray(),
164164
set: function (array $value) {
165-
$old_value = $this->programming_language_tags;
165+
$old_value = $this->programming_languages_tags;
166166
$this->syncTagsWithType($value, 'programming_languages_tags');
167167
TagFrequencyChanged::dispatch('programming_languages_tags', $old_value, $value);
168168

app/Models/ResourceEdits.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ public function sluggable(): array
6060
{
6161
return [
6262
'slug' => [
63-
'source' => 'edit_title',
63+
'source' => ['edit_title', 'computer_science_resource_id'],
64+
'unique' => true,
6465
],
6566
];
6667
}

app/Observers/ComputerScienceResourceObserver.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ public function restored(ComputerScienceResource $computerScienceResource): void
4141

4242
public function deleting(ComputerScienceResource $computerScienceResource): void
4343
{
44-
$computerScienceResource->topic_tags = [];
45-
$computerScienceResource->programming_language_tags = [];
44+
$computerScienceResource->topics_tags = [];
45+
$computerScienceResource->programming_languages_tags = [];
4646
$computerScienceResource->general_tags = [];
4747

4848
// Delete the image

app/Services/ComputerScienceResourceService.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,11 @@ public function createResource(array $validatedData): ComputerScienceResource
8686
]);
8787

8888
// Add topics as tags
89-
$resource->topic_tags = $validatedData['topic_tags'];
89+
$resource->topics_tags = $validatedData['topics_tags'];
9090

9191
// Add programming languages as tags (if provided)
92-
if (isset($validatedData['programming_language_tags'])) {
93-
$resource->programming_language_tags = $validatedData['programming_language_tags'];
92+
if (isset($validatedData['programming_languages_tags'])) {
93+
$resource->programming_languages_tags = $validatedData['programming_languages_tags'];
9494
}
9595

9696
// Add general tags (if provided)

0 commit comments

Comments
 (0)