Skip to content

Commit

Permalink
add class skeletons
Browse files Browse the repository at this point in the history
  • Loading branch information
hermitex committed Oct 11, 2022
1 parent 89e2d28 commit 73433ae
Show file tree
Hide file tree
Showing 30 changed files with 997 additions and 0 deletions.
51 changes: 51 additions & 0 deletions app/controllers/books_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
class BooksController < ApplicationController
before_action :set_book, only: [:show, :update, :destroy]

# GET /books
def index
@books = Book.all

render json: @books
end

# GET /books/1
def show
render json: @book
end

# POST /books
def create
@book = Book.new(book_params)

if @book.save
render json: @book, status: :created, location: @book
else
render json: @book.errors, status: :unprocessable_entity
end
end

# PATCH/PUT /books/1
def update
if @book.update(book_params)
render json: @book
else
render json: @book.errors, status: :unprocessable_entity
end
end

# DELETE /books/1
def destroy
@book.destroy
end

private
# Use callbacks to share common setup or constraints between actions.
def set_book
@book = Book.find(params[:id])
end

# Only allow a list of trusted parameters through.
def book_params
params.require(:book).permit(:title, :category, :cover_image, :author, :condition, :is_available, :user_id)
end
end
51 changes: 51 additions & 0 deletions app/controllers/ratings_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
class RatingsController < ApplicationController
before_action :set_rating, only: [:show, :update, :destroy]

# GET /ratings
def index
@ratings = Rating.all

render json: @ratings
end

# GET /ratings/1
def show
render json: @rating
end

# POST /ratings
def create
@rating = Rating.new(rating_params)

if @rating.save
render json: @rating, status: :created, location: @rating
else
render json: @rating.errors, status: :unprocessable_entity
end
end

# PATCH/PUT /ratings/1
def update
if @rating.update(rating_params)
render json: @rating
else
render json: @rating.errors, status: :unprocessable_entity
end
end

# DELETE /ratings/1
def destroy
@rating.destroy
end

private
# Use callbacks to share common setup or constraints between actions.
def set_rating
@rating = Rating.find(params[:id])
end

# Only allow a list of trusted parameters through.
def rating_params
params.require(:rating).permit(:rate, :user_id)
end
end
51 changes: 51 additions & 0 deletions app/controllers/reviews_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
class ReviewsController < ApplicationController
before_action :set_review, only: [:show, :update, :destroy]

# GET /reviews
def index
@reviews = Review.all

render json: @reviews
end

# GET /reviews/1
def show
render json: @review
end

# POST /reviews
def create
@review = Review.new(review_params)

if @review.save
render json: @review, status: :created, location: @review
else
render json: @review.errors, status: :unprocessable_entity
end
end

# PATCH/PUT /reviews/1
def update
if @review.update(review_params)
render json: @review
else
render json: @review.errors, status: :unprocessable_entity
end
end

# DELETE /reviews/1
def destroy
@review.destroy
end

private
# Use callbacks to share common setup or constraints between actions.
def set_review
@review = Review.find(params[:id])
end

# Only allow a list of trusted parameters through.
def review_params
params.require(:review).permit(:content, :rate, :book_id, :user_id)
end
end
51 changes: 51 additions & 0 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
class UsersController < ApplicationController
before_action :set_user, only: [:show, :update, :destroy]

# GET /users
def index
@users = User.all

render json: @users
end

# GET /users/1
def show
render json: @user
end

# POST /users
def create
@user = User.new(user_params)

if @user.save
render json: @user, status: :created, location: @user
else
render json: @user.errors, status: :unprocessable_entity
end
end

# PATCH/PUT /users/1
def update
if @user.update(user_params)
render json: @user
else
render json: @user.errors, status: :unprocessable_entity
end
end

# DELETE /users/1
def destroy
@user.destroy
end

private
# Use callbacks to share common setup or constraints between actions.
def set_user
@user = User.find(params[:id])
end

# Only allow a list of trusted parameters through.
def user_params
params.require(:user).permit(:username, :email, :phone, :location)
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 < ApplicationRecord
belongs_to :user
end
3 changes: 3 additions & 0 deletions app/models/rating.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Rating < ApplicationRecord
belongs_to :user
end
4 changes: 4 additions & 0 deletions app/models/review.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class Review < ApplicationRecord
belongs_to :book
belongs_to :user
end
3 changes: 3 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class User < ApplicationRecord
has_many :books
end
4 changes: 4 additions & 0 deletions app/serializers/book_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class BookSerializer < ActiveModel::Serializer
attributes :id, :title, :category, :cover_image, :author, :condition, :is_available
has_one :user
end
4 changes: 4 additions & 0 deletions app/serializers/rating_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class RatingSerializer < ActiveModel::Serializer
attributes :id, :rate
has_one :user
end
5 changes: 5 additions & 0 deletions app/serializers/review_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class ReviewSerializer < ActiveModel::Serializer
attributes :id, :content, :rate
has_one :book
has_one :user
end
3 changes: 3 additions & 0 deletions app/serializers/user_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class UserSerializer < ActiveModel::Serializer
attributes :id, :username, :email, :phone, :location
end
4 changes: 4 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
Rails.application.routes.draw do

resources :ratings
resources :reviews
resources :books
resources :users
# Routing logic: fallback requests for React Router.
# Leave this here to help deploy your app later!
get "*path", to: "fallback#index", constraints: ->(req) { !req.xhr? && req.format.html? }
Expand Down
12 changes: 12 additions & 0 deletions db/migrate/20221011121838_create_users.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class CreateUsers < ActiveRecord::Migration[6.1]
def change
create_table :users do |t|
t.string :username
t.string :email
t.string :phone
t.string :location

t.timestamps
end
end
end
15 changes: 15 additions & 0 deletions db/migrate/20221011122442_create_books.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class CreateBooks < ActiveRecord::Migration[6.1]
def change
create_table :books do |t|
t.string :title
t.string :category
t.string :cover_image
t.string :author
t.string :condition
t.boolean :is_available
t.references :user, null: false, foreign_key: true

t.timestamps
end
end
end
12 changes: 12 additions & 0 deletions db/migrate/20221011122916_create_reviews.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class CreateReviews < ActiveRecord::Migration[6.1]
def change
create_table :reviews do |t|
t.text :content
t.integer :rate
t.references :book, null: false, foreign_key: true
t.references :user, null: false, foreign_key: true

t.timestamps
end
end
end
10 changes: 10 additions & 0 deletions db/migrate/20221011123006_create_ratings.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class CreateRatings < ActiveRecord::Migration[6.1]
def change
create_table :ratings do |t|
t.integer :rate
t.references :user, null: false, foreign_key: true

t.timestamps
end
end
end
63 changes: 63 additions & 0 deletions db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions spec/models/book_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'rails_helper'

RSpec.describe Book, type: :model do
pending "add some examples to (or delete) #{__FILE__}"
end
5 changes: 5 additions & 0 deletions spec/models/rating_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'rails_helper'

RSpec.describe Rating, type: :model do
pending "add some examples to (or delete) #{__FILE__}"
end
5 changes: 5 additions & 0 deletions spec/models/review_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'rails_helper'

RSpec.describe Review, type: :model do
pending "add some examples to (or delete) #{__FILE__}"
end
5 changes: 5 additions & 0 deletions spec/models/user_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'rails_helper'

RSpec.describe User, type: :model do
pending "add some examples to (or delete) #{__FILE__}"
end
Loading

0 comments on commit 73433ae

Please sign in to comment.