-
Notifications
You must be signed in to change notification settings - Fork 46
【チャレンジ課題】電気料金を返すAPIの実装 monakam01 #73
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
Open
monakam01
wants to merge
33
commits into
enechange:master
Choose a base branch
from
monakam01:dev_branch
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
33 commits
Select commit
Hold shift + click to select a range
ce0d022
add api routes
nkmr-mty f0863ab
add api controller with test code
nkmr-mty 5408876
add gem active_hash
51405de
add csv files
eaadf5a
add models
4970ab2
rename controller, add codes
74a4b2f
add secret_key_base for production
771639c
update secret_key_base for production
46c3d9f
revise amp validatemethod, correct method name for error processing
ec7f650
add rounding decimal process, separate provider management data from …
4cc5b0e
add provider model
8eb6b5c
create migration, seed file, executed migration
e897b3c
rename existing model with 'csv' suffix, add provider model
f535198
add model for db management
2371165
correct seed.rb
061e019
change data reference from CSV to DB on processing calculation
2cdbfd2
add rubocop
5909b8b
rubocop code correction
78237d9
remove csv data and models
b9f9e59
add empty lines
14325c5
restore credentials.yml.enc
e48eade
correct meter charge amount calculation
56818cc
correct rouding calculation, correct single meter_rate_charge pattern…
5f7d63e
change column provider_name and plan_name to name
517b890
correct meter_rate calculation condition for single meter charge step…
519b5a6
rubocop code correction: db/seeds.rb
db2b5d6
add service class, spread processing respoisibiities from controller …
e1890a7
prevent n+1
a0d952f
add rspec gem
18d89ba
add spec
ce69d19
change api method post to get
344ac7d
truncate total amount
f437eb7
remove active_hash
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| --require spec_helper |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| Documentation: | ||
| Enabled: false | ||
| MethodLength: | ||
| CountComments: true | ||
| Max: 25 | ||
| Style/FrozenStringLiteralComment: | ||
| Enabled: false | ||
| Metrics: | ||
| Enabled: false | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
serverside_challenge_2/challenge/app/controllers/api/v1/power_supply_plan_controller.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Api | ||
| module V1 | ||
| class PowerSupplyPlanController < ApplicationController | ||
| def simulate_all | ||
| simulator = PowerSupplyPlanSimulator.new(params[:amp], params[:meter_rate]) | ||
| result = simulator.call | ||
|
|
||
| return render json: [result] if result.is_a?(Hash) && result.key?(:error) | ||
|
|
||
| render json: result | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| class BasicCharge < ApplicationRecord | ||
| belongs_to :power_supply_plan | ||
| end |
27 changes: 27 additions & 0 deletions
27
serverside_challenge_2/challenge/app/models/meter_rate_charge.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| class MeterRateCharge < ApplicationRecord | ||
| belongs_to :power_supply_plan | ||
|
|
||
| def calculate_charge(meter_rate) | ||
| min_rate = min_meter_rate | ||
| max_rate = max_meter_rate | ||
| step_price = price | ||
| return 0 if min_rate > meter_rate | ||
|
|
||
| if max_rate.nil? | ||
| return step_price * meter_rate if min_rate.zero? | ||
|
|
||
| return step_price * (meter_rate - min_rate + 1) | ||
| end | ||
| if max_rate.to_i < meter_rate.to_i | ||
| return step_price * (max_rate - min_rate) if min_rate.zero? | ||
|
|
||
| step_price * (max_rate - min_rate + 1) | ||
| else | ||
| return step_price * (meter_rate - min_rate) if min_rate.zero? | ||
|
|
||
| step_price * (meter_rate - min_rate + 1) | ||
| end | ||
| end | ||
| end |
25 changes: 25 additions & 0 deletions
25
serverside_challenge_2/challenge/app/models/power_supply_plan.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| class PowerSupplyPlan < ApplicationRecord | ||
| belongs_to :provider | ||
| has_many :meter_rate_charges | ||
| has_many :basic_charges | ||
|
|
||
| def calculate_total_amount(amp, meter_rate) | ||
| basic_charge_record = basic_charges.where(amp: amp) | ||
| return nil if basic_charge_record.blank? | ||
|
|
||
| basic_charge_amount = basic_charge_record.first.price.to_f | ||
| meter_rate_charge_amount = calculate_meter_rate_charge(meter_rate) | ||
| (basic_charge_amount + meter_rate_charge_amount).floor(0).to_i | ||
| end | ||
|
|
||
| def calculate_meter_rate_charge(meter_rate) | ||
| total_meter_rate_charges = 0 | ||
|
|
||
| meter_rate_charges.each do |step| | ||
| total_meter_rate_charges += step.calculate_charge(meter_rate.to_i) | ||
| end | ||
| total_meter_rate_charges | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| class Provider < ApplicationRecord | ||
| has_many :power_supply_plans | ||
| end |
60 changes: 60 additions & 0 deletions
60
serverside_challenge_2/challenge/app/services/power_supply_plan_simulator.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| class PowerSupplyPlanSimulator | ||
| VALID_AMPS = [10, 15, 20, 30, 40, 50, 60].freeze | ||
|
|
||
| def initialize(amp, meter_rate) | ||
| @amp = amp | ||
| @meter_rate = meter_rate | ||
| @errors = [] | ||
| end | ||
|
|
||
| def call | ||
| unless validate_params | ||
| return { | ||
| error: { | ||
| code: 400, | ||
| message: @errors.join('\n') | ||
| } | ||
| } | ||
| end | ||
|
|
||
| result = [] | ||
| PowerSupplyPlan.includes(:provider, :meter_rate_charges, :basic_charges).each do |plan| | ||
| calculate_result = plan.calculate_total_amount(@amp, @meter_rate) | ||
| next unless calculate_result | ||
|
|
||
| result << { | ||
| "provider_name": plan.provider.name, | ||
| "plan_name": plan.name, | ||
| "price": calculate_result | ||
| } | ||
| end | ||
| result | ||
| end | ||
|
|
||
| def validate_params | ||
| if @amp.blank? | ||
| @errors << "リクエストパラメータ 'amp' が不足しています。" | ||
| else | ||
| @errors << "リクエストパラメータ 'amp' の値が不正です。'10 / 15 / 20 / 30 / 40 / 50 / 60' のいずれかの値を指定してください。" unless valid_amp?(@amp) | ||
| end | ||
| if @meter_rate.blank? | ||
| @errors << "リクエストパラメータ 'meter_rate' が不足しています。" | ||
| else | ||
| unless numeric?(@meter_rate) && @meter_rate.to_i >= 0 | ||
| @errors << "リクエストパラメータ 'meter_rate' の値が不正です。0以上の整数を指定してください。" | ||
| end | ||
| end | ||
| @errors.blank? | ||
| end | ||
|
|
||
| def numeric?(string) | ||
| Integer(string) | ||
| true | ||
| rescue ArgumentError, TypeError | ||
| false | ||
| end | ||
|
|
||
| def valid_amp?(amp) | ||
| VALID_AMPS.include?(amp.to_i) | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| bZE5VzGsIQoeBEOa79I28cq/Ax0SuIaPhy5+NFktLVZLNGpuzqmkTkAiWc+arbGUDU8aaedg9zUvnBBc0dPrATeWISHtFxVwJMScZQeSW1k7BUgSD6B6YsewlgtpEDFDAXoGZEQoTbM2FY3lDCd0dZF7I1+DsB4I9Z5NamOgOlNEK8gMQD3p3Csqc/mVhmdI3PjBEmblvTjkZIVOdfX1CmqJ/RZm1Lxu3RdNMaiXiOZL7I+oXE1SFfjWm7+CTwb+Vfs6nGMviDHDKh9k3jyb7Vflpx/frY4vYzP8WMzgbJPX4UHmxa7qS51tJ1ECpK265nS1mLsqxL7wRbjyzfUNtXknSSopGD7sDDm7F9ta8/e1nzU79uTH1LKFpWQWGMSbXmSu4EKPu655BA9ckdwnSsf67RYNX7/Nw5pp--9AT1V0z/SBAdVTZ5--JK/4x776vcnN7qGZuylM0g== | ||
| aLpSiYFmK7YeuO/q62KJR/UyXPpBWhH+oS6rzGWSQH/9p53vCpRMvfaUsZSneP9wYMioohjDqHdeY0bpOAXjMPy5suQk7yGANivapLmd2qNLt1wpIYzKGJiEwSP2gQD4Hyo/K6qGF2fchngDFhy0WWAe8Q0kcIM7WzUSV+VilcYylHcHHpdYhjsBx4+7ev2dbWLlYxbYuFSDGklGP85oYBmIH2QIzcYmwHhBkjwzFWvIl3xqL+/8iQlYsFyBsTk3tntgD/Au3kv6qYXQnf3ITXgHTKlaooxLTy9ZYwMOhJvPwtkMsWv9CVY+ACkUi5h9WY+rSHiBVCDHJwnEhwpz8/n4kzGXLuGRburdDZkA1IZb+j7+KzX625Y+x4+zR7P1wc8vJRw+/5O4MSndI28y0KkX15Qd8vMYNrowsXx+0WFtvo8LTh0J6p84b4bJyVkD1bGUpvpz+UADtGfTbuDqqRXNDCRM7NITVYcOg74QKiwi57gzRJgKOYt1KhXMLA5y4V+xo2zBZuTzGAqNS2rHcfwil9lcsmsTOQL7felWv2C2qqUllUo3CqTGNlXo3A24guP25tr3vDm2IrH7V/TLSjJcDAI1JUi35wwrCErgxrwK9eG0Y4w=--97QWVDZ45R2Bjr3G--byi/t6soITzi5vJv5PEsvw== |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
serverside_challenge_2/challenge/db/migrate/20250723134304_create_basic_charges.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| class CreateBasicCharges < ActiveRecord::Migration[7.0] | ||
| def change | ||
| create_table :providers do |t| | ||
| t.string :provider_name, :null => false | ||
| t.string :rounding_amount_method, :null => false | ||
|
|
||
| t.timestamps | ||
| end | ||
|
|
||
| create_table :power_supply_plans do |t| | ||
| t.belongs_to :provider | ||
| t.string :plan_name, :null => false | ||
|
|
||
| t.timestamps | ||
| end | ||
|
|
||
| create_table :meter_rate_charges do |t| | ||
| t.belongs_to :power_supply_plan | ||
| t.integer :min_meter_rate, :null => false | ||
| t.integer :max_meter_rate | ||
| t.decimal :price, precision: 6, scale: 2, :null => false | ||
|
|
||
| t.timestamps | ||
| end | ||
|
|
||
| create_table :basic_charges do |t| | ||
| t.belongs_to :power_supply_plan | ||
| t.integer :amp, :null => false | ||
| t.decimal :price, precision: 6, scale: 2, :null => false | ||
|
|
||
| t.timestamps | ||
| end | ||
| end | ||
| end |
9 changes: 9 additions & 0 deletions
9
...enge_2/challenge/db/migrate/20250726103216_remove_rounding_amount_method_from_provider.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| class RemoveRoundingAmountMethodFromProvider < ActiveRecord::Migration[7.0] | ||
| def up | ||
| remove_column :providers, :rounding_amount_method, :string | ||
| end | ||
|
|
||
| def down | ||
| add_column :providers, :rounding_amount_method, :string | ||
| end | ||
| end |
5 changes: 5 additions & 0 deletions
5
...de_challenge_2/challenge/db/migrate/20250726111141_rename_provider_name_column_to_name.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| class RenameProviderNameColumnToName < ActiveRecord::Migration[7.0] | ||
| def change | ||
| rename_column :providers, :provider_name, :name | ||
| end | ||
| end |
5 changes: 5 additions & 0 deletions
5
...rside_challenge_2/challenge/db/migrate/20250726111346_rename_plan__name_column_to_name.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| class RenamePlanNameColumnToName < ActiveRecord::Migration[7.0] | ||
| def change | ||
| rename_column :power_supply_plans, :plan_name, :name | ||
| end | ||
| end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
全体的に、コントローラーにすべての責務が集中してしまっている印象を受けました。
具体的には、以下のような処理がすべて controller に記述されているため、保守性や可読性の観点から、責務ごとに適切に分離した方が良いと考えています。
現時点で大きく実装を変更するのは大変かと思いますので、「今後リファクタリングする場合に、どのように責務を分けて実装するべきか」について、テキストベースで構いませんので、考えを共有いただけると嬉しいです。
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.
コントローラの処理を最小限にし、処理を分離いたしました。db2b5d6