diff --git a/lib/Pear/LocalLoop/Controller/Api/Upload.pm b/lib/Pear/LocalLoop/Controller/Api/Upload.pm index 87a3fba..7b037d5 100644 --- a/lib/Pear/LocalLoop/Controller/Api/Upload.pm +++ b/lib/Pear/LocalLoop/Controller/Api/Upload.pm @@ -150,7 +150,7 @@ sub post_upload { street_name => $validation->param('street_name'), town => $validation->param('town'), postcode => $validation->param('postcode'), - pending => \"1" + pending => 1, }); $organisation = $entity->organisation; } diff --git a/lib/Pear/LocalLoop/Schema/Result/Organisation.pm b/lib/Pear/LocalLoop/Schema/Result/Organisation.pm index a178e07..aa75de7 100644 --- a/lib/Pear/LocalLoop/Schema/Result/Organisation.pm +++ b/lib/Pear/LocalLoop/Schema/Result/Organisation.pm @@ -5,7 +5,7 @@ use warnings; use base 'DBIx::Class::Core'; -__PACKAGE__->load_components("InflateColumn::DateTime"); +__PACKAGE__->load_components("InflateColumn::DateTime", "FilterColumn"); __PACKAGE__->table("organisations"); @@ -51,7 +51,7 @@ __PACKAGE__->add_columns( }, pending => { data_type => 'boolean', - default_value => \"0", + default => \"false", is_nullable => 0, }, submitted_by_id => { @@ -68,4 +68,40 @@ __PACKAGE__->belongs_to( "entity_id", ); +__PACKAGE__->filter_column( pending => { + filter_to_storage => 'to_bool', + filter_from_storage => 'from_bool', +}); + +# Only works when calling ->deploy, but atleast helps for tests +sub sqlt_deploy_hook { + my ( $source_instance, $sqlt_table ) = @_; + my $pending_field = $sqlt_table->get_field('pending'); + if ( $sqlt_table->schema->translator->producer_type =~ /SQLite$/ ) { + $pending_field->{default_value} = 0; + } else { + $pending_field->{default_value} = \"false"; + } +} + +sub to_bool { + my ( $self, $val ) = @_; + my $driver_name = $self->result_source->schema->storage->dbh->{Driver}->{Name}; + if ( $driver_name eq 'SQLite' ) { + return $val ? 1 : 0; + } else { + return $val ? 'true' : 'false'; + } +} + +sub from_bool { + my ( $self, $val ) = @_; + my $driver_name = $self->result_source->schema->storage->dbh->{Driver}->{Name}; + if ( $driver_name eq 'SQLite' ) { + return $val; + } else { + return lc $val eq 'true' ? 1 : 0; + } +} + 1; diff --git a/lib/Pear/LocalLoop/Schema/Result/User.pm b/lib/Pear/LocalLoop/Schema/Result/User.pm index 7c57d40..b5457c5 100644 --- a/lib/Pear/LocalLoop/Schema/Result/User.pm +++ b/lib/Pear/LocalLoop/Schema/Result/User.pm @@ -11,6 +11,7 @@ __PACKAGE__->load_components( qw/ InflateColumn::DateTime PassphraseColumn TimeStamp + FilterColumn /); __PACKAGE__->table("users"); @@ -48,7 +49,7 @@ __PACKAGE__->add_columns( }, "is_admin" => { data_type => "boolean", - default_value => \"0", + default_value => \"false", is_nullable => 0, }, ); @@ -77,6 +78,41 @@ __PACKAGE__->has_many( { cascade_copy => 0, cascade_delete => 0 }, ); +sub sqlt_deploy_hook { + my ( $source_instance, $sqlt_table ) = @_; + my $pending_field = $sqlt_table->get_field('is_admin'); + if ( $sqlt_table->schema->translator->producer_type =~ /SQLite$/ ) { + $pending_field->{default_value} = 0; + } else { + $pending_field->{default_value} = \"false"; + } +} + +__PACKAGE__->filter_column( is_admin => { + filter_to_storage => 'to_bool', + filter_from_storage => 'from_bool', +}); + +sub to_bool { + my ( $self, $val ) = @_; + my $driver_name = $self->result_source->schema->storage->dbh->{Driver}->{Name}; + if ( $driver_name eq 'SQLite' ) { + return $val ? 1 : 0; + } else { + return $val ? 'true' : 'false'; + } +} + +sub from_bool { + my ( $self, $val ) = @_; + my $driver_name = $self->result_source->schema->storage->dbh->{Driver}->{Name}; + if ( $driver_name eq 'SQLite' ) { + return $val; + } else { + return lc $val eq 'true' ? 1 : 0; + } +} + sub generate_session { my $self = shift; diff --git a/share/ddl/PostgreSQL/deploy/6/001-auto.sql b/share/ddl/PostgreSQL/deploy/6/001-auto.sql index 0d97746..21223b0 100644 --- a/share/ddl/PostgreSQL/deploy/6/001-auto.sql +++ b/share/ddl/PostgreSQL/deploy/6/001-auto.sql @@ -1,6 +1,6 @@ -- -- Created by SQL::Translator::Producer::PostgreSQL --- Created on Fri Sep 1 15:14:28 2017 +-- Created on Tue Sep 5 17:22:16 2017 -- ; -- @@ -76,7 +76,7 @@ CREATE TABLE "organisations" ( "postcode" character varying(16), "country" character varying(255), "sector" character varying(1), - "pending" boolean DEFAULT 0 NOT NULL, + "pending" boolean DEFAULT false NOT NULL, "submitted_by_id" integer, PRIMARY KEY ("id") ); @@ -109,7 +109,7 @@ CREATE TABLE "users" ( "email" text NOT NULL, "join_date" timestamp NOT NULL, "password" character varying(100) NOT NULL, - "is_admin" boolean DEFAULT 0 NOT NULL, + "is_admin" boolean DEFAULT false NOT NULL, PRIMARY KEY ("id"), CONSTRAINT "users_email" UNIQUE ("email") ); diff --git a/share/ddl/PostgreSQL/upgrade/5-6/001-auto.sql b/share/ddl/PostgreSQL/upgrade/5-6/001-auto.sql index c970017..f6c5187 100644 --- a/share/ddl/PostgreSQL/upgrade/5-6/001-auto.sql +++ b/share/ddl/PostgreSQL/upgrade/5-6/001-auto.sql @@ -34,7 +34,7 @@ CREATE TABLE "organisations" ( "postcode" character varying(16), "country" character varying(255), "sector" character varying(1), - "pending" boolean DEFAULT 0 NOT NULL, + "pending" boolean DEFAULT false NOT NULL, "submitted_by_id" integer, PRIMARY KEY ("id") ); @@ -59,7 +59,7 @@ CREATE TABLE "users" ( "email" text NOT NULL, "join_date" timestamp NOT NULL, "password" character varying(100) NOT NULL, - "is_admin" boolean DEFAULT 0 NOT NULL, + "is_admin" boolean DEFAULT false NOT NULL, PRIMARY KEY ("id"), CONSTRAINT "users_email" UNIQUE ("email") ); diff --git a/share/ddl/_source/deploy/6/001-auto.yml b/share/ddl/_source/deploy/6/001-auto.yml index 2448ed4..6d9e363 100644 --- a/share/ddl/_source/deploy/6/001-auto.yml +++ b/share/ddl/_source/deploy/6/001-auto.yml @@ -652,7 +652,7 @@ schema: pending: data_type: boolean default_value: !!perl/ref - =: 0 + =: false is_nullable: 0 is_primary_key: 0 is_unique: 0 @@ -1000,7 +1000,7 @@ schema: is_admin: data_type: boolean default_value: !!perl/ref - =: 0 + =: false is_nullable: 0 is_primary_key: 0 is_unique: 0 diff --git a/t/api/upload.t b/t/api/upload.t index fb64136..74b3409 100644 --- a/t/api/upload.t +++ b/t/api/upload.t @@ -201,12 +201,11 @@ $json = { organisation_id => 1, session_key => $session_key, }; -$upload = {json => Mojo::JSON::encode_json($json)}; -$t->post_ok('/api/upload' => form => $upload ) -->status_is(200) -->or($framework->dump_error) -->json_is('/success', Mojo::JSON->true) -->json_like('/message', qr/Upload Successful/); +$t->post_ok('/api/upload' => json => $json ) + ->status_is(200) + ->or($framework->dump_error) + ->json_is('/success', Mojo::JSON->true) + ->json_like('/message', qr/Upload Successful/); is $schema->resultset('Transaction')->count, 1, "1 transaction"; print "test 13 - organisation_id missing (type 1: already validated)\n";