Skip to content

Commit

Permalink
get wallets
Browse files Browse the repository at this point in the history
  • Loading branch information
lyna-azerouk committed Nov 8, 2024
1 parent 4cafcdb commit 0798076
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 5 deletions.
12 changes: 12 additions & 0 deletions api/controllers/wallet_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,18 @@ def self.create(params)
end
end


def self.index(params)
WalletController.find_user(params)

if @user.present?
user_wallets = @user.get_wallets
ApiResponseHelper.render_success(200, user_wallets)
else
ApiResponseHelper.render_failure(404, "user_not_found")
end
end

def self.find_user(params)
@user = User.find_by(id: params['user_id'])
@user
Expand Down
15 changes: 12 additions & 3 deletions api/modal/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,23 @@ def eq_password(password)
Password.new(self.password) == password
end

def get_wallets
formatd_wallets = []

self.wallets.each do |wallet|
formatd_wallets << wallet.get_wallet
end
formatd_wallets
end

def self.token_expired?(session_token)
begin
decoded_token = User.decode(session_token)
expiration_time = decoded_token[0]['exp']

return expiration_time && Time.at(expiration_time) < Time.now
expiration_time && Time.at(expiration_time) < Time.now
rescue JWT::DecodeError => e
return false
false
end
end

Expand All @@ -40,7 +49,7 @@ def self.find_user_by_token(session_token)
user_id = decoded_token[0]['user_id']

user = User.find_by(id: user_id)
return user
user
end

private
Expand Down
2 changes: 1 addition & 1 deletion api/modal/wallet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Wallet < ActiveRecord::Base
enumerize :name, in: [:visa, :mastercard, :american_express]

validates :csv, :name, :number, :exprired_at, presence: true
validates :number, uniqueness: true
# validates :number, uniqueness: true
validate :number_lenght?
validate :luhn_valid?

Expand Down
10 changes: 9 additions & 1 deletion api/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
'Hello world!'
end

#User EndPoints
post '/users/create' do
params = JSON.parse(request.body.read)

Expand All @@ -27,7 +28,14 @@
UserController.authentificated(params)
end

#Card EndPoints
post '/users/:user_id/cards' do
result = params.merge(JSON.parse(request.body.read))
WalletController.create(result)
end
end

get '/users/:user_id/cards' do
result = params.merge(JSON.parse(request.body.read))
WalletController.index(result)
end

0 comments on commit 0798076

Please sign in to comment.