README
This commit is contained in:
parent
22429a7b32
commit
e44d9f0b6f
3 changed files with 91 additions and 4 deletions
73
README.md
73
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"
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -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.' });
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
Loading…
Reference in a new issue