Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
19 changes: 16 additions & 3 deletions app/controllers/api/v1/budgets_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,21 @@ class BudgetsController < ApplicationController

# GET /api/v1/budgets
def index
@budgets = current_user.budgets
render json: @budgets, status: :ok
per = params.fetch(:per, 20)
page = params.fetch(:page, 1)

scoped = current_user.budgets
budgets = scoped.order(created_at: :desc).page(page).per(per)

render json: {
data: BudgetSerializer.new(budgets).serializable_hash[:data],
meta: {
page: budgets.current_page,
per: budgets.limit_value,
total_pages: budgets.total_pages,
total_count: budgets.total_count
}
}, status: :ok
end

# POST /api/v1/budgets
Expand Down Expand Up @@ -50,7 +63,7 @@ def set_budget

# Strong params
def budget_params
params.require(:budget).permit(:name, :financial_goal, :user_id)
params.require(:budget).permit(:name, :financial_goal)
end
end
end
Expand Down
28 changes: 19 additions & 9 deletions app/controllers/api/v1/transactions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,25 @@ module Api
module V1
class TransactionsController < ApplicationController
before_action :authenticate_user!
before_action :set_budget
before_action :set_budget, if: -> { params[:budget_id].present? }
before_action :set_transaction, only: [:show, :update, :destroy]

# GET /api/v1/budgets/:budget_id/transactions
# GET /api/v1/budgets/:budget_id/transactions or /api/v1/transactions
def index
per = params.fetch(:per, 20)
page = params.fetch(:page, 1)
from = params[:from]
to = params[:to]
category_id = params[:category_id]
scoped = @budget ? @budget.transactions.includes(:budget, :category) : Transaction.for_user(current_user)

scoped = @budget.transactions
scoped = scoped.on_or_after(from)
scoped = scoped.on_or_before(to)
scoped = scoped.for_category(category_id)
transactions = scoped.ordered_newest_first.page(page).per(per)
transactions = scoped
.on_or_after(from)
.on_or_before(to)
.for_category(category_id)
.ordered_newest_first
.page(page)
.per(per)

render json: {
data: TransactionSerializer.new(transactions).serializable_hash[:data],
Expand Down Expand Up @@ -63,11 +66,18 @@ def destroy
private

def set_budget
@budget = Budget.find(params[:budget_id])
@budget = current_user.budgets.find(params[:budget_id])
end

def set_transaction
@transaction = @budget.transactions.find(params[:id])
@transaction =
if @budget
@budget.transactions.find(params[:id])
else
Transaction.joins(:budget)
.where(budgets: { user_id: current_user.id })
.find(params[:id])
end
end

def transaction_params
Expand Down
5 changes: 5 additions & 0 deletions app/models/transaction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,9 @@ class Transaction < ApplicationRecord
scope :on_or_before, ->(to_date) { where('date <= ?', to_date) if to_date.present? }
scope :for_category, ->(category_id) { where(category_id: category_id) if category_id.present? }
scope :ordered_newest_first, -> { order(date: :desc, created_at: :desc) }
scope :for_user, ->(user) {
includes(:budget, :category)
.joins(:budget)
.where(budgets: { user_id: user.id })
}
end
Loading
Loading