From 76135155c1ed5ee16fc344b438ac4639295fbe4e Mon Sep 17 00:00:00 2001 From: Aqeeb Imtiaz Harun Date: Tue, 15 Sep 2020 21:46:05 +0600 Subject: [PATCH 1/4] add server, routes & Product Types --- routes.ts | 19 +++++++++++++++++++ server.ts | 12 ++++++++++++ types.ts | 6 ++++++ 3 files changed, 37 insertions(+) create mode 100644 routes.ts create mode 100644 server.ts create mode 100644 types.ts diff --git a/routes.ts b/routes.ts new file mode 100644 index 0000000..0d66050 --- /dev/null +++ b/routes.ts @@ -0,0 +1,19 @@ +import { Router } from "https://deno.land/x/oak@/mod.ts"; +import { + getProducts, + getProduct, + addProduct, + updateProduct, + deleteProduct, +} from "./controllers/products.ts"; + +const router = new Router(); + +router + .get("/api/v1/products", getProducts) + .get("/api/v1/products/:id", getProduct) + .post("/api/v1/products", addProduct) + .put("/api/v1/products/:id", updateProduct) + .delete("/api/v1/products/:id", deleteProduct); + +export default router; diff --git a/server.ts b/server.ts new file mode 100644 index 0000000..ed2412b --- /dev/null +++ b/server.ts @@ -0,0 +1,12 @@ +import { Application } from "https://deno.land/x/oak@/mod.ts"; +import router from "./routes.ts"; + +const port = 5000; +const app = new Application(); + +app.use(router.routes()); +app.use(router.allowedMethods()); + +console.log(`Server running on port: ${port}`); + +await app.listen({ port }); diff --git a/types.ts b/types.ts new file mode 100644 index 0000000..b9d3998 --- /dev/null +++ b/types.ts @@ -0,0 +1,6 @@ +export interface Product { + id: string; + name: string; + description: string; + price: number; +} From bc45e9dbec5af647fab176898ef3b3123ca41baa Mon Sep 17 00:00:00 2001 From: Aqeeb Imtiaz Harun Date: Tue, 15 Sep 2020 21:46:41 +0600 Subject: [PATCH 2/4] add getProducts, getProduct, addProduct methods --- controllers/products.ts | 102 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 controllers/products.ts diff --git a/controllers/products.ts b/controllers/products.ts new file mode 100644 index 0000000..1f4c88d --- /dev/null +++ b/controllers/products.ts @@ -0,0 +1,102 @@ +import { v4 } from "https://deno.land/std/uuid/mod.ts"; +import { Product } from "../types.ts"; + +let products: Product[] = [ + { + id: "1", + name: "Product One", + description: "This is product one", + price: 29.99, + }, + { + id: "2", + name: "Product Two", + description: "This is product two", + price: 39.99, + }, + { + id: "3", + name: "Product Three", + description: "This is product three", + price: 59.99, + }, +]; + +// @description: Get all products +// @route: GET /api/v1/products +const getProducts = ({ response }: { response: any }) => { + response.body = { + success: true, + data: products, + }; +}; + +// @description: Get single product +// @route: GET /api/v1/product/:id +const getProduct = ({ params, response }: { params: { id: string }, response: any }) => { + const product : Product | undefined = products.find( p => p.id === params.id); + + if (product) { + response.status = 200 + response.body = { + success: true, + data: product, + }; + } + else { + response.status = 404; + response.body = { + success: true, + message: 'No Product found.', + }; + } + +}; + +// @description: Add new product +// @route: POST /api/v1/product +const addProduct = async ({ request, response }: { request: any, response: any }) => { + const body = await request.body(); + + if(!request.hasBody) { + response.status = 400 + response.body = { + success: true, + message: "No Data.", + }; + } + else { + const product: Product = body.value; + + product.id = v4.generate(); + + products.push(product); + + response.status = 201 + response.body = { + success: true, + data: product, + }; + } + +}; + +// @description: Update single product +// @route: PUT /api/v1/product/:id +const updateProduct = ({ response }: { response: any }) => { + response.body = { + success: true, + data: products, + }; +}; + +// @description: Delete single product +// @route: DELETE /api/v1/product/:id +const deleteProduct = ({ response }: { response: any }) => { + response.body = { + success: true, + data: products, + }; +}; + +export { getProducts, getProduct, addProduct, updateProduct, deleteProduct }; From 7b6fc981663789c78e9968e6616be15e6a05ce3f Mon Sep 17 00:00:00 2001 From: Aqeeb Imtiaz Harun Date: Sun, 20 Sep 2020 21:37:55 +0600 Subject: [PATCH 3/4] add update product method --- controllers/products.ts | 45 +++++++++++++++++++++++++++++++++++------ 1 file changed, 39 insertions(+), 6 deletions(-) diff --git a/controllers/products.ts b/controllers/products.ts index 1f4c88d..0d353e9 100644 --- a/controllers/products.ts +++ b/controllers/products.ts @@ -50,7 +50,6 @@ const getProduct = ({ params, response }: { params: { id: string }, response: an message: 'No Product found.', }; } - }; // @description: Add new product @@ -83,11 +82,45 @@ const addProduct = async ({ request, response }: { request: any, response: any } // @description: Update single product // @route: PUT /api/v1/product/:id -const updateProduct = ({ response }: { response: any }) => { - response.body = { - success: true, - data: products, - }; +const updateProduct = async ({ + params, + request, + response, +}: { + params: { id: string }; + request: any; + response: any; +}) => { + const product: Product | undefined = products.find( + (p) => p.id === params.id + ); + + if (product) { + const body = await request.body(); + + const updateData: { + name?: string; + description?: string; + price?: number; + } = body.value; + + products = products.map((p) => + p.id === params.id ? { ...p, ...updateData } : p + ); + + response.status = 200; + + response.body = { + success: true, + data: products, + }; + } else { + response.status = 404; + response.body = { + success: true, + message: "No Product found.", + }; + } }; // @description: Delete single product From 3033747ed07b0b43af74a0a27dc7115e161b44d7 Mon Sep 17 00:00:00 2001 From: Aqeeb Imtiaz Harun Date: Sun, 20 Sep 2020 21:53:28 +0600 Subject: [PATCH 4/4] add delete product method & change update product method's error response --- controllers/products.ts | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/controllers/products.ts b/controllers/products.ts index 0d353e9..7ce7d96 100644 --- a/controllers/products.ts +++ b/controllers/products.ts @@ -109,7 +109,6 @@ const updateProduct = async ({ ); response.status = 200; - response.body = { success: true, data: products, @@ -117,19 +116,26 @@ const updateProduct = async ({ } else { response.status = 404; response.body = { - success: true, - message: "No Product found.", + success: false, + msg: "No product found", }; } }; // @description: Delete single product // @route: DELETE /api/v1/product/:id -const deleteProduct = ({ response }: { response: any }) => { - response.body = { - success: true, - data: products, - }; +const deleteProduct = ({ + params, + response, +}: { + params: { id: string }; + response: any; +}) => { + products = products.filter( p => p.id !== params.id ) + response.body = { + success: true, + msg: "Product removed.", + }; }; export { getProducts, getProduct, addProduct, updateProduct, deleteProduct };