diff --git a/hw4/src/data/categories.json b/hw4/src/data/categories.json new file mode 100644 index 0000000..e2bf991 --- /dev/null +++ b/hw4/src/data/categories.json @@ -0,0 +1,14 @@ +[ + { + "id" : "1", + "name" : "Toys" + }, + { + "id" : "2", + "name" : "Fruites" + }, + { + "id" : "3", + "name" : "Clothes" + } +] \ No newline at end of file diff --git a/hw4/src/index.ts b/hw4/src/index.ts index 644ccea..3f44080 100644 --- a/hw4/src/index.ts +++ b/hw4/src/index.ts @@ -1,6 +1,7 @@ import express from 'express'; import cors from 'cors'; import { router as productsRouter } from './routes/products'; +import { router as categoriesRouter } from './routes/category'; const app = express(); @@ -10,6 +11,7 @@ app.use(cors()); app.get('/hello', (req, res) => res.send('hello back')); app.use('/api/products', productsRouter); +app.use('/api/category', categoriesRouter); app.set('port', process.env.PORT || 8000); diff --git a/hw4/src/models/ICategory.ts b/hw4/src/models/ICategory.ts new file mode 100644 index 0000000..3313b7e --- /dev/null +++ b/hw4/src/models/ICategory.ts @@ -0,0 +1,4 @@ +export interface ICategory { + id: string; + name: string; +} diff --git a/hw4/src/models/products.ts b/hw4/src/models/IProducts.ts similarity index 73% rename from hw4/src/models/products.ts rename to hw4/src/models/IProducts.ts index 9f297df..cfa3871 100644 --- a/hw4/src/models/products.ts +++ b/hw4/src/models/IProducts.ts @@ -1,4 +1,4 @@ -export interface Product { +export interface IProduct { id: string; categoryId: string; name: string; diff --git a/hw4/src/routes/category.ts b/hw4/src/routes/category.ts new file mode 100644 index 0000000..afb5dce --- /dev/null +++ b/hw4/src/routes/category.ts @@ -0,0 +1,87 @@ +import { NextFunction, Request, Response, Router } from 'express'; +import { generateId } from '../utils/id-helper'; +import * as handlers from '../utils/common'; +import { ICategory } from '../models/ICategory'; +import { IProduct } from '../models/IProducts'; +import { getCatrgories } from '../store/categories-store'; +import { getProducts } from '../store/products-store'; + +const router = Router(); + +const categories: ICategory[] = getCatrgories(); +const products: IProduct[] = getProducts(); + +const resolveCategoryHandler = (req: Request, res: Response, next: NextFunction): void => { + console.log('enter resolveCategoryHandler'); + const categoryId = req.params.id; + if (!handlers.isValidUuid(categoryId)) { + res.sendStatus(400); + return; + } + + const categoryIndex = categories.findIndex((category) => category.id === categoryId); + if (categoryIndex < 0) { + res.sendStatus(404); + return; + } + res.locals.categoryIndex = categoryIndex; + res.locals.category = categories[categoryIndex]; + next(); +}; + +router.get('/', (req, res) => { + console.log('enter route.get(/)'); + + res.send(categories); +}); + +router.get('/:id/products', resolveCategoryHandler, (req, res) => { + console.log('enter route.get(/:id/products)'); + + const category = req.body as ICategory; + const productsForCategory: IProduct[] = []; + + products.forEach((p) => { + if (p.categoryId === category.id) productsForCategory.push(p); + }); + + res.send(productsForCategory); +}); + +router.get('/:id', resolveCategoryHandler, (req, res) => { + console.log('enter route.get(/:id)'); + + const category = req.body as ICategory; + + res.send(category); +}); + +router.post('/', handlers.validateNameHandler, (req, res) => { + console.log('enter route.post(/)'); + + const category = req.body as ICategory; + category.id = generateId(); + categories.push(category); + + res.status(201).send(category); +}); + +router.put('/:id', handlers.validateNameHandler, resolveCategoryHandler, (req, res) => { + console.log('enter route.put(/:id)'); + + const category = req.body as ICategory; + category.id = res.locals.category.id; + Object.assign(res.locals.category, category); + + res.send(categories); +}); + +router.delete('/:id', resolveCategoryHandler, (req, res) => { + console.log('enter route.delete(/:id)'); + + categories.splice(res.locals.categoryIndex, 1); + + res.sendStatus(204); +}); + +export { router }; diff --git a/hw4/src/routes/products.ts b/hw4/src/routes/products.ts index 0a8a8d5..e348664 100644 --- a/hw4/src/routes/products.ts +++ b/hw4/src/routes/products.ts @@ -1,12 +1,11 @@ import { NextFunction, Request, Response, Router } from 'express'; import { generateId } from '../utils/id-helper'; -import productsData from '../data/products.json'; import * as handlers from '../utils/common'; -import { Product } from '../models/products'; - -const products: Product[] = productsData; +import { IProduct } from '../models/IProducts'; +import { getProducts } from '../store/products-store'; const router = Router(); +const products: IProduct[] = getProducts(); const resolveProductHandler = (req: Request, res: Response, next: NextFunction): void => { console.log('enter resolveProductHandler'); @@ -34,14 +33,14 @@ router.get('/', (req, res) => { router.get('/:id', resolveProductHandler, (req, res) => { console.log('enter route.get(/:id)'); - const product = req.body as Product; + const product = req.body as IProduct; res.send(product); }); router.post('/', handlers.validateNameHandler, (req, res) => { console.log('enter route.post(/)'); - const product = req.body as Product; + const product = req.body as IProduct; product.id = generateId(); products.push(product); @@ -50,7 +49,7 @@ router.post('/', handlers.validateNameHandler, (req, res) => { router.put('/:id', handlers.validateNameHandler, resolveProductHandler, (req, res) => { console.log('enter route.put(/:id)'); - const product = req.body as Product; + const product = req.body as IProduct; product.id = res.locals.product.id; Object.assign(res.locals.product, product); diff --git a/hw4/src/store/categories-store.ts b/hw4/src/store/categories-store.ts new file mode 100644 index 0000000..dc826eb --- /dev/null +++ b/hw4/src/store/categories-store.ts @@ -0,0 +1,10 @@ +import { ICategory } from '../models/ICategory'; +import categoriesData from '../data/categories.json'; + +export function getCatrgories(): ICategory[] { + return categoriesData; +} + +export function getCatrgoriesAsync(): Promise { + return Promise.resolve(getCatrgories()); +} diff --git a/hw4/src/store/products-store.ts b/hw4/src/store/products-store.ts new file mode 100644 index 0000000..c15f85e --- /dev/null +++ b/hw4/src/store/products-store.ts @@ -0,0 +1,10 @@ +import { IProduct } from '../models/IProducts'; +import productsData from '../data/products.json'; + +export function getProducts(): IProduct[] { + return productsData; +} + +export function getProductsAsync(): Promise { + return Promise.resolve(getProducts()); +}