-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow apis to interact with judgment data (#757)
* first cut * refactory * and add in the judgements...
- Loading branch information
Showing
12 changed files
with
260 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
# frozen_string_literal: true | ||
|
||
module Api | ||
module V1 | ||
class JudgementsController < Api::ApiController | ||
before_action :find_book | ||
before_action :check_book | ||
before_action :set_judgement, only: [ :show, :update, :destroy ] | ||
before_action :check_judgement, only: [ :show, :update, :destroy ] | ||
|
||
def index | ||
@judgements = @book.judgements | ||
|
||
respond_with @judgements | ||
end | ||
|
||
def show | ||
respond_with @judgement | ||
end | ||
|
||
def create | ||
puts 'HERE I AM' | ||
@judgement = @book.judgements.build judgement_params | ||
|
||
if @judgement.save | ||
respond_with @book, @judgement | ||
else | ||
render json: @judgement.errors, status: :bad_request | ||
end | ||
end | ||
|
||
def update | ||
update_params = judgement_params | ||
if @judgement.update update_params | ||
respond_with @judgement | ||
else | ||
render json: @judgement.errors, status: :bad_request | ||
end | ||
end | ||
|
||
def destroy | ||
@judgement.destroy | ||
render json: {}, status: :no_content | ||
end | ||
|
||
private | ||
|
||
def judgement_params | ||
params.require(:judgement).permit(:rating, :unrateable, :query_doc_pair_id, :user_id) | ||
end | ||
|
||
def find_book | ||
@book = current_user.books_involved_with.where(id: params[:book_id]).first | ||
end | ||
|
||
def check_book | ||
render json: { message: 'Book not found!' }, status: :not_found unless @book | ||
end | ||
|
||
def set_judgement | ||
@judgement = @book.judgements.where(id: params[:id]).first | ||
end | ||
|
||
def check_judgement | ||
render json: { message: 'Query Doc Pair not found!' }, status: :not_found unless @judgement | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
# frozen_string_literal: true | ||
|
||
module Api | ||
module V1 | ||
class QueryDocPairsController < Api::ApiController | ||
before_action :find_book | ||
before_action :check_book | ||
before_action :set_query_doc_pair, only: [ :show, :update, :destroy ] | ||
before_action :check_query_doc_pair, only: [ :show, :update, :destroy ] | ||
|
||
def index | ||
@query_doc_pairs = @book.query_doc_pairs.includes([ :judgements ]) | ||
|
||
respond_with @query_doc_pairs | ||
end | ||
|
||
def show | ||
respond_with @query_doc_pair | ||
end | ||
|
||
def create | ||
@query_doc_pair = @book.query_doc_pairs.build query_doc_pair_params | ||
|
||
if @query_doc_pair.save | ||
respond_with @query_doc_pair | ||
else | ||
render json: @query_doc_pair.errors, status: :bad_request | ||
end | ||
end | ||
|
||
def update | ||
update_params = query_doc_pair_params | ||
if @query_doc_pair.update update_params | ||
respond_with @query_doc_pair | ||
else | ||
render json: @query_doc_pair.errors, status: :bad_request | ||
end | ||
end | ||
|
||
def destroy | ||
@query_doc_pair.destroy | ||
render json: {}, status: :no_content | ||
end | ||
|
||
private | ||
|
||
def query_doc_pair_params | ||
params.require(:query_doc_pair).permit(:document_fields, :position, :query_text, :doc_id) | ||
end | ||
|
||
def find_book | ||
@book = current_user.books_involved_with.where(id: params[:book_id]).first | ||
end | ||
|
||
def check_book | ||
render json: { message: 'Book not found!' }, status: :not_found unless @book | ||
end | ||
|
||
def set_query_doc_pair | ||
@query_doc_pair = @book.query_doc_pairs.where(id: params[:id]).first | ||
end | ||
|
||
def check_query_doc_pair | ||
render json: { message: 'Query Doc Pair not found!' }, status: :not_found unless @query_doc_pair | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
app/views/api/v1/query_doc_pairs/_query_doc_pair.json.jbuilder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# frozen_string_literal: true | ||
|
||
json.query_text query_doc_pair.query_text | ||
json.doc_id query_doc_pair.doc_id | ||
json.position query_doc_pair.position | ||
json.document_fields query_doc_pair.document_fields |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# frozen_string_literal: true | ||
|
||
json.partial! 'query_doc_pair', query_doc_pair: @query_doc_pair |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# frozen_string_literal: true | ||
|
||
json.all_cases do | ||
json.array! @query_doc_pairs, partial: 'query_doc_pair', as: :query_doc_pair, locals: {} | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# frozen_string_literal: true | ||
|
||
json.partial! 'query_doc_pair', query_doc_pair: @query_doc_pair |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# frozen_string_literal: true | ||
|
||
json.partial! 'query_doc_pair', query_doc_pair: @query_doc_pair |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'test_helper' | ||
|
||
module Api | ||
module V1 | ||
class JudgementsControllerTest < ActionController::TestCase | ||
let(:doug) { users(:doug) } | ||
|
||
before do | ||
@controller = Api::V1::JudgementsController.new | ||
|
||
login_user doug | ||
end | ||
|
||
describe 'Creating a judgement' do | ||
let(:random) { users(:random) } | ||
let(:book) { books(:james_bond_movies) } | ||
let(:qdp) { query_doc_pairs(:one) } | ||
|
||
before do | ||
login_user random | ||
end | ||
|
||
test 'on an existing query_doc_pair' do | ||
assert_difference 'qdp.judgements.count', 1 do | ||
post :create, params: { | ||
book_id: book.id, | ||
judgement: { | ||
rating: 1, | ||
query_doc_pair_id: qdp.id, | ||
}, | ||
} | ||
assert_response :created | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
57 changes: 57 additions & 0 deletions
57
test/controllers/api/v1/query_doc_pairs_controller_test.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'test_helper' | ||
|
||
module Api | ||
module V1 | ||
class QueryDocPairsControllerTest < ActionController::TestCase | ||
let(:doug) { users(:doug) } | ||
|
||
before do | ||
@controller = Api::V1::QueryDocPairsController.new | ||
|
||
login_user doug | ||
end | ||
|
||
describe 'Creating a query_doc_pair' do | ||
let(:random) { users(:random) } | ||
let(:book) { books(:james_bond_movies) } | ||
let(:qdp) { query_doc_pairs(:one) } | ||
|
||
before do | ||
login_user random | ||
end | ||
|
||
test 'successfully creates a query_doc_pair and adds it to book' do | ||
count = book.query_doc_pairs.count | ||
|
||
post :create, | ||
params: { book_id: book.id, | ||
query_doc_pair: { | ||
document_fields: qdp.document_fields, | ||
position: qdp.position, | ||
query_text: qdp.query_text, | ||
doc_id: qdp.doc_id, | ||
} } | ||
|
||
assert_response :ok | ||
|
||
assert_equal json_response['doc_id'], qdp.doc_id | ||
assert_equal book.query_doc_pairs.count, count + 1 | ||
end | ||
|
||
test 'requires a query text' do | ||
post :create, | ||
params: { book_id: book.id, | ||
query_doc_pair: { document_fields: qdp.document_fields, position: qdp.position, | ||
query_text: '', doc_id: qdp.doc_id } } | ||
|
||
assert_response :bad_request | ||
|
||
body = response.parsed_body | ||
assert body['query_text'].include? "can't be blank" | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters