1
1
package DB::Schema::Result::Course ;
2
- use base qw/ DBIx::Class::Core/ ;
3
2
use strict;
4
3
use warnings;
4
+ use feature ' signatures' ;
5
+ no warnings qw/ experimental::signatures/ ;
5
6
7
+ use base qw/ DBIx::Class::Core DB::Validation/ ;
6
8
use Mojo::JSON qw/ true false/ ;
7
9
8
10
=head1 DESCRIPTION
@@ -33,11 +35,6 @@ C<visible>: a boolean on whether the course is visible or not.
33
35
34
36
=cut
35
37
36
- our @VALID_DATES = qw/ open end/ ;
37
- our @REQUIRED_DATES = qw/ / ;
38
- our $VALID_PARAMS = { visible => q{ [01]} };
39
- our $REQUIRED_PARAMS = { _ALL_ => [' visible' ] };
40
-
41
38
__PACKAGE__ -> table(' course' );
42
39
43
40
__PACKAGE__ -> load_components(qw/ InflateColumn::Serializer InflateColumn::Boolean Core/ );
@@ -46,23 +43,20 @@ __PACKAGE__->add_columns(
46
43
course_id => {
47
44
data_type => ' integer' ,
48
45
size => 16,
49
- is_nullable => 0,
50
46
is_auto_increment => 1,
51
47
},
52
48
course_name => {
53
- data_type => ' text' ,
54
- is_nullable => 0,
49
+ data_type => ' text' ,
55
50
},
56
51
course_dates => {
57
52
data_type => ' text' ,
58
- is_nullable => 0,
59
53
default_value => ' {}' ,
54
+ retrieve_on_insert => 1,
60
55
serializer_class => ' JSON' ,
61
56
serializer_options => { utf8 => 1 }
62
57
},
63
58
visible => {
64
59
data_type => ' boolean' ,
65
- is_nullable => 0,
66
60
default_value => 1,
67
61
retrieve_on_insert => 1
68
62
}
@@ -83,4 +77,53 @@ __PACKAGE__->has_many(problem_pools => 'DB::Schema::Result::ProblemPool', 'cours
83
77
# set up the one-to-one relationship to course settings;
84
78
__PACKAGE__ -> has_one(course_settings => ' DB::Schema::Result::CourseSettings' , ' course_id' );
85
79
80
+ =head2 C<valid_fields >
81
+
82
+ subroutine that returns a hash of the valid fields for json columns
83
+
84
+ =cut
85
+
86
+ sub valid_fields ($, $field_name ) {
87
+ if ($field_name eq ' course_dates' ) {
88
+ return {
89
+ open => q{ \d+} ,
90
+ end => q{ \d+}
91
+ };
92
+ } elsif ($field_name eq ' course_params' ) {
93
+ return { visible => ' bool' };
94
+ } else {
95
+ return {};
96
+ }
97
+ }
98
+
99
+ =head2 C<additional_validation >
100
+
101
+ subroutine that checks json columns for consistency
102
+
103
+ =cut
104
+
105
+ sub additional_validation ($course , $field_name ) {
106
+ return 1 if ($field_name ne ' course_dates' );
107
+
108
+ my $dates = $course -> get_inflated_column(' course_dates' );
109
+ DB::Exception::ImproperDateOrder-> throw(message => ' The course dates are not in order' )
110
+ unless $dates -> {end } > $dates -> {open };
111
+
112
+ return 1;
113
+ }
114
+
115
+ =head2 C<required >
116
+
117
+ subroutine that returns a hashref describing the required fields in JSON columns
118
+
119
+ =cut
120
+
121
+ sub required ($, $field_name ) {
122
+ if ($field_name eq ' set_dates' ) {
123
+ return { ' _ALL_' => [ ' open' , ' end' ] };
124
+ } else {
125
+ return {};
126
+ }
127
+ }
128
+
86
129
1;
0 commit comments