| @@ -49,6 +49,12 @@ while($request->Accept() >= 0) { | |||
| } | |||
| } | |||
| send_response($code, $type, $data); | |||
| } | |||
| sub send_response { | |||
| my ($code, $type, $data) = @_; | |||
| printf("Status: %s\r\n", $code); | |||
| printf("Content-type: %s\r\n", $type) if defined $type; | |||
| printf("Content-length: %i\r\n", length($data)) if defined $data; | |||
| @@ -56,6 +62,13 @@ while($request->Accept() >= 0) { | |||
| print $data if defined $data; | |||
| } | |||
| sub fatal_api_error { | |||
| my($code,$type,$body)=api_error(@_); | |||
| send_response($code, $type, $body); | |||
| exit; | |||
| } | |||
| sub load_conf { | |||
| my($file) = @_; | |||
| @@ -111,18 +124,61 @@ sub db_get_document_object { | |||
| } | |||
| } | |||
| my $out = { | |||
| id => $document->{id}, | |||
| pageId => [ @pageids ], | |||
| name => $document->{name}, | |||
| created => $document->{created}, | |||
| owner => $document->{owner}, | |||
| status => $document->{status}, | |||
| languages => [ keys %lang ], | |||
| }; | |||
| if ( defined $document ) { | |||
| my $out = { | |||
| id => $document->{id}, | |||
| pageId => [ @pageids ], | |||
| name => $document->{name}, | |||
| created => $document->{created}, | |||
| owner => $document->{owner}, | |||
| status => $document->{status}, | |||
| languages => [ keys %lang ], | |||
| }; | |||
| return (200, "application/json", $out); | |||
| } | |||
| return (404, "application/json", ""); | |||
| } | |||
| sub db_get_page_object { | |||
| my($id) = @_; | |||
| return (200, "application/json", $out); | |||
| my $out; | |||
| my %lang; | |||
| my @tags; | |||
| my $q = sqlquery($dbh, "SELECT * FROM pages_lang WHERE pageId = ?", $id); | |||
| while(my $hash = $q->fetchrow_hashref()) { | |||
| $lang{$hash->{language}}++; | |||
| } | |||
| $q = sqlquery($dbh, " | |||
| SELECT tags.tag AS tag | |||
| FROM pages_tags LEFT JOIN tags ON pages_tags.tagId = tags.id | |||
| WHERE pageId = ?i SORT BY tag", $id); | |||
| while(my ($tag) = $q->fetchrow_array()) { push @tags, $tag; } | |||
| $q = sqlquery($dbh, "SELECT * FROM pages WHERE documentId = ?", $id); | |||
| while(my $hash = $q->fetchrow_hashref()) { | |||
| $out = { | |||
| id => $id, | |||
| documentId => $hash->{documentId}, | |||
| name => $hash->{name}, | |||
| created => $hash->{created}, | |||
| owner => $hash->{owner}, | |||
| status => $hash->{status}, | |||
| language => [ keys %lang ], | |||
| tags => [ @tags ], | |||
| }; | |||
| } | |||
| if ( exists $out->{id} ) { | |||
| return (200, "application/json", $out); | |||
| } | |||
| return (404, "application/json", ""); | |||
| } | |||
| # create an empty document object. | |||
| @@ -177,7 +233,11 @@ sub api_v1_GET_documents_id { | |||
| return db_get_document_object($id); | |||
| } | |||
| sub api_v1_GET_pages_id { return api_error(501,"Not yet implemented"); } | |||
| # get a specific page | |||
| sub api_v1_GET_pages_id { | |||
| my($id, $qs, $post, $user) = @_; | |||
| return db_get_page_object($id); | |||
| } | |||
| # change document properties | |||
| sub api_v1_PATCH_documents_id { | |||
| @@ -221,17 +281,6 @@ sub get_array { | |||
| return \@arr; | |||
| } | |||
| sub fatal_api_error { | |||
| my($code,$type,$body)=api_error(@_); | |||
| printf("Status: %s\r\n", $code); | |||
| printf("Content-type: %s\r\n", $type); | |||
| printf("Content-length: %i\r\n", length($body)); | |||
| printf("\r\n"); | |||
| print $body; | |||
| exit; | |||
| } | |||
| sub api_error { | |||
| my($code, $text)=@_; | |||