Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
gilmagno committed Jul 19, 2012
0 parents commit 2fa0d04
Show file tree
Hide file tree
Showing 78 changed files with 2,761 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Changes
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
This file documents the revision history for Perl extension DealsManager.

0.01 2012-07-03 17:53:25
- initial revision, generated by Catalyst
31 changes: 31 additions & 0 deletions Makefile.PL
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/usr/bin/env perl
# IMPORTANT: if you delete this file your app will not work as
# expected. You have been warned.
use inc::Module::Install 1.02;
use Module::Install::Catalyst; # Complain loudly if you don't have
# Catalyst::Devel installed or haven't said
# 'make dist' to create a standalone tarball.

name 'DealsManager';
all_from 'lib/DealsManager.pm';

requires 'Catalyst::Runtime' => '5.90012';
requires 'Catalyst::Plugin::ConfigLoader';
requires 'Catalyst::Plugin::Static::Simple';
requires 'Catalyst::Action::RenderView';
requires 'Moose';
requires 'namespace::autoclean';
requires 'Config::General'; # This should reflect the config file format you've chosen
# See Catalyst::Plugin::ConfigLoader for supported formats

requires 'Catalyst::Plugin::Authentication';
requires 'Catalyst::Plugin::Session';
requires 'Catalyst::Plugin::Session::Store::File';
requires 'Catalyst::Plugin::Session::State::Cookie';

test_requires 'Test::More' => '0.88';
catalyst;

install_script glob('script/*.pl');
auto_install;
WriteAll;
1 change: 1 addition & 0 deletions README
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Run script/dealsmanager_server.pl to test the application.
73 changes: 73 additions & 0 deletions database_schema_and_initial_data.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
-- create a database, connect to it and then run the following code
create table users (
id serial primary key,
username varchar,
password varchar);

CREATE TABLE roles (
id serial primary key,
role varchar
);

CREATE TABLE users_roles (
user_id INTEGER REFERENCES users,
role_id INTEGER REFERENCES roles,
PRIMARY KEY (user_id, role_id)
);

create table contacts (
id serial primary key,
name varchar,
phone varchar,
address varchar,
email varchar,
notes text,
created timestamp with time zone);

create table deals (
id serial primary key,
name varchar,
responsible_id int references users,
contact_id int references contacts,
status varchar,
price int,
probability int,
created timestamp with time zone,
updated timestamp with time zone);

create table notes (
id serial primary key,
note text,
deal_id int references deals on update cascade on delete cascade,
user_id int references users on update cascade on delete restrict,
created timestamp with time zone);

create table dashboard (
id serial primary key,
content text,
created timestamp with time zone,
"type" varchar, -- deal created, deal updated, deal deleted, note created, note deleted,
user_id int references users,
deal_id int references deals);


-- data

insert into users (username, password) values
('admin', 'admin');

insert into roles (role) values
('admin');

insert into users_roles (user_id, role_id) values
(1, 1);

insert into contacts (name) values
('Sérgio Ruoso');

insert into deals (name, responsible_id, contact_id, price) values
('Deal 1', 1, 1, '700');

insert into notes (note, deal_id, user_id) values
('Body of note', 1, 1),
('Note note', 1, 1);
3 changes: 3 additions & 0 deletions dealsmanager.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# rename this file to dealsmanager.yml and put a ':' after 'name' if
# you want to use YAML like in old versions of Catalyst
name DealsManager
8 changes: 8 additions & 0 deletions dealsmanager.psgi
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use strict;
use warnings;

use DealsManager;

my $app = DealsManager->apply_default_middlewares(DealsManager->psgi_app);
$app;

78 changes: 78 additions & 0 deletions lib/DealsManager.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package DealsManager;
use Moose;
use namespace::autoclean;

use Catalyst::Runtime 5.80;

# Set flags and add plugins for the application.
#
# Note that ORDERING IS IMPORTANT here as plugins are initialized in order,
# therefore you almost certainly want to keep ConfigLoader at the head of the
# list if you're using it.
#
# -Debug: activates the debug mode for very useful log messages
# ConfigLoader: will load the configuration from a Config::General file in the
# application's home directory
# Static::Simple: will serve static files from the application's root
# directory

use Catalyst qw/
-Debug
ConfigLoader
Static::Simple
Authentication
Authorization::Roles
Session
Session::Store::File
Session::State::Cookie
/;

extends 'Catalyst';

our $VERSION = '0.01';

# Configure the application.
#
# Note that settings in dealsmanager.conf (or other external
# configuration file that you set up manually) take precedence
# over this when using ConfigLoader. Thus configuration
# details given here can function as a default configuration,
# with an external configuration file acting as an override for
# local deployment.

__PACKAGE__->config(
name => 'DealsManager',

# Disable deprecated behavior needed by old applications
disable_component_resolution_regex_fallback => 1,

enable_catalyst_header => 1, # Send X-Catalyst header

'Plugin::Authentication' => {
default => {
class => 'SimpleDB',
user_model => 'DB::User'
}
},

'Controller::HTML::FormFu' => {
model_stash => {
schema => 'DB'
}
},

);

# Start the application
__PACKAGE__->setup();

=head1 LICENSE
This library is free software. You can redistribute it and/or modify
it under the same terms as Perl itself.
=cut

1;
35 changes: 35 additions & 0 deletions lib/DealsManager/Controller/Auth.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package DealsManager::Controller::Auth;
use Moose;
use namespace::autoclean;

BEGIN { extends 'Catalyst::Controller'; }

sub login :Local {
my ($self, $c) = @_;
}

sub login_do :Local {
my ($self, $c) = @_;

my $authentication_response =
$c->authenticate({ username => $c->req->params->{username},
password => $c->req->params->{password} });

if ( $authentication_response ) {
$c->res->redirect('/deals');
}
else {
$c->res->redirect('/auth/login');
}
}

sub logout :Local {
my ($self, $c) = @_;

$c->logout;
$c->res->redirect('/deals');
}

__PACKAGE__->meta->make_immutable;

1;
85 changes: 85 additions & 0 deletions lib/DealsManager/Controller/Contacts.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package DealsManager::Controller::Contacts;
use Moose;
use namespace::autoclean;

BEGIN { extends 'Catalyst::Controller::HTML::FormFu'; }

sub base :Chained('/') PathPart('contacts') CaptureArgs(0) {
my ( $self, $c ) = @_;

$c->stash->{resulset} = $c->model('DB::Contact');
}

sub object :Chained('base') PathPart('') CaptureArgs(1) {
my ( $self, $c, $id ) = @_;

$c->stash->{object} =
$c->stash->{resulset}->search_rs({ 'me.id' => $id });
}

sub index :Chained('base') PathPart('') Args(0) {
my ( $self, $c ) = @_;

my $rs = $c->stash->{resulset};

$c->stash->{contacts} = [ $rs->all ];
}

sub show :Chained('object') PathPart('') Args(0) {
my ( $self, $c ) = @_;

my $contact = $c->stash->{object}->first;

$c->stash->{object} = $c->stash->{object}
->search_rs(undef,
{ prefetch => [ 'deals' ] } )
->first;
}

sub create :Chained('base') PathPart('create') Args(0) FormConfig('contacts/form.yml') {
my ( $self, $c ) = @_;

my $form = $c->stash->{form};

if ( $form->submitted_and_valid ) {
my $new_contact = $c->stash->{resulset}->new_result({});
$form->model->update( $new_contact );
$c->res->redirect('/contacts');
}
}

sub edit :Chained('object') PathPart('edit') Args(0) FormConfig('contacts/form.yml') {
my ( $self, $c ) = @_;

my $form = $c->stash->{form};

if ( $form->submitted_and_valid ) {
$form->model->update( $c->stash->{object}->first );
$c->res->redirect('/contacts');
}
else {
$form->model->default_values( $c->stash->{object}->first );
}
}

sub delete :Chained('object') PathPart('delete') Args(0) {
my ( $self, $c ) = @_;

$c->stash->{object}->first->delete;
$c->res->redirect('/contacts');
}

=head1 AUTHOR
sgrs,,,
=head1 LICENSE
This library is free software. You can redistribute it and/or modify
it under the same terms as Perl itself.
=cut

__PACKAGE__->meta->make_immutable;

1;
Loading

0 comments on commit 2fa0d04

Please sign in to comment.