A RESTful API for managing Gunpla (Gundam model kit) listings and orders. Built with Node.js, Express, and PostgreSQL.
- User Authentication: JWT-based authentication for secure access
- Product Listings: Browse and search Gunpla kits with filtering options
- Order Management: Create and manage orders with inventory tracking
- Database Transactions: ACID-compliant transactions to prevent overselling
- Input Validation: Comprehensive validation for all API endpoints
- Pagination: Support for paginated results on listings
- Backend: Node.js, Express.js
- Database: PostgreSQL with connection pooling
- Authentication: JSON Web Tokens (JWT)
- Environment: dotenv for configuration management
- Development: nodemon for auto-restart during development
- Clone the repository:
git clone <repository-url>
cd gunpla-api- Install dependencies:
npm install- Set up environment variables:
Create a
.envfile in the root directory with the following variables:
PORT=3000
DATABASE_URL=postgresql://username:password@localhost:5432/database_name
JWT_SECRET=your-secret-key-change-in-production
SHIPPING_CENTS=799
TAX_RATE=0- Start the development server:
npm run devThe API will be available at http://localhost:3000
GET /health- Check API and database connectivity
GET /listings- Get all active listings with filtering and pagination
Query Parameters:
q- Search query for kit name (fuzzy match)grade- Filter by kit grade (e.g., HG, RG, MG)minPrice- Minimum price in centsmaxPrice- Maximum price in centslimit- Results per page (default: 20, max: 100)offset- Pagination offset (default: 0)
POST /orders- Create a new order (requires authentication)
Headers:
Authorization: Bearer <token>
Request Body:
{
"buyerEmail": "user@example.com",
"listingId": 123,
"qty": 2
}The API uses the following main tables:
users- User informationgunpla_kits- Gunpla kit cataloglistings- Active product listings with inventoryorders- Order informationorder_items- Order line items
- JWT authentication for all protected endpoints
- Input validation and sanitization
- SQL injection protection with parameterized queries
- Row-level locking to prevent race conditions during order creation
- Inventory management with atomic updates
The code follows standard JavaScript/Node.js conventions with:
- Consistent indentation and formatting
- Descriptive variable and function names
- Proper error handling with try-catch blocks
- Database transactions for critical operations
To add tests, consider using:
- Jest for unit testing
- Supertest for API endpoint testing
- Test database setup for integration tests
Before deploying to production:
- Set a strong
JWT_SECRETenvironment variable - Configure proper CORS settings
- Enable HTTPS
- Set up proper logging (consider Winston)
- Configure database connection pooling for your environment
- Add rate limiting and monitoring
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests for new functionality
- Submit a pull request
This project is licensed under the MIT License.
For issues and questions, please create an issue in the GitHub repository.