Skip to content
Open

Dev #35

Show file tree
Hide file tree
Changes from all commits
Commits
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
10 changes: 7 additions & 3 deletions app/controllers/api/books_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class Api::BooksController < ApplicationController

def index
books = Book.all
render json: BookSerializer.new(books).serializable_hash[:data].pluck(:attributes)
render json: serialize(books)
end

def show
Expand All @@ -15,12 +15,16 @@ def discover
id_array = current_user.user_books.pluck(:book_id)
books = Book.where.not(id: id_array)

render json: books
render json: serialize(books)
end

private

def get_book
@book = Book.find(params[:id])
end
end

def serialize(data)
return BookSerializer.new(data).serializable_hash[:data].pluck(:attributes)
end
end
15 changes: 9 additions & 6 deletions app/controllers/api/user_books_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ def index
end

def show
render json: serialize_data(@user_book)
render json: serialize(@user_book)
end

def create
user_book = current_user.user_books.create(user_book_params)
if user_book.save
render json: serialize_data(user_book)
render json: serialize(user_book)
else
render json: {errors: user_book.errors}, status: :unprocessable_entity
end
Expand All @@ -32,7 +32,7 @@ def destroy

def update
if @user_book.update(user_book_params)
render json: serialize_data(@user_book)
render json: serialize(@user_book)
else
render json: {errors: @user_book.errors}, status: :unprocessable_entity
end
Expand All @@ -49,7 +49,10 @@ def user_book_params
end

def serialize_data(data)
options = { include: [:book] }
return UserBookSerializer.new(data, options).serializable_hash[:data].pluck(:attributes)
return UserBookSerializer.new(data).serializable_hash[:data].pluck(:attributes)
end
end

def serialize(data)
return UserBookSerializer.new(data).serializable_hash[:data][:attributes]
end
end
3 changes: 2 additions & 1 deletion app/models/book.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ class Book < ApplicationRecord
validates :author, presence: true
validates :publisher, presence: true
validates :cover_image_url, presence: true
end

end
1 change: 1 addition & 0 deletions app/models/user_book.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ class UserBook < ApplicationRecord
# scope :finished, ->(user) { where.not(finish_date: nil)}

delegate :author, :title, :synopsis, :cover_image_url, :publisher, to: :book
alias_attribute :user_book_id, :id
end
2 changes: 1 addition & 1 deletion app/serializers/user_book_serializer.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class UserBookSerializer
include FastJsonapi::ObjectSerializer
attributes :id,
attributes :user_book_id,
:user_id,
:book_id,
:rating,
Expand Down
4 changes: 0 additions & 4 deletions spec/models/book_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,5 @@
it 'has many users through list items' do
should respond_to(:users)
end

it 'has many list items' do
should respond_to(:list_items)
end
end
end
4 changes: 2 additions & 2 deletions spec/models/user_book_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'rails_helper'

RSpec.describe UserBook, type: :model do
RSpec.describe UserBooks, type: :model do
subject {FactoryBot.create(:user_book)}


Expand Down Expand Up @@ -64,4 +64,4 @@
expect(@user_book.finish_date).to eq(nil)
end
end
end
end
6 changes: 3 additions & 3 deletions spec/models/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@
should respond_to(:books)
end

it 'has many list items' do
should respond_to(:list_items)
it 'has many user books' do
should respond_to(:user_books)
end
end
end
end
8 changes: 4 additions & 4 deletions spec/requests/books_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
end

it 'returns all books' do
get '/books'
get '/api/books'
json = JSON.parse(response.body)

expect(response.content_type).to eq("application/json; charset=utf-8")
Expand All @@ -20,7 +20,7 @@
end

it 'returns a specific book' do
get '/book', params: { id: @book2.id }
get '/api/book', params: { id: @book2.id }
json = JSON.parse(response.body)

expect(response.content_type).to eq("application/json; charset=utf-8")
Expand All @@ -30,9 +30,9 @@
end

it 'returns all unread books' do
ListItem.create(user_id: @user.id, book_id: @book3.id)
UserBook.create(user_id: @user.id, book_id: @book3.id)

get '/discover'
get '/api/discover'
json = JSON.parse(response.body)

expect(response.content_type).to eq("application/json; charset=utf-8")
Expand Down
45 changes: 6 additions & 39 deletions spec/requests/user_books_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,6 @@
@user_book2 = UserBook.create(user_id: @user.id, book_id: @book2.id, start_date: Time.now, finish_date: Time.now)
end

it 'returns index of user_books' do
get '/api/user_books'
json = JSON.parse(response.body)

expect(response.content_type).to eq("application/json; charset=utf-8")
expect(response).to have_http_status(:success)
expect(json.length).to eq(2)
end

it 'returns a specific user_book' do
get "/api/user_books/#{@user_book2.id}"
json = JSON.parse(response.body)
Expand All @@ -42,31 +33,27 @@
json = JSON.parse(response.body)

expect(response).to have_http_status(:success)
expect(json["message"]).to eq('Successfully created!')
end

it 'removes a user_book' do
delete "/api/user_books/#{@user_book1.id}"

json = JSON.parse(response.body)
expect(response).to have_http_status(:success)
expect(json["message"]).to eq('Successfully removed!')
end

it 'marks a user_book as read' do
patch "/api/user_books/#{@user_book2.id}", params: { user_book: { finish_date: Time.now } }

json = JSON.parse(response.body)
expect(response).to have_http_status(:success)
expect(json["message"]).to eq('Successfully updated!')
end

it 'marks a user_book as unread' do
patch "/api/user_books/#{@user_book2.id}", params: { user_book: { finish_date: nil } }

json = JSON.parse(response.body)
expect(response).to have_http_status(:success)
expect(json["message"]).to eq('Successfully updated!')
end

it 'updates the rating of a user_book' do
Expand All @@ -75,35 +62,33 @@
json = JSON.parse(response.body)

expect(response).to have_http_status(:success)
expect(json["message"]).to eq('Successfully updated!')
expect(json['user_book']['rating']).to eq(2)
expect(json['rating']).to eq(2)
end

it 'updates the notes of a user_book' do
patch "/api/user_books/#{@user_book1.id}", params: { user_book: { notes: "This is a good read" } }

json = JSON.parse(response.body)
expect(response).to have_http_status(:success)
expect(json["message"]).to eq('Successfully updated!')
expect(json['user_book']['notes']).to eq("This is a good read")
expect(json['notes']).to eq("This is a good read")
end

it 'shows all books marked to be read' do
get "/api/user_books"
json = JSON.parse(response.body)

expect(response).to have_http_status(:success)
expect(json["data"][0]["attributes"]["user_id"]).to eq(@user.id)
expect(json["data"][0]["attributes"]["book_id"]).to eq(@book1.id)
expect(json[0]["user_id"]).to eq(@user.id)
expect(json[0]["book_id"]).to eq(@book1.id)
end

it 'shows all books marked as finished' do
get "/api/user_books", params: { finish_date: Time.now}
json = JSON.parse(response.body)

expect(response).to have_http_status(:success)
expect(json["data"][0]["attributes"]["user_id"]).to eq(@user.id)
expect(json["data"][0]["attributes"]["book_id"]).to eq(@book2.id)
expect(json[0]["user_id"]).to eq(@user.id)
expect(json[0]["book_id"]).to eq(@book2.id)
end
end

Expand All @@ -115,23 +100,6 @@
@book = FactoryBot.create(:book)
end


it 'will notify if reading list is empty' do
get "/api/user_books"
json = JSON.parse(response.body)

expect(response).to have_http_status(:unprocessable_entity)
expect(json["errors"]).to eq("List is empty")
end

it 'will notify if finished list is empty' do
get "/api/user_books", params: { finish_date: 'Time.now'}
json = JSON.parse(response.body)

expect(response).to have_http_status(:unprocessable_entity)
expect(json["errors"]).to eq("List is empty")
end

it 'will not duplicate if user already has the book' do
UserBook.create(user_id: @book_less_user.id, book_id: @book.id)

Expand All @@ -144,7 +112,6 @@
json = JSON.parse(response.body)

expect(response).to have_http_status(:unprocessable_entity)
expect(json["errors"][0]).to eq("Rating is not included in the list")
end
end
end
12 changes: 6 additions & 6 deletions spec/requests/users_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

expect(response.content_type).to eq("application/json; charset=utf-8")
expect(response).to have_http_status(:unprocessable_entity)
expect(json['status']['message']).to eq("User couldn't be created successfully. Username can't be blank")
expect(json['errors']['username'][0]).to eq("can't be blank")
end

it 'will not sign up without password' do
Expand All @@ -20,7 +20,8 @@

expect(response.content_type).to eq("application/json; charset=utf-8")
expect(response).to have_http_status(:unprocessable_entity)
expect(json['status']['message']).to eq("User couldn't be created successfully. Password can't be blank and Password is too short (minimum is 6 characters)")
expect(json['errors']['password'][0]).to eq("can't be blank")
expect(json['errors']['password'][1]).to eq("is too short (minimum is 6 characters)")
end

it 'will not sign up if password is less than 6 characters' do
Expand All @@ -30,7 +31,7 @@

expect(response.content_type).to eq("application/json; charset=utf-8")
expect(response).to have_http_status(:unprocessable_entity)
expect(json['status']['message']).to eq("User couldn't be created successfully. Password is too short (minimum is 6 characters)")
expect(json['errors']['password'][0]).to eq("is too short (minimum is 6 characters)")
end

it 'will sign up with correct details' do
Expand All @@ -40,7 +41,6 @@

expect(response.content_type).to eq("application/json; charset=utf-8")
expect(response).to have_http_status(:success)
expect(json['status']['message']).to eq("Signed up sucessfully.")
end
end

Expand Down Expand Up @@ -76,9 +76,9 @@

context 'user is not verified' do
it 'will not allow access to app' do
get '/current_user'
get '/api/current_user'

expect(response).to have_http_status(:unauthorized)
end
end
end
end