-
Notifications
You must be signed in to change notification settings - Fork 147
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
API v1 financial_transactions endpoints
- Loading branch information
Showing
5 changed files
with
96 additions
and
0 deletions.
There are no files selected for viewing
20 changes: 20 additions & 0 deletions
20
app/controllers/api/v1/financial_transactions_controller.rb
This file contains 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,20 @@ | ||
class Api::V1::FinancialTransactionsController < Api::V1::BaseController | ||
include Concerns::CollectionScope | ||
|
||
before_action :require_ordergroup | ||
|
||
def index | ||
render_collection search_scope | ||
end | ||
|
||
def show | ||
render json: scope.find(params.require(:id)) | ||
end | ||
|
||
private | ||
|
||
def scope | ||
current_ordergroup.financial_transactions | ||
end | ||
|
||
end |
This file contains 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,49 @@ | ||
module Concerns::CollectionScope | ||
extend ActiveSupport::Concern | ||
|
||
private | ||
|
||
def scope | ||
raise NotImplementedError, 'Please override #scope when you use Concerns::CollectionScope' | ||
end | ||
|
||
def default_per_page | ||
20 | ||
end | ||
|
||
def max_per_page | ||
250 | ||
end | ||
|
||
def per_page | ||
# allow max_per_page and default_per_page to be nil as well | ||
if params[:per_page] | ||
[params[:per_page].to_i, max_per_page].compact.min | ||
else | ||
default_per_page | ||
end | ||
end | ||
|
||
def search_scope | ||
s = scope | ||
s = scope.ransack(params[:q]).result(distinct: true) if params[:q] | ||
s = s.page(params[:page].to_i).per(per_page) if per_page && per_page >= 0 | ||
s | ||
end | ||
|
||
def render_collection(scope) | ||
render json: scope, meta: collection_meta(scope) | ||
end | ||
|
||
def collection_meta(scope, extra = {}) | ||
return unless scope.respond_to?(:total_count) && per_page | ||
|
||
{ | ||
page: params[:page].to_i, | ||
per_page: per_page, | ||
total_pages: (scope.total_count / [1, per_page].max).ceil, | ||
total_count: scope.total_count | ||
}.merge(extra) | ||
end | ||
|
||
end |
This file contains 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 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,18 @@ | ||
class FinancialTransactionSerializer < ActiveModel::Serializer | ||
include ApplicationHelper | ||
|
||
attributes :id, :user_id, :user_name, :amount, :note, :created_at | ||
|
||
def user_name | ||
show_user object.user | ||
end | ||
|
||
def amount | ||
object.amount.to_f | ||
end | ||
|
||
def created_at | ||
# use Rails convention in API, which is less easier to migrate later | ||
object.created_on | ||
end | ||
end |
This file contains 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