Browse Source

#101 マイグレとコントローラ

'#101'
みてるぞ 1 week ago
parent
commit
c9bae764f7
3 changed files with 18 additions and 7 deletions
  1. +9
    -6
      backend/app/controllers/posts_controller.rb
  2. +6
    -0
      backend/db/migrate/20250909075500_add_original_created_at_to_posts.rb
  3. +3
    -1
      backend/db/schema.rb

+ 9
- 6
backend/app/controllers/posts_controller.rb View File

@@ -8,8 +8,11 @@ class PostsController < ApplicationController
limit = params[:limit].presence&.to_i limit = params[:limit].presence&.to_i
cursor = params[:cursor].presence cursor = params[:cursor].presence


q = filtered_posts.order(created_at: :desc)
q = q.where('posts.created_at < ?', Time.iso8601(cursor)) if cursor
created_at = ('COALESCE(posts.original_created_before - INTERVAL 1 SECOND,' +
'posts.original_created_from,' +
'posts.created_at)')
q = filtered_posts.order(Arel.sql("#{ created_at } DESC"))
q = q.where("#{ created_at } < ?", Time.iso8601(cursor)) if cursor


posts = limit ? q.limit(limit + 1) : q posts = limit ? q.limit(limit + 1) : q


@@ -20,14 +23,14 @@ class PostsController < ApplicationController
end end


render json: { posts: posts.map { |post| render json: { posts: posts.map { |post|
post.as_json(include: { tags: { only: [:id, :name, :category, :post_count] } }).tap { |json|
post.as_json(include: { tags: { only: [:id, :name, :category, :post_count] } }).tap do |json|
json['thumbnail'] = json['thumbnail'] =
if post.thumbnail.attached? if post.thumbnail.attached?
rails_storage_proxy_url(post.thumbnail, only_path: false) rails_storage_proxy_url(post.thumbnail, only_path: false)
else else
nil nil
end end
}
end
}, next_cursor: } }, next_cursor: }
end end


@@ -39,7 +42,7 @@ class PostsController < ApplicationController


render json: (post render json: (post
.as_json(include: { tags: { only: [:id, :name, :category, :post_count] } }) .as_json(include: { tags: { only: [:id, :name, :category, :post_count] } })
.merge(viewed: viewed))
.merge(viewed:))
end end


# GET /posts/1 # GET /posts/1
@@ -60,7 +63,7 @@ class PostsController < ApplicationController
return head :forbidden unless current_user.member? return head :forbidden unless current_user.member?


# TODO: URL が正規のものがチェック,不正ならエラー # TODO: URL が正規のものがチェック,不正ならエラー
# TODO: title、URL は必須にする.
# TODO: URL は必須にする(タイトルは省略可)
# TODO: サイトに応じて thumbnail_base 設定 # TODO: サイトに応じて thumbnail_base 設定
title = params[:title] title = params[:title]
url = params[:url] url = params[:url]


+ 6
- 0
backend/db/migrate/20250909075500_add_original_created_at_to_posts.rb View File

@@ -0,0 +1,6 @@
class AddOriginalCreatedAtToPosts < ActiveRecord::Migration[8.0]
def change
add_column :posts, :original_created_from, :datetime, after: :created_at
add_column :posts, :original_created_before, :datetime, after: :original_created_from
end
end

+ 3
- 1
backend/db/schema.rb View File

@@ -10,7 +10,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.


ActiveRecord::Schema[8.0].define(version: 2025_07_29_210600) do
ActiveRecord::Schema[8.0].define(version: 2025_09_09_075500) do
create_table "active_storage_attachments", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t| create_table "active_storage_attachments", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
t.string "name", null: false t.string "name", null: false
t.string "record_type", null: false t.string "record_type", null: false
@@ -83,6 +83,8 @@ ActiveRecord::Schema[8.0].define(version: 2025_07_29_210600) do
t.bigint "parent_id" t.bigint "parent_id"
t.bigint "uploaded_user_id" t.bigint "uploaded_user_id"
t.datetime "created_at", null: false t.datetime "created_at", null: false
t.datetime "original_created_from"
t.datetime "original_created_before"
t.datetime "updated_at", null: false t.datetime "updated_at", null: false
t.index ["parent_id"], name: "index_posts_on_parent_id" t.index ["parent_id"], name: "index_posts_on_parent_id"
t.index ["uploaded_user_id"], name: "index_posts_on_uploaded_user_id" t.index ["uploaded_user_id"], name: "index_posts_on_uploaded_user_id"


Loading…
Cancel
Save