-
Notifications
You must be signed in to change notification settings - Fork 11
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
Feature/creating calculators constructor #534
base: develop
Are you sure you want to change the base?
Changes from 58 commits
a8708fe
212ffdc
18486f8
69c8d5c
aa79011
cf4c354
c9d5d87
7cfca42
7e8bbf1
e7287bb
456d188
95acb0c
e4b32b2
9e36431
5e4f0d5
88efc69
87b11e1
2388ad9
fa2f751
09b6800
3704ab9
3be2d64
12802a6
6cbd864
412b789
9dd58bd
e6ee05a
081147b
485a656
fc272ca
70ae996
c8c24be
2dd2787
97e7e72
fd842e5
acd0655
bc14bd4
5addf7c
a74832b
e895ef1
d53eb19
e27c50f
d41413d
c234958
f5a9b52
c87ad72
946704e
eb34f09
296967e
41f7f6e
0e1be29
40a2ace
02977de
48ba5f8
3c58823
043c1d5
04920a5
50cc0db
e299b8b
5dc88bf
9a393fc
64af587
5036409
53941e0
be27d91
45571d9
d6c0000
aa5eff2
973f759
18fdb98
9e2a8b8
49d314d
0bca812
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 |
---|---|---|
@@ -1,13 +1,37 @@ | ||
# frozen_string_literal: true | ||
|
||
class Api::V1::CalculatorsController < ApplicationController | ||
VALUES = [ | ||
{ name: "bought_diapers", result: 8956 }, | ||
{ name: "money_spent", result: 7841 }, | ||
{ name: "garbage_created", result: 342 } | ||
].freeze | ||
|
||
def compute | ||
render json: { result: VALUES } | ||
def calculate | ||
@validation = CalculatorValidator.new(params) | ||
|
||
if @validation.valid? | ||
result = Calculators::CalculateService.new(product_resource, calculator_params).calculate | ||
|
||
render json: result.to_json, status: :ok | ||
else | ||
render( | ||
json: { | ||
error: @validation.error | ||
}, status: :unprocessable_entity | ||
) | ||
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. та одною строкою |
||
end | ||
end | ||
|
||
private | ||
|
||
def collection | ||
Calculator.all | ||
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.
|
||
end | ||
|
||
def resource | ||
collection.friendly.find(params[:slug]) | ||
end | ||
|
||
def calculator_params | ||
params.permit(:period, :price_id) | ||
end | ||
|
||
def product_resource | ||
Product.find(resource.product_id) | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { Controller } from "@hotwired/stimulus"; | ||
import { FetchRequest } from "@rails/request.js"; | ||
import { toastUtils } from "helpers/toast_helper"; | ||
|
||
export default class extends Controller { | ||
static targets = ["period", "priceCategory"]; | ||
static outlets = ["calculationresults"]; | ||
static values = { | ||
url: { | ||
type: String, | ||
default: "en/api/v1/calculators", | ||
}, | ||
}; | ||
|
||
submit(e) { | ||
e.preventDefault(); | ||
|
||
let formData = { | ||
period: this.periodTarget.value, | ||
price_id: this.priceCategoryTarget.value | ||
}; | ||
|
||
const request = new FetchRequest("POST", this.urlValue, { | ||
responseKind: "json", | ||
body: JSON.stringify(formData), | ||
}); | ||
|
||
this.sendRequest(request); | ||
} | ||
|
||
async sendRequest(request) { | ||
const response = await request.perform(); | ||
const result = await response.json; | ||
|
||
if (response.ok) { | ||
this.calculationresultsOutlet.showResults(result); | ||
} else if (response.statusCode == 422) { | ||
toastUtils.showToast(result.error, "error"); | ||
} | ||
} | ||
|
||
getBasicOption(i) { | ||
let option = document.createElement("option"); | ||
|
||
option.value = i; | ||
option.innerText = i; | ||
|
||
return option; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { Controller } from "@hotwired/stimulus"; | ||
|
||
export default class extends Controller { | ||
static targets = [ | ||
"moneySpentResult", | ||
"itemsUsedResult", | ||
]; | ||
|
||
showResults(data) { | ||
this.moneySpentResultTarget.innerHTML = data.moneySpent; | ||
this.itemsUsedResultTarget.innerHTML = data.itemsUsed; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,6 +28,8 @@ class Calculator < ApplicationRecord | |
|
||
has_many :fields, dependent: :destroy | ||
|
||
belongs_to :product | ||
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. Посортовано асоціації не гуд |
||
|
||
accepts_nested_attributes_for :fields, allow_destroy: true | ||
|
||
validates :name, presence: true | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# frozen_string_literal: true | ||
|
||
class Calculators::CalculateService | ||
attr_accessor :product, :period, :items_used, :money_spent, :params | ||
|
||
DAYS = { | ||
"day" => 1, | ||
"week" => 7, | ||
"month" => 30.5, | ||
"year" => 365 | ||
}.freeze | ||
|
||
def initialize(product, params) | ||
@product = product | ||
@period = DAYS[params[:period]] | ||
@items_used = 0 | ||
@money_spent = 0 | ||
@params = params | ||
end | ||
|
||
def calculate | ||
money_spent = (product.default_usage_per_day * selected_price * period).to_i | ||
items_used = (product.default_usage_per_day * period).to_i | ||
|
||
{ | ||
moneySpent: money_spent, | ||
itemsUsed: items_used | ||
} | ||
end | ||
|
||
private | ||
|
||
def selected_price | ||
product.prices.find(params[:price_id]).sum | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# frozen_string_literal: true | ||
|
||
class DiaperCalculatorValidator | ||
attr_reader :params, :error | ||
|
||
def initialize(params) | ||
@params = params | ||
end | ||
|
||
def valid? | ||
childs_years = params.fetch(:childs_years, nil) | ||
childs_months = params.fetch(:childs_months, nil) | ||
|
||
if childs_years.blank? && childs_months.blank? | ||
@error = I18n.t("calculators.errors.year_and_month_error_msg") | ||
false | ||
elsif childs_years.blank? | ||
@error = I18n.t("calculators.errors.year_error_msg") | ||
false | ||
elsif childs_months.blank? | ||
@error = I18n.t("calculators.errors.month_error_msg") | ||
false | ||
else | ||
true | ||
end | ||
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. тоді і цей метод буде, і гарний спосіб описання валідацій 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. |
||
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.
чому не svg?
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.
я просто замалював старий малюнок і зверху намалював сміття щоб хоч +- було ясно що показує