Skip to content

Commit

Permalink
initial import
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon Elsbrock committed Aug 29, 2011
0 parents commit 09e80b2
Show file tree
Hide file tree
Showing 39 changed files with 1,424 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
cashpoint.db
22 changes: 22 additions & 0 deletions MANIFEST
@@ -0,0 +1,22 @@
MANIFEST
bin/app.pl
config.yml
environments/development.yml
environments/production.yml
views/index.tt
views/layouts/main.tt
MANIFEST.SKIP
lib/Cashpoint/API.pm
public/css/style.css
public/css/error.css
public/images/perldancer-bg.jpg
public/images/perldancer.jpg
public/500.html
public/404.html
public/dispatch.fcgi
public/favicon.ico
public/dispatch.cgi
public/javascripts/jquery.js
t/002_index_route.t
t/001_base.t
Makefile.PL
13 changes: 13 additions & 0 deletions MANIFEST.SKIP
@@ -0,0 +1,13 @@
^\.git\/
maint
^tags$
.last_cover_stats
Makefile$
^blib
^pm_to_blib
^.*.bak
^.*.old
^t.*sessions
^cover_db
^.*\.log
^.*\.swp$
21 changes: 21 additions & 0 deletions Makefile.PL
@@ -0,0 +1,21 @@
use strict;
use warnings;
use ExtUtils::MakeMaker;

WriteMakefile(
NAME => 'Cashpoint::API',
AUTHOR => q{YOUR NAME <youremail@example.com>},
VERSION_FROM => 'lib/Cashpoint/API.pm',
ABSTRACT => 'YOUR APPLICATION ABSTRACT',
($ExtUtils::MakeMaker::VERSION >= 6.3002
? ('LICENSE'=> 'perl')
: ()),
PL_FILES => {},
PREREQ_PM => {
'Test::More' => 0,
'YAML' => 0,
'Dancer' => 1.3072,
},
dist => { COMPRESS => 'gzip -9f', SUFFIX => 'gz', },
clean => { FILES => 'Cashpoint-API-*' },
);
4 changes: 4 additions & 0 deletions bin/app.pl
@@ -0,0 +1,4 @@
#!/usr/bin/env perl
use Dancer;
use Cashpoint::API;
dance;
10 changes: 10 additions & 0 deletions bin/createdb.pl
@@ -0,0 +1,10 @@
#!/usr/bin/perl

use strict;
use warnings;

use Dancer;
use Dancer::Plugin::DBIC;
use Cashpoint::Model;

schema->deploy({ add_drop_table => 1});
29 changes: 29 additions & 0 deletions config.yml
@@ -0,0 +1,29 @@
# This is the main configuration file of your Dancer app
# env-related settings should go to environments/$env.yml
# all the settings in this file will be loaded at Dancer's startup.

# Your application's name
appname: "Cashpoint::API"

# The default layout to use for your application (located in
# views/layouts/main.tt)
layout: "main"

# when the charset is set to UTF-8 Dancer will handle for you
# all the magic of encoding and decoding. You should not care
# about unicode within your app when this setting is set (recommended).
charset: "UTF-8"

# template engine
# simple: default and very basic template engine
# template_toolkit: TT

template: "simple"

# template: "template_toolkit"
# engines:
# template_toolkit:
# encoding: 'utf8'
# start_tag: '[%'
# end_tag: '%]'

36 changes: 36 additions & 0 deletions environments/development.yml
@@ -0,0 +1,36 @@
# configuration file for development environment

# the logger engine to use
# console: log messages to STDOUT (your console where you started the
# application server)
# file: log message to a file in log/
logger: "console"

# the log level for this environement
# core is the lowest, it shows Dancer's core log messages as well as yours
# (debug, warning and error)
log: "core"

# should Dancer consider warnings as critical errors?
warnings: 1

# should Dancer show a stacktrace when an error is caught?
show_errors: 1

# auto_reload is a development and experimental feature
# you should enable it by yourself if you want it
# Module::Refresh is needed
#
# Be aware it's unstable and may cause a memory leak.
# DO NOT EVER USE THAT FEATURE IN PRODUCTION
# OR TINY KITTENS SHALL DIE WITH LOTS OF SUFFERING
auto_reload: 0

plugins:
DBIC:
foo:
dsn: dbi:SQLite:dbname=./cashpoint.db
schema_class: Cashpoint::Model
options:
RaiseError: 1
PrintError: 1
17 changes: 17 additions & 0 deletions environments/production.yml
@@ -0,0 +1,17 @@
# configuration file for production environment

# only log warning and error messsages
log: "warning"

# log message to a file in logs/
logger: "file"

# don't consider warnings critical
warnings: 0

# hide errors
show_errors: 0

# cache route resolution for maximum performance
route_cache: 1

10 changes: 10 additions & 0 deletions lib/Cashpoint/API.pm
@@ -0,0 +1,10 @@
package Cashpoint::API;
use Dancer ':syntax';
use Cashpoint::API::Products;
use Cashpoint::API::Purchases;

our $VERSION = '0.1';

set serializer => 'JSON';

true;
Binary file added lib/Cashpoint/API/.Products.pm.swp
Binary file not shown.
Binary file added lib/Cashpoint/API/.Purchases.pm.swp
Binary file not shown.
17 changes: 17 additions & 0 deletions lib/Cashpoint/API/Cashcards.pm
@@ -0,0 +1,17 @@
package Cashpoint::API;

use strict;
use warnings;

use Data::Dumper;
use Dancer ':syntax';
use Dancer::Plugin::REST;
use Dancer::Plugin::DBIC;

our $VERSION = '0.1';

set serializer => 'JSON';

#post '/cashcard'

42;
63 changes: 63 additions & 0 deletions lib/Cashpoint/API/Products.pm
@@ -0,0 +1,63 @@
package Cashpoint::API;

use strict;
use warnings;

use Data::Dumper;
use Dancer ':syntax';
use Dancer::Plugin::REST;
use Dancer::Plugin::DBIC;

our $VERSION = '0.1';

set serializer => 'JSON';

get '/products/:ean' => sub {
my $product = schema()->resultset('Product')->search({ean => params->{ean}})
->single;
return status_not_found('product not found') unless $product;
return status_ok({
name => $product->name,
ean => $product->ean,
#threshold => $product->threshold,
});
};

post '/products' => sub {
my @errors = ();

if (0) {
} if (!params->{name} || params->{name} !~ /^.{5,30}$/) {
push @errors, 'name must be at least 5 and up to 30 chars long';
} if (!params->{ean} || params->{ean} !~ /^.{5,30}$/) {
push @errors, 'ean must be at least 5 and up to 20 chars long';
} if (params->{threshold} && params->{threshold} !~ /^\d+$/) {
push @errors, 'threshold must be greater than 0';
} if (schema->resultset('Product')->search({ ean => params->{ean}})->count) {
@errors = ('product already exists');
}

return status_bad_request(\@errors) if @errors;

schema->resultset('Product')->create({
ean => params->{ean},
name => params->{name},
threshold => params->{threshold} || 0,
added_on => time,
});

# xxx: check if inserted
return status_created();
};

del '/products/:ean' => sub {
my $product = schema()->resultset('Product')->search({ean => params->{ean}})
->single;
return status_not_found('product not found') unless $product;
$product->delete;

# xxx: check if deleted
return status_ok();
};

42;
93 changes: 93 additions & 0 deletions lib/Cashpoint/API/Purchases.pm
@@ -0,0 +1,93 @@
package Cashpoint::API;

use strict;
use warnings;

use Data::Dumper;
use Dancer ':syntax';
use Dancer::Plugin::REST;
use Dancer::Plugin::DBIC;

use Time::Piece;
use Scalar::Util::Numeric qw/isfloat/;

our $VERSION = '0.1';

set serializer => 'JSON';

get '/products/:ean/purchases' => sub {
my $product = schema()->resultset('Product')->search({ean => params->{ean}})
->single;
return status_not_found('product not found') unless $product;

my $purchases = $product->search_related('Purchases', {}, {
order_by => { -desc => 'purchaseid' }
});

my @data = ();
while (my $p = $purchases->next) {
push @data, {
supplier => $p->supplier,
purchasedate => $p->purchasedate->dmy('/'),
expirydate => $p->expirydate ? $p->expirydate->dmy('/') : undef,
amount => $p->amount,
price => $p->price,
};
}

return status_ok(\@data);
};

post '/products/:ean/purchases' => sub {
my $product = schema()->resultset('Product')->search({ean => params->{ean}})
->single;
return status_not_found('product not found') unless $product;


#supplier, purchasedate, expirydate, amount, price
my @errors = ();
my $pdate = Time::Piece->strptime(params->{purchasedate} || 0, "%d/%m/%Y");
my $edate = Time::Piece->strptime(params->{expirydate} || 0, "%d/%m/%Y");

if (0) {
} if (!params->{supplier} || params->{supplier} !~ /^.{5,30}$/) {
push @errors, 'supplier must be at least 5 and up to 30 chars long';
} if (!params->{purchasedate} || !$pdate) {
push @errors, 'purchase date must follow dd/mm/yyyy formatting';
} if (params->{expirydate} && !$edate) {
push @errors, 'expiry date must follow dd/mm/yyyy formatting';
} if (!params->{amount} || params->{amount} !~ /^\d+$/) {
push @errors, 'amount must be greater zero';
} if (!params->{price} || !isfloat(params->{price})) {
push @errors, 'price must be a decimal';
}

return status_bad_request(\@errors) if @errors;

my $insert = $product->create_related('Purchases', {
userid => 0, # FIXME
supplier => params->{supplier},
purchasedate => $pdate->datetime,
expirydate => params->{expirydate} ? $edate->datetime : undef,
amount => params->{amount},
price => params->{price},
});

# xxx: check if inserted
return status_created({id => $insert->id});
};

del '/products/:ean/purchases/:id' => sub {
my $product = schema()->resultset('Product')->search({ean => params->{ean}})
->single;
return status_not_found('product not found') unless $product;

my $purchase = schema()->resultset('Purchase')->search({purchaseid => params->{id}})
->single;
return status_not_found('purchase not found') unless $purchase;

$purchase->delete;
return status_ok();
};

42;
11 changes: 11 additions & 0 deletions lib/Cashpoint/Model.pm
@@ -0,0 +1,11 @@
#!/usr/bin/perl
package Cashpoint::Model;

use strict;
use warnings;

use base qw/DBIx::Class::Schema/;

__PACKAGE__->load_namespaces();

1;
Binary file added lib/Cashpoint/Model/Result/.Credit.pm.swp
Binary file not shown.
Binary file added lib/Cashpoint/Model/Result/.Purchase.pm.swp
Binary file not shown.

0 comments on commit 09e80b2

Please sign in to comment.