Permalink
Browse files

Tons of stuff

  • Loading branch information...
1 parent 9f9e7ef commit 4867321298044a1a9d7037bee64915a0cffdc092 @ironcamel committed Nov 17, 2011
View
@@ -3,8 +3,9 @@ WriteMakefile(
NAME => 'GraphSpace',
VERSION_FROM => 'lib/GraphSpace.pm',
PREREQ_PM => {
- 'Dancer' => '0',
+ 'Dancer' => '1.3080',
'Dancer::Plugin::DBIC' => '0',
+ 'DateTime' => '0',
'File::Slurp' => '0',
'JSON' => '0',
'Plack' => '0',
View
@@ -1,6 +1,6 @@
appname: GraphSpace
layout: bootstrap
-logger: file
+#logger: file
#logger: console
session: YAML
serializer: JSON
@@ -16,7 +16,7 @@ plugins:
DBIC:
graphdb:
schema_class: 'GraphSpace::Schema'
- dsn: dbi:SQLite:dbname=/home/naveed/projects/GraphSpace/data/graph-space.db
+ dsn: dbi:SQLite:dbname=/home/naveed/projects/GraphSpace2/data/graph-space.db
user: graphspace_db_user
pass: graphspace_db_password
#NYTProf:
View
@@ -7,13 +7,4 @@
my $schema = GraphSpace::Schema->connect($dsn);
$schema->deploy;
-my @users = (
- { id => 'chrisp', password => 'chrisp', name => 'Chris Poirel' },
- { id => 'test', password => 'test', name => 'Test User' },
-);
-
-for my $user (@users) {
- $schema->resultset('User')->create($user);
-}
-
print "done\n";
View
@@ -1,51 +1,28 @@
CREATE TABLE user (
- id TEXT primary key,
+ id VARCHAR(100) primary key,
password TEXT,
name TEXT,
email TEXT
);
CREATE TABLE graph (
- id INTEGER PRIMARY KEY AUTOINCREMENT,
- name TEXT,
+ id VARCHAR(500),
+ user_id VARCHAR(100),
json TEXT,
- user_id TEXT,
+ created TIMESTAMP,
+ modified TIMESTAMP,
FOREIGN KEY(user_id) REFERENCES user(id)
);
-create index graph_user_id_idx on graph(user_id);
CREATE TABLE graph_tag (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT UNIQUE
);
CREATE TABLE graph_to_tag (
- graph_id INTEGER,
+ graph_id VARCHAR(500),
tag_id INTEGER,
PRIMARY KEY (graph_id, tag_id),
FOREIGN KEY(graph_id) REFERENCES graph(id),
FOREIGN KEY(tag_id) REFERENCES graph(id)
);
-
-insert into user (id, password, name) values ('test', 'test', 'Test Account');
-/*
-insert into user (id, password, name) values ('chrisp', 'chrisp', 'Chris Poirel');
-insert into user (id, password, name) values ('chrisl', 'chrisl', 'Chris Lasher');
-insert into user (id, password, name) values ('arjun', 'arjun', 'Arjun');
-insert into user (id, password, name) values ('naveed', 'naveed', 'Naveed');
-
-INSERT INTO graph(id, json) VALUES('5', '{
- "graph": {
- "data": {
- "nodes": [
- { "id": "1" },
- { "id": "2" }
- ],
- "edges" : [
- { "id": "2to1", "source": "2", "target": "1" }
- ]
- }
- "metadata": { "name": "sample 1" }
-}');
-
-*/
@@ -1,3 +1,4 @@
log: "debug"
+logger: file
warnings: 1
show_errors: 1
View
@@ -3,6 +3,7 @@ use Dancer ':syntax';
use v5.10;
use Dancer::Plugin::DBIC;
+use DateTime;
use File::Slurp qw(read_file);
our $VERSION = '0.0001';
@@ -23,7 +24,7 @@ hook before => sub {
hook before_template_render => sub {
my $tokens = shift;
- $tokens->{user_id} = session 'user_id';
+ $tokens->{user_id} = param('user_id') // session('user_id');
};
get '/' => sub { redirect uri_for '/graphs' };
@@ -117,21 +118,20 @@ get '/graphs' => sub {
};
};
-get '/ajax/graphs/:graph_id' => sub {
- my $graph = get_graph(params->{graph_id});
+get '/ajax/users/:user_id/graphs/:graph_id' => sub {
+ my $graph = get_graph();
return $graph ? $graph->json : { error => 'graph not found' };
};
-get '/graphs/:graph_id' => sub {
- my $graph_id = param 'graph_id';
+get '/users/:user_id/graphs/:graph_id' => sub {
my $size = param('size') || '';
my $template = $size eq 'large' ? 'large' : 'graph';
- my $graph = get_graph($graph_id);
- return send_error "The graph [$graph_id] does not exist", 404
+ my $graph = get_graph();
+ return send_error "The graph does not exist", 404
unless $graph;
template $template => {
- graph => $graph,
- graph_tags => [ $graph->tags ],
+ graph => $graph,
+ graph_tags => [ $graph->tags ],
};
};
@@ -143,113 +143,117 @@ get '/tags' => sub {
post '/graphs/:graph_id/tags' => sub {
my $tag_name = request->body;
my $graph_id = params->{graph_id};
- add_tags($graph_id, [ $tag_name ]);
+ add_tags([ $tag_name ]);
return { name => $tag_name };
};
del '/graphs/:graph_id/tags/:tag' => sub {
my $tag_name = params->{tag};
my $graph_id = params->{graph_id};
- delete_tags($graph_id, [$tag_name]);
+ delete_tags([$tag_name]);
return 1;
};
get '/foo' => sub { template 'foo' => {}, {layout => 0}};
-get '/api/graphs/:graph_id' => sub {
- my $graph_id = params->{graph_id};
- my $graph = get_graph($graph_id);
+get '/api/users/:user_id/graphs/:graph_id' => sub {
+ my $graph = get_graph();
if (not $graph) {
status 404;
- return "No such graph exists with id $graph_id\n";
+ return { error => "No such graph exists" };
}
content_type 'application/json';
return $graph->json;
};
-post '/api/graphs' => sub {
+any [qw(post put)] => '/api/users/*/**' => sub {
+ my ($user_id) = splat;
+ if ($user_id ne var 'api_user') {
+ status 403;
+ return { error => "You can't mess with others graphs." };
+ }
+ my $json = request->body;
+ my $data = from_json $json, { utf8 => 1 };
+ var data => $data;
+ pass;
+};
+
+post '/api/users/:user_id/graphs' => sub {
+ my $user_id = param 'user_id';
+ my $graph_id = int rand() * 1_000_000_000;
my $json = request->body;
- my $data = from_json $json;
- my $name = $data->{metadata}{name}
- or return send_error("The graph metadata must contain a name\n", 400);
+ my $data = var 'data';
+ my $now = DateTime->now();
my $graph = schema->resultset('Graph')->create({
- name => $name,
- json => $json,
- user_id => var('api_user'),
+ id => $graph_id,
+ user_id => $user_id,
+ json => $json,
+ created => $now,
+ modified => $now,
});
- my $graph_id = $graph->id;
my $tags = $data->{metadata}{tags};
- add_tags($graph_id, $tags) if $tags;
+ add_tags($tags) if $tags;
status 201;
header location => uri_for("/api/graphs/$graph_id");
- return {
- id => $graph_id,
- url => uri_for("/graphs/$graph_id")->as_string,
- };
+ return graph_response();
};
-put '/api/graphs/:graph_id' => sub {
- my $graph_id = params->{graph_id};
- my $graph = get_graph($graph_id);
- if (not $graph) {
- status 404;
- return { error => "No graph exists with an id of $graph_id" };
- }
+put '/api/users/:user_id/graphs/:graph_id' => sub {
+ my $user_id = param 'user_id';
+ my $graph_id = param 'graph_id';
my $json = request->body;
- my $data = from_json $json;
- my $name = $data->{metadata}{name};
- if (not defined $name) {
- status 400;
- return { error => "The graph metadata must contain a name" };
- }
- $graph->update({
- name => $name,
- json => $json,
- user_id => var('api_user'),
+ my $data = var 'data';
+ #debug $data;
+ my $now = DateTime->now();
+ schema->resultset('Graph')->update_or_create({
+ id => $graph_id,
+ user_id => $user_id,
+ json => $json,
+ created => $now, # TODO: fix this
+ modified => $now,
});
- delete_all_tags($graph_id);
+ delete_all_tags();
my $tags = $data->{metadata}{tags};
- add_tags($graph_id, $tags) if $tags;
- return {
- id => $graph_id,
- url => uri_for("/graphs/$graph_id")->as_string,
- };
+ #debug $json;
+ #debug $data;
+ add_tags($tags) if $tags;
+ return graph_response();
};
-del '/graphs/:graph_id' => \&delete_graph;
+del '/users/:user_id/graphs/:graph_id' => \&delete_graph;
-del '/api/graphs/:graph_id' => \&delete_graph;
+del '/api/users/:user_id/graphs/:graph_id' => \&delete_graph;
-sub get_graph { schema->resultset('Graph')->find($_[0]) }
+sub get_graph {
+ my $graph_id = param 'graph_id';
+ my $user_id = param 'user_id';
+ return schema->resultset('Graph')->find($graph_id, $user_id);
+}
sub delete_graph {
- my $graph_id = params->{graph_id};
- my $graph = get_graph($graph_id);
+ my $graph = get_graph();
if (not $graph) {
status 404;
- return { error => "No graph exists with an id of $graph_id" };
+ return { error => "No such graph exists" };
}
my $user_id = session('user_id') // var('api_user') // '';
if ($graph->user_id ne $user_id) {
status 403;
return { error => "You can only delete your own graphs" };
}
-
+ delete_all_tags();
$graph->delete;
- # TODO: Should we delete orphaned tags? DBIC takes care of deleting rows
- # from relationship table.
- return { id => $graph_id };
+ return '';
};
sub validate_tags {
my ($tags) = @_;
}
sub add_tags {
- my ($graph_id, $tags) = @_;
- my $graph = get_graph($graph_id);
+ my ($tags) = @_;
+ my $graph = get_graph();
for my $tag_name (@$tags) {
- #debug "adding tag $tag_name to graph_id $graph_id";
$tag_name =~ s/\s/-/g; # We are not allowing whitespace in tags.
$tag_name = lc $tag_name;
my $tag = schema->resultset('GraphTag')
@@ -259,10 +263,9 @@ sub add_tags {
}
sub delete_tags {
- my ($graph_id, $tags) = @_;
- my $graph = get_graph($graph_id);
+ my ($tags) = @_;
+ my $graph = get_graph();
for my $tag_name (@$tags) {
- #debug "deleting tag $tag_name from graph_id $graph_id";
my $tag = schema->resultset('GraphTag')->find({ name => $tag_name });
if ($tag) {
$graph->remove_from_tags($tag);
@@ -272,14 +275,21 @@ sub delete_tags {
}
sub delete_all_tags {
- my ($graph_id) = @_;
- debug "deleting all tags for graph_id $graph_id";
- my $graph = get_graph($graph_id);
+ my $graph = get_graph();
+ return unless $graph;
for my $tag ($graph->tags) {
- debug "deleting tag " . $tag->name . " from graph_id $graph_id";
$graph->remove_from_tags($tag);
$tag->delete if $tag->graphs->count == 0;
}
}
+sub graph_response {
+ my $graph_id = param 'graph_id';
+ my $user_id = param 'user_id';
+ return {
+ id => $graph_id,
+ url => uri_for("/users/$user_id/graphs/$graph_id")->as_string,
+ };
+}
+
true;
@@ -6,9 +6,11 @@ use warnings;
__PACKAGE__->table('graph');
__PACKAGE__->add_columns(
- id => { data_type => 'varchar(500)', is_nullable => 0 },
- user_id => { data_type => 'varchar(100)', is_nullable => 0 },
- json => { data_type => 'text', is_nullable => 0 },
+ id => { data_type => 'varchar(500)', is_nullable => 0 },
+ user_id => { data_type => 'varchar(100)', is_nullable => 0 },
+ json => { data_type => 'text', is_nullable => 0 },
+ created => { data_type => 'timestamp', is_nullable => 0 },
+ modified => { data_type => 'timestamp', is_nullable => 0 },
);
__PACKAGE__->set_primary_key('id', 'user_id');
@@ -28,13 +28,13 @@ __PACKAGE__->set_primary_key("graph_id", "tag_id");
__PACKAGE__->belongs_to(
graph => "GraphSpace::Schema::Result::Graph",
{
- 'foreign.id' => 'user.graph_id',
- 'foreign.user_id' => 'user.user_id',
+ 'foreign.id' => 'self.graph_id',
+ 'foreign.user_id' => 'self.user_id',
},
{
is_deferrable => 1,
- on_delete => "CASCADE",
- on_update => "CASCADE",
+ on_delete => "CASCADE",
+ on_update => "CASCADE",
},
);
Oops, something went wrong.

0 comments on commit 4867321

Please sign in to comment.