repositorypackage
0.0.0-20250119160605-d24e0e9f1487
Repository: https://github.com/geoo115/ecommerce.git
Documentation: pkg.go.dev
# README
Ecommerce API
A robust REST API built with Go and Gin framework for managing an ecommerce platform. Features include user authentication, product management, shopping cart, orders, payments, and more.
Table of Contents
Prerequisites
- Go 1.16 or higher
- PostgreSQL
- Postman for testing
Installation
- Clone the repository:
git clone https://github.com/geoo115/Ecommerce.git
cd Ecommerce
- Install dependencies:
go mod tidy
- Set up environment variables:
DB_HOST=localhost
DB_PORT=5432
DB_USER=postgres
DB_PASSWORD=your_password
DB_NAME=ecommerce
JWT_SECRET=your_secret_key
- Start the server:
go run main.go
API Documentation and Testing Guide
This section provides detailed instructions for testing all API endpoints using Postman or similar tools.
Authentication
Sign Up
POST /signup
Test body:
{
"username": "testuser",
"password": "securepass123",
"email": "[email protected]",
"phone": "1234567890",
"role": "customer" // Optional: use "admin" for admin account
}
Login
POST /login
Test body:
{
"username": "testuser",
"password": "securepass123"
}
Logout
POST /logout
Authorization: Bearer <token>
Categories
List Categories
GET /categories
Add Category (Admin Only)
POST /categories
Authorization: Bearer <token>
Test body:
{
"name": "Electronics"
}
Delete Category (Admin Only)
DELETE /categories/:id
Authorization: Bearer <token>
Products
List Products
GET /products
Query parameters:
category_id=1
page=1
limit=10
Get Single Product
GET /product/:id
Add Product (Admin Only)
POST /product
Authorization: Bearer <token>
Test body:
{
"name": "Test Product",
"price": 999.99,
"category_id": 1,
"description": "Test description",
"stock": 50
}
Edit Product (Admin Only)
PUT /product/:id
Authorization: Bearer <token>
Test body:
{
"name": "Updated Product",
"price": 899.99,
"description": "Updated description",
"stock": 45
}
Delete Product (Admin Only)
DELETE /product/:id
Authorization: Bearer <token>
Search Products
GET /products/search?query=laptop
Cart
View Cart
GET /cart
Authorization: Bearer <token>
Add to Cart
POST /cart
Authorization: Bearer <token>
Test body:
{
"product_id": 1,
"quantity": 2
}
Remove from Cart
DELETE /cart/:id
Authorization: Bearer <token>
Orders
Place Order
POST /orders
Authorization: Bearer <token>
Test body:
{
"address_id": 1,
"payment_mode": "Credit Card"
}
List Orders
GET /orders
Authorization: Bearer <token>
Get Single Order
GET /orders/:id
Authorization: Bearer <token>
Cancel Order
PUT /orders/:id/cancel
Authorization: Bearer <token>
Reviews
Add Review
POST /reviews
Authorization: Bearer <token>
Test body:
{
"product_id": 1,
"rating": 5,
"comment": "Excellent product!"
}
List Reviews for Product
GET /reviews/:product_id
Wishlist
View Wishlist
GET /wishlist
Authorization: Bearer <token>
Add to Wishlist
POST /wishlist
Authorization: Bearer <token>
Test body:
{
"product_id": 1
}
Remove from Wishlist
DELETE /wishlist/:id
Authorization: Bearer <token>
Address
Add Address
POST /address
Authorization: Bearer <token>
Test body:
{
"address": "123 Test Street",
"city": "Test City",
"zip_code": "12345"
}
Edit Address
PUT /address/:id
Authorization: Bearer <token>
Test body:
{
"address": "456 Updated Street",
"city": "New City",
"zip_code": "54321"
}
Delete Address
DELETE /address/:id
Authorization: Bearer <token>
Payments
Process Payment
POST /payments
Authorization: Bearer <token>
Test body:
{
"order_id": 1,
"payment_method": "Credit Card",
"amount": 999.99
}
Get Payment Status
GET /payments/:order_id
Authorization: Bearer <token>
Checkout
POST /checkout
Authorization: Bearer <token>
Admin Reports
Sales Report (Admin Only)
GET /admin/reports/sales?start_date=2024-01-01&end_date=2025-01-31
Authorization: Bearer <token>
Inventory Report (Admin Only)
GET /admin/reports/inventory?start_date=2024-01-01&end_date=2025-01-31
Authorization: Bearer <token>
Testing Steps
- Start by creating a new user account using the signup endpoint
- Login to get the JWT token
- For admin operations, create an admin account and use its token
- Add the token to your request headers for authenticated endpoints
- Test each endpoint with both valid and invalid data to ensure proper error handling
- For testing order flow:
- Add products to cart
- Create address
- Place order
- Process payment
- Check order status
Testing with Postman
- Import the Postman collection from the
postman
directory - Set up environment variables in Postman:
BASE_URL
:http://localhost:8080
TOKEN
: After login, set this to the received JWT token
Error Handling
The API returns standard HTTP status codes:
- 200: Successful operation
- 201: Resource created
- 400: Bad request (invalid input)
- 401: Unauthorized (invalid/missing token)
- 403: Forbidden (insufficient permissions)
- 404: Resource not found
- 500: Internal server error
Error Response Format:
{
"error": "Error message here"
}
Contributing
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
Contact
Geoo115 - GitHub Profile
Project Link: https://github.com/geoo115/Ecommerce