1. WebGUI 8 Apps

    Doug Bell

    Plain Black Corp | WebGUI

  2. WebGUI

  3. WebGUI 8

  4. WebGUI 8

  5. Hello, World!

    package WebGUI::Asset::HelloWorld;
    
    use Moose;
    use WebGUI::Definition::Asset;
    extends 'WebGUI::Asset';
    
    define 'assetName' => 'Hello World';
    
    sub www_view {
        my ( $self ) = @_;
        my $resp = $self->session->response;
        $resp->content_type('text/plain');
        return "Hello, World!\n";
    }
    
    1;
    
  6. Moose Definition

    use WebGUI::Definition::Asset;
    define 'assetName' => 'Hello World';
    
  7. Plack

    $session->request->content_type( 'text/html' );
    
  8. Installing Asset

    // etc/site.conf
    {
        "assets" : {
            "WebGUI::Asset::HelloWorld" : {
                "category" : "basic"
            },
        ...
        }
    }
    
  9. Starting WebGUI

    # plackup
    
  10. Schema Manager

  11. Schema Manager Wobject

    package WebGUI::Asset::Wobject::SchemaManager;
    use Moose;
    use WebGUI::Definition::Asset;
    extends 'WebGUI::Asset::Wobject';
    define 'assetName' => 'Schema Manager';
    use My::Schema;
    has 'schema' => (
        is      => 'ro',
        lazy    => 1,
        default => sub {
            my $self = $_[0];
            My::Schema->connect(
                sub{ $self->session->db->dbh },
                { unsafe => 1 } 
            );
        },
    );
    
  12. Schema Manager Wobject (cont)

    has 'resultClass' => (
        is      => 'ro',
        default => 'My::Schema::Result::Employee',
    );
    
    has 'rs' => (
        is      => 'ro',
        lazy    => 1,
        default => sub {
            $_[0]->schema->resultset( $_[0]->resultClass )
        },
    );
    
  13. My::Schema

    package My::Schema;
    use base 'DBIx::Class::Schema';
    __PACKAGE__->load_namespaces();
    
    package My::Schema::Result::Employee;
    use base 'DBIx::Class::Core';
    __PACKAGE__->table('employee');
    __PACKAGE__->add_columns(qw/ id name salary notes /);
    __PACKAGE__->set_primary_key('id');
    
    
  14. Some Rows

    mysql> select * from employee;
    +----+--------------+-------------------+--------------------------------------+
    | id | name         | salary            | notes                                |
    +----+--------------+-------------------+--------------------------------------+
    | 1  | Doug Bell    | A pittance        | Barely worth the damage he causes    | 
    | 2  | Chris Hanson | Nothing           | Knows and uses VB.                   | 
    | 3  | Graham Knop  | Less than nothing | Knows and uses VB. Doesn't enjoy it. | 
    +----+--------------+-------------------+--------------------------------------+
    3 rows in set (0.00 sec)
    
  15. A Template

    [% FOREACH row IN rows %]
    ([% row.id %]) [% row.name %] makes [% row.salary %]<br/>
    [% row.notes %]<br/>
    [% END %]
    
  16. View the Rows

    package WebGUI::Asset::Wobject::SchemaManager;
    # ...
    sub www_view {
        my ( $self ) = @_;
        my $session = $self->session;
        my $rs = $self->rs;
        my $template
            = WebGUI::Asset->newById( $session, 'TMPL_ID' );
        $template->setParam( 
            rows => [ 
                map { { $_->get_columns } } $rs->all 
            ] );
        return $template;
    }
    
  17. The Output

    (1) Doug Bell makes A pittance
    Barely worth the damage he causes
    (2) Chris Hanson makes Nothing
    Knows and uses VB.
    (3) Graham Knop makes Less than nothing
    Knows and uses VB. Doesn't enjoy it.
  18. Templates

  19. Templates (cont)

  20. Edit Row Form

    use WebGUI::FormBuilder;
    sub getRowForm {
        my ( $self, $row ) = @_;
        my $fb = WebGUI::FormBuilder->new( $self->session );
        $fb->addField( "ReadOnly",
            name        => 'id',
            label       => 'ID',
            value       => $row ? $row->id
                        : $self->session->id->generate,
        );
        $fb->addField( "Text",
            name        => 'name',
            label       => 'Name',
            value       => $row ? $row->name : '',
        );
    
  21. Edit Row Form (cont)

        $fb->addField( "Text",
            name        => 'salary',
            label       => 'Salary',
            value       => $row ? $row->salary : '',
        );
        $fb->addField( "Textarea",
            name        => 'notes',
            label       => 'Notes',
            value       => $row ? $row->notes : '',
        );
        return $fb;
    }
    
  22. FormBuilder

  23. Edit Row Page

    sub www_editRow {
        my ( $self ) = @_;
        my $ses = $self->session;
    
        my $row;
        if ( my $id = $ses->request->param('id') ) {
            $row = $self->rs->find({ id => $id });
        }
    
        my $fb = $self->getRowForm( $row );
        $fb->addField( 'Hidden', 
            name => 'func', value => 'editRowSave',
        );
        $fb->addField( 'Submit', name => 'Save' );
        return $fb->toHtml;
    }
    
  24. Save Row Page

    sub www_editRowSave {
        my ( $self ) = @_;
        my $req = $self->session->request;
    
        my $values = $self->getRowForm->process;
        my $row = $self->rs->update_or_create( $values );
    
        $self->session->response->setRedirect( 
            $self->getUrl
        );
        return;
    }
    
  25. Results as JSON

    override dispatch => sub {
        my ( $self, $frag ) = @_;
        if ( $frag eq '.json' ) {
            return JSON->new->encode(
                [
                    map { { $_->get_columns } } 
                    $self->rs->all 
                ]
            );
        }
        super();
    };
    
  26. Dispatch

  27. Dispatch Output

    [ {
        "notes":"Knows and uses VB. Doesn't enjoy it.",
        "name":"Graham Knop",
        "id":"3",
        "salary":"Less than nothing"
    }, {
        "notes":"Knows and users VB",
        "name":"Chris Hanson",
        "id":"2",
        "salary":"Nothing"
    },{
        "notes":"Barely worth the damage he causes.",
        "name":"Doug Bell",
        "id":"1",
        "salary":"A pittance"
    } ]
    
  28. Demo!

    Broken in lots of ways!

  29. Future Development

  30. Questions

    Slides are licensed under a CC-BY-SA 3.0 license.

    Code is licensed under the Artistic License or GNU GPL v1.0 or later, at your discretion (the same terms as Perl 5 itself).