package MediaWords::Model::DBIS;

# custom DBIx::Simple::MediaWords based model for mediawords

use strict;
use warnings;

use Modern::Perl "2015";
use MediaWords::CommonLibs;

use base qw(Catalyst::Model);

use DBIx::Simple::MediaWords;
use MediaWords::DB;

sub new
{
    my $self = shift->SUPER::new( @_ );

    my @info = @{ $self->{ connect_info } || [] };

    return $self;
}

# hand out a database connection.  reuse the last connection unless the request has changed
# since the last call.
sub dbis
{
    my ( $self, $request ) = @_;

    my $db = $self->{ dbis };

    my $prev_req_id = $self->{ prev_req_id } || '';
    my $req_id = scalar( $request );

    return $db if ( $db && ( $req_id eq $prev_req_id ) );

    $self->{ prev_req_id } = $req_id;

    # we put an eval and print the error here b/c the web auth dies silently on a database error
    eval {
        $db = DBIx::Simple::MediaWords->connect( MediaWords::DB::connect_info )
          || die DBIx::Simple::MediaWords->error;

        $db->dbh->{ RaiseError } = 1;
        $self->{ dbis } = $db;
    };
    if ( $@ )
    {
        LOGDIE "Database error: $@";
    }

    return $db;
}

1;

package MediaWords::Model::DBIS;

# custom DBIx::Simple::MediaWords based model for mediawords

use strict;
use warnings;

use Modern::Perl "2015";
use MediaWords::CommonLibs;

use base qw(Catalyst::Model);

use MediaWords::DB;

sub new
{
    my $self = shift->SUPER::new( @_ );

    my @info = @{ $self->{ connect_info } || [] };

    return $self;
}

# hand out a database connection.  reuse the last connection unless the request has changed
# since the last call.
sub dbis
{
    my ( $self, $request ) = @_;

    my $db = $self->{ dbis };

    my $prev_req_id = $self->{ prev_req_id } || '';
    my $req_id = scalar( $request );

    return $db if ( $db && ( $req_id eq $prev_req_id ) );

    $self->{ prev_req_id } = $req_id;

    # we put an eval and print the error here b/c the web auth dies silently on a database error
    eval {        
        $db = MediaWords::DB::connect_to_db
        $self->{ dbis } = $db;
    };
    if ( $@ )
    {
        LOGDIE "Database error: $@";
    }

    return $db;
}

1;

Revert "Revert "Simplify connecting to the database in Catalyst""
Revert "Revert "Simplify connecting to the database in Catalyst""

This reverts commit 8a525f37d8a01ec172ab4d465b04896d238799e5.
