App-Dochazka-REST

 view release on metacpan or  search on metacpan

lib/App/Dochazka/REST/Model/Schedule.pm  view on Meta::CPAN

        {
            #
            # 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 ],



( run in 1.033 second using v1.01-cache-2.11-cpan-39bf76dae61 )