-
Notifications
You must be signed in to change notification settings - Fork 37
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
base: master
Are you sure you want to change the base?
【杉浦】サーバーサイドチャレンジ #64
Changes from all commits
2d0e1f6
fbf8c57
ce209c8
0da6997
71d3b24
f6c033e
675c97e
f878631
bb28fbe
b587b0d
7897eb5
4ae8dcf
2f38a6f
361e9ed
4433905
61f7216
02e8d61
55c0bd6
f56dc8f
8048f64
bfe6ec6
e8d7e97
b21efd4
a561a15
f448415
374c00c
aa7a79c
dfdf0be
c6132e2
f85d282
5f2b97a
667295a
d78268d
3715348
ff81652
ab8039c
2732db6
b44d716
ad109cb
83e2963
9c20b5b
8237268
1fe47bd
b50eb03
16eb210
3073ba1
4d25d26
84151f9
cb54d61
abbaaf9
8f5a0b9
85566cb
c7e43e2
cfa68d6
6a2c0ab
7cf00d2
a461768
2d21a1a
56a75f6
1ed43bb
cfeacf9
06bec35
7a9e3ec
8714e1b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 |
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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
--require spec_helper |
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 |
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"] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
FROM ruby:3.1.2 | ||
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 |
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. コントローラーはリクエスト、レスポンスを扱うのに徹する、という設計はいいと思いました。 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. こちらの実装意図は無く手癖になります。 私からの提案としてはbefore_actionを廃止しresponse前に移動致しました。 |
||
end |
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 |
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.
Cloud環境用のMigrationとDB seed実行用のimageです。