Giter Site home page Giter Site logo

product-catalog-app's Introduction

Product catalog app

Product catalog implemented with node.js, express.js, and mongodb npm version 6.11.3 node version 13.2.0

Get started

Application expects certain environment variables to be defined. During development dotenv was used for this purpose. Define your own environment variables with a .env file at project root level

Example:

PORT=3001
MONGODB_URI='mongodb+srv://<username>:<password>@cluster0-6n9o6.mongodb.net/product_catalog?retryWrites=true&w=majority'
TEST_MONGODB_URI='mongodb+srv://<username>:<password>@cluster0-6n9o6.mongodb.net/test_product_catalog?retryWrites=true&w=majority'
SECRET='qwertyuiop123456789'
OPENEXCHANGERATES_API_KEY='abc123'

PORT - Which port application runs at

MONGODB_URI - Connetion string for mongodb database for development

TEST_MONGODB_URI - Connection string for mongodb database for integration testing

SECRET - Secret for generating tokens during login

OPENEXCHANGERATES_API_KEY - API key for currency conversion rates (https://openexchangerates.org/)


Once you have set up environment file you can proceed

Install dependencies:

$ npm install

Makes sure everything works by running tests:

$ npm run test

Run application:

$ npm run watch

API documentation

User API

Get existing users:

REQUEST:
GET localhost:3001/api/users

RESPONSE:
[
    {
        "products": [
            "5de56d0f36c6ede6095b50a2",
            "5de56d1636c6ede6095b50a3"
        ],
        "email": "[email protected]",
        "country": "Finland",
        "id": "5de56cb8bdaf7fe5fe4258d1"
    },
    {
        "products": [],
        "email": "[email protected]",
        "country": "Denmark",
        "id": "5de56cc1bdaf7fe5fe4258d2"
    }
]

Create new user:

REQUEST:
POST localhost:3001/api/users
Content-Type: "application/json"
{
	"email": "[email protected]",
	"password": "Pass1234",
	"country": "Finland"
}

RESPONSE:
{
    "products": [],
    "email": "[email protected]",
    "country": "Denmark",
    "id": "5de56cc1bdaf7fe5fe4258d2"
}

Login API

Login with created user:

REQUEST:
POST localhost:3001/api/login
Content-Type: "application/json"
{
	"email": "[email protected]",
	"password": "Pass1234"
}
RESPONSE:
{
    "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI6ImplbnNAZXhhbXBsZS5jb21iLCJpZCI6IjYkZTU2Y2MxYmRhZjdmZTVmZTQyNThkMiIsImlhdCI6MTU3NTMxNjc4OH0.K4vIwnJUkPiIyEz8zWRD85AGM96Pc7q0xip-Zf-zcnE",
    "email": "[email protected]"
}

Product API

Get all products. If Authorization header is passed, prices are converted to the currency of users country:

REQUEST:
GET localhost:3001/api/products
Authorization: "bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI6ImplbnNAZXhhbXBsZS5jb21iLCJpZCI6IjYkZTU2Y2MxYmRhZjdmZTVmZTQyNThkMiIsImlhdCI6MTU3NTMxNjc4OH0.K4vIwnJUkPiIyEz8zWRD85AGM96Pc7q0xip-Zf-zcnE"

RESPONSE:
[
    {
        "likes": 0,
        "title": "jeans",
        "price": 597.73,
        "user": {
            "email": "[email protected]",
            "country": "Finland",
            "id": "5de56cb8bdaf7fe5fe4258d1"
        },
        "id": "5de56d0f36c6ede6095b50a2"
    },
    {
        "likes": 0,
        "title": "jacket",
        "price": 1120.73,
        "user": {
            "email": "[email protected]",
            "country": "Finland",
            "id": "5de56cb8bdaf7fe5fe4258d1"
        },
        "id": "5de56d1636c6ede6095b50a3"
    }
]

It is also possible to sort and query for products. Sort:

REQUEST:
GET localhost:3001/api/products?sort=price

RESPONSE:
[
    {
        "likes": 0,
        "title": "jacket",
        "price": 150,
        "user": {
            "email": "[email protected]",
            "country": "Finland",
            "id": "5de56cb8bdaf7fe5fe4258d1"
        },
        "id": "5de56d1636c6ede6095b50a3"
    },
    {
        "likes": 0,
        "title": "jeans",
        "price": 80,
        "user": {
            "email": "[email protected]",
            "country": "Finland",
            "id": "5de56cb8bdaf7fe5fe4258d1"
        },
        "id": "5de56d0f36c6ede6095b50a2"
    }
]

Query:

REQUEST:
GET localhost:3001/api/products
{
	"price":
		{
			"gte": 100,
			"lte": 200
		}
}

RESPONSE:
[
    {
        "likes": 0,
        "title": "jacket",
        "price": 150,
        "user": {
            "email": "[email protected]",
            "country": "Finland",
            "id": "5de56cb8bdaf7fe5fe4258d1"
        },
        "id": "5de56d1636c6ede6095b50a3"
    }
]

Create new product:

REQUEST:
POST localhost:3001/api/products
Authorization: "bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI6ImplbnNAZXhhbXBsZS5jb21iLCJpZCI6IjYkZTU2Y2MxYmRhZjdmZTVmZTQyNThkMiIsImlhdCI6MTU3NTMxNjc4OH0.K4vIwnJUkPiIyEz8zWRD85AGM96Pc7q0xip-Zf-zcnE"
Content-Type: "application/json"
{
	"title": "jacket",
	"price": 150
}

RESPONSE:
{
    "likes": 0,
    "title": "jacket",
    "price": 150,
    "user": "5de56cb8bdaf7fe5fe4258d1",
    "id": "5de56d1636c6ede6095b50a3"
}

Update product:

REQUEST:
PUT localhost:3001/api/products/5de56d1636c6ede6095b50a3
Authorization: "bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI6ImplbnNAZXhhbXBsZS5jb21iLCJpZCI6IjYkZTU2Y2MxYmRhZjdmZTVmZTQyNThkMiIsImlhdCI6MTU3NTMxNjc4OH0.K4vIwnJUkPiIyEz8zWRD85AGM96Pc7q0xip-Zf-zcnE"
Content-Type: "application/json"
{
	"title": "jacket",
	"price": 155
}

RESPONSE:
{
    "likes": 0,
    "title": "jacket",
    "price": 155,
    "user": "5de56cb8bdaf7fe5fe4258d1",
    "id": "5de56d1636c6ede6095b50a3"
}

Delete Product:

REQUEST:
DELETE localhost:3001/api/products/5de56d1636c6ede6095b50a3
Authorization: "bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI6ImplbnNAZXhhbXBsZS5jb21iLCJpZCI6IjYkZTU2Y2MxYmRhZjdmZTVmZTQyNThkMiIsImlhdCI6MTU3NTMxNjc4OH0.K4vIwnJUkPiIyEz8zWRD85AGM96Pc7q0xip-Zf-zcnE"

Like a Product:

REQUEST:
PATCH localhost:3001/api/products/5de56d1636c6ede6095b50a3
Authorization: "bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI6ImplbnNAZXhhbXBsZS5jb21iLCJpZCI6IjYkZTU2Y2MxYmRhZjdmZTVmZTQyNThkMiIsImlhdCI6MTU3NTMxNjc4OH0.K4vIwnJUkPiIyEz8zWRD85AGM96Pc7q0xip-Zf-zcnE"

RESPONSE:
{
    "likes": 1,
    "title": "jacket",
    "price": 155,
    "user": "5de56cb8bdaf7fe5fe4258d1",
    "id": "5de56d1636c6ede6095b50a3"
}

product-catalog-app's People

Watchers

James Cloos avatar Antti Paloposki avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.