Skip to content

kristiyankiryakov/Wall-e

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

57 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Wallet API

A Spring Boot REST API that enables user management and wallet operations including creating users, managing wallets, and handling deposits/withdrawals.

API Design Notes

  • User-Wallet Relationship: There's a one-to-many relationship between users and wallets.

Technologies Used

  • Java 17
  • Spring Boot
  • PostgreSQL
  • Docker
  • JPA/Hibernate
  • Maven

Prerequisites

Before running this application, make sure you have the following installed:

  • Java Development Kit (JDK) 17 or later
  • Docker and Docker Compose
  • Maven
  • Git

Getting Started

1. Clone the Repository

git clone https://github.com/kristiyankiryakov/Wall-e.git
cd Wall-e

2. Database Setup

The application uses PostgreSQL running in a Docker container. Start the database using Docker Compose:

docker compose up

This will start a PostgreSQL instance with the following configuration:

  • Port: 5432
  • Database: postgres
  • Username: postgres
  • Password: postgres

3. Configure Application

The application uses profile-specific YAML configuration:

  • application.yml: Contains the base configuration and production settings
  • application-dev.yml: Contains development-specific settings with default JWT values

The development profile is activated by default for ease of testing.

For production deployments, ensure you provide the actual JWT configuration through environment variables:

  • JWT_SECRET
  • JWT_EXPIRATION

4. Build and Run

Build the application using Maven:

mvn clean install

Run the application:

mvn spring-boot:run

The API will be available at http://localhost:8080

API Endpoints

Authentication

Register

  • POST /api/auth/register
  • Creates a new user account
  • Request Body (AuthRequest):
    {
      "username": "string",     // Required
      "password": "string"      // Required, minimal length = 6
    }
  • Response (AuthResponse):
    {
      "token": "string"
    }

Login

  • POST /api/auth/login
  • Authenticates user
  • Request Body (AuthRequest):
    {
      "username": "string",     // Required
      "password": "string"      // Required
    }
  • Response (AuthResponse):
    {
      "token": "string"
    }

Wallets

Create Wallet

  • POST /api/wallets
  • Creates a new wallet for a user
  • Request Body(WalletRequest)
    {
    "walletName": "string"     // Required
    }
  • Response (WalletResponse):
    {
      "id" : "number",
      "name": "string",        
      "owner": "string",       // Username of the owner
      "balance": "number"
    }

View Balance

  • GET /api/wallets/{walletId}
  • Retrieves the current wallet
  • Path Variables:
    • walletId: The ID of the wallet
  • Response (WalletResponse):
    {
      "id" : "number",
      "name": "string",        
      "owner": "string",       // Username of the owner
      "balance": "number"
    }

Deposit Money

  • PUT /api/wallets/{walletId}/deposit
  • Adds funds to a wallet
  • Path Variables:
    • walletId: The ID of the wallet
  • Request Body (TransactionRequest):
    {
      "amount": "number"    // Required, must be positive
    }
  • Response (TransactionResponse):
    {
      "walletId": "number",
      "userId": "number",
      "previousBalance": "number",
      "currentBalance": "number",
      "transactionType": "string",
      "transactionAmount": "number"
    }

Withdraw Money

  • PUT /api/wallets/{walletId}/withdraw
  • Withdraws funds from a wallet
  • Path Variables:
    • walletId: The ID of the wallet
  • Request Body (TransactionRequest):
    {
      "amount": "number"    // Required, must be positive
    }
  • Response (TransactionResponse):
    {
      "walletId": "number",
      "userId": "number",
      "previousBalance": "number",
      "currentBalance": "number",
      "transactionType": "string",
      "transactionAmount": "number"
    }

Transaction History

View transactions of a wallet

  • GET /api/transactions/{walletId}
  • Retrieves the transactions of a wallet
  • Path Variables:
  • walletId: The ID of the wallet
  • Response (List<TransactionDto>):
    {
      "id" : "number",
      "amount": "number",        
      "type": "string",     
      "walletId": "number"
    }

Testing

Running Tests

Execute the test suite using Maven:

mvn test

Manual Testing

You can use tools like Postman or curl to test the API endpoints. Here's an example using curl:

# Create a new user
curl -X POST http://localhost:8080/api/auth/register \
  -H "Content-Type: application/json" \
  -d '{"username": "John_Doe", "password": "john123"}'

Authentication Required Routes

The following endpoints require authentication via Bearer token in the Authorization header:

# Wallet endpoints
curl -X GET http://localhost:8080/api/wallets \
  -H "Authorization: Bearer your_jwt_token_here"

# Transaction endpoints
curl -X GET http://localhost:8080/api/transactions \
  -H "Authorization: Bearer your_jwt_token_here"

CI/CD Pipeline

This project uses GitHub Actions for continuous integration and deployment. The pipeline is triggered on:

  • Push to the main branch
  • Pull requests to the main branch

Pipeline Overview

The workflow file is located at .github/workflows/ci-cd.yml and consists of the following steps:

Build Job

The build job runs on Ubuntu latest and sets up a PostgreSQL service container for integration tests.

Database Container Configuration
  • Image: postgres:latest
  • Credentials:
    • Username: postgres
    • Password: postgres
    • Database: postgres
  • Port: 5432
  • Health checks configured for reliability
Steps
  1. Checkout Code
  • Uses: actions/checkout@v3
  • Fetches the repository code
  1. Setup JDK
  • Uses: actions/setup-java@v3
  • Configures JDK 17 (Temurin distribution)
  1. Cache Dependencies
  • Uses: actions/cache@v3
  • Caches Maven dependencies to speed up builds
  • Cache key based on pom.xml hash
  1. Build Project
  • Runs: mvn clean install
  • Compiles the code and packages the application
  1. Run Tests
  • Runs: mvn test -X
  • Executes all tests with detailed logging

Workflow File

name: Java CI/CD Pipeline

on:
  push:
    branches:
      - main
  pull_request:
    branches:
      - main

jobs:
  build:
    runs-on: ubuntu-latest

    services:
      postgres:
        image: postgres:latest
        env:
          POSTGRES_USER: postgres
          POSTGRES_PASSWORD: postgres
          POSTGRES_DB: postgres
        ports:
          - 5432:5432
        options: --health-cmd="pg_isready -U user" --health-timeout=30s --health-start-period=5s --health-retries=3

    steps:
      - name: Checkout code
        uses: actions/checkout@v3

      - name: Set up JDK 17
        uses: actions/setup-java@v3
        with:
          java-version: 17
          distribution: 'temurin'

      - name: Cache Maven repository
        uses: actions/cache@v3
        with:
          path: ~/.m2
          key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }}
          restore-keys: |
            ${{ runner.os }}-maven

      - name: Build with Maven
        run: mvn clean install

      - name: Run tests
        run: mvn test -X

Pipeline Status

You can view the status of pipeline runs in the "Actions" tab of the GitHub repository.

About

Restful API - allowing users to deposit/withdraw money from their wallets

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages