11package DB::Schema::Result::Course ;
2- use base qw/ DBIx::Class::Core/ ;
32use strict;
43use warnings;
4+ use feature ' signatures' ;
5+ no warnings qw/ experimental::signatures/ ;
56
7+ use base qw/ DBIx::Class::Core DB::Validation/ ;
68use Mojo::JSON qw/ true false/ ;
79
810=head1 DESCRIPTION
@@ -33,11 +35,6 @@ C<visible>: a boolean on whether the course is visible or not.
3335
3436=cut
3537
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-
4138__PACKAGE__ -> table(' course' );
4239
4340__PACKAGE__ -> load_components(qw/ InflateColumn::Serializer InflateColumn::Boolean Core/ );
@@ -46,23 +43,20 @@ __PACKAGE__->add_columns(
4643 course_id => {
4744 data_type => ' integer' ,
4845 size => 16,
49- is_nullable => 0,
5046 is_auto_increment => 1,
5147 },
5248 course_name => {
53- data_type => ' text' ,
54- is_nullable => 0,
49+ data_type => ' text' ,
5550 },
5651 course_dates => {
5752 data_type => ' text' ,
58- is_nullable => 0,
5953 default_value => ' {}' ,
54+ retrieve_on_insert => 1,
6055 serializer_class => ' JSON' ,
6156 serializer_options => { utf8 => 1 }
6257 },
6358 visible => {
6459 data_type => ' boolean' ,
65- is_nullable => 0,
6660 default_value => 1,
6761 retrieve_on_insert => 1
6862 }
@@ -83,4 +77,53 @@ __PACKAGE__->has_many(problem_pools => 'DB::Schema::Result::ProblemPool', 'cours
8377# set up the one-to-one relationship to course settings;
8478__PACKAGE__ -> has_one(course_settings => ' DB::Schema::Result::CourseSettings' , ' course_id' );
8579
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+
861291;
0 commit comments