Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

コメントテーブルにposterカラムを追加する #116

Merged
merged 2 commits into from
Oct 22, 2024
Merged
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
2 changes: 1 addition & 1 deletion backend/app/controllers/comments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@ def destroy
private

def comment_params
params.require(:comment).permit(:content)
params.require(:comment).permit(:content, :poster)
end
end
12 changes: 7 additions & 5 deletions backend/app/models/comment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
#
# Table name: comments
#
# id :bigint not null, primary key
# content(内容) :string(1024) not null
# created_at :datetime not null
# updated_at :datetime not null
# memo_id(メモID) :bigint not null
# id :bigint not null, primary key
# content(内容) :string(1024) not null
# poster(Slackのユーザー名) :string(50) not null
# created_at :datetime not null
# updated_at :datetime not null
# memo_id(メモID) :bigint not null
#
# Indexes
#
Expand All @@ -20,5 +21,6 @@
#
class Comment < ApplicationRecord
validates :content, presence: true, length: { maximum: 1024 }
validates :poster, presence: true, length: { maximum: 50 }
belongs_to :memo
end
1 change: 1 addition & 0 deletions backend/config/locales/models/memo/ja.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ ja:
poster: Slackでの投稿者名
comment:
content: 内容
poster: Slackでの投稿者名
user:
account_name: アカウント名
password: パスワード
Expand Down
1 change: 1 addition & 0 deletions backend/db/Schemafile
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ end
create_table 'comments', charset: 'utf8mb4', collation: 'utf8mb4_0900_ai_ci', force: :cascade do |t|
t.bigint "memo_id", null: false, comment: 'メモID'
t.string "content", limit: 1024, null: false, comment: '内容'
t.string 'poster', limit: 50, null: false, comment: 'Slackのユーザー名'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

複数テーブルでposterが登場するのであれば、postersテーブルを作って、そのテーブルとのFKを持った方がいいかな!と思いました!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

コメントありがとうございます!
現状、画面側でmemoやcommentを作成するときにSlackの投稿者名を文字列で入力するような形なのですが、
そのリクエストボディで受け取ったposterをpostersテーブルに追加するってことですかね?
そしてmemoテーブル、commentテーブルにはposter_idを持つという意味ですか?

サーバ側では、入力されたposterの名前でposterテーブル検索して、存在したらそのidを、存在しなければ新しく追加して、設定という感じですかね?💦

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kuri0616
あーーーー、画面からもメモを登録することあるんですね
であればりくやさんがいうように、以下のようにしないとですね。
今回は工数かかるから自由文字列でもいいと思いますよ!
(progakuメンバーしか使わないし、、、、)

サーバ側では、入力されたposterの名前でposterテーブル検索して、存在したらそのidを、存在しなければ新しく追加して、設定という感じですかね?💦

t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["memo_id"], name: "index_comments_on_memo_id"
Expand Down
1 change: 1 addition & 0 deletions backend/db/schema.rb

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

12 changes: 7 additions & 5 deletions backend/spec/factories/comments.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
#
# Table name: comments
#
# id :bigint not null, primary key
# content(内容) :string(1024) not null
# created_at :datetime not null
# updated_at :datetime not null
# memo_id(メモID) :bigint not null
# id :bigint not null, primary key
# content(内容) :string(1024) not null
# poster(Slackのユーザー名) :string(50) not null
# created_at :datetime not null
# updated_at :datetime not null
# memo_id(メモID) :bigint not null
#
# Indexes
#
Expand All @@ -22,6 +23,7 @@
FactoryBot.define do
factory :comment do
content { 'sample_comment' }
poster { Faker::Name.name }
memo
end
end
44 changes: 39 additions & 5 deletions backend/spec/models/comment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
#
# Table name: comments
#
# id :bigint not null, primary key
# content(内容) :string(1024) not null
# created_at :datetime not null
# updated_at :datetime not null
# memo_id(メモID) :bigint not null
# id :bigint not null, primary key
# content(内容) :string(1024) not null
# poster(Slackのユーザー名) :string(50) not null
# created_at :datetime not null
# updated_at :datetime not null
# memo_id(メモID) :bigint not null
#
# Indexes
#
Expand Down Expand Up @@ -71,6 +72,39 @@
expect(comment).to be_valid
end
end

context 'posterが空文字の場合' do
before { comment.poster = '' }

it 'valid?メソッドがfalseを返し、エラーメッセージが格納される' do
aggregate_failures do
comment.valid?
expect(comment.errors.full_messages).to eq ['Slackでの投稿者名を入力してください']
end
end
end

context 'posterがnilの場合' do
before { comment.poster = nil }

it 'valid?メソッドがfalseを返し、エラーメッセージが格納される' do
aggregate_failures do
expect(comment).not_to be_valid
expect(comment.errors.full_messages).to eq ['Slackでの投稿者名を入力してください']
end
end
end

context 'posterが50文字以上の場合' do
before { comment.poster = 'a' * 51 }

it 'valid?メソッドがfalseを返し、エラーメッセージが格納される' do
aggregate_failures do
expect(comment).not_to be_valid
expect(comment.errors.full_messages).to eq ['Slackでの投稿者名は50文字以内で入力してください']
end
end
end
end

describe 'アソシエーションのテスト' do
Expand Down
7 changes: 5 additions & 2 deletions backend/spec/requests/comments_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
describe 'POST /memos/:memo_id/comments' do
context 'ログイン中かつコンテンツが有効な場合' do
let(:memo) { create(:memo) }
let(:params) { { content: Faker::Lorem.paragraph(sentence_count: 3) } }
let(:params) do
{ content: Faker::Lorem.paragraph(sentence_count: 3),
poster: Faker::Name.name }
end

before { sign_in(user) }

Expand Down Expand Up @@ -34,7 +37,7 @@
post "/memos/#{memo.id}/comments", params: { comment: params }, as: :json
end.not_to change(Comment, :count)
expect(response).to have_http_status(:unprocessable_content)
expect(response.parsed_body['errors']).to eq(['内容を入力してください'])
expect(response.parsed_body['errors']).to eq %w[内容を入力してください Slackでの投稿者名を入力してください]
end
end
end
Expand Down
Loading