Skip to content

Comments

shimizu-challenge#76

Open
shimizu0429 wants to merge 8 commits intoenechange:masterfrom
shimizu0429:master
Open

shimizu-challenge#76
shimizu0429 wants to merge 8 commits intoenechange:masterfrom
shimizu0429:master

Conversation

@shimizu0429
Copy link

@shimizu0429 shimizu0429 commented Oct 2, 2025

shimizu-challenge


Note

Introduces electricity pricing API (YAML and DB-backed), supporting models/migrations, config/routes, and deployment/docker setup with minor env and gem updates.

  • API:
    • Add ApiController with GET /api/get_price (YAML) and GET /api/get_price_db (DB) returning computed electricity fees.
  • Domain Models & DB:
    • Implement EnergyPlan (YAML-based), EnergyPlanDb with BasicFee and UnitFee.
    • Add migrations: energy_plans, basic_fees, unit_fees.
  • Configuration:
    • Add config/api_settings.yml and initializer config/initializers/load_settings.rb.
    • Update config/routes.rb to expose API endpoints; add minimal layout and public/index.html test UI.
    • Adjust config/application.rb to include cookies/session/flash middleware.
    • Update config/database.yml (dev DB host/name; production via DATABASE_URL).
    • Tweak config/environments/production.rb (hosts, logging) and config/puma.rb.
  • Build/Deploy:
    • Add .dockerignore, overhaul Dockerfile for production run, add fly.toml.
  • Dependencies:
    • Add rails-html-sanitizer; update Rails and related gems in Gemfile.lock.

Written by Cursor Bugbot for commit 6433bae. This will update automatically on new commits. Configure here.

cursor[bot]

This comment was marked as outdated.

Copy link

@sugita-seiya sugita-seiya left a comment

Choose a reason for hiding this comment

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

ご提出ありがとうございます!
細かい点ですがコメントしましたので、ご確認お願いします。
併せて、追加で3点コメントさせてください。

◽️1つ目
弊社ではテストコードを非常に重視しており、可能な範囲でテストの追加をご対応いただけますと幸いです。
もしご都合などでテスト追加が難しい場合は、ファイル単位でテスト観点を箇条書きで共有いただけますでしょうか。

例:
controllers/api_controller.rb
- 正常系
    - xxxx
- 異常系
    - xxxx

◽️2つ目
Image
計算結果において、電力量としてマイナス値(price: -1)が返却されているため、計算処理の見直しをお願いします。

◽️3つ目
Image

使用量に非常に大きい数値を入力した際、計算結果として不自然に大きな値(例:1.5285e+49 など)が返却されています。
こちらもご確認お願いします。

Choose a reason for hiding this comment

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

コントローラ全体がネストされておらず、不要な空白も多いため、Rubocop を導入してコードスタイルを統一するのがよさそうです。
さらに、ApiController というクラス名は抽象的で、今後 API が増えた際に責務が曖昧になる可能性があります。

Copy link
Author

Choose a reason for hiding this comment

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

ElectricityApiControllerに変更いたしました

Comment on lines +47 to +48
# 基本料金 + 従量料金 を小数点2桁で丸めて返す
(fee + usage_fee).round(2)

Choose a reason for hiding this comment

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

Q

小数点2桁で丸めた理由はなんでしょうか?
実装の意図をお聞きしたいです。

Copy link
Author

Choose a reason for hiding this comment

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

対象プランと料金表の基本料金、従量料金単価ともに少数2桁で表現してありましたのでそれに揃える形で少数2桁で丸めております。

Comment on lines 52 to 54
Rails.logger.error("Error calculating fee: #{e.message}")
-1
end

Choose a reason for hiding this comment

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

IMO

-1 がマジックナンバーになっているように見えます。
特別な意味を持たせている場合は、定数化を検討いただくと、実装意図がより明確になり、コードも読みやすくなりそうです。

Copy link
Author

Choose a reason for hiding this comment

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

\config\initializers\constants.rb を作成し定数としました。

Copy link

@sugita-seiya sugita-seiya left a comment

Choose a reason for hiding this comment

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

資料作成ありがとうございます。

energy_plansテーブルに provider_nameplan_name を持たせる設計だと、同一会社に複数のプランが存在するケースでデータの不整合が発生するリスクがあります。
そのため、DB正規化をした方が良いと考えていますが、設計方針についての考えを教えていただきたいです。

Image

@@ -0,0 +1,25 @@
class Api::ElectricityController < ApplicationController

Choose a reason for hiding this comment

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

Q

こちらのファイルは何のために存在するファイルでしょうか。

Copy link
Author

Choose a reason for hiding this comment

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

どのようなことを試したか残しておりましたが使用していないファイルは削除いたしました

Choose a reason for hiding this comment

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

ありがとうございます。

@@ -0,0 +1,56 @@
# app/models/energy_plan_db.rb

Choose a reason for hiding this comment

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

IMO

不要なファイルは混乱の元になるので削除した方が良いと思いました。

Copy link
Author

Choose a reason for hiding this comment

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

不要なファイルは削除いたしました
energy_plan_db.rbはDBからのデータ取得に使用しております

Choose a reason for hiding this comment

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

ご対応有難うございます。

Comment on lines 6 to 7
get "api/get_price", to: "api#get_price" # JSON API
get "api/get_price_db", to: "api#get_price_db" # JSON API

Choose a reason for hiding this comment

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

Q

APIの設計的にGET api/get_priceというpathのget_部分が冗長だと思いました。
また、このパスは何かの価格を取得するということはわかりますが、何をするAPIなのか不明確だと思いました。
お考えお聞きできますでしょうか。

Copy link
Author

Choose a reason for hiding this comment

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

get_priceとget_price_dbとあり、get_priceは設定値をYAML、get_price_dbは設定値をDBから取得し計算しております。

Choose a reason for hiding this comment

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

ご回答ありがとうございます。修正などは不要です。

@@ -0,0 +1,64 @@
class ApiController < ApplicationController
def get_price

Choose a reason for hiding this comment

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

一つのControllerに3つの異なるpathのget actionが含まれています。
ファイル分割した方が良いと思いました。

Copy link
Author

Choose a reason for hiding this comment

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

今まではControllerに複数のアクションを持つような設計をしておりました。
Rubyでの開発経験が無いのでRubyでは基本1つのアクションであったり御社の規約がそのようであれば合わせます。

Choose a reason for hiding this comment

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

ご回答ありがとうございます。

Rubyでの開発経験が無いので
こちら伺っています。修正などは不要です。

Comment on lines +2 to +45
- provider_name: 東京電力エナジーパートナー
plan_name: 従量電灯B
basic_fee:
10: 286.0
15: 429.0
20: 572.0
30: 858.0
40: 1144.0
50: 1430.0
60: 1716.0
unit_fee:
"0-120": 19.88
"121-300": 26.48
"301-0": 30.57

- provider_name: 東京電力エナジーパートナー
plan_name: スタンダードS
basic_fee:
10: 311.75
15: 467.63
20: 623.50
30: 935.25
40: 1247.00
50: 1558.75
60: 1870.50
unit_fee:
"0-120": 29.80
"121-300": 36.40
"301-0": 40.49

- provider_name: 東京ガス
plan_name: ずっとも電気1
basic_fee:
30: 858.0
40: 1144.0
50: 1430.0
60: 1716.0
unit_fee:
"0-140": 23.67
"141-350": 23.88
"351-0": 26.41

- provider_name: Looopでんき
plan_name: おうちプラン

Choose a reason for hiding this comment

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

Q

developmentとproductionで同一のデータ定義をしている理由はありますでしょうか。

Copy link
Author

Choose a reason for hiding this comment

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

developmentはローカル環境、productionはサーバー環境での設定値になりますので同じ値でも分けて定義しております

Choose a reason for hiding this comment

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

承知しました。修正など不要です。

本番、開発用でそれぞれ定義するとマスタ更新時の手間が増えると思いお聞きしました。
yamlの場合設定の共通化が可能ですので、それを使うと良いと思いました。

unit_fee:
"0-0": 28.8

- provider_name: YAMLtest

Choose a reason for hiding this comment

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

Q

YAMLで計算をクリックした場合の結果にテスト用らしきデータが出てきますが、要件にないので何を想定して出力しているのかをお聞きしたいです。

    {
      "provider_name": "YAMLtest",
      "plan_name": "テスト",
      "price": -1
    }

Copy link
Author

Choose a reason for hiding this comment

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

画面から確認するのにYAMLからデータを取得しているか、DBからデータを取得しているか目に見えて分かるようにYAMLのほうに仮で設定値を追加いたしました。

Choose a reason for hiding this comment

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

承知しました。ご回答ありがとうございます。

# この従量料金が適用される最小使用量 (kWh)
min = uf.min_kwh.to_f
# 最大使用量が未設定(nil) または 0 の場合は「上限なし」として扱う
max = uf.max_kwh.nil? || uf.max_kwh.zero? ? Float::INFINITY : uf.max_kwh.to_f

Choose a reason for hiding this comment

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

Q

max_kwh,min_kwhはUI上整数値受付となっていますがDBとこの処理ではfloatで扱われています。
整数値ではなくfloatで扱っている理由はありますでしょうか。

Copy link
Author

Choose a reason for hiding this comment

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

計算する際にfloatで合わせたら楽かなと思いfloatにしました。特に楽ということもありませんでしたね。

Choose a reason for hiding this comment

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

ご回答ありがとうございます。承知しました。

secret_key_base: 022eeea32ce3537c8caa3d494d34cbb5ea55bc5ff97a6f656a329973dd27c408f335ffca6d39184a8ab695062aa1f3cb5a64eb8c4cea22c39d12c651d0590212

production:
secret_key_base: 022eeea32ce3537c8caa3d494d34cbb5ea55bc5ff97a6f656a329973dd27c408f335ffca6d39184a8ab695062aa1f3cb5a64eb8c4cea22c39d12c651d0590212 No newline at end of file
Copy link

Choose a reason for hiding this comment

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

Bug: Hardcoded Secret Key Across Environments

The secret_key_base is hardcoded and identical across development, test, and production environments. This is a serious security vulnerability. Secret keys are typically unique per environment and not stored in version control, which exposes the production key and can lead to session hijacking and application compromise.

Fix in Cursor Fix in Web


[[services]]
protocol = 'tcp'
internal_port = 8080
Copy link

Choose a reason for hiding this comment

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

Bug: Port Mismatch in Configuration

The fly.toml config sets internal_port to 8080, but the Dockerfile CMD starts the Rails server on port 3000. This port mismatch makes the application unreachable.

Fix in Cursor Fix in Web

database: app_test
host: db
port: 5432
database: challenge_db
Copy link

Choose a reason for hiding this comment

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

Bug: Missing Test Environment Configuration

The database configuration is missing the test environment section. Rails applications rely on a separate test database, and its absence will lead to test failures.

Fix in Cursor Fix in Web

@shimizu0429
Copy link
Author

コメントについて返信させていただきます。
■3つのコメントについて
●1つ目について
今までのプロジェクトでは、エンジニアごとに開発サーバーに開発環境を持ち単体テスト、統合開発環境で結合テスト
こちらのテストについてはテスターが行っておりテストコードを作成する機会がありませんでした。
テスト観点を箇条書きで書き出します。

正常系
有効な契約アンペア数がセットされているか(10 / 15 / 20 / 30 / 40 / 50 / 60)
有効な使用量がセットされているか(0~99,999,999)
返却メッセージ無し
異常系

有効な契約アンペア数がセットされているか(10 / 15 / 20 / 30 / 40 / 50 / 60)
無効な使用量がセットされているか(-1, 999,999,999)等
返却メッセージ:対応する基本料金が見つかりません

無効な契約アンペア数がセットされているか(0 / 99)等
有効な使用量がセットされているか(0~99,999,999)
返却メッセージ:使用量が異常値です

無効な契約アンペア数がセットされているか(0 / 99)等
無効な使用量がセットされているか(-1, 999,999,999)等
返却メッセージ:使用量が異常値です

●2つ目について
ずっとも電気1は10Aの設定値がセットされていないのでエラーで返却されます。
エラーが判別しにくいのでエラーがあった場合はmessageにエラーメッセージを返却するようにいたしました。

●3つ目について
使用量には0~99999999の値以外にはエラーとなるよう変更いたしました。
上限は常識的に入るであろう値で考えております。

■energy_plansテーブルについて
会社名の正規化も考えましたがこのプロジェクト規模ですと会社名のみの正規化となりテーブルを分けるよりは
energy_plansで文字列として扱った方が管理しやすいと思い現在の形にいたしました。

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.

4 participants