diff --git a/CHANGELOG.md b/CHANGELOG.md index d0af4c1..9297886 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. - Add `Api::DirectReceipts::Edit` action - Add `Api::DirectReceipts::New` action - Add `Api::DirectReceipts::Update` action +- Add `DirectReceipts::Create` action +- Add `DirectReceipts::Edit` action +- Add `DirectReceipts::New` action +- Add `DirectReceipts::Update` action ### Changed - Allow `require`ing individual presets diff --git a/spec/bill/actions/direct_receipts/create_spec.cr b/spec/bill/actions/direct_receipts/create_spec.cr new file mode 100644 index 0000000..c1e59bc --- /dev/null +++ b/spec/bill/actions/direct_receipts/create_spec.cr @@ -0,0 +1,15 @@ +require "../../../spec_helper" + +describe Bill::DirectReceipts::Create do + it "creates direct receipt" do + response = ApiClient.exec(DirectReceipts::Create, transaction: { + user_id: UserFactory.create.id, + description: "New receipt", + amount: 90 + }) + + # ameba:disable Performance/AnyInsteadOfEmpty + TransactionQuery.new.any?.should be_true + response.status.should eq(HTTP::Status::FOUND) + end +end diff --git a/spec/bill/actions/direct_receipts/edit_spec.cr b/spec/bill/actions/direct_receipts/edit_spec.cr new file mode 100644 index 0000000..1488318 --- /dev/null +++ b/spec/bill/actions/direct_receipts/edit_spec.cr @@ -0,0 +1,14 @@ +require "../../../spec_helper" + +describe Bill::DirectReceipts::Edit do + it "renders edit page" do + user = UserFactory.create + transaction = TransactionFactory.create &.user_id(user.id) + + response = ApiClient.exec(DirectReceipts::Edit.with( + transaction_id: transaction.id + )) + + response.body.should eq("DirectReceipts::EditPage") + end +end diff --git a/spec/bill/actions/direct_receipts/new_spec.cr b/spec/bill/actions/direct_receipts/new_spec.cr new file mode 100644 index 0000000..e5cf923 --- /dev/null +++ b/spec/bill/actions/direct_receipts/new_spec.cr @@ -0,0 +1,9 @@ +require "../../../spec_helper" + +describe Bill::DirectReceipts::New do + it "renders new page" do + response = ApiClient.exec(DirectReceipts::New) + + response.body.should eq("DirectReceipts::NewPage") + end +end diff --git a/spec/bill/actions/direct_receipts/update_spec.cr b/spec/bill/actions/direct_receipts/update_spec.cr new file mode 100644 index 0000000..6270293 --- /dev/null +++ b/spec/bill/actions/direct_receipts/update_spec.cr @@ -0,0 +1,21 @@ +require "../../../spec_helper" + +describe Bill::DirectReceipts::Update do + it "updates direct receipt" do + new_description = "Another receipt" + + user = UserFactory.create + + transaction = TransactionFactory.create &.user_id(user.id) + .description("New receipt") + .status(:draft) + + response = ApiClient.exec( + DirectReceipts::Update.with(transaction_id: transaction.id), + transaction: {description: new_description} + ) + + transaction.reload.description.should eq(new_description) + response.status.should eq(HTTP::Status::FOUND) + end +end diff --git a/spec/support/app/src/actions/direct_receipts/create.cr b/spec/support/app/src/actions/direct_receipts/create.cr new file mode 100644 index 0000000..b0e0f05 --- /dev/null +++ b/spec/support/app/src/actions/direct_receipts/create.cr @@ -0,0 +1,7 @@ +class DirectReceipts::Create < BrowserAction + include Bill::DirectReceipts::Create + + post "/direct-receipts" do + run_operation + end +end diff --git a/spec/support/app/src/actions/direct_receipts/edit.cr b/spec/support/app/src/actions/direct_receipts/edit.cr new file mode 100644 index 0000000..230040c --- /dev/null +++ b/spec/support/app/src/actions/direct_receipts/edit.cr @@ -0,0 +1,8 @@ +class DirectReceipts::Edit < BrowserAction + include Bill::DirectReceipts::Edit + + get "/direct-receipts/:transaction_id/edit" do + operation = UpdateDirectReceipt.new(transaction) + html EditPage, operation: operation + end +end diff --git a/spec/support/app/src/actions/direct_receipts/new.cr b/spec/support/app/src/actions/direct_receipts/new.cr new file mode 100644 index 0000000..18fb480 --- /dev/null +++ b/spec/support/app/src/actions/direct_receipts/new.cr @@ -0,0 +1,8 @@ +class DirectReceipts::New < BrowserAction + include Bill::DirectReceipts::New + + get "/direct-receipts/new" do + operation = ReceiveDirectPayment.new + html NewPage, operation: operation + end +end diff --git a/spec/support/app/src/actions/direct_receipts/update.cr b/spec/support/app/src/actions/direct_receipts/update.cr new file mode 100644 index 0000000..f6492de --- /dev/null +++ b/spec/support/app/src/actions/direct_receipts/update.cr @@ -0,0 +1,7 @@ +class DirectReceipts::Update < BrowserAction + include Bill::DirectReceipts::Update + + patch "/direct-receipts/:transaction_id" do + run_operation + end +end diff --git a/spec/support/app/src/pages/direct_receipts/edit_page.cr b/spec/support/app/src/pages/direct_receipts/edit_page.cr new file mode 100644 index 0000000..bdb0e5b --- /dev/null +++ b/spec/support/app/src/pages/direct_receipts/edit_page.cr @@ -0,0 +1,7 @@ +struct DirectReceipts::EditPage < MainLayout + needs operation : UpdateDirectReceipt + + def content + text "DirectReceipts::EditPage" + end +end diff --git a/spec/support/app/src/pages/direct_receipts/new_page.cr b/spec/support/app/src/pages/direct_receipts/new_page.cr new file mode 100644 index 0000000..310bb39 --- /dev/null +++ b/spec/support/app/src/pages/direct_receipts/new_page.cr @@ -0,0 +1,7 @@ +struct DirectReceipts::NewPage < MainLayout + needs operation : ReceiveDirectPayment + + def content + text "DirectReceipts::NewPage" + end +end diff --git a/src/bill/actions/direct_receipts/create.cr b/src/bill/actions/direct_receipts/create.cr new file mode 100644 index 0000000..c99056f --- /dev/null +++ b/src/bill/actions/direct_receipts/create.cr @@ -0,0 +1,28 @@ +module Bill::DirectReceipts::Create + macro included + # post "/receipts" do + # run_operation + # end + + def run_operation + ReceiveDirectPayment.create(params) do |operation, transaction| + if operation.saved? + do_run_operation_succeeded(operation, transaction.not_nil!) + else + response.status_code = 400 + do_run_operation_failed(operation) + end + end + end + + def do_run_operation_succeeded(operation, transaction) + flash.success = Rex.t(:"action.receipt.create.success") + redirect to: Transactions::Show.with(transaction_id: transaction.id) + end + + def do_run_operation_failed(operation) + flash.failure = Rex.t(:"action.receipt.create.failure") + html NewPage, operation: operation + end + end +end diff --git a/src/bill/actions/direct_receipts/edit.cr b/src/bill/actions/direct_receipts/edit.cr new file mode 100644 index 0000000..b904969 --- /dev/null +++ b/src/bill/actions/direct_receipts/edit.cr @@ -0,0 +1,12 @@ +module Bill::DirectReceipts::Edit + macro included + # get "/receipts/:transaction_id/edit" do + # operation = UpdateDirectReceipt.new(transaction) + # html EditPage, operation: operation + # end + + getter transaction : Transaction do + TransactionQuery.find(transaction_id) + end + end +end diff --git a/src/bill/actions/direct_receipts/new.cr b/src/bill/actions/direct_receipts/new.cr new file mode 100644 index 0000000..f5ebc2f --- /dev/null +++ b/src/bill/actions/direct_receipts/new.cr @@ -0,0 +1,8 @@ +module Bill::DirectReceipts::New + macro included + # get "/receipts/new" do + # operation = ReceiveDirectPayment.new + # html NewPage, operation: operation + # end + end +end diff --git a/src/bill/actions/direct_receipts/update.cr b/src/bill/actions/direct_receipts/update.cr new file mode 100644 index 0000000..7d5136f --- /dev/null +++ b/src/bill/actions/direct_receipts/update.cr @@ -0,0 +1,35 @@ +module Bill::DirectReceipts::Update + macro included + # patch "/receipts/:transaction_id" do + # run_operation + # end + + def run_operation + UpdateDirectReceipt.update( + transaction, + params + ) do |operation, updated_transaction| + if operation.saved? + do_run_operation_succeeded(operation, updated_transaction) + else + response.status_code = 400 + do_run_operation_failed(operation) + end + end + end + + getter transaction : Transaction do + TransactionQuery.find(transaction_id) + end + + def do_run_operation_succeeded(operation, transaction) + flash.success = Rex.t(:"action.receipt.update.success") + redirect to: Transactions::Show.with(transaction_id: transaction.id) + end + + def do_run_operation_failed(operation) + flash.failure = Rex.t(:"action.receipt.update.failure") + html EditPage, operation: operation + end + end +end