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

【杉浦】サーバーサイドチャレンジ #64

Open
wants to merge 64 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
64 commits
Select commit Hold shift + click to select a range
2d0e1f6
rubocopの追加
Oct 27, 2024
fbf8c57
rspecの導入
Oct 27, 2024
ce209c8
BreakManの導入
Oct 27, 2024
0da6997
ERD出力の対応
Oct 27, 2024
71d3b24
開発環境用のdotenv対応
Oct 27, 2024
f6c033e
DB接続情報を環境変数で指定可能に対応
Oct 27, 2024
675c97e
電力料金表CSVファイルの追加
Nov 2, 2024
f878631
fix: ローカル実行環境のdbのportを指定
Nov 2, 2024
bb28fbe
Railsのtimezone設定の追加
Nov 2, 2024
b587b0d
Factorybotの設定追加
Nov 2, 2024
7897eb5
ElectricPowerCompany(電力会社) modelの追加
Nov 2, 2024
4ae8dcf
Plan modelの追加
Nov 2, 2024
2f38a6f
Rubocopによる自動修正
Nov 2, 2024
361e9ed
Plan modelに対するelectric_power_companyのvalidationを追加
Nov 2, 2024
4433905
BasicPrice modelの追加
Nov 2, 2024
61f7216
MeasuredRate modelの追加
Nov 2, 2024
02e8d61
model specのcontextの階層修正
Nov 2, 2024
55c0bd6
db schema.rbの追加
Nov 2, 2024
f56dc8f
Seedデータの投入対応
Nov 2, 2024
8048f64
CSVデータ投入の妥当性検証の追加
Nov 2, 2024
bfe6ec6
ER図の出力
Nov 2, 2024
e8d7e97
rack-timeout, rack-corsの追加
Nov 2, 2024
b21efd4
電力会社のmodel名をElectricPowerCompanyからProviderへ変更
Nov 2, 2024
a561a15
A数に合致する基本料金の収集methodの追加
Nov 2, 2024
f448415
fix: 基本料金の計算methodの名前を変更
Nov 3, 2024
374c00c
従量料金の計算methodの追加
Nov 3, 2024
aa7a79c
プラン事の基本料金、従量料金の計算するmethodを実装
Nov 3, 2024
dfdf0be
API用のライブラリを追加
Nov 3, 2024
c6132e2
計算用APIを追加
Nov 3, 2024
f85d282
Seedのデータを投入しエラーが発生しないことのテストを追加
Nov 3, 2024
5f2b97a
openapi定義の出力
Nov 3, 2024
667295a
openapiの設定値更新
Nov 3, 2024
d78268d
react projectの追加
Nov 3, 2024
3715348
frontendのapiリクエストの処理を追加
Nov 3, 2024
ff81652
CORSの設定の変更
Nov 3, 2024
ab8039c
apiのディレクトリ構成を変更
Nov 3, 2024
2732db6
docs dirにerdとopenapiを設置
Nov 3, 2024
b44d716
infra構成図の追加
Nov 3, 2024
ad109cb
frontendnお最低限のUIを修正
Nov 4, 2024
83e2963
infra構成図のpng形式を登録
Nov 4, 2024
9c20b5b
frontendのローカル実行環境をdocker化
Nov 4, 2024
8237268
デプロイ環境で共通のDocker fileを利用するために、Dockerfileのコマンドを対応
Nov 5, 2024
1fe47bd
yamlの仕様で値に記号が入った場合にエラーが発生する場合の対応
Nov 5, 2024
b50eb03
デプロイ環境用のsecret情報の作成
Nov 5, 2024
16eb210
deploy環境用のapi URLの修正
Nov 5, 2024
3073ba1
seedファイルのメッセージ追加
Nov 5, 2024
4d25d26
migration & seedで利用するためのcontainer image
Nov 5, 2024
84151f9
gitignoreの追加
Nov 5, 2024
cb54d61
インフラ構成図の更新
Nov 5, 2024
abbaaf9
インフラ構成図の変更
Nov 6, 2024
8f5a0b9
dir構成を元に戻す
Nov 6, 2024
85566cb
Merge pull request #2 from kunihiko-sugiura/fix/change_dir
kunihiko-sugiura Nov 6, 2024
c7e43e2
Readmeの更新
Nov 6, 2024
cfa68d6
Seed用CSVの行が増えた場合にエラーが発生するテストを修正
Nov 6, 2024
6a2c0ab
Frontendの電気使用量の入力可能桁数を4から5へ変更
Nov 6, 2024
7cf00d2
fix: Plan modelのcheck_parameters methodをprivateに変更
Nov 10, 2024
a461768
Revert "fix: Plan modelのcheck_parameters methodをprivateに変更"
Nov 10, 2024
2d21a1a
fix: Plan.check_parameters methodをprivateに変更する
Nov 10, 2024
56a75f6
fix: APIリクエストのエラー時のメッセージの検証をincludeから厳密にするためeqへ変更
Nov 10, 2024
1ed43bb
fix: DockerfileのCMDが複数回指定されている問題の対応
Nov 10, 2024
cfeacf9
fix: エラーメッセージが英語と日本語が混在していたため日本語に統一
Nov 11, 2024
06bec35
fix: 電気使用量の範囲チェックの簡略化
Nov 11, 2024
7a9e3ec
fix: 「のいずれかを指定してください。」のメッセージ比較が他のテストと統一されていないtestの対応
Nov 12, 2024
8714e1b
fix: Frontendの計算ボタンの有効状態の判定を==から===へ変更
Nov 12, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions serverside_challenge_2/challenge/.env.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# DB設定
DATABASE_HOST=db
DATABASE_PORT=5432
DATABASE_USER=postgres
DATABASE_PASSWORD=password

# サービスのドメイン設定
SERVICE_PROTOCOL=http
FRONTEND_DOMAIN=localhost:5173
7 changes: 7 additions & 0 deletions serverside_challenge_2/challenge/.env.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
DATABASE_HOST=db
DATABASE_PORT=5432
DATABASE_USER=postgres
DATABASE_PASSWORD=password

SERVICE_PROTOCOL=http
FRONTEND_DOMAIN=localhost:5173
4 changes: 4 additions & 0 deletions serverside_challenge_2/challenge/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,7 @@

# Ignore master key for decrypting credentials and more.
/config/master.key

# Application
.env
/config/credentials/production.key
1 change: 1 addition & 0 deletions serverside_challenge_2/challenge/.rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--require spec_helper
5 changes: 5 additions & 0 deletions serverside_challenge_2/challenge/.rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Omakase Ruby styling for Rails
inherit_gem:
rubocop-rails-omakase: rubocop.yml

# Your own specialized rules go here
6 changes: 4 additions & 2 deletions serverside_challenge_2/challenge/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
FROM ruby:3.1.2
RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs vim
RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs vim graphviz
RUN mkdir /app
WORKDIR /app
ADD Gemfile /app/Gemfile
ADD Gemfile.lock /app/Gemfile.lock
RUN bundle install
ADD . /app
ADD . /app

CMD ["./start-api.sh"]
8 changes: 8 additions & 0 deletions serverside_challenge_2/challenge/Dockerfile.migration
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FROM ruby:3.1.2
Copy link
Author

Choose a reason for hiding this comment

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

Cloud環境用のMigrationとDB seed実行用のimageです。

RUN apt-get update -qq && apt-get install -y build-essential libpq-dev
RUN mkdir /app
WORKDIR /app
ADD Gemfile /app/Gemfile
ADD Gemfile.lock /app/Gemfile.lock
RUN bundle install
ADD . /app
16 changes: 13 additions & 3 deletions serverside_challenge_2/challenge/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ ruby "3.1.2"

# Bundle edge Rails instead: gem "rails", github: "rails/rails", branch: "main"
gem "rails", "~> 7.0.8"

gem "rails-i18n", "~> 7.0", ">= 7.0.10"
# Use postgresql as the database for Active Record
gem "pg", "~> 1.1"

Expand Down Expand Up @@ -34,15 +34,25 @@ gem "bootsnap", require: false
# gem "image_processing", "~> 1.2"

# Use Rack CORS for handling Cross-Origin Resource Sharing (CORS), making cross-origin AJAX possible
# gem "rack-cors"
gem "rack-cors", "~> 2.0", ">= 2.0.2"
gem "rack-timeout", "~> 0.7.0"
gem "jb", "~> 0.8.2"

group :development, :test do
# See https://guides.rubyonrails.org/debugging_rails_applications.html#debugging-with-the-debug-gem
gem "debug", platforms: %i[ mri mingw x64_mingw ]
gem "rspec-rails"
gem "factory_bot_rails"
gem "database_cleaner-active_record"
gem "dotenv-rails"
gem "rspec-openapi"
end

group :development do
# Speed up commands on slow machines / big apps [https://github.com/rails/spring]
# gem "spring"
gem "rubocop-rails-omakase", require: false
gem "rubocop-rspec"
gem "brakeman", require: false
gem "rails-erd", "~> 1.7", ">= 1.7.2"
end

110 changes: 110 additions & 0 deletions serverside_challenge_2/challenge/Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,34 @@ GEM
i18n (>= 1.6, < 2)
minitest (>= 5.1)
tzinfo (~> 2.0)
ast (2.4.2)
bootsnap (1.18.3)
msgpack (~> 1.2)
brakeman (6.2.2)
racc
builder (3.2.4)
choice (0.2.0)
concurrent-ruby (1.2.3)
crass (1.0.6)
database_cleaner-active_record (2.2.0)
activerecord (>= 5.a)
database_cleaner-core (~> 2.0.0)
database_cleaner-core (2.0.1)
date (3.3.4)
debug (1.9.1)
irb (~> 1.10)
reline (>= 0.3.8)
diff-lcs (1.5.1)
dotenv (3.1.4)
dotenv-rails (3.1.4)
dotenv (= 3.1.4)
railties (>= 6.1)
erubi (1.12.0)
factory_bot (6.5.0)
activesupport (>= 5.0.0)
factory_bot_rails (6.4.4)
factory_bot (~> 6.5)
railties (>= 5.0.0)
globalid (1.2.1)
activesupport (>= 6.1)
i18n (1.14.1)
Expand All @@ -84,6 +102,9 @@ GEM
irb (1.11.2)
rdoc
reline (>= 0.4.2)
jb (0.8.2)
json (2.7.4)
language_server-protocol (3.17.0.3)
loofah (2.22.0)
crass (~> 1.0.2)
nokogiri (>= 1.12.0)
Expand All @@ -109,17 +130,26 @@ GEM
nio4r (2.7.0)
nokogiri (1.16.2-aarch64-linux)
racc (~> 1.4)
nokogiri (1.16.2-arm64-darwin)
racc (~> 1.4)
nokogiri (1.16.2-x86_64-linux)
racc (~> 1.4)
parallel (1.26.3)
parser (3.3.5.0)
ast (~> 2.4.1)
racc
pg (1.5.4)
psych (5.1.2)
stringio
puma (5.6.8)
nio4r (~> 2.0)
racc (1.7.3)
rack (2.2.8)
rack-cors (2.0.2)
rack (>= 2.0.0)
rack-test (2.1.0)
rack (>= 1.3)
rack-timeout (0.7.0)
rails (7.0.8)
actioncable (= 7.0.8)
actionmailbox (= 7.0.8)
Expand All @@ -138,41 +168,121 @@ GEM
activesupport (>= 5.0.0)
minitest
nokogiri (>= 1.6)
rails-erd (1.7.2)
activerecord (>= 4.2)
activesupport (>= 4.2)
choice (~> 0.2.0)
ruby-graphviz (~> 1.2)
rails-html-sanitizer (1.6.0)
loofah (~> 2.21)
nokogiri (~> 1.14)
rails-i18n (7.0.10)
i18n (>= 0.7, < 2)
railties (>= 6.0.0, < 8)
railties (7.0.8)
actionpack (= 7.0.8)
activesupport (= 7.0.8)
method_source
rake (>= 12.2)
thor (~> 1.0)
zeitwerk (~> 2.5)
rainbow (3.1.1)
rake (13.1.0)
rdoc (6.6.2)
psych (>= 4.0.0)
regexp_parser (2.9.2)
reline (0.4.2)
io-console (~> 0.5)
rexml (3.3.9)
rspec-core (3.13.2)
rspec-support (~> 3.13.0)
rspec-expectations (3.13.3)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.13.0)
rspec-mocks (3.13.2)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.13.0)
rspec-openapi (0.18.3)
actionpack (>= 5.2.0)
rails-dom-testing
rspec-core
rspec-rails (7.0.1)
actionpack (>= 7.0)
activesupport (>= 7.0)
railties (>= 7.0)
rspec-core (~> 3.13)
rspec-expectations (~> 3.13)
rspec-mocks (~> 3.13)
rspec-support (~> 3.13)
rspec-support (3.13.1)
rubocop (1.67.0)
json (~> 2.3)
language_server-protocol (>= 3.17.0)
parallel (~> 1.10)
parser (>= 3.3.0.2)
rainbow (>= 2.2.2, < 4.0)
regexp_parser (>= 2.4, < 3.0)
rubocop-ast (>= 1.32.2, < 2.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 2.4.0, < 3.0)
rubocop-ast (1.32.3)
parser (>= 3.3.1.0)
rubocop-minitest (0.36.0)
rubocop (>= 1.61, < 2.0)
rubocop-ast (>= 1.31.1, < 2.0)
rubocop-performance (1.22.1)
rubocop (>= 1.48.1, < 2.0)
rubocop-ast (>= 1.31.1, < 2.0)
rubocop-rails (2.27.0)
activesupport (>= 4.2.0)
rack (>= 1.1)
rubocop (>= 1.52.0, < 2.0)
rubocop-ast (>= 1.31.1, < 2.0)
rubocop-rails-omakase (1.0.0)
rubocop
rubocop-minitest
rubocop-performance
rubocop-rails
rubocop-rspec (3.2.0)
rubocop (~> 1.61)
ruby-graphviz (1.2.5)
rexml
ruby-progressbar (1.13.0)
stringio (3.1.0)
thor (1.3.0)
timeout (0.4.1)
tzinfo (2.0.6)
concurrent-ruby (~> 1.0)
unicode-display_width (2.6.0)
websocket-driver (0.7.6)
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.5)
zeitwerk (2.6.13)

PLATFORMS
aarch64-linux
arm64-darwin-24
x86_64-linux

DEPENDENCIES
bootsnap
brakeman
database_cleaner-active_record
debug
dotenv-rails
factory_bot_rails
jb (~> 0.8.2)
pg (~> 1.1)
puma (~> 5.0)
rack-cors (~> 2.0, >= 2.0.2)
rack-timeout (~> 0.7.0)
rails (~> 7.0.8)
rails-erd (~> 1.7, >= 1.7.2)
rails-i18n (~> 7.0, >= 7.0.10)
rspec-openapi
rspec-rails
rubocop-rails-omakase
rubocop-rspec
tzinfo-data

RUBY VERSION
Expand Down
43 changes: 19 additions & 24 deletions serverside_challenge_2/challenge/README.md
Original file line number Diff line number Diff line change
@@ -1,24 +1,19 @@
# README

This README would normally document whatever steps are necessary to get the
application up and running.

Things you may want to cover:

* Ruby version

* System dependencies

* Configuration

* Database creation

* Database initialization

* How to run the test suite

* Services (job queues, cache servers, search engines, etc.)

* Deployment instructions

* ...
## 環境構築
- Dockerの起動
```sh
$ cd serverside_challenge_2/challenge
$ docker compose build
$ docker compose up
$ docker exec -it challenge-api /bin/bash
```

- データベースの初期化
```sh
root@a35275a66e97:/app# rails db:create
root@a35275a66e97:/app# rails db:migrate
root@a35275a66e97:/app# rails db:seed
```

- アプリケーションの実行
ブラウザにて以下のURLにアクセス
http://localhost:5173/
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Api::Electricity::CalculateController < ApplicationController
before_action :price_calculate, only: :create

def create
if @prices[:errors].present?
render json: @prices[:errors], status: :bad_request
else
render "electricity/calculate/create", status: :ok
end
end

private

def create_params
params.permit(:amperage, :electricity_usage_kwh)
end

def price_calculate
@prices = Plan.calc_prices(create_params[:amperage], create_params[:electricity_usage_kwh])
end
Comment on lines +18 to +20
Copy link

Choose a reason for hiding this comment

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

コントローラーはリクエスト、レスポンスを扱うのに徹する、という設計はいいと思いました。
action が一つだけだし only もついているので害はないですが、このメソッドを before_action にしたのはどういった意図からでしょうか?

Copy link
Author

@kunihiko-sugiura kunihiko-sugiura Nov 10, 2024

Choose a reason for hiding this comment

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

こちらの実装意図は無く手癖になります。
普段はshow,update,destroy時の共通のfind処理をbefore_actionで実装しています。
検索処理は共通処理ではないですがbefore_actionで同様に実装する事があり、今回同様に実装していました。

私からの提案としてはbefore_actionを廃止しresponse前に移動致しました。
こちら別途Pull Requestとして作成致しました。

end
28 changes: 28 additions & 0 deletions serverside_challenge_2/challenge/app/models/basic_price.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# frozen_string_literal: true

class BasicPrice < ApplicationRecord
belongs_to :plan

AMPERAGE_LIST = [ 10, 15, 20, 30, 40, 50, 60 ].freeze
ERR_MESS_INVALID_AMPERAGE = "#{AMPERAGE_LIST.join('/')}のいずれかを指定してください。".freeze

validates :amperage, presence: true, inclusion: { in: AMPERAGE_LIST }
validates :price, numericality: { only_numeric: true, greater_than_or_equal_to: 0, less_than_or_equal_to: 99999.99 }
validates :plan, presence: true, uniqueness: { scope: :amperage }

class << self
def calc_prices(amperage)
rows = BasicPrice.where(amperage: amperage)
rows.inject({}) do |sum, row|
sum[row.plan_id] = row.price
sum
end
end

def check_amperage?(amperage)
res = { is_error: !AMPERAGE_LIST.include?(amperage) }
res[:error_object] = { field: "amperage", message: ERR_MESS_INVALID_AMPERAGE } if res[:is_error]
res
end
end
end
Loading