Skip to content

rikw22/challenge-money

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

40 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Challenge Money

A REST API service for managing customer accounts and financial transactions.

Tech Stack

  • Language: Go 1.25.3
  • Web Framework: chi v5
  • Database: PostgreSQL 18.0
  • Database Driver: pgx/v5
  • Validation: validator/v10
  • Containerization: Docker Compose

Project Structure

challenge-money/
├── internal/
│   ├── account/          # Account domain logic
│   │   ├── handler.go
│   │   ├── models.go
│   │   ├── repository.go
│   │   └── handler_test.go
│   ├── transaction/      # Transaction domain logic
│   │   ├── handler.go
│   │   ├── models.go
│   │   ├── repository.go
│   │   └── handler_test.go
│   ├── health/           # Health check handlers
│   ├── database/         # Database connection utilities
│   └── httperrors/       # Custom HTTP error handling
├── main.go               # Application entry point
├── init.sql              # Database schema and seed data
├── docker-compose.yaml   # PostgreSQL container setup
├── Makefile              # Build and development tasks
└── run                   # Quick start script

Getting Started

Prerequisites

  • Go 1.25.3 or later
  • Docker and Docker Compose
  • (Optional) Air for live reload during development

Installation

  1. Clone the repository:
git clone https://github.com/rikw22/challenge-money
cd challenge-money
  1. Install dependencies:
go mod download
  1. (Optional) Install Air for live reload:
go install github.com/air-verse/air@latest

Running the Application

Quick Start

Use the provided run script:

./run

This will:

  • Start PostgreSQL container via docker-compose
  • Initialize the database with schema and seed data
  • Start the API server with live reload (if Air is installed)

Alternative Methods

Using Makefile:

make run

Manual setup:

# Start PostgreSQL
docker compose up -d

# Run the application
go run main.go

The server will start on http://localhost:8080 by default.

API Endpoints

Postman Collection

https://github.com/rikw22/challenge-money/raw/refs/heads/main/docs/postman_collection.json

Http Client Collection

https://raw.githubusercontent.com/rikw22/challenge-money/refs/heads/main/docs/requests.http

Health Check

curl http://localhost:8080/health

Response (200 OK):

{
  "status": "UP"
}

Create Account

curl -X POST http://localhost:8080/accounts \
  -H "Content-Type: application/json" \
  -d '{
    "document_number": "12345678901"
  }'

Response (201 Created):

{
  "account_id": 1,
  "document_number": "12345678901"
}

Get Account

curl http://localhost:8080/accounts/1

Response (200 OK):

{
  "account_id": 1,
  "document_number": "12345678901",
  "created_at": "2025-10-27T00:18:14Z"
}

Create Transaction

curl -X POST http://localhost:8080/transactions \
  -H "Content-Type: application/json" \
  -d '{
    "account_id": 1,
    "operation_type_id": 4,
    "amount": 123.45
  }'

Operation Types:

  • 1 - Normal Purchase (negative amount)
  • 2 - Purchase with installments (negative amount)
  • 3 - Withdrawal (negative amount)
  • 4 - Credit Voucher (positive amount)

Response (201 Created):

{
  "id": "019a096b-ad9f-7f0e-88a4-9c93a754b029",
  "account_id": 1,
  "operation_type_id": 4,
  "amount": 123.45
}

Database Schema

Connection Details

When using docker compose:

  • Host: localhost
  • Port: 5432
  • Database: postgres
  • User: postgres
  • Password: postgres

Testing

Run all tests:

make test
# or
go test ./...

Run tests with coverage:

make test-coverage
# or
go test -coverprofile=coverage.out ./...
go tool cover -html=coverage.out

Development

Building the Application

make build
# or
go build

Docker Commands

# Start PostgreSQL
make docker-up

# Stop PostgreSQL
make docker-down

Environment Variables

Variable Description Default
PORT Server port 8080
DATABASE_URL PostgreSQL connection string postgresql://postgres:postgres@localhost:5432/postgres?sslmode=disable

Future Improvements

  • Idempotence handling for transactions
  • Graceful Shutdown - https://github.com/go-chi/chi/blob/master/_examples/graceful/main.go
  • Integration tests
  • Database migrations (e.g., golang-migrate, goose)
  • API documentation (Swagger/OpenAPI)
  • Rate limiting
  • Authentication and authorization
  • Audit logging
  • Transaction rollback mechanisms
  • Balance calculation and tracking

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published