Skip to content

Commit 4f6069e

Browse files
committed
FIX: setting_id -> global_setting_id and switching inflator to FilterColumn
FIX: setting_id -> global_setting_id and switching inflator to FilterColumn
1 parent 259ff87 commit 4f6069e

File tree

15 files changed

+171
-207
lines changed

15 files changed

+171
-207
lines changed

lib/DB/Schema/Result/CourseSetting.pm

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ C<course_id>: database id of the course for the setting (foreign key)
2121
2222
=item *
2323
24-
C<setting_id>: database id that the given setting is related to (foreign key)
24+
C<global_setting_id>: database id of the global setting that the given setting is related to (foreign key)
2525
2626
=item *
2727
@@ -33,7 +33,7 @@ C<value>: the value of the setting as a JSON so different types of data can be s
3333

3434
__PACKAGE__->table('course_setting');
3535

36-
__PACKAGE__->load_components(qw/InflateColumn::Serializer InflateColumn::JSONValue Core/);
36+
__PACKAGE__->load_components(qw/FilterColumn Core/);
3737

3838
__PACKAGE__->add_columns(
3939
course_setting_id => {
@@ -46,24 +46,33 @@ __PACKAGE__->add_columns(
4646
data_type => 'integer',
4747
size => 16,
4848
},
49-
setting_id => {
49+
global_setting_id => {
5050
data_type => 'integer',
5151
size => 16,
5252
},
5353
value => {
54-
data_type => 'text',
55-
default_value => '{}',
56-
retrieve_on_insert => 1,
57-
inflate_value => 1,
54+
data_type => 'text',
55+
is_nullable => 1
5856
},
5957
);
6058

59+
__PACKAGE__->filter_column(
60+
value => {
61+
filter_to_storage => sub {
62+
return JSON::MaybeXS->new({ utf8 => 1, allow_nonref => 1 })->encode($_[1] // '');
63+
},
64+
filter_from_storage => sub {
65+
return JSON::MaybeXS->new({ utf8 => 1, allow_nonref => 1 })->decode($_[1] // '');
66+
}
67+
}
68+
);
69+
6170
__PACKAGE__->set_primary_key('course_setting_id');
6271

63-
__PACKAGE__->add_unique_constraint([qw/course_id setting_id/]);
72+
__PACKAGE__->add_unique_constraint([qw/course_id global_setting_id/]);
6473

6574
__PACKAGE__->belongs_to(course => 'DB::Schema::Result::Course', 'course_id');
6675

67-
__PACKAGE__->belongs_to(global_setting => 'DB::Schema::Result::GlobalSetting', 'setting_id');
76+
__PACKAGE__->belongs_to(global_setting => 'DB::Schema::Result::GlobalSetting', 'global_setting_id');
6877

6978
1;

lib/DB/Schema/Result/GlobalSetting.pm

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ This is the database schema for the Global Course Settings.
1313
1414
=item *
1515
16-
C<setting_id>: database id (autoincrement integer)
16+
C<global_setting_id>: database id (autoincrement integer)
1717
1818
=item *
1919
@@ -53,10 +53,10 @@ C<subcategory>: the subcategory of the setting (may be null)
5353

5454
__PACKAGE__->table('global_setting');
5555

56-
__PACKAGE__->load_components(qw/InflateColumn::Serializer InflateColumn::JSONValue Core/);
56+
__PACKAGE__->load_components(qw/InflateColumn::Serializer FilterColumn Core/);
5757

5858
__PACKAGE__->add_columns(
59-
setting_id => {
59+
global_setting_id => {
6060
data_type => 'integer',
6161
size => 16,
6262
is_auto_increment => 1,
@@ -66,10 +66,8 @@ __PACKAGE__->add_columns(
6666
size => 256,
6767
},
6868
default_value => {
69-
data_type => 'text',
70-
default_value => '{}',
71-
retrieve_on_insert => 1,
72-
inflate_value => 1,
69+
data_type => 'text',
70+
default_value => '""'
7371
},
7472
description => {
7573
data_type => 'text',
@@ -87,7 +85,6 @@ __PACKAGE__->add_columns(
8785
options => {
8886
data_type => 'text',
8987
is_nullable => 1,
90-
retrieve_on_insert => 1,
9188
serializer_class => 'JSON',
9289
serializer_options => { utf8 => 1 }
9390
},
@@ -102,9 +99,20 @@ __PACKAGE__->add_columns(
10299
is_nullable => 1
103100
}
104101
);
102+
use Data::Dumper;
103+
__PACKAGE__->filter_column(
104+
default_value => {
105+
filter_to_storage => sub {
106+
return JSON::MaybeXS->new({ utf8 => 1, allow_nonref => 1 })->encode($_[1] // '');
107+
},
108+
filter_from_storage => sub {
109+
return JSON::MaybeXS->new({ utf8 => 1, allow_nonref => 1 })->decode($_[1] // '');
110+
}
111+
}
112+
);
105113

106-
__PACKAGE__->set_primary_key('setting_id');
114+
__PACKAGE__->set_primary_key('global_setting_id');
107115

108-
__PACKAGE__->has_many(course_settings => 'DB::Schema::Result::CourseSetting', 'setting_id');
116+
__PACKAGE__->has_many(course_settings => 'DB::Schema::Result::CourseSetting', 'global_setting_id');
109117

110118
1;

lib/DB/Schema/ResultSet/Course.pm

Lines changed: 21 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ This gets a single global/default setting.
296296
297297
=over
298298
299-
=item * C<info> which is a hash of either a C<setting_id> or C<setting_name> with information
299+
=item * C<info> which is a hash of either a C<global_setting_id> or C<setting_name> with information
300300
on the setting.
301301
302302
=item * C<$as_result_set>, a boolean if the return is to be a result_set
@@ -315,11 +315,10 @@ sub getGlobalSetting ($self, %args) {
315315

316316
DB::Exception::SettingNotFound->throw(message => $setting_info->{setting_name}
317317
? "The setting with name $setting_info->{setting_name} is not found"
318-
: "The setting with setting_id $setting_info->{setting_id} is not found")
318+
: "The setting with global_setting_id $setting_info->{global_setting_id} is not found")
319319
unless $global_setting;
320320
return $global_setting if $args{as_result_set};
321-
my $setting_to_return = { $global_setting->get_inflated_columns };
322-
return $setting_to_return;
321+
return { $global_setting->get_inflated_columns };
323322
}
324323

325324
=head2 getCourseSettings
@@ -351,12 +350,9 @@ sub getCourseSettings ($self, %args) {
351350
my @settings_from_db = $course->course_settings;
352351

353352
return \@settings_from_db if $args{as_result_set};
354-
my @settings_to_return = map {
355-
$args{merged}
356-
? { $_->get_inflated_columns, $_->global_setting->get_inflated_columns }
357-
: { $_->get_inflated_columns };
358-
} @settings_from_db;
359-
return \@settings_to_return;
353+
return $args{merged}
354+
? [ map { { $_->get_inflated_columns, $_->global_setting->get_inflated_columns } } @settings_from_db ]
355+
: [ map { { $_->get_inflated_columns } } @settings_from_db ];
360356
}
361357

362358
=pod
@@ -369,7 +365,7 @@ This gets a single course setting.
369365
370366
=over
371367
372-
=item * C<info> which is a hash of either a C<setting_id> or C<setting_name> with information
368+
=item * C<info> which is a hash of either a C<global_setting_id> or C<setting_name> with information
373369
on the setting.
374370
375371
=item * C<merged>, a boolean on whether the course setting is merged with its corresponding
@@ -388,30 +384,28 @@ A single course setting as either a hashref or a C<DBIx::Class::ResultSet::Cours
388384
sub getCourseSetting ($self, %args) {
389385
my $global_setting = $self->getGlobalSetting(info => $args{info}, as_result_set => 1);
390386
DB::Exception::SettingNotFound->throw(
391-
message => "The global setting with name: '" . $args{info}->{setting_name} . "' is not a defined info.")
387+
message => "The global setting with name: '" . $args{info}{setting_name} . "' is not defined.")
392388
unless defined($global_setting);
393389

394390
my $course = $self->getCourse(info => getCourseInfo($args{info}), as_result_set => 1);
395-
my $setting = $course->course_settings->find({ setting_id => $global_setting->setting_id });
391+
my $setting = $course->course_settings->find({ global_setting_id => $global_setting->global_setting_id });
396392

397393
DB::Exception::SettingNotFound->throw(
398394
message => 'The course setting with '
399395
. (
400-
$args{info}->{setting_name} ? " name: '$args{info}->{setting_name}'"
401-
: "setting_id of $args{info}->{setting_id} is not a found in the course "
396+
$args{info}{setting_name} ? " name: '$args{info}{setting_name}'"
397+
: "global_setting_id of $args{info}{global_setting_id} is not found in the course "
402398
)
403399
. (
404-
$args{info}->{course_name} ? ("with name '" . $args{info}->{course_name} . "'")
405-
: "with course_id of $args{info}->{course_id}"
400+
$args{info}{course_name} ? ("with name '" . $args{info}{course_name} . "'")
401+
: "with course_id of $args{info}{course_id}"
406402
)
407403
) unless defined($setting);
408404

409405
return $setting if $args{as_result_set};
410-
my $setting_to_return =
411-
$args{merged}
406+
return $args{merged}
412407
? { $setting->get_inflated_columns, $setting->global_setting->get_inflated_columns }
413408
: { $setting->get_inflated_columns };
414-
return $setting_to_return;
415409
}
416410

417411
=pod
@@ -425,7 +419,7 @@ Update a single course setting.
425419
=over
426420
427421
=item * C<info> which is a hash containing information about the course (either a
428-
C<course_id> or C<course_name>) and a setting (either a C<setting_id> or C<setting_name>).
422+
C<course_id> or C<course_name>) and a setting (either a C<global_setting_id> or C<setting_name>).
429423
430424
=item * C<params> the updated value of the course setting.
431425
@@ -446,14 +440,12 @@ sub updateCourseSetting ($self, %args) {
446440
my $course = $self->getCourse(info => getCourseInfo($args{info}), as_result_set => 1);
447441
my $global_setting = $self->getGlobalSetting(info => getSettingInfo($args{info}));
448442

449-
my $course_setting = $course->course_settings->find({
450-
setting_id => $global_setting->{setting_id}
451-
});
443+
my $course_setting = $course->course_settings->find({ global_setting_id => $global_setting->{global_setting_id} });
452444

453445
my $params = {
454-
course_id => $course->course_id,
455-
setting_id => $global_setting->{setting_id},
456-
value => $args{params}{value}
446+
course_id => $course->course_id,
447+
global_setting_id => $global_setting->{global_setting_id},
448+
value => $args{params}{value} =~ /^$/ ? undef : $args{params}{value}
457449
};
458450

459451
isValidSetting($global_setting, $params->{value});
@@ -462,12 +454,9 @@ sub updateCourseSetting ($self, %args) {
462454
defined($course_setting) ? $course_setting->update($params) : $course->add_to_course_settings($params);
463455

464456
return $up_setting if $args{as_result_set};
465-
my $setting_to_return =
466-
($args{merged})
457+
return ($args{merged})
467458
? { $up_setting->get_inflated_columns, $up_setting->global_setting->get_inflated_columns }
468459
: { $up_setting->get_inflated_columns };
469-
470-
return $setting_to_return;
471460
}
472461

473462
=pod
@@ -481,7 +470,7 @@ Delete a single course setting.
481470
=over
482471
483472
=item * C<info> which is a hash containing information about the course (either a
484-
C<course_id> or C<course_name>) and a setting (either a C<setting_id> or C<setting_name>).
473+
C<course_id> or C<course_name>) and a setting (either a C<global_setting_id> or C<setting_name>).
485474
486475
=item * C<$as_result_set>, a boolean if the return is to be a result_set
487476

lib/DB/Utils.pm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ sub getPoolProblemInfo ($in) {
4242
}
4343

4444
sub getSettingInfo ($in) {
45-
return _get_info($in, qw/setting_name setting_id/);
45+
return _get_info($in, qw/setting_name global_setting_id/);
4646
}
4747

4848
# This is a generic internal subroutine to check that the info passed in contains certain fields.

lib/DBIx/Class/InflateColumn/JSONValue.pm

Lines changed: 0 additions & 45 deletions
This file was deleted.

lib/WeBWorK3.pm

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -219,12 +219,12 @@ sub problemRoutes ($app, $course_routes) {
219219
sub settingsRoutes ($app, $course_routes) {
220220
my $global_settings = $app->routes->any('/webwork3/api/global-settings')->requires(authenticated => 1);
221221
$global_settings->get('/')->to('Settings#getGlobalSettings');
222-
$global_settings->get('/:setting_id')->to('Settings#getGlobalSetting');
222+
$global_settings->get('/:global_setting_id')->to('Settings#getGlobalSetting');
223223
$global_settings->post('/check-timezone')->to('Settings#checkTimeZone');
224224
$course_routes->get('/settings')->to('Settings#getCourseSettings');
225-
$course_routes->get('/settings/:setting_id')->to('Settings#getCourseSetting');
226-
$course_routes->put('/settings/:setting_id')->to('Settings#updateCourseSetting');
227-
$course_routes->delete('/settings/:setting_id')->to('Settings#deleteCourseSetting');
225+
$course_routes->get('/settings/:global_setting_id')->to('Settings#getCourseSetting');
226+
$course_routes->put('/settings/:global_setting_id')->to('Settings#updateCourseSetting');
227+
$course_routes->delete('/settings/:global_setting_id')->to('Settings#deleteCourseSetting');
228228
return;
229229
}
230230

0 commit comments

Comments
 (0)