ProductAPI is a RESTful API for managing products and their reviews. The project introduces a new way of File-Driven testing in Golang, and new way of creating indexes in MongoDB.
Check them out:
The API consists of three models:
User
type User struct {
Id primitive.ObjectID
Name string
Password string
Email string
}
Product
type Product struct {
Id primitive.ObjectID
Name string
Description string
ThumbnailImageUrl string
Reviews []Review
RatingSum float64
RatingCount int
}
Review
type Review struct {
Id primitive.ObjectID
Text string
Rating int
Reviewer User
}
- Clone the repo using
git clone https://github.com/ShauryaAg/ProductAPI.git
- Move into the project folder
cd ProductAPI/
- Create a
.env
file in the project folder
PORT=<PORT>
MONGODB_URI=<Your Mongo URI>
SECRET=<Secret>
SEED_DB=<true/false>
- Run using
docker-compose up --build
- Install the dependecies using
go mod download
- Run using
go run server.go
Note: you need to have the mongo database running on your local machine for this to work
Set the
SEED_DB
environment variable totrue
to seed the database with sample data.
The Repo contains a Makefile
which can be used to build and run the API locally.
make build
Used to build the docker containers
make up
Used to run the docker containers
make test
Used to run the goapp test cases
-
/api/auth/register
- Allowed Methods:
POST
- Accepted Fields:
{name, email, password}
- Returns:
{id, email, token}
- Allowed Methods:
-
/api/auth/login
- Allowed Methods:
POST
- Accepted Fields:
{email, password}
- Returns:
{id, name, email, token}
- Allowed Methods:
-
/api/auth/user
- Allowed Methods:
GET
- Authorization:
Bearer <Token>
- Returns:
User details
- Allowed Methods:
-
api/product
-
Allowed Methods:
GET, POST
-
GET
- Query params:
{q, page, limit}
- Returns:
All products matching the query at the given page
- Query params:
-
POST
- Authorization:
Bearer <Token>
- Accepted Fields:
{name, description, thumbnailImageUrl}
- Returns:
Product Details
- Authorization:
-
-
-
api/review/<product_id>
- Allowed Methods:
POST
- Authorization:
Bearer <Token>
- Accepted Fields:
{text, rating}
- Returns:
Review Details
- Allowed Methods: