Files
api-sessions/QUICK_REFERENCE.md
2026-01-23 01:03:09 +01:00

5.6 KiB

API Sessions - Quick Reference

🚀 Quick Start

# Start the application
./mvnw spring-boot:run

# Or use management script
./manage.sh start

# Run tests
./test-api.sh

🔑 Default Credentials

Username Password Role
admin admin123 ROLE_ADMIN
user user123 ROLE_USER
john john123 ROLE_USER

📡 API Endpoints

Authentication (No token required)

# Login
curl -X POST http://localhost:8080/api/auth/login \
  -H "Content-Type: application/json" \
  -d '{"username":"admin","password":"admin123"}'

Products (Token required)

Get all products

curl -X GET http://localhost:8080/api/products \
  -H "Authorization: Bearer YOUR_TOKEN"

Get product by ID

curl -X GET http://localhost:8080/api/products/1 \
  -H "Authorization: Bearer YOUR_TOKEN"

Search products

curl -X GET "http://localhost:8080/api/products/search?name=laptop" \
  -H "Authorization: Bearer YOUR_TOKEN"

Filter by category

curl -X GET http://localhost:8080/api/products/category/Electronics \
  -H "Authorization: Bearer YOUR_TOKEN"

Create product

curl -X POST http://localhost:8080/api/products \
  -H "Authorization: Bearer YOUR_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "New Product",
    "description": "Description",
    "price": 99.99,
    "stock": 10,
    "category": "Electronics"
  }'

Update product

curl -X PUT http://localhost:8080/api/products/1 \
  -H "Authorization: Bearer YOUR_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "Updated Product",
    "description": "Updated description",
    "price": 149.99,
    "stock": 15,
    "category": "Electronics"
  }'

Delete product

curl -X DELETE http://localhost:8080/api/products/1 \
  -H "Authorization: Bearer YOUR_TOKEN"

🛠️ Management Commands

./manage.sh start       # Start application
./manage.sh stop        # Stop application
./manage.sh restart     # Restart application
./manage.sh test        # Run full tests
./manage.sh quick-test  # Run quick tests
./manage.sh status      # Check status
./manage.sh logs        # View logs
./manage.sh console     # Show H2 console info
./manage.sh help        # Show all commands

🗄️ H2 Console

📝 Complete Workflow Example

# 1. Start the application
./manage.sh start

# 2. Wait for startup (check status)
./manage.sh status

# 3. Login and save token
TOKEN=$(curl -s -X POST http://localhost:8080/api/auth/login \
  -H "Content-Type: application/json" \
  -d '{"username":"admin","password":"admin123"}' | \
  grep -o '"token":"[^"]*' | cut -d'"' -f4)

# 4. Use the token to access API
curl -X GET http://localhost:8080/api/products \
  -H "Authorization: Bearer $TOKEN"

# 5. Create a product
curl -X POST http://localhost:8080/api/products \
  -H "Authorization: Bearer $TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "Test Product",
    "description": "Test Description",
    "price": 99.99,
    "stock": 10,
    "category": "Test"
  }'

# 6. View all products
curl -X GET http://localhost:8080/api/products \
  -H "Authorization: Bearer $TOKEN" | python3 -m json.tool

🧪 Testing

# Run comprehensive tests (14 tests)
./test-api.sh

# Run quick validation
./quick-test.sh

# Manual test - get token
./manage.sh start
sleep 10
curl -X POST http://localhost:8080/api/auth/login \
  -H "Content-Type: application/json" \
  -d '{"username":"admin","password":"admin123"}'

📊 Project Structure

api-sessions/
├── src/main/java/          # Source code
├── src/main/resources/     # Configuration
├── test-api.sh            # Comprehensive tests
├── quick-test.sh          # Quick tests
├── manage.sh              # Management script
├── README.md              # Full documentation
├── IMPLEMENTATION_SUMMARY.md  # Implementation details
└── QUICK_REFERENCE.md     # This file

🔍 Troubleshooting

Application won't start

# Check if port 8080 is in use
lsof -i :8080

# Clean and rebuild
./manage.sh clean
./manage.sh build
./manage.sh start

Tests failing

# Check application status
./manage.sh status

# Restart application
./manage.sh restart
sleep 10

# Try tests again
./test-api.sh

Can't get token

# Check credentials (case-sensitive)
# Username: admin
# Password: admin123

# Verify endpoint is accessible
curl -X POST http://localhost:8080/api/auth/login \
  -H "Content-Type: application/json" \
  -d '{"username":"admin","password":"admin123"}'

📚 Additional Resources

  • Full documentation: README.md
  • Implementation details: IMPLEMENTATION_SUMMARY.md
  • Application logs: app.log (when using manage.sh)
  • H2 Console: http://localhost:8080/h2-console

Pro Tips

  1. Save token to variable: Makes testing easier

    TOKEN=$(curl -s ... | grep -o '"token":"[^"]*' | cut -d'"' -f4)
    
  2. Pretty print JSON: Use python3 json.tool

    curl ... | python3 -m json.tool
    
  3. Check logs in real-time:

    ./manage.sh logs
    
  4. Quick product count:

    curl -s http://localhost:8080/api/products \
      -H "Authorization: Bearer $TOKEN" | \
      python3 -c "import sys, json; print(len(json.load(sys.stdin)))"
    

Version: 1.0.0
Last Updated: 2026-01-23
Status: Ready for use