Skip to content

Commit

Permalink
More work on migration of annotations to the DB instead of ES.
Browse files Browse the repository at this point in the history
Metas (likes, etc.), comments, editing of annotation, etc. mostly
working at this point. No ES yet though as I want to work out the bugs
in the DB side first.
  • Loading branch information
coogle committed Mar 18, 2014
1 parent dcfa329 commit 9be80a4
Show file tree
Hide file tree
Showing 16 changed files with 1,115 additions and 756 deletions.
77 changes: 47 additions & 30 deletions app/controllers/AnnotationApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,21 @@ public function __construct(){
// Returns json annotation if id found,
// 404 with error message if id not found,
// 404 if no id passed

/**
* Get annotations by document ID and annotation ID
* @param interger $docId
* @param string $annotationId optional, if not provided get all
* @throws Exception
*/
public function getIndex($docId, $annotationId = null){
try{
$userId = null;
if(Auth::check()){
$userId = Auth::user()->id;
}

$results = DBAnnotation::loadAnnotationsForAnnotator($docId, $annotationId, $userId);
$results = Annotation::loadAnnotationsForAnnotator($docId, $annotationId, $userId);
}catch(Exception $e){
throw $e;
App::abort(500, $e->getMessage());
Expand All @@ -30,18 +37,23 @@ public function getIndex($docId, $annotationId = null){
return Response::json($results);
}

/**
* Create a new annotation
* @param document ID $doc
*/
public function postIndex($doc){
$body = Input::all();
$body['doc'] = $doc;

$annotation = new Annotation();
$annotation->body($body);

$id = $annotation->save();
$annotation = Annotation::createFromAnnotatorArray($body);

return Redirect::to('/api/docs/' . $doc . '/annotations/' . $id, 303);
return Redirect::to('/api/docs/' . $doc . '/annotations/' . $annotation->id, 303);
}

/**
* Update an existing annotation
* @param string $id
*/
public function putIndex($id = null){

//If no id requested, return 404
Expand All @@ -50,18 +62,18 @@ public function putIndex($id = null){
}

$body = Input::all();

$id = Input::get('id');

$annotation = Annotation::find($id);

$annotation->body($body);

$results = $annotation->update();

return Response::json($results);

$annotation = Annotation::createFromAnnotatorArray($body);

return Response::json($annotation);
}

/**
* Delete an annotation by doc ID and annotation ID
*
* @param int $doc
* @param int $annotation
*/
public function deleteIndex($doc, $annotation){
//If no id requested, return 404
if($annotation === null){
Expand All @@ -75,6 +87,9 @@ public function deleteIndex($doc, $annotation){
return Response::make(null, 204);
}

/**
* Return search results for annotations
*/
public function getSearch(){
return false;
}
Expand Down Expand Up @@ -114,43 +129,45 @@ public function postLikes($doc, $annotation = null){
App::abort(404, 'No note id passed');
}

$postAction = Annotation::addUserAction($annotation, Auth::user()->id, 'like');
$annotation = Annotation::find($annotation);
$annotation->saveUserAction(Auth::user()->id, Annotation::ACTION_LIKE);

return Response::json($postAction);
return Response::json($annotation->toAnnotatorArray());
}

public function postDislikes($doc, $annotation = null){
if($annotation === null){
App::abort(404, 'No note id passed');
}

$postAction = Annotation::addUserAction($annotation, Auth::user()->id, 'dislike');
$annotation = Annotation::find($annotation);
$annotation->saveUserAction(Auth::user()->id, Annotation::ACTION_DISLIKE);

return Response::json($postAction);
return Response::json($annotation->toAnnotatorArray());
}

public function postFlags($doc, $annotation = null){
if($annotation === null){
App::abort(404, 'No note id passed');
}

$postAction = Annotation::addUserAction($annotation, Auth::user()->id, 'flag');
$annotation = Annotation::find($annotation);
$annotation->saveUserAction(Auth::user()->id, Annotation::ACTION_FLAG);

return Response::json($postAction);
return Response::json($annotation->toAnnotatorArray());
}

public function postComments($doc, $annotation = null){
if($annotation === null){
throw new Exception("Unable to post comment without annotation id.");
}
public function postComments($docId, $annotationId){

$comment = Input::get('comment');

$annotation = Annotation::find($annotation);

$results = $annotation->addComment($comment);
$annotation = Annotation::where('doc', '=', $docId)
->where('id', '=', $annotationId)
->first();

return Response::json($results);
$results = $annotation->addOrUpdateComment($comment);

return Response::json($annotation->toAnnotatorArray());
}
}

61 changes: 61 additions & 0 deletions app/controllers/DevController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

class DevController extends BaseController
{
public function getTest()
{
$input = array(
'permissions' => array(
'read' => array(),
'update' => array(1),
'delete' => array(1),
'admin' => array(1)
),
'user' => array(
'id' => 1,
'email' => '[email protected]',
'user_level' => 1,
'name' => "John C"
),
'ranges' => array(
array(
'start' => '/div[2]/div[1]/p[2]',
'startOffset' => 0,
'end' => '/div[2]/div[1]/p[2]',
'endOffset' => 127
)
),
'quote' => "This is the quote",
'text' => "This is the comment text",
'tags' => array('tacobell'),
'uri' => '/docs/testing',
'comments' => array(
array(
'id' => 1,
'text' => 'This is the comment on the comment',
'created' => "Wed, 05 Mar 2014 12:40:47 -0500",
'updated' => "Wed, 05 Mar 2014 12:40:47 -0500",
'user' => array(
'id' => 2,
'user_level' => 3,
'email' => '[email protected]',
'name' => "Joe B"
)
)
),
'doc' => 1,
'created' => "Wed, 05 Mar 2014 12:40:47 -0500",
'updated' => "Wed, 05 Mar 2014 12:40:47 -0500",
'user_action' => null,
'flags' => 1,
'likes' => 0,
'dislikes' => 0
);

$annotation = Annotation::createFromAnnotatorArray($input);

var_dump($annotation);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ public function up()
{
Schema::create('annotations', function($table) {
$table->engine = "InnoDB";
$table->string('id');

$table->increments('id');
$table->string('search_id');
$table->integer('user_id')->unsigned();
$table->integer('doc')->unsigned();
$table->string('quote');
Expand All @@ -25,9 +27,10 @@ public function up()
$table->integer('flags');
$table->timestamps();

$table->primary('id');
$table->foreign('user_id')->references('id')->on('users');
$table->foreign('doc')->references('id')->on('docs');
$table->unique('search_id');

});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ public function up()
$table->engine = "InnoDB";

$table->integer('id')->unsigned();
$table->string('annotation_id');
$table->integer('annotation_id')->unsigned();
$table->integer('user_id')->unsigned();
$table->string('text');
$table->timestamps();

$table->foreign('annotation_id')->references('id')->on('annotations');
$table->foreign('annotation_id')->references('id')->on('annotations')->on_delete('cascade');
$table->foreign('user_id')->references('id')->on('users');
$table->unique(array('annotation_id', 'id'));
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ public function up()
$table->engine = "InnoDB";

$table->increments('id');
$table->string('annotation_id');
$table->integer('annotation_id')->unsigned();
$table->string('tag');
$table->timestamps();

$table->foreign('annotation_id')->references('id')->on('annotations');
$table->foreign('annotation_id')->references('id')->on('annotations')->on_delete('cascade');
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function up()
$table->engine = "InnoDB";

$table->increments('id');
$table->string('annotation_id');
$table->integer('annotation_id')->unsigned();
$table->integer('user_id')->unsigned();
$table->integer('read');
$table->integer('update');
Expand All @@ -26,7 +26,7 @@ public function up()
$table->timestamps();

$table->foreign('user_id')->references('id')->on('users');
$table->foreign('annotation_id')->references('id')->on('annotations');
$table->foreign('annotation_id')->references('id')->on('annotations')->on_delete('cascade');
$table->unique(array('user_id', 'annotation_id'));

});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,18 @@ class CreateAnnotationRanges extends Migration {
public function up()
{
Schema::create('annotation_ranges', function($table) {

$table->engine = "InnoDB";

$table->increments('id');
$table->string('annotation_id');
$table->integer('annotation_id')->unsigned();
$table->string('start');
$table->string('end');
$table->integer('start_offset')->unsigned();
$table->integer('end_offset')->unsigned();
$table->timestamps();

$table->foreign('annotation_id')->references('id')->on('annotations');
$table->foreign('annotation_id')->references('id')->on('annotations')->on_delete('cascade');
$table->unique(array('annotation_id', 'start_offset'));
$table->unique(array('annotation_id', 'end_offset'));
});
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class AlterNoteMetaTable extends Migration {

/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('note_meta', function($table) {
$table->dropColumn('note_id');
$table->integer('annotation_id')->unsigned();
});

}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
throw new Exception("Can't rollback this migration");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class AddForeignIndexNoteMeta extends Migration {

/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('note_meta', function($table) {
$table->foreign('annotation_id')->references('id')->on('annotations')->on_delete('cascade');
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('note_meta', function($table) {
$table->dropIndex('note_meta_annotation_id_foreign');
});

}

}
Loading

0 comments on commit 9be80a4

Please sign in to comment.