From 3ff25caba7a3783c41723bfb3453a909c0500e6f Mon Sep 17 00:00:00 2001 From: Slava Korenblit Date: Sun, 29 Nov 2020 09:41:12 +0200 Subject: [PATCH 1/5] categories --- hw4/src/data/categories.json | 0 hw4/src/index.ts | 2 ++ hw4/src/models/category.ts | 4 +++ hw4/src/routes/category.ts | 67 ++++++++++++++++++++++++++++++++++++ 4 files changed, 73 insertions(+) create mode 100644 hw4/src/data/categories.json create mode 100644 hw4/src/models/category.ts create mode 100644 hw4/src/routes/category.ts diff --git a/hw4/src/data/categories.json b/hw4/src/data/categories.json new file mode 100644 index 0000000..e69de29 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/category.ts b/hw4/src/models/category.ts new file mode 100644 index 0000000..9d15d5f --- /dev/null +++ b/hw4/src/models/category.ts @@ -0,0 +1,4 @@ +export interface Category { + id: string; + name: string; +} diff --git a/hw4/src/routes/category.ts b/hw4/src/routes/category.ts new file mode 100644 index 0000000..dc56b68 --- /dev/null +++ b/hw4/src/routes/category.ts @@ -0,0 +1,67 @@ +import { NextFunction, Request, Response, Router } from 'express'; +import { generateId } from '../utils/id-helper'; +import categoriesData from '../data/categories.json'; +import * as handlers from '../utils/common'; +import { Category } from '../models/category'; + +const categories: Category[] = []; + +const router = Router(); + +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', resolveCategoryHandler, (req, res) => { + console.log('enter route.get(/:id)'); + const category = req.body as Category; + + res.send(category); +}); + +router.post('/', handlers.validateNameHandler, (req, res) => { + console.log('enter route.post(/)'); + const category = req.body as Category; + 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 Category; + 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 }; From 84a301a912f3f8e0ef11af17c245f463519276fe Mon Sep 17 00:00:00 2001 From: Slava Korenblit Date: Sun, 29 Nov 2020 09:51:39 +0200 Subject: [PATCH 2/5] move products and categories to data folder --- hw4/src/data/categories.ts | 6 ++++++ hw4/src/data/products.ts | 6 ++++++ hw4/src/routes/category.ts | 11 ++++++++--- hw4/src/routes/products.ts | 3 +-- 4 files changed, 21 insertions(+), 5 deletions(-) create mode 100644 hw4/src/data/categories.ts create mode 100644 hw4/src/data/products.ts diff --git a/hw4/src/data/categories.ts b/hw4/src/data/categories.ts new file mode 100644 index 0000000..3b63f7c --- /dev/null +++ b/hw4/src/data/categories.ts @@ -0,0 +1,6 @@ +import { Category } from '../models/category'; +import categoriesData from '../data/categories.json'; + +const categories: Category[] = []; + +export { categories }; diff --git a/hw4/src/data/products.ts b/hw4/src/data/products.ts new file mode 100644 index 0000000..fb967f1 --- /dev/null +++ b/hw4/src/data/products.ts @@ -0,0 +1,6 @@ +import { Product } from '../models/products'; +import productsData from '../data/products.json'; + +const products: Product[] = productsData; + +export { products }; diff --git a/hw4/src/routes/category.ts b/hw4/src/routes/category.ts index dc56b68..4f50b45 100644 --- a/hw4/src/routes/category.ts +++ b/hw4/src/routes/category.ts @@ -1,10 +1,8 @@ import { NextFunction, Request, Response, Router } from 'express'; import { generateId } from '../utils/id-helper'; -import categoriesData from '../data/categories.json'; import * as handlers from '../utils/common'; import { Category } from '../models/category'; - -const categories: Category[] = []; +import { categories } from '../data/categories'; const router = Router(); @@ -32,6 +30,13 @@ router.get('/', (req, res) => { res.send(categories); }); +router.get('/:id/products', resolveCategoryHandler, (req, res) => { + console.log('enter route.get(/:id/products)'); + const category = req.body as Category; + + res.send(category); +}); + router.get('/:id', resolveCategoryHandler, (req, res) => { console.log('enter route.get(/:id)'); const category = req.body as Category; diff --git a/hw4/src/routes/products.ts b/hw4/src/routes/products.ts index 0a8a8d5..5017596 100644 --- a/hw4/src/routes/products.ts +++ b/hw4/src/routes/products.ts @@ -1,10 +1,9 @@ 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'; +import { products } from '../data/products'; -const products: Product[] = productsData; const router = Router(); From cc36f3df0e835988b28f2ec9b378fcddab466f89 Mon Sep 17 00:00:00 2001 From: Slava Korenblit Date: Sun, 29 Nov 2020 17:34:56 +0200 Subject: [PATCH 3/5] get('/:id/products' --- hw4/src/data/categories.json | 14 ++++++++++++++ hw4/src/routes/category.ts | 14 +++++++++++++- hw4/src/routes/products.ts | 1 - 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/hw4/src/data/categories.json b/hw4/src/data/categories.json index e69de29..e2bf991 100644 --- a/hw4/src/data/categories.json +++ 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/routes/category.ts b/hw4/src/routes/category.ts index 4f50b45..54bdb65 100644 --- a/hw4/src/routes/category.ts +++ b/hw4/src/routes/category.ts @@ -3,6 +3,8 @@ import { generateId } from '../utils/id-helper'; import * as handlers from '../utils/common'; import { Category } from '../models/category'; import { categories } from '../data/categories'; +import { Product } from '../models/products'; +import { products } from '../data/products'; const router = Router(); @@ -32,13 +34,20 @@ router.get('/', (req, res) => { router.get('/:id/products', resolveCategoryHandler, (req, res) => { console.log('enter route.get(/:id/products)'); + const category = req.body as Category; + const productsForCategory: Product[] = []; - res.send(category); + 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 Category; res.send(category); @@ -46,6 +55,7 @@ router.get('/:id', resolveCategoryHandler, (req, res) => { router.post('/', handlers.validateNameHandler, (req, res) => { console.log('enter route.post(/)'); + const category = req.body as Category; category.id = generateId(); categories.push(category); @@ -55,6 +65,7 @@ router.post('/', handlers.validateNameHandler, (req, res) => { router.put('/:id', handlers.validateNameHandler, resolveCategoryHandler, (req, res) => { console.log('enter route.put(/:id)'); + const category = req.body as Category; category.id = res.locals.category.id; Object.assign(res.locals.category, category); @@ -64,6 +75,7 @@ router.put('/:id', handlers.validateNameHandler, resolveCategoryHandler, (req, r router.delete('/:id', resolveCategoryHandler, (req, res) => { console.log('enter route.delete(/:id)'); + categories.splice(res.locals.categoryIndex, 1); res.sendStatus(204); diff --git a/hw4/src/routes/products.ts b/hw4/src/routes/products.ts index 5017596..6da0bd6 100644 --- a/hw4/src/routes/products.ts +++ b/hw4/src/routes/products.ts @@ -4,7 +4,6 @@ import * as handlers from '../utils/common'; import { Product } from '../models/products'; import { products } from '../data/products'; - const router = Router(); const resolveProductHandler = (req: Request, res: Response, next: NextFunction): void => { From d10a2f47a9767279facaab23a97a3b203e48268d Mon Sep 17 00:00:00 2001 From: Slava Korenblit Date: Wed, 2 Dec 2020 17:44:41 +0200 Subject: [PATCH 4/5] renaming --- hw4/src/data/categories.ts | 6 ------ hw4/src/data/products.ts | 6 ------ hw4/src/models/{category.ts => ICategory.ts} | 2 +- hw4/src/models/{products.ts => IProducts.ts} | 2 +- hw4/src/routes/category.ts | 19 ++++++++++--------- hw4/src/routes/products.ts | 10 +++++----- 6 files changed, 17 insertions(+), 28 deletions(-) delete mode 100644 hw4/src/data/categories.ts delete mode 100644 hw4/src/data/products.ts rename hw4/src/models/{category.ts => ICategory.ts} (52%) rename hw4/src/models/{products.ts => IProducts.ts} (73%) diff --git a/hw4/src/data/categories.ts b/hw4/src/data/categories.ts deleted file mode 100644 index 3b63f7c..0000000 --- a/hw4/src/data/categories.ts +++ /dev/null @@ -1,6 +0,0 @@ -import { Category } from '../models/category'; -import categoriesData from '../data/categories.json'; - -const categories: Category[] = []; - -export { categories }; diff --git a/hw4/src/data/products.ts b/hw4/src/data/products.ts deleted file mode 100644 index fb967f1..0000000 --- a/hw4/src/data/products.ts +++ /dev/null @@ -1,6 +0,0 @@ -import { Product } from '../models/products'; -import productsData from '../data/products.json'; - -const products: Product[] = productsData; - -export { products }; diff --git a/hw4/src/models/category.ts b/hw4/src/models/ICategory.ts similarity index 52% rename from hw4/src/models/category.ts rename to hw4/src/models/ICategory.ts index 9d15d5f..3313b7e 100644 --- a/hw4/src/models/category.ts +++ b/hw4/src/models/ICategory.ts @@ -1,4 +1,4 @@ -export interface Category { +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 index 54bdb65..5ad3699 100644 --- a/hw4/src/routes/category.ts +++ b/hw4/src/routes/category.ts @@ -1,13 +1,14 @@ import { NextFunction, Request, Response, Router } from 'express'; import { generateId } from '../utils/id-helper'; import * as handlers from '../utils/common'; -import { Category } from '../models/category'; -import { categories } from '../data/categories'; -import { Product } from '../models/products'; -import { products } from '../data/products'; +import { ICategory } from '../models/ICategory'; +import { IProduct } from '../models/IProducts'; const router = Router(); +const categories: ICategory[] = []; +const products: IProduct[] = []; + const resolveCategoryHandler = (req: Request, res: Response, next: NextFunction): void => { console.log('enter resolveCategoryHandler'); const categoryId = req.params.id; @@ -35,8 +36,8 @@ router.get('/', (req, res) => { router.get('/:id/products', resolveCategoryHandler, (req, res) => { console.log('enter route.get(/:id/products)'); - const category = req.body as Category; - const productsForCategory: Product[] = []; + const category = req.body as ICategory; + const productsForCategory: IProduct[] = []; products.forEach((p) => { if (p.categoryId === category.id) productsForCategory.push(p); @@ -48,7 +49,7 @@ router.get('/:id/products', resolveCategoryHandler, (req, res) => { router.get('/:id', resolveCategoryHandler, (req, res) => { console.log('enter route.get(/:id)'); - const category = req.body as Category; + const category = req.body as ICategory; res.send(category); }); @@ -56,7 +57,7 @@ router.get('/:id', resolveCategoryHandler, (req, res) => { router.post('/', handlers.validateNameHandler, (req, res) => { console.log('enter route.post(/)'); - const category = req.body as Category; + const category = req.body as ICategory; category.id = generateId(); categories.push(category); @@ -66,7 +67,7 @@ router.post('/', handlers.validateNameHandler, (req, res) => { router.put('/:id', handlers.validateNameHandler, resolveCategoryHandler, (req, res) => { console.log('enter route.put(/:id)'); - const category = req.body as Category; + const category = req.body as ICategory; category.id = res.locals.category.id; Object.assign(res.locals.category, category); diff --git a/hw4/src/routes/products.ts b/hw4/src/routes/products.ts index 6da0bd6..9be8fd4 100644 --- a/hw4/src/routes/products.ts +++ b/hw4/src/routes/products.ts @@ -1,10 +1,10 @@ import { NextFunction, Request, Response, Router } from 'express'; import { generateId } from '../utils/id-helper'; import * as handlers from '../utils/common'; -import { Product } from '../models/products'; -import { products } from '../data/products'; +import { IProduct } from '../models/IProducts'; const router = Router(); +const products: IProduct[] = []; const resolveProductHandler = (req: Request, res: Response, next: NextFunction): void => { console.log('enter resolveProductHandler'); @@ -32,14 +32,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); @@ -48,7 +48,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); From 8469f556179436982ce1c13263e459240bb9acf9 Mon Sep 17 00:00:00 2001 From: Slava Korenblit Date: Wed, 2 Dec 2020 17:55:33 +0200 Subject: [PATCH 5/5] use store utils --- hw4/src/routes/category.ts | 6 ++++-- hw4/src/routes/products.ts | 3 ++- hw4/src/store/categories-store.ts | 10 ++++++++++ hw4/src/store/products-store.ts | 10 ++++++++++ 4 files changed, 26 insertions(+), 3 deletions(-) create mode 100644 hw4/src/store/categories-store.ts create mode 100644 hw4/src/store/products-store.ts diff --git a/hw4/src/routes/category.ts b/hw4/src/routes/category.ts index 5ad3699..afb5dce 100644 --- a/hw4/src/routes/category.ts +++ b/hw4/src/routes/category.ts @@ -3,11 +3,13 @@ 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[] = []; -const products: IProduct[] = []; +const categories: ICategory[] = getCatrgories(); +const products: IProduct[] = getProducts(); const resolveCategoryHandler = (req: Request, res: Response, next: NextFunction): void => { console.log('enter resolveCategoryHandler'); diff --git a/hw4/src/routes/products.ts b/hw4/src/routes/products.ts index 9be8fd4..e348664 100644 --- a/hw4/src/routes/products.ts +++ b/hw4/src/routes/products.ts @@ -2,9 +2,10 @@ import { NextFunction, Request, Response, Router } from 'express'; import { generateId } from '../utils/id-helper'; import * as handlers from '../utils/common'; import { IProduct } from '../models/IProducts'; +import { getProducts } from '../store/products-store'; const router = Router(); -const products: IProduct[] = []; +const products: IProduct[] = getProducts(); const resolveProductHandler = (req: Request, res: Response, next: NextFunction): void => { console.log('enter resolveProductHandler'); 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()); +}