Skip to content

Latest commit

 

History

History
152 lines (103 loc) · 3.14 KB

README.md

File metadata and controls

152 lines (103 loc) · 3.14 KB

ProductAPI

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
}

Setup locally

  • 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>
Using Docker ProductAPI
  • Run using docker-compose up --build

OR

Using Golang ProductAPI
  • 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

Seeding the Database

Set the SEED_DB environment variable to true to seed the database with sample data.

Usage

The Repo contains a Makefile which can be used to build and run the API locally.

Building
make build

Used to build the docker containers

Running
make up

Used to run the docker containers

Testing
make test

Used to run the goapp test cases

Endpoint Usage

Auth

  • /api/auth/register

    • Allowed Methods: POST
    • Accepted Fields: {name, email, password}
    • Returns: {id, email, token}
  • /api/auth/login

    • Allowed Methods: POST
    • Accepted Fields: {email, password}
    • Returns: {id, name, email, token}
  • /api/auth/user

    • Allowed Methods: GET
    • Authorization: Bearer <Token>
    • Returns: User details

Products

  • api/product

    • Allowed Methods: GET, POST

      • GET

        • Query params: {q, page, limit}
        • Returns: All products matching the query at the given page
      • POST

        • Authorization: Bearer <Token>
        • Accepted Fields: {name, description, thumbnailImageUrl}
        • Returns: Product Details

Reviews

  • api/review/<product_id>

    • Allowed Methods: POST
    • Authorization: Bearer <Token>
    • Accepted Fields: {text, rating}
    • Returns: Review Details