Class: Api::V5::CreditCardsController

Inherits:
BaseController
  • Object
show all
Includes:
Api::V5::Concerns::Authorization
Defined in:
app/controllers/api/v5/credit_cards_controller.rb

Overview

CreditCard Controller

Constant Summary

Constants inherited from BaseController

BaseController::CACHE_NAMESPACE, BaseController::INTERNAL_SERVER_ERROR_MESSAGE, BaseController::ResponseSchema

Instance Method Summary collapse

Methods inherited from BaseController

#identity_cache_memoization

Methods included from LogrageCustomLogger

#append_info_to_payload

Methods included from ResponseCacheConcern

#my_response_cache

Instance Method Details

#createObject



28
29
30
31
32
33
34
35
# File 'app/controllers/api/v5/credit_cards_controller.rb', line 28

def create
  card = current_user.gb_primepay_cards.new(permitted_params)
  if card.valid? && card.save
    render json: serialize(card)
  else
    render json: { data: nil, success: false, message: card.errors.full_messages.to_sentence }
  end
end

#destroyObject



46
47
48
49
50
51
52
53
# File 'app/controllers/api/v5/credit_cards_controller.rb', line 46

def destroy
  card = current_user.gb_primepay_cards.find params.require(:id)
  if card.destroy!
    render json: { data: nil, success: true, message: 'Card deleted successfully' }
  else
    render json: { data: nil, success: false, message: card.errors.full_messages.to_sentence }
  end
end

#indexObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'app/controllers/api/v5/credit_cards_controller.rb', line 9

def index
  expiration_threshold = Time.current.in_time_zone('Asia/Bangkok').beginning_of_month
  threshold_month = expiration_threshold.month.to_s.rjust(2, '0')
  threshold_year = expiration_threshold.year.to_s.last(2)

  cards = current_user.gb_primepay_cards.
    where('(expiration_year > :year) OR (expiration_year = :year AND expiration_month >= :month)', year: threshold_year, month: threshold_month).
    select('id, last_digits, number, expiration_month, expiration_year, card_type, is_primary, updated_at').
    order(:expiration_year, :expiration_month, :number)

  my_response_cache cards.cache_key, :json do
    ActiveModelSerializers::SerializableResource.new(
      cards,
      each_serializer: Api::V5::CreditCardsSerializer,
      adapter: :json_api,
    ).serializable_hash.merge(success: true, message: 'success').as_json
  end
end

#updateObject



37
38
39
40
41
42
43
44
# File 'app/controllers/api/v5/credit_cards_controller.rb', line 37

def update
  card = current_user.gb_primepay_cards.find(params.require(:id))
  if card.update(permitted_params)
    render json: serialize(card)
  else
    render json: { data: nil, success: false, message: card.errors.full_messages.to_sentence }
  end
end