This commit is contained in:
@@ -48,3 +48,4 @@ group :development, :test do
|
||||
end
|
||||
|
||||
gem 'bcrypt', '~> 3.1'
|
||||
gem 'rack-cors'
|
||||
|
||||
@@ -187,6 +187,9 @@ GEM
|
||||
raabro (1.4.0)
|
||||
racc (1.8.1)
|
||||
rack (3.2.0)
|
||||
rack-cors (3.0.0)
|
||||
logger
|
||||
rack (>= 3.0.14)
|
||||
rack-session (2.1.1)
|
||||
base64 (>= 0.1.0)
|
||||
rack (>= 3.0.0)
|
||||
@@ -325,6 +328,7 @@ DEPENDENCIES
|
||||
kamal
|
||||
mysql2 (~> 0.5)
|
||||
puma (>= 5.0)
|
||||
rack-cors
|
||||
rails (~> 8.0.2)
|
||||
rubocop-rails-omakase
|
||||
solid_cable
|
||||
|
||||
@@ -1,5 +1,14 @@
|
||||
class PostsController < ApplicationController
|
||||
before_action :set_post, only: [:good, :bad, :destroy]
|
||||
before_action :set_post, only: [:show, :good, :bad, :destroy]
|
||||
|
||||
def show
|
||||
render json: @post.as_json.merge(image_url: (
|
||||
if @post.image.attached?
|
||||
Rails.application.routes.url_helpers.rails_blob_url(@post.image, only_path: true)
|
||||
else
|
||||
nil
|
||||
end))
|
||||
end
|
||||
|
||||
# POST /posts/:id/good
|
||||
def good
|
||||
|
||||
@@ -10,7 +10,14 @@ class ThreadPostsController < ApplicationController
|
||||
.select('posts.*, (good - bad) AS score')
|
||||
.order("#{ sort } #{ order }")
|
||||
|
||||
render json: posts
|
||||
render json: posts.map { |post|
|
||||
post.as_json.merge(image_url: (
|
||||
if post.image.attached?
|
||||
Rails.application.routes.url_helpers.rails_blob_url(post.image, only_path: true)
|
||||
else
|
||||
nil
|
||||
end))
|
||||
}
|
||||
end
|
||||
|
||||
# POST /api/threads/:thread_id/posts
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
class ThreadsController < ApplicationController
|
||||
# GET /api/threads
|
||||
def index
|
||||
threads = Topic.order(updated_at: :desc)
|
||||
render json: threads
|
||||
threads = Topic.includes(:posts).order(updated_at: :desc)
|
||||
render json: threads.map { |t|
|
||||
t.as_json(methods: [:post_count])
|
||||
}
|
||||
end
|
||||
|
||||
# GET /api/threads/:id
|
||||
@@ -15,7 +17,7 @@ class ThreadsController < ApplicationController
|
||||
def create
|
||||
thread = Topic.new(thread_params)
|
||||
if thread.save
|
||||
render json: thread, status: :created
|
||||
render json: thread.as_json, status: :created
|
||||
else
|
||||
render json: { errors: thread.errors.full_messages }, status: :unprocessable_entity
|
||||
end
|
||||
@@ -24,6 +26,6 @@ class ThreadsController < ApplicationController
|
||||
private
|
||||
|
||||
def thread_params
|
||||
params.require(:thread).permit(:title, :description)
|
||||
params.require(:thread).permit(:name, :description)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -6,4 +6,8 @@ class Topic < ApplicationRecord
|
||||
scope :active, -> { where deleted_at: nil }
|
||||
|
||||
validates :name, presence: true
|
||||
|
||||
def post_count
|
||||
posts.count
|
||||
end
|
||||
end
|
||||
|
||||
@@ -5,12 +5,12 @@
|
||||
|
||||
# Read more: https://github.com/cyu/rack-cors
|
||||
|
||||
# Rails.application.config.middleware.insert_before 0, Rack::Cors do
|
||||
# allow do
|
||||
# origins "example.com"
|
||||
#
|
||||
# resource "*",
|
||||
# headers: :any,
|
||||
# methods: [:get, :post, :put, :patch, :delete, :options, :head]
|
||||
# end
|
||||
# end
|
||||
Rails.application.config.middleware.insert_before 0, Rack::Cors do
|
||||
allow do
|
||||
origins 'http://bbs.kekec.wiki:5173', 'https://bbs.kekec.wiki'
|
||||
|
||||
resource "*",
|
||||
headers: :any,
|
||||
methods: [:get, :post, :put, :patch, :delete, :options, :head]
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user