Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
8d5b4ef
added model test file
meyerben Jul 28, 2016
bed082b
Merge branch 'Postgres_Setup' of https://github.com/KeystoneIT/qi int…
meyerben Jul 28, 2016
a73a6ab
added t/model.t
meyerben Aug 1, 2016
4c61209
added Model/Users.pm
meyerben Aug 1, 2016
e3b0b8c
added Model/Assets.pm
meyerben Aug 1, 2016
d7013ca
renamed script/Qi to script/qi
meyerben Aug 1, 2016
9526a2f
Merge branch '2.0' of https://github.com/KeystoneIT/qi into model
meyerben Aug 1, 2016
bee3ffa
create Controller/Assets.pm
meyerben Aug 1, 2016
03dd0aa
wrote several basic tests
meyerben Aug 2, 2016
10a63e7
created Qi/Model/Base.pm
meyerben Aug 8, 2016
0503b7c
setup simple api route for deleting assets
meyerben Aug 8, 2016
3d7c6af
added remove sub
meyerben Aug 8, 2016
a5935c1
fixes bug which required manually inserting into add_stamp column for…
meyerben Aug 8, 2016
8b8a0d5
simple delete capabilities
meyerben Aug 9, 2016
0d10d21
removed unused Assets controller and users model
meyerben Aug 11, 2016
1211a8e
modified startup to enable Assets Controller
meyerben Aug 11, 2016
71b050c
added basic tests for base model
meyerben Aug 11, 2016
fe497db
created Assets Controller
meyerben Aug 11, 2016
533f142
created select, insert, update, and delete subroutines
meyerben Aug 11, 2016
78a871e
renamed routes
meyerben Aug 15, 2016
a3005ff
removed strict /warnings
meyerben Aug 15, 2016
b89669f
added Model/Assets.pm
meyerben Aug 15, 2016
c2dc87e
no longer stash json response
meyerben Aug 15, 2016
b2288b3
no longer call on base model directly
meyerben Aug 16, 2016
3fe24c6
Assets Controller now uses Assets Model
meyerben Aug 16, 2016
878909d
created Assets Model to subclass base
meyerben Aug 16, 2016
d4ab896
cleaned up Base Model to be subclassed and work independently of the …
meyerben Aug 16, 2016
f0bc445
test for json response
meyerben Aug 16, 2016
2890bd6
updated gitignore
meyerben Aug 16, 2016
9c9877e
added comments
meyerben Aug 16, 2016
d762f90
tweak database tables
meyerben Aug 17, 2016
54a681c
updated test to check for primary key
meyerben Aug 17, 2016
200df5a
renamed Base to Crud
meyerben Aug 17, 2016
978fe33
Controller now recives primary key on success
meyerben Aug 17, 2016
ef90de1
changed returning to primary key
meyerben Aug 17, 2016
3fd640b
created locations model
meyerben Aug 17, 2016
b22ae81
added testing for locations
meyerben Aug 18, 2016
7ea282c
Support locations model
meyerben Aug 18, 2016
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
sql/
qi.conf
qi.mysql
modeltest
25 changes: 23 additions & 2 deletions lib/Qi.pm
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package Qi;
use warnings;
use Mojo::Base 'Mojolicious';
use Mojo::Pg;
use SQL::Abstract;

use Data::Dumper qw(Dumper);

# This method will run once at server start
sub startup {
Expand All @@ -10,7 +14,10 @@ sub startup {
my $config = $self->plugin('Config');

#Model
$self->helper(pg => sub { state $pg = Mojo::Pg->new(shift->config('pg')) });
$self->helper(pg => sub { state $pg = Mojo::Pg->new(shift->config('pg')) });
#$self->helper(sql => sub{ state $sql = SQL::Abstract->new});
$self->helper(assets => sub { state $assets = Qi::Model::Assets->new(pg => shift->pg) });
$self->helper(locations => sub { state $locations = Qi::Model::Locations->new(pg => shift->pg) });

# Documentation browser under "/perldoc"
$self->plugin('PODRenderer');
Expand All @@ -20,9 +27,23 @@ sub startup {

# Normal route to controller
$r->get('/')->to('example#welcome');

######## API #########
my $api = $r->under('/api');

#Assets
$api->get('/assets')->to('assets#get')->name('get_assets'); # get assets
$api->post('/assets')->to('assets#insert')->name('post_asset'); # insert asset
$api->put('/assets')->to('assets#update')->name('update_asset'); # update asset
$api->delete('/assets')->to('assets#remove')->name('remove_asset'); # remove asset

#Locations
$api->get('/locations')->to('locations#get')->name('get_locations'); #get locations



# Migrate to latest version if necessary
my $path = $self->home->rel_file('migrations/Qi.sql');
my $path = $self->home->rel_file('migrations/qi.sql');
$self->pg->migrations->name('qi')->from_file($path)->migrate;
}

Expand Down
48 changes: 48 additions & 0 deletions lib/Qi/Controller/Assets.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package Qi::Controller::Assets;
use Mojo::Base 'Mojolicious::Controller';
use Qi::Model::Assets;

sub get {
my $self = shift;
my $json = $self->req->json; #this is optional. Not specifying an asset returns all assets
$self->render(
json => {
table => 'assets', #repond with the table
(($json) ? 'asset' : 'assets') => $self->assets->select($json) #respond with a specified asset or all assets
}
);
}

sub insert {
my $self = shift;
$self->render(
json => {
table => 'assets',
tag => $self->assets->insert($self->req->json)->{tag} #respond with the tag that was inserted
}
);
}

sub update {
my $self = shift;
my $json = $self->req->json;
$self->render(
json => {
table => 'assets',
tag => $self->assets->update($json)->{tag} #respond with tag of updated entry (if tag is updated, the new tag will be returned)
}
);
}

sub remove {
my $self = shift;
my $json = $self->req->json;
$self->render(
json => {
table => 'assets',
tag => $self->assets->delete($json)->{tag} #respond with tag that was deleted
}
);
}

1;
12 changes: 0 additions & 12 deletions lib/Qi/Controller/Example.pm

This file was deleted.

16 changes: 16 additions & 0 deletions lib/Qi/Controller/Locations.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package Qi::Controller::Locations;
use Mojo::Base 'Mojolicious::Controller';
use Qi::Model::Locations;

sub get {
my $self = shift;
my $json = $self->req->json; #this is optional. Not specifying an asset returns all assets
$self->render(
json => {
table => 'locations', #repond with the table
(($json) ? 'location' : 'locations') => $self->locations->select($json) #respond with a specified asset or all assets
}
);
}

1;
19 changes: 19 additions & 0 deletions lib/Qi/Model/Assets.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package Qi::Model::Assets;
use Mojo::Base 'Qi::Model::Crud';

has table => 'assets';
has primary_key => 'tag';

#sub update_timestamp {
# my ($self, $tag) = @_;
# $self->sql
# my $results = eval {
# #my $sql = 'update ? set add_stamp= (now) where tag=?';
# #$self->pg->db->query($sql, $self->table, $tag)->hash;
# $self->update({data => {add_stamp => \'now()'}, where => {tag => $tag}})
#
# }
# if ($@){ warn "there's an error";}
# else {warn "Success!";}
#}
1;
50 changes: 50 additions & 0 deletions lib/Qi/Model/Crud.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package Qi::Model::Crud;
use Mojo::Base -base;
use SQL::Abstract;
use Mojo::Collection;

has sql => sub { state $sql = SQL::Abstract->new };

#### self Methods ####
#has 'sql'; # $self->sql for SQL::Abstract
has 'pg'; # $self->pg for Mojo::Pg
has 'table' => sub {die "Forgot to define table"};
has 'primary_key' => sub {die "Forgot to define primary_key"};


# **sql** is optional

sub select {
my ($self, $request) = @_;
my($stmt, @bind) = $self->sql->select($self->table , '*', $request); #select * from $self->table **where $request**
if($request) { #if there is a where clause
$self->pg->db->query($stmt, @bind)->hash; #respond with 1 row
}
else { #if no where clause
$self->pg->db->query($stmt, @bind)->hashes; #respond with all rows
}
}

sub insert {
my ($self, $request) = @_;
my($stmt, @bind) = $self->sql->insert($self->table, $request, {returning => $self->primary_key}); #insert into $self->table $request
$self->pg->db->query($stmt, @bind)->hash; #respond with primary key
}

sub update {
my ($self, $request) = @_;
my($stmt, @bind) = $self->sql->update($self->table, $request->{data}, $request->{where}); #update $self->table set $request->{data} where $request->{where}
$stmt = $stmt . ' returning ' . $self->primary_key;
$self->pg->db->query($stmt, @bind)->hash;
}

sub delete {
my ($self, $request) = @_;
my($stmt, @bind) = $self->sql->delete($self->table, $request); #delete from $self->table where $request
warn $request;
$stmt = $stmt . ' returning ' . $self->primary_key;
warn $stmt;
$self->pg->db->query($stmt, @bind)->hash;
}

1;
7 changes: 7 additions & 0 deletions lib/Qi/Model/Locations.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package Qi::Model::Locations;
use Mojo::Base 'Qi::Model::Crud';

has table => 'locations';
has primary_key => 'location_id';

1;
10 changes: 5 additions & 5 deletions migrations/qi.sql
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ CREATE TABLE if not exists assets (
revenue_percentage varchar(10) DEFAULT NULL,
comments varchar(512) DEFAULT NULL,
change_stamp timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
add_stamp timestamp NOT NULL
add_stamp timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE if not exists barcode_map (
Expand All @@ -51,7 +51,7 @@ CREATE TABLE if not exists barcode_map (
);

CREATE TABLE if not exists buyers (
buyer_id serial NOT NULL DEFAULT '0',
buyer_id serial NOT NULL,
name varchar(255) DEFAULT NULL,
PRIMARY KEY ("buyer_id")
);
Expand Down Expand Up @@ -94,7 +94,7 @@ CREATE TABLE if not exists sessions (
);

CREATE TABLE if not exists sold_via (
sold_via_id serial NOT NULL DEFAULT '0',
sold_via_id serial NOT NULL,
name varchar(255) DEFAULT NULL,
PRIMARY KEY ("sold_via_id")
);
Expand All @@ -113,7 +113,7 @@ CREATE TABLE if not exists users (
PRIMARY KEY ("user_id")
);

DROP VIEW asset_vw;
DROP VIEW if exists asset_vw;

CREATE VIEW asset_vw AS
select
Expand All @@ -135,7 +135,7 @@ CREATE VIEW asset_vw AS

-- 1 down

drop table if exists asset_types;
drop table if exists asset_types CASCADE;
drop table if exists assets;
drop table if exists barcode_map;
drop table if exists buyers;
Expand Down
File renamed without changes.
41 changes: 40 additions & 1 deletion t/basic.t
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,45 @@ use Test::More;
use Test::Mojo;

my $t = Test::Mojo->new('Qi');
$t->get_ok('/')->status_is(200)->content_like(qr/Mojolicious/i);
$t->get_ok('/')->status_is(200);

$t = $t->post_ok('/api/assets' => {Accept =>'application/json' , dataType => 'json'} => json => {tag => '000000A' , add_stamp => '2016-08-10 16:21:21'})
->status_is(200)
->json_has({table => 'assets'})
->json_has({tag => '000000A'});

$t = $t->put_ok('/api/assets' => {Accept =>'application/json' , dataType => 'json'} => json => { data => { tag => '000000B'} , where => {tag => '000000A'}})
->status_is(200)
->json_has({table => 'assets'})
->json_has({tag => '000000B'});

$t = $t->get_ok('/api/assets' => {Accept =>'application/json'})
->status_is(200)
->json_has({table => 'assets'})
->json_has({'assets'});

$t->get_ok('/api/assets' => {Accept => 'application/json' , dataType => 'json'} => json => {tag => '000000B'})
->status_is(200)
->json_has({table => 'assets'})
->json_has({'asset'});

$t->delete_ok('/api/assets' => {Accept => 'application/json' , dataType => 'json'} => json => {tag => '000000B'})
->status_is(200)
->json_has({table => 'assets'})
->json_has({tag => '000000B'});

$t = $t->get_ok('/api/locations' => {Accept =>'application/json'})
->status_is(200)
->json_has({table => 'locations'})
->json_has({'locations'});

$t = $t->get_ok('/api/locations' => {Accept =>'application/json'})
->status_is(200)
->json_has({table => 'locations'})
->json_has({'locations'});





done_testing();