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

課題 (ServerSide Challenge2) 電気料金のシミュレーションアプリ #68

Open
wants to merge 23 commits into
base: master
Choose a base branch
from

Conversation

tontoko
Copy link

@tontoko tontoko commented Feb 9, 2025

概要

ServerSide Challenge2 の課題です。
電気料金のシミュレーションアプリを作成しました。

RailsのAPIとAstroのフロントエンドで構成されています。

1.-.-.Microsoft.Edge.Beta.2025-02-10.00-43-57.mp4

機能

テーブル構成

erDiagram
    ELECTRICITY_PROVIDERS {
      bigint id
      string name
      datetime created_at
      datetime updated_at
    }
    ELECTRICITY_PLANS {
      bigint id
      string name
      bigint electricity_provider_id
      datetime created_at
      datetime updated_at
    }
    ELECTRICITY_PLAN_BASIC_FEES {
      bigint id
      integer ampere
      decimal fee
      bigint electricity_plan_id
      datetime created_at
      datetime updated_at
    }
    ELECTRICITY_PLAN_USAGE_FEES {
      bigint id
      integer min_usage
      decimal fee
      bigint electricity_plan_id
      datetime created_at
      datetime updated_at
    }

    ELECTRICITY_PROVIDERS ||--o{ ELECTRICITY_PLANS : "has"
    ELECTRICITY_PLANS ||--o{ ELECTRICITY_PLAN_BASIC_FEES : "has"
    ELECTRICITY_PLANS ||--o{ ELECTRICITY_PLAN_USAGE_FEES : "has"
Loading

実装上気を付けたポイント

  • テーブルの命名
    • ガス会社などを追加した場合に分かりやすい・名前被りが発生しないよう、明示的に電力会社であるという名前にしています。
  • 従量料金のテーブル設計
    • READMEの従量料金の範囲(0-120kWh, 121-300Kwhなど)をそのままそれぞれカラムに当てはめるとレンジ被りのデータ不整合が起きうる設計になってしまうので、各区間の下限のみをカラムに持つようにしました。これであれば各プランの最も高い区間の範囲(東電なら 301-∞kWh)も表現できます。
  • csvデータのインポートには、複数モデルの更新をまとめたフォームオブジェクトを使用しました。

やっていないこと

  • tailwindなどで見た目を整えた方が良いですが、バックエンド課題なこともあり今回は行いませんでした。

# 現在と次の値のセットのループ
# ループの最後の(next_feeがない)場合は処理されない仕様なので、最後の値は別途計算する
usage_fees.each_cons(2) do |current_fee, next_fee|
next unless usage_kwh > current_fee.min_usage
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
next unless usage_kwh > current_fee.min_usage
next if usage_kwh <= current_fee.min_usage

の方が可読性に優れているように見えますが、unlessを使用した理由はありますか?

Copy link
Author

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つのみであればアリかなとも思います)

Comment on lines +6 to +7
contract_ampere = params[:contract_ampere].to_i
usage_kwh = params[:usage_kwh].to_i
Copy link
Collaborator

@RyuyaIshibashi RyuyaIshibashi Feb 11, 2025

Choose a reason for hiding this comment

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

frontendの実装的にはintegerが来ることが自明ですが、backendのバリデーションとしてこのタイミングでintegerに変換することは適当でしょうか?

Copy link
Author

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オブジェクトにしてバリデーションしてもよいかと思いました。

Comment on lines +33 to +36
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
Copy link
Collaborator

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と計算されそうですが、これは正しい実装でしょうか?

Copy link
Author

Choose a reason for hiding this comment

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

whereの条件が間違っていますね、終点を含む(..usage_kwh)形が正しかったです。。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants