Skip to content
Open
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
The diff you're trying to view is too large. We only load the first 3000 changed files.
3 changes: 2 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ GEM

PLATFORMS
x86_64-darwin-19
x86_64-linux

DEPENDENCIES
activerecord (~> 6.1)
Expand All @@ -106,4 +107,4 @@ DEPENDENCIES
thin (~> 1.8)

BUNDLED WITH
2.2.23
2.3.4
31 changes: 6 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,13 @@ by a separate **React frontend** that interacts with the database via the API.
For this project, you must:

- Use Active Record to interact with a database.
- Have at least two models with a one-to-many relationship.
- At a minimum, set up the following API routes in Sinatra:
- create and read actions for both models
- full CRUD capability for one of the models:
The update action should be implemented using a form that is
pre-filled with existing values for the object. On submission of
the form, the object should update. Note: Using a like button or
similar will not meet the update requirement.
- Have a minimum of two models with a one-to-many relationship.
- Create API routes in Sinatra that handles at least three different CRUD
actions for at least one of your Active Record models.
- Build a separate React frontend application that interacts with the API to
perform CRUD actions.
- Implement proper front end state management. You should be updating state using a
setState function after receiving your response from a POST, PATCH, or DELETE
request. You should NOT be relying on a GET request to update state.
- Use good OO design patterns. You should have separate classes for each of your
models, and create instance and class methods as necessary.
- Routes in your application (both client side and back end) should follow RESTful
conventions.
- Use your back end optimally. Pass JSON for related associations to the front
end from the back end. You should use active record methods in your controller to grab
the needed data from your database and provide as JSON to the front end. You
should NOT be relying on filtering front end state or a separate fetch request to
retrieve related data.
models, and create instance and class methods as necessary.

For example, build a todo list application with a React frontend interface and a
Sinatra backend API, where a user can:
Expand All @@ -62,11 +47,6 @@ This repository has all the starter code needed to get a Sinatra backend up and
running. [**Fork and clone**][fork link] this repository to get started. Then, run
`bundle install` to install the gems.

**Important**: Be sure you fork a copy of the repo into your GitHub account
before cloning it. You can do this by using the link above or by clicking the
"Octocat" button at the top of this page, then clicking "Fork" in the upper
right corner of the repo page.

[fork link]: https://github.com/learn-co-curriculum/phase-3-sinatra-react-project/fork

The `app/controllers/application_controller.rb` file has an example GET route
Expand All @@ -86,7 +66,7 @@ This will run your server on port
Your backend and your frontend should be in **two different repositories**.

Create a new repository in a **separate folder** with a React app for your
frontend. To do this, `cd` out of the backend project directory, and use
frontend. `cd` out of the backend project directory, and use
[create-react-app][] to generate the necessary code for your React frontend:

```console
Expand Down Expand Up @@ -136,3 +116,4 @@ fetch("http://localhost:9292/test")
[dbdiagram.io]: https://dbdiagram.io/
[postman download]: https://www.postman.com/downloads/
[network tab]: https://developer.chrome.com/docs/devtools/network/
# phase-3-sinatra-react-project
45 changes: 43 additions & 2 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,49 @@ class ApplicationController < Sinatra::Base
set :default_content_type, 'application/json'

# Add your routes here
get "/" do
{ message: "Good luck with your project!" }.to_json

get "/books" do
books = Book.all
books.to_json(include: :reviews)
end

get "/books/" do
book = Book.all(title: params[:title], author: params[:author], likes: params[:likes])
book.to_json(include: :reviews)
end


get 'reviews/:id' do
review = Review.find(params[:id])
review.to_json
end

#create a new book
post "/books" do
new_book = Book.create(title: params[:title], author: params[:author], likes: params[:likes])
new_book.to_json
end

#create a new review
post '/books/:book_id/reviews' do
book = Book.find_by(id: params[:book_id])
new_review = book.reviews.create(text: params[:text])
new_review.to_json(include: :book)

end

#update book likes
patch '/books/:id' do
book = Book.find_by(id: params[:id])
book.update(likes: params[:likes])
book.to_json(include: :reviews)
end

#delete book review
delete '/reviews/:id' do
review = Review.find_by(id: params[:id])
review.destroy
review.to_json
end

end
3 changes: 3 additions & 0 deletions app/models/book.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Book < ActiveRecord::Base
has_many :reviews
end
File renamed without changes.
3 changes: 3 additions & 0 deletions app/models/review.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Review < ActiveRecord::Base
belongs_to :book
end
9 changes: 9 additions & 0 deletions db/migrate/20220209162053_create_books.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class CreateBooks < ActiveRecord::Migration[6.1]
def change
create_table :books do |t|
t.string :title
t.string :author
t.integer :likes
end
end
end
8 changes: 8 additions & 0 deletions db/migrate/20220209162102_create_reviews.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class CreateReviews < ActiveRecord::Migration[6.1]
def change
create_table :reviews do |t|
t.string :text
t.integer :book_id
end
end
end
26 changes: 26 additions & 0 deletions db/schema.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
#
# This file is the source Rails uses to define your schema when running `bin/rails
# db:schema:load`. When creating a new database, `bin/rails db:schema:load` tends to
# be faster and is potentially less error prone than running all of your
# migrations from scratch. Old migrations may fail to apply correctly if those
# migrations use external dependencies or application code.
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2022_02_09_162102) do

create_table "books", force: :cascade do |t|
t.string "title"
t.string "author"
t.integer "likes"
end

create_table "reviews", force: :cascade do |t|
t.string "text"
t.integer "book_id"
end

end
24 changes: 23 additions & 1 deletion db/seeds.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,27 @@
puts "🌱 Seeding spices..."

# Seed your database here
all_reviews = ["This play is as relevant today as it was in 1959, and more relevant to American students than Shakespeare may ever be.", "I did not like this book at all. All the drug, booze, swearing was just too much for me to overlook. Perhaps there is an element of society that lives this kind of life, but I really didn't enjoy reading about it.",
"French girls farther is a museum custodian and the jewel now resides behind 13 locked doors like Russian dolls. Guess the girl and soldier meet up … could not take any more.", "Every story in this collection is a gem. ",
"Boring... ", "I dont need to feel ashamed for how Im not “woke enough” these days. I get enough of that on Twitter.", "I bought this because it was required reading for my English class.",
"I enjoyed this novel very much because it rang true about life in general during this period and because it was so well written.", "This is a fantastic edition to own, and I'm glad to have it. 5 stars for the book.",
" I came to a conclusion that there's simply nothing else like it, and it tops my list."]

puts "✅ Done seeding!"
Book.create(title: "A Raisin in the Sun", author: "Lorraine Hansberry", likes: 0)
Book.create(title: "The Goldfinch", author: "Donna Tartt", likes: 0)
Book.create(title: "All the Light We Can Not See", author: "Anthony Doerr", likes: 0)
Book.create(title: "Welcome to the Monkey House", author: "Kurt Vonnegut", likes: 0)
Book.create(title: "Paradise Lost", author: "John Milton", likes: 0)
Book.create(title: "Bad Feminist", author: "Roxane Gay", likes: 0)
Book.create(title: "Tracks", author: "Louise Erdrich", likes: 0)
Book.create(title: "The Outsiders", author: "SE Hinton", likes: 0)
Book.create(title: "The Lord of the Rings", author: "JRR Tolkien", likes: 0)
Book.create(title: "And Then There Were None", author: "Agatha Christie", likes: 0)

Book.all.each do |book|
Review.create(text: all_reviews.sample, book_id: book.id)
end



puts "✅ Done seeding!"
2 changes: 1 addition & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,4 @@
# Rack::Test::Methods needs this to run our controller
def app
Rack::Builder.parse_file('config.ru').first
end
end
29 changes: 29 additions & 0 deletions vendor/bundle/ruby/3.0.0/bin/build
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/usr/bin/env ruby3.0
#
# This file was generated by RubyGems.
#
# The application 'rspec-json_expectations' is installed as part of a gem, and
# this file is here to facilitate running it.
#

require 'rubygems'

Gem.use_gemdeps

version = ">= 0.a"

str = ARGV.first
if str
str = str.b[/\A_(.*)_\z/, 1]
if str and Gem::Version.correct?(str)
version = str
ARGV.shift
end
end

if Gem.respond_to?(:activate_bin_path)
load Gem.activate_bin_path('rspec-json_expectations', 'build', version)
else
gem "rspec-json_expectations", version
load Gem.bin_path("rspec-json_expectations", "build", version)
end
29 changes: 29 additions & 0 deletions vendor/bundle/ruby/3.0.0/bin/bundle
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/usr/bin/env ruby3.0
#
# This file was generated by RubyGems.
#
# The application 'bundler' is installed as part of a gem, and
# this file is here to facilitate running it.
#

require 'rubygems'

version = ">= 0.a"

str = ARGV.first
if str
str = str.b[/\A_(.*)_\z/, 1]
if str and Gem::Version.correct?(str)
version = str
ENV['BUNDLER_VERSION'] = str

ARGV.shift
end
end

if Gem.respond_to?(:activate_bin_path)
load Gem.activate_bin_path('bundler', 'bundle', version)
else
gem "bundler", version
load Gem.bin_path("bundler", "bundle", version)
end
29 changes: 29 additions & 0 deletions vendor/bundle/ruby/3.0.0/bin/bundler
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/usr/bin/env ruby3.0
#
# This file was generated by RubyGems.
#
# The application 'bundler' is installed as part of a gem, and
# this file is here to facilitate running it.
#

require 'rubygems'

version = ">= 0.a"

str = ARGV.first
if str
str = str.b[/\A_(.*)_\z/, 1]
if str and Gem::Version.correct?(str)
version = str
ENV['BUNDLER_VERSION'] = str

ARGV.shift
end
end

if Gem.respond_to?(:activate_bin_path)
load Gem.activate_bin_path('bundler', 'bundler', version)
else
gem "bundler", version
load Gem.bin_path("bundler", "bundler", version)
end
29 changes: 29 additions & 0 deletions vendor/bundle/ruby/3.0.0/bin/coderay
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/usr/bin/env ruby3.0
#
# This file was generated by RubyGems.
#
# The application 'coderay' is installed as part of a gem, and
# this file is here to facilitate running it.
#

require 'rubygems'

Gem.use_gemdeps

version = ">= 0.a"

str = ARGV.first
if str
str = str.b[/\A_(.*)_\z/, 1]
if str and Gem::Version.correct?(str)
version = str
ARGV.shift
end
end

if Gem.respond_to?(:activate_bin_path)
load Gem.activate_bin_path('coderay', 'coderay', version)
else
gem "coderay", version
load Gem.bin_path("coderay", "coderay", version)
end
31 changes: 31 additions & 0 deletions vendor/bundle/ruby/3.0.0/bin/htmldiff
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/bin/sh
'exec' "ruby3.0" '-x' "$0" "$@"
#!/usr/bin/ruby3.0
#
# This file was generated by RubyGems.
#
# The application 'diff-lcs' is installed as part of a gem, and
# this file is here to facilitate running it.
#

require 'rubygems'

Gem.use_gemdeps

version = ">= 0.a"

str = ARGV.first
if str
str = str.b[/\A_(.*)_\z/, 1]
if str and Gem::Version.correct?(str)
version = str
ARGV.shift
end
end

if Gem.respond_to?(:activate_bin_path)
load Gem.activate_bin_path('diff-lcs', 'htmldiff', version)
else
gem "diff-lcs", version
load Gem.bin_path("diff-lcs", "htmldiff", version)
end
Loading