App-Dochazka-REST
view release on metacpan or search on metacpan
lib/App/Dochazka/REST/Model/Schedule.pm view on Meta::CPAN
=item * L<reset> method (recycles an existing object)
=item * basic accessors (L<sid>, L<schedule>, L<remark>)
=item * L<insert> method (inserts the schedule if it isn't in the database already)
=item * L<delete> method
=item * L<load> method (not implemented yet)
#=item * L<get_schedule_json> function (get JSON string associated with a given SID)
#
=back
For basic workflow, see C<t/model/schedule.t>.
=head1 EXPORTS
This module provides the following exports:
=over
#=item * C<get_schedule_json>
#
=item * C<get_all_schedules>
=item * C<sid_exists> (boolean)
=back
=cut
use Exporter qw( import );
our @EXPORT_OK = qw(
get_all_schedules
sid_exists
);
=head1 METHODS
=head2 insert
Instance method. Attempts to INSERT a record into the 'schedules' table.
Field values are taken from the object. Returns a status object.
If the "schedule" field of the schedule to be inserted matches an existing
schedule, no new record is inserted. Instead, the existing schedule record
is returned. In such a case, the "scode", "remark", and "disabled" fields
are ignored - except when they are NULL in the existing record.
=cut
sub insert {
my $self = shift;
my ( $context ) = validate_pos( @_, { type => HASHREF } );
# if the exact same schedule is already in the database, we
# don't insert it again
my $status = select_single(
conn => $context->{'dbix_conn'},
sql => $site->SQL_SCHEDULES_SELECT_BY_SCHEDULE,
keys => [ $self->{schedule} ],
);
$log->info( "select_single returned: " . Dumper $status );
if ( $status->level eq 'OK' ) {
my $found_sched = App::Dochazka::REST::Model::Schedule->spawn(
sid => $status->payload->[0],
scode => $status->payload->[1],
schedule => $status->payload->[2],
remark => $status->payload->[3],
disabled => $status->payload->[4],
);
$self->{'sid'} = $found_sched->sid;
{
#
# the exact schedule exists, but if any of { scode, remark, disabled }
# are NULL and we have a value, update the record to reflect the value
# (in other words, do not prefer NULLs over real values)
#
my $do_update = 0;
if ( ! defined( $found_sched->scode ) and defined( $self->scode ) ) {
$found_sched->scode( $self->scode );
$do_update = 1;
}
if ( ! defined( $found_sched->remark ) and defined( $self->remark ) ) {
$found_sched->remark( $self->remark );
$do_update = 1;
}
if ( ! defined( $found_sched->disabled ) and defined( $self->disabled ) ) {
$found_sched->disabled( $self->disabled );
$do_update = 1;
}
if ( $do_update ) {
$status = $found_sched->update( $context );
if ( $status->level eq 'OK' and $status->code eq 'DOCHAZKA_CUD_OK' ) {
$status->code( 'DOCHAZKA_SCHEDULE_UPDATE_OK' );
}
return $status;
}
return $CELL->status_ok( 'DOCHAZKA_SCHEDULE_EXISTS', args => [ $self->{sid} ],
payload => $found_sched );
}
} elsif( $status->level ne 'NOTICE' ) {
$log->info( "select_single status was neither OK nor NOTICE; returning it as-is" );
return $status;
}
# no exact match found, insert a new record
$log->debug( __PACKAGE__ . "::insert calling cud to insert new schedule" );
$status = cud(
conn => $context->{'dbix_conn'},
eid => $context->{'current'}->{'eid'},
object => $self,
sql => $site->SQL_SCHEDULE_INSERT,
attrs => [ 'scode', 'schedule', 'remark' ],
);
if ( $status->ok ) {
$status->code( 'DOCHAZKA_SCHEDULE_INSERT_OK' );
$log->info( "Inserted new schedule with SID " . $self->{sid} );
}
return $status;
}
=head2 update
Although we do not allow the 'sid' or 'schedule' fields to be updated, schedule
records have 'scode', 'remark' and 'disabled' fields that can be updated via this
method.
=cut
sub update {
my $self = shift;
my ( $context ) = validate_pos( @_, { type => HASHREF } );
return $CELL->status_err( 'DOCHAZKA_MALFORMED_400' ) unless $self->{'sid'};
my $status = cud(
conn => $context->{'dbix_conn'},
eid => $context->{'current'}->{'eid'},
object => $self,
sql => $site->SQL_SCHEDULE_UPDATE,
attrs => [ 'scode', 'remark', 'disabled', 'sid' ],
);
return $status;
}
=head2 delete
Instance method. Attempts to DELETE a schedule record. This may succeed
if no other records in the database refer to this schedule.
=cut
sub delete {
my $self = shift;
my ( $context ) = validate_pos( @_, { type => HASHREF } );
my $status = cud(
conn => $context->{'dbix_conn'},
eid => $context->{'current'}->{'eid'},
object => $self,
sql => $site->SQL_SCHEDULE_DELETE,
attrs => [ 'sid' ],
);
$self->reset( sid => $self->{sid} ) if $status->ok;
$log->debug( "Entering " . __PACKAGE__ . "::delete with status " . Dumper( $status ) );
return $status;
}
=head2 load_by_scode
Analogous function to L<App::Dochazka::REST::Model::Activity/"load_by_aid">.
=cut
sub load_by_scode {
my $self = shift;
my ( $conn, $scode ) = validate_pos( @_,
{ isa => 'DBIx::Connector' },
{ type => SCALAR },
);
return load(
conn => $conn,
class => __PACKAGE__,
sql => $site->SQL_SCHEDULE_SELECT_BY_SCODE,
keys => [ $scode ],
);
}
=head2 load_by_sid
Analogous function to L<App::Dochazka::REST::Model::Activity/"load_by_aid">.
=cut
sub load_by_sid {
my $self = shift;
my ( $conn, $sid ) = validate_pos( @_,
{ isa => 'DBIx::Connector' },
{ type => SCALAR },
);
return load(
conn => $conn,
class => __PACKAGE__,
sql => $site->SQL_SCHEDULE_SELECT_BY_SID,
keys => [ $sid ],
);
}
=head1 FUNCTIONS
=head2 sid_exists
Boolean function
=cut
BEGIN {
no strict 'refs';
*{'sid_exists'} = App::Dochazka::REST::Model::Shared::make_test_exists( 'sid' );
}
=head2 get_all_schedules
Returns a list of all schedule objects, ordered by sid. Takes one
argument - a paramhash that can contain only one key, 'disabled',
which can be either true or false (defaults to true).
=cut
sub get_all_schedules {
my %PH = validate( @_, {
conn => { isa => 'DBIx::Connector' },
disabled => { type => SCALAR, default => 0 }
} );
my $sql = $PH{disabled}
? $site->SQL_SCHEDULES_SELECT_ALL_INCLUDING_DISABLED
: $site->SQL_SCHEDULES_SELECT_ALL_EXCEPT_DISABLED;
# run the query and gather the results
return load_multiple(
conn => $PH{'conn'},
class => __PACKAGE__,
sql => $sql,
keys => [],
);
}
#=head2 get_schedule_json
#
#Given a SID, queries the database for the JSON string associated with the SID.
#Returns undef if not found.
#
#=cut
#
#sub get_schedule_json {
# my ( $sid ) = @_;
# die "Problem with arguments in get_schedule_json" if not defined $sid;
#
# my $json;
# try {
# $conn->do( fixup => sub {
# ( $json ) = $_->selectrow_array( $site->SQL_SCHEDULES_SELECT_SCHEDULE,
# undef,
# $sid );
# } );
# }
#
# if ( $json ) {
# $log->debug( __PACKAGE__ . "::get_schedule_json got schedule from database: $json" );
# return decode_schedule_json( $json );
# }
# return;
#}
=head1 AUTHOR
Nathan Cutler, C<< <presnypreklad@gmail.com> >>
=cut
1;
( run in 0.548 second using v1.01-cache-2.11-cpan-d7f47b0818f )