From e44d9f0b6f7fb353f87ddf04b3fe6903280cc08b Mon Sep 17 00:00:00 2001 From: Kfir Dayan Date: Sun, 11 Jun 2023 12:50:33 +0300 Subject: [PATCH] README --- README.md | 73 +++++++++++++++++++++++++++- src/controllers/ProductController.ts | 18 ++++++- src/routes/product.ts | 4 +- 3 files changed, 91 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index d7f234e..49c15f4 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,6 @@ Technologies used: * Docker (docker-compose) * bcrypt * JWT -* Swagger ## How to run ## 1. Clone the repository @@ -24,6 +23,76 @@ Technologies used: The database will be running on port 27017 ## API ## -The API is documented using Swagger. You can access the documentation by running the application and accessing the following URL: http://localhost:3000/api-docs +app.use('/products', productRouter); +app.use('/cart', cartRouter); + +# Users # +## POST /users ## - Create a new user +### Request body ### +```json +{ + "name": "string", + "email": "string", + "password": "string" +} +``` +### Response body ### +```json +{ + "user": { + "_id": "string", + "name": "string", + "email": "string", + "password": "string", + "createdAt": "string", + "updatedAt": "string", + "__v": "number" + }, + "token": "string" +} +``` +## POST /users/login ## - Login +### Request body ### +```json +{ + "email": "string", + "password": "string" +} +``` +### Response ### +# body # +```json +{ + "access-token": "string" +} +``` +# headers # +``` Cookie - "access-token"": "string" ``` + + +# Products # +## GET /products ## - Get all products + accepts query params: page, limit. Default values: page = 0, limit = 50. +### Response body ### +```json +{ + "products": [ + { + "_id": "string", + "name": "string", + "description": "string", + "price": "number", + "createdAt": "string", + "updatedAt": "string", + "__v": "number" + } + ], + "total": "number" +} +``` + + + + diff --git a/src/controllers/ProductController.ts b/src/controllers/ProductController.ts index 9d4991c..8c0dfbd 100644 --- a/src/controllers/ProductController.ts +++ b/src/controllers/ProductController.ts @@ -31,8 +31,9 @@ export async function createProduct(req: Request, res: Response) { export async function listProducts(req: Request, res: Response) { try { const { page, limit } = req.query; + const dbPage = Number(page) || 0; const dbLimit = Number(limit) || 50; - const products = await Product.find().sort({ price: 1 }).skip(Number(page) * Number(dbLimit)).limit(Number(dbLimit)); + const products = await Product.find().sort({ price: 1 }).skip(Number(dbPage) * Number(dbLimit)).limit(Number(dbLimit)); res.json(products); } catch (error) { @@ -40,3 +41,18 @@ export async function listProducts(req: Request, res: Response) { res.status(500).json({ error: 'An error occurred while listing the products.' }); } } + +export async function getProduct(req: Request, res: Response) { + const { id } = req.params; + try { + const product = await Product.findById(id); + if(!product) { + res.status(404).json({ error: 'Product not found.' }); + return; + } + res.json(product); + } catch (error) { + console.error('Error getting product:', error); + res.status(500).json({ error: 'An error occurred while getting the product.' }); + } +} diff --git a/src/routes/product.ts b/src/routes/product.ts index aa16636..af0eb74 100644 --- a/src/routes/product.ts +++ b/src/routes/product.ts @@ -1,10 +1,12 @@ import express from 'express'; import { authenticateToken } from '../middlewares/checkAuth'; -import { createProduct, listProducts } from '../controllers/ProductController'; +import { createProduct, listProducts, getProduct } from '../controllers/ProductController'; const productRouter = express.Router(); productRouter.post('/', authenticateToken, createProduct) productRouter.get('/', listProducts); +productRouter.get('/:id', getProduct); + export default productRouter; \ No newline at end of file