This project provides a RESTful API for a basic e-commerce platform built using Go, Gin, and GORM. It includes endpoints for user management, product catalog, categories, wishlists, and shopping carts.
- Features
- Technologies Used
- Prerequisites
- Setup
- Environment Variables
- Running the Application
- API Endpoints
- Database
- Error Handling
- Authentication/Authorization (TODO)
- Input Validation
- License
- User Management: Create, list, get, update, and delete user accounts.
- Product Management: Create, list, get, update, and delete products.
- Category Management: Create, list, get, update, and delete product categories.
- Wishlist Management: Add products to a user's wishlist, view a user's wishlist, view all wishlists and remove items from a wishlist.
- Cart Management: Add products to a user's shopping cart, view a user's cart, update quantities of items in the cart, and remove items from the cart.
- Database Seeding: Seed initial categories and products for development/testing.
- Automatic Database Migrations: GORM automatically migrates the database schema based on the defined models.
- Go: Programming language
- Gin: Web framework
- GORM: ORM (Object-Relational Mapper) for database interaction
- MySQL: Database (but easily adaptable to other databases supported by GORM)
- godotenv: For loading environment variables from a
.envfile
- Go installed (version 1.18 or higher)
- MySQL database server running
makecommand installed (optional, but recommended for convenience)
-
Clone the repository:
git clone <repository_url> cd <project_directory>
-
Install dependencies:
go mod download
-
Create a
.envfile:Copy the example
.env.examplefile to.envand configure the database connection settings:cp .env.example .env
Edit
.envand fill in the appropriate values.
The following environment variables are required:
PORT: The port the application will listen on (e.g.,8080).DB_USER: The MySQL database user.DB_PASSWORD: The MySQL database password.DB_HOST: The MySQL database host (e.g.,localhost).DB_PORT: The MySQL database port (e.g.,3306).DB_NAME: The MySQL database name.SEED_PRODUCT_COUNT: The number of products to seed when the application starts (optional).
-
Initialize the database:
The application automatically runs database migrations on startup. Ensure your MySQL server is running and the connection details in
.envare correct. -
Run the application:
go run main.go
The application will start, and you can access the API endpoints.
All API endpoints return JSON responses.
POST /api/users: Create a new user. Requires JSON body with user details (firstName, lastName, email, password, phone, address).GET /api/users: List all users.GET /api/users/:userid: Get a single user by ID.PATCH /api/users/:userid: Update an existing user. Requires JSON body with the fields to update.DELETE /api/users/:userid: Delete a user.
POST /api/product: Create a new product. Requires JSON body with product details (SKU, name, description, price, categoryID).GET /api/product: List all products.GET /api/product/:productid/: Get a single product by ID.PATCH /api/product/:productid/: Update an existing product. Requires JSON body with the fields to update.DELETE /api/product/:productid/: Delete a product.
POST /api/categories: Create a new category. Requires JSON body with category details (name, description).GET /api/categories: List all categories.GET /api/categories/:categoryid/: Get a single category by ID.PATCH /api/categories/:categoryid/: Update an existing category. Requires JSON body with the fields to update (name, description).DELETE /api/categories/:categoryid/: Delete a category.
POST /api/wishlists: Add a product to a user's wishlist. Requires JSON body withuserIDandproductID.GET /api/wishlists/:userid/: View a user's wishlist.GET /api/wishlists: View all wishlists.DELETE /api/wishlists/:wishlistid/: Remove an item from a user's wishlist.
POST /api/carts: Add a product to a user's shopping cart. Requires JSON body withuserID,productID, andquantity.GET /api/carts/:userid/: View a user's shopping cart.PATCH /api/carts/:cartid/: Update the quantity of an item in a user's shopping cart. Requires JSON body withquantity.DELETE /api/carts/:cartid/: Remove an item from a user's shopping cart.
The application uses a MySQL database. The database schema is automatically created and updated by GORM based on the model definitions in models/models.go. The following tables are created:
usersproductscategorieswishlistscarts
The API uses a consistent error handling pattern:
400 Bad Request: For invalid requests (e.g., missing required fields, invalid data types).404 Not Found: For requests to non-existent resources.500 Internal Server Error: For unexpected server errors.
Error messages are returned in JSON format with a message field.
Important: Currently, there is NO authentication or authorization implemented in this project. All API endpoints are publicly accessible.
Future development should include:
- User Authentication: Implement user login/registration using a secure authentication mechanism (e.g., JWTs - JSON Web Tokens).
- Authorization: Implement authorization rules to restrict access to resources based on user roles and permissions.
The API uses Gin's binding and validation features to validate input data. The binding:"required" tag is used to mark required fields. Custom validation logic can be added as needed.
This project is licensed under the MIT License.