Skip to content

Commit 1b99d98

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

File tree

15 files changed

+167
-198
lines changed

15 files changed

+167
-198
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: 17 additions & 23 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
@@ -369,7 +368,7 @@ This gets a single course setting.
369368
370369
=over
371370
372-
=item * C<info> which is a hash of either a C<setting_id> or C<setting_name> with information
371+
=item * C<info> which is a hash of either a C<global_setting_id> or C<setting_name> with information
373372
on the setting.
374373
375374
=item * C<merged>, a boolean on whether the course setting is merged with its corresponding
@@ -388,21 +387,21 @@ A single course setting as either a hashref or a C<DBIx::Class::ResultSet::Cours
388387
sub getCourseSetting ($self, %args) {
389388
my $global_setting = $self->getGlobalSetting(info => $args{info}, as_result_set => 1);
390389
DB::Exception::SettingNotFound->throw(
391-
message => "The global setting with name: '" . $args{info}->{setting_name} . "' is not a defined info.")
390+
message => "The global setting with name: '" . $args{info}{setting_name} . "' is not defined.")
392391
unless defined($global_setting);
393392

394393
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 });
394+
my $setting = $course->course_settings->find({ global_setting_id => $global_setting->global_setting_id });
396395

397396
DB::Exception::SettingNotFound->throw(
398397
message => 'The course setting with '
399398
. (
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 "
399+
$args{info}{setting_name} ? " name: '$args{info}{setting_name}'"
400+
: "global_setting_id of $args{info}{global_setting_id} is not found in the course "
402401
)
403402
. (
404-
$args{info}->{course_name} ? ("with name '" . $args{info}->{course_name} . "'")
405-
: "with course_id of $args{info}->{course_id}"
403+
$args{info}{course_name} ? ("with name '" . $args{info}{course_name} . "'")
404+
: "with course_id of $args{info}{course_id}"
406405
)
407406
) unless defined($setting);
408407

@@ -425,7 +424,7 @@ Update a single course setting.
425424
=over
426425
427426
=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>).
427+
C<course_id> or C<course_name>) and a setting (either a C<global_setting_id> or C<setting_name>).
429428
430429
=item * C<params> the updated value of the course setting.
431430
@@ -446,14 +445,12 @@ sub updateCourseSetting ($self, %args) {
446445
my $course = $self->getCourse(info => getCourseInfo($args{info}), as_result_set => 1);
447446
my $global_setting = $self->getGlobalSetting(info => getSettingInfo($args{info}));
448447

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

453450
my $params = {
454-
course_id => $course->course_id,
455-
setting_id => $global_setting->{setting_id},
456-
value => $args{params}{value}
451+
course_id => $course->course_id,
452+
global_setting_id => $global_setting->{global_setting_id},
453+
value => $args{params}{value} =~ /^$/ ? undef : $args{params}{value}
457454
};
458455

459456
isValidSetting($global_setting, $params->{value});
@@ -462,12 +459,9 @@ sub updateCourseSetting ($self, %args) {
462459
defined($course_setting) ? $course_setting->update($params) : $course->add_to_course_settings($params);
463460

464461
return $up_setting if $args{as_result_set};
465-
my $setting_to_return =
466-
($args{merged})
462+
return ($args{merged})
467463
? { $up_setting->get_inflated_columns, $up_setting->global_setting->get_inflated_columns }
468464
: { $up_setting->get_inflated_columns };
469-
470-
return $setting_to_return;
471465
}
472466

473467
=pod
@@ -481,7 +475,7 @@ Delete a single course setting.
481475
=over
482476
483477
=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>).
478+
C<course_id> or C<course_name>) and a setting (either a C<global_setting_id> or C<setting_name>).
485479
486480
=item * C<$as_result_set>, a boolean if the return is to be a result_set
487481

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)