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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions backend/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ gem "bootsnap", require: false
# gem "image_processing", "~> 1.2"

# Use Rack CORS for handling Cross-Origin Resource Sharing (CORS), making cross-origin AJAX possible
# gem "rack-cors"
gem "rack-cors"

group :development, :test do
# See https://guides.rubyonrails.org/debugging_rails_applications.html#debugging-with-the-debug-gem
Expand All @@ -45,4 +45,3 @@ group :development do
# Speed up commands on slow machines / big apps [https://github.com/rails/spring]
# gem "spring"
end

3 changes: 3 additions & 0 deletions backend/Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ GEM
nio4r (~> 2.0)
racc (1.7.3)
rack (2.2.8)
rack-cors (2.0.1)
rack (>= 2.0.0)
rack-test (2.1.0)
rack (>= 1.3)
rails (7.0.8)
Expand Down Expand Up @@ -169,6 +171,7 @@ DEPENDENCIES
debug
mysql2 (~> 0.5)
puma (~> 5.0)
rack-cors
rails (~> 7.0.8)
tzinfo-data

Expand Down
51 changes: 51 additions & 0 deletions backend/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: %i[ 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, :author)
end
end
2 changes: 2 additions & 0 deletions backend/app/models/book.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class Book < ApplicationRecord
end
7 changes: 7 additions & 0 deletions backend/config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,12 @@ class Application < Rails::Application
# Middleware like session, flash, cookies can be added back manually.
# Skip views, helpers and assets when generating a new resource.
config.api_only = true

config.middleware.insert_before 0, Rack::Cors do
allow do
origins 'http://localhost:8000' # あなたのNext.jsアプリのホストに変更
resource '*', headers: :any, methods: [:get, :post, :put, :patch, :delete, :options]
end
end
end
end
2 changes: 1 addition & 1 deletion backend/config/environments/development.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
# Highlight code that triggered database queries in logs.
config.active_record.verbose_query_logs = true


config.hosts << "backend"
# Raises error for missing translations.
# config.i18n.raise_on_missing_translations = true

Expand Down
1 change: 1 addition & 0 deletions backend/config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Rails.application.routes.draw do
resources :books
# Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html

# Defines the root path route ("/")
Expand Down
10 changes: 10 additions & 0 deletions backend/db/migrate/20231123214553_create_books.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class CreateBooks < ActiveRecord::Migration[7.0]
def change
create_table :books do |t|
t.string :title
t.string :author

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

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

17 changes: 17 additions & 0 deletions backend/db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,20 @@
#
# movies = Movie.create([{ name: "Star Wars" }, { name: "Lord of the Rings" }])
# Character.create(name: "Luke", movie: movies.first)

Book.create!(
[
{
title: 'Railsチュートリアル',
author: "チュートリアル"
},
{
title: 'Railsの教科書',
author: 'hoge'
},
{
title: 'パーフェクトrails',
author: 'hogehoge'
}
]
)
38 changes: 38 additions & 0 deletions backend/test/controllers/books_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
require "test_helper"

class BooksControllerTest < ActionDispatch::IntegrationTest
setup do
@book = books(:one)
end

test "should get index" do
get books_url, as: :json
assert_response :success
end

test "should create book" do
assert_difference("Book.count") do
post books_url, params: { book: { author: @book.author, title: @book.title } }, as: :json
end

assert_response :created
end

test "should show book" do
get book_url(@book), as: :json
assert_response :success
end

test "should update book" do
patch book_url(@book), params: { book: { author: @book.author, title: @book.title } }, as: :json
assert_response :success
end

test "should destroy book" do
assert_difference("Book.count", -1) do
delete book_url(@book), as: :json
end

assert_response :no_content
end
end
9 changes: 9 additions & 0 deletions backend/test/fixtures/books.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

one:
title: MyString
author: MyString

two:
title: MyString
author: MyString
7 changes: 7 additions & 0 deletions backend/test/models/book_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require "test_helper"

class BookTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end
3 changes: 3 additions & 0 deletions frontend/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
FROM node:18
WORKDIR /usr/src/app
RUN npm install axios
Loading