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

個別ダウンロード用のAPIを作成 #146

Merged
merged 10 commits into from
Jul 14, 2018
Merged

Conversation

mori-atsushi
Copy link
Contributor

close #125

なぜ実装する必要があるのか、

  • 画像を一枚一枚ダウンロードするときに、直リンクからだとダウンロードログが残らないため

どんな機能を実装した(したかった)のか

  • ダウンロード用のURLを叩くと、ダウンロードログが保存され、画像が返ってくるAPIを作成

どうやって実装した(したかった)のか

  • download controllerに新規methodを追加

emoji = Emoji.find_by(params[:emoji_id])
emoji.download_logs.create(user: current_user)
image = emoji.image.slack
send_file image.file.path
Copy link

Choose a reason for hiding this comment

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

[Brakeman]

Model attribute used in file name

@@ -8,6 +8,13 @@ class Api::V1::DownloadController < Api::V1::BaseController
ZIP_FILENAME = "emojis.zip".freeze

def index
emoji = Emoji.find_by(params[:emoji_id])
Copy link

Choose a reason for hiding this comment

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

[Brakeman]

Possible SQL injection

@codecov
Copy link

codecov bot commented Jul 9, 2018

Codecov Report

Merging #146 into master will increase coverage by <.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #146      +/-   ##
==========================================
+ Coverage   99.88%   99.88%   +<.01%     
==========================================
  Files          35       35              
  Lines         843      873      +30     
==========================================
+ Hits          842      872      +30     
  Misses          1        1
Impacted Files Coverage Δ
app/models/emoji.rb 100% <100%> (ø) ⬆️
spec/requests/api/v1/donwlod_spec.rb 100% <100%> (ø) ⬆️
app/controllers/api/v1/download_controller.rb 100% <100%> (ø) ⬆️
spec/models/emoji_spec.rb 100% <100%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 0301d71...eea714b. Read the comment docs.

@@ -8,6 +8,13 @@ class Api::V1::DownloadController < Api::V1::BaseController
ZIP_FILENAME = "emojis.zip".freeze

def index
emoji = Emoji.find(params[:emoji_id])
emoji.download_logs.create(user: current_user)
Copy link
Contributor

Choose a reason for hiding this comment

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

download#indexでdownloadがcreateされるのは違和感があります

Copy link
Contributor Author

Choose a reason for hiding this comment

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

createされてるのはdownloadじゃなくてdownload_logsだけど同様?
donwloadされた副作用としてdonwlod logのcreateなんだけど

Copy link
Contributor

Choose a reason for hiding this comment

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

emojis#showdownload#indexって何が違うんだっけ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

emoji#showはemoji情報の取得、jsonが返ってくる
download#indexは画像のダウンロード、image fileが返ってくる

Copy link
Contributor

Choose a reason for hiding this comment

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

emojis#showで返ってくるemoji情報に画像のURL入ってない?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

emojiを参照すればいつもダウンロードログを作るわけじゃないし、今のほうがスッキリしてると思うんだけど。

Copy link
Contributor

Choose a reason for hiding this comment

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

ログを残したいエンドポイントを作るときに毎回controller側に

DownloadLog.create(hogehoge)

って書かないといけないのイケてなくない?って思ってしまう

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ログを残したいのはダウンロードコントローラだけだし、emoji.download_logs.create ってしてるので、emoji modelのメソッド作るのと対して変わらんと思うんだが

Copy link
Contributor Author

Choose a reason for hiding this comment

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

あぁ、

emoji = Emoji.find(params[:emoji_id])
emoji.download_logs.create(user: current_user)

を同時にしたいわけか。

Copy link
Contributor

Choose a reason for hiding this comment

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

Rails(というかMVCアーキテクチャ)はcontrollerをどこまで薄くできるかが腕の見せどころ

DownloadLog.create!(emoji_id: emoji_id, user: current_user)
emojis = DownloadLog.transaction do
emoji_ids.map do |emoji_id|
Emoji.find_with_logging!(emoji_id, current_user)
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

Copy link
Contributor

Choose a reason for hiding this comment

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

N+1を回避するメソッドも生やしたくなってこない?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

もう疲れたんだが…w
結局createは一括でできないからmodelでN+1するかcontrollerでN+1するかの違い?

Copy link
Contributor

@euglena1215 euglena1215 Jul 13, 2018

Choose a reason for hiding this comment

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

createは一括でできないけどfindは一括でできるよねって話
(createもbulk insertできるgem入れれば一括でできるんだけど)
https://qiita.com/amidara/items/2d42e7cd518e6820e083

# models/emoji.rb
  def self.where_with_logging!(condition, user = nil, limit = 100) # 同時ダウンロードできる最大数って指定してるんだっけ?
    emojis = Emoji.where(condition).limit(limit)
    DownloadLog.transaction do
      emojis.each {|emoji| download_logs.create!(emoji: emoji, user: user) }
    end
    emojis
  end

  def self.find_with_logging!(id, user = nil)
    self.where_with_logging!(id: id, user, 1).first
  end

Copy link
Contributor Author

Choose a reason for hiding this comment

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

話はわかるが計算コスト変わらない気がするんよね

Copy link
Contributor

Choose a reason for hiding this comment

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

findのN+1が潰せるのはデカイよ

Copy link
Contributor Author

Choose a reason for hiding this comment

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

早めにリリースしたいので別Issue対応でお願いシャス。 #168

@mori-atsushi mori-atsushi merged commit ca2a5e4 into master Jul 14, 2018
@mori-atsushi mori-atsushi deleted the 125-emoji-download branch July 14, 2018 20:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants