-
Notifications
You must be signed in to change notification settings - Fork 41
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
課題 (ServerSide Challenge2) 電気料金のシミュレーションアプリ #68
base: master
Are you sure you want to change the base?
Conversation
fix: CSVアップロード時のエラーメッセージを改善
# 現在と次の値のセットのループ | ||
# ループの最後の(next_feeがない)場合は処理されない仕様なので、最後の値は別途計算する | ||
usage_fees.each_cons(2) do |current_fee, next_fee| | ||
next unless usage_kwh > current_fee.min_usage |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
next unless usage_kwh > current_fee.min_usage | |
next if usage_kwh <= current_fee.min_usage |
の方が可読性に優れているように見えますが、unlessを使用した理由はありますか?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
たしかにifの方が可読性が高いですね、この場合ifにすべきだと思いました。
この形で書いておいてなんですが、個人的にはunlessはrubocopで禁止しても良いかなと思っています。
(条件がbooleanの入った変数1つのみであればアリかなとも思います)
contract_ampere = params[:contract_ampere].to_i | ||
usage_kwh = params[:usage_kwh].to_i |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
frontendの実装的にはintegerが来ることが自明ですが、backendのバリデーションとしてこのタイミングでintegerに変換することは適当でしょうか?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
to_iだと不正な文字列の場合に0に直されてしまうので、Integerで変換した上で例外処理で400を返してあげるべきでした。
入力値バリデーションが長くなっているので、Serviceクラスにバリデーションを移動するか、こちらもFormオブジェクトにしてバリデーションしてもよいかと思いました。
usage_fees = plan.electricity_plan_usage_fees.where(min_usage: ...usage_kwh).order(min_usage: :asc) | ||
|
||
# 従量料金設定が1つしかない場合はそのまま掛け算すれば良い | ||
return usage_kwh * usage_fees.last.fee if usage_fees.size == 1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TEP従量電灯で、121kWhでリクエストすると使用量は19.88 * 121と計算されそうですが、これは正しい実装でしょうか?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
whereの条件が間違っていますね、終点を含む(..usage_kwh
)形が正しかったです。。
概要
ServerSide Challenge2 の課題です。
電気料金のシミュレーションアプリを作成しました。
RailsのAPIとAstroのフロントエンドで構成されています。
1.-.-.Microsoft.Edge.Beta.2025-02-10.00-43-57.mp4
機能
テーブル構成
実装上気を付けたポイント
やっていないこと