BananaCrystal / email-templates

This repository contains in-house email templates that serve as structure for our emails. Each template can be updated and modified to fit requirements.
MIT License
0 stars 0 forks source link

online_wallets_controller.rb #38

Open geekelo opened 2 months ago

geekelo commented 2 months ago
module Api::V1
  class P2pTrades::OnlineWalletsController < Api::BaseController
    include ::P2pTrades::OnlineWalletHelper

    def index
      render json: online_wallets
    end

    def create
      response = P2pApi::Requests::OnlineWallet::CreateService.new(
        profile: current_user.profile,
        params: online_wallets_params
      ).call

      if [200, 201].include? response[:status]
        clear_online_wallets_cache
        render json: response
      else
        render json: response, status: response[:status]
      end
    end

    def destroy
      if online_wallet_ids.include?(params[:id])
        P2pApi::OnlineWallet::DeleteJob.perform_now(params[:id])
        clear_online_wallets_cache
        head :ok
      else
        render json: { error: "Not authorized" }, status: :forbidden
      end
    end
  end
end
geekelo commented 2 months ago

The Api::V1::P2pTrades::OnlineWalletsController manages online wallet operations in a P2P trading context. Here’s a breakdown of its functionality:

Controller Overview

This controller handles operations related to online wallets, including retrieving, creating, and deleting wallets.

Actions

  1. index:

    • Purpose: Retrieves and returns a list of online wallets.
    • Process:
      • Calls the online_wallets method from the included helper module to fetch the list.
      • Renders the list as JSON.
  2. create:

    • Purpose: Creates a new online wallet based on the provided parameters.
    • Process:
      • Uses the P2pApi::Requests::OnlineWallet::CreateService to handle the creation.
      • Parameters:
        • profile: The current user's profile.
        • params: Parameters for creating the online wallet.
      • Response Handling:
      • If the response status is 200 or 201, clears the cache and renders the response.
      • Otherwise, renders the response with the appropriate status code.
  3. destroy:

    • Purpose: Deletes an online wallet.
    • Process:
      • Checks if the provided wallet ID is in the list of authorized wallet IDs.
      • If authorized, triggers the P2pApi::OnlineWallet::DeleteJob to perform the deletion and clears the cache.
      • If not authorized, returns a 403 Forbidden response.

Summary

Key Points

This structure helps maintain clean, readable code by separating concerns and leveraging service objects for business logic.