From a121ad15ce9d42e98c1a978e53ffd5a9a3f65995 Mon Sep 17 00:00:00 2001 From: Swarnendu Date: Sun, 24 Mar 2024 08:39:54 +0530 Subject: [PATCH] Products --- .gitignore | 1 + package-lock.json | 29 +++++----- package.json | 6 +-- src/app.ts | 20 ++++--- src/controllers/product.ts | 108 +++++++++++++++++++++++++++++++++++++ 5 files changed, 140 insertions(+), 24 deletions(-) create mode 100644 src/controllers/product.ts diff --git a/.gitignore b/.gitignore index a547bf3..1e81c07 100644 --- a/.gitignore +++ b/.gitignore @@ -7,6 +7,7 @@ yarn-error.log* pnpm-debug.log* lerna-debug.log* +.env node_modules dist dist-ssr diff --git a/package-lock.json b/package-lock.json index 73247ac..8fe69cb 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,15 +9,15 @@ "version": "1.0.0", "license": "ISC", "dependencies": { - "@types/validator": "^13.11.9", "dotenv": "^16.4.5", - "express": "^4.18.3", - "mongoose": "^8.2.1", + "express": "^4.19.1", + "mongoose": "^8.2.2", "node-cache": "^5.1.2", "validator": "^13.11.0" }, "devDependencies": { "@types/express": "^4.17.21", + "@types/validator": "^13.11.9", "nodemon": "^3.1.0" } }, @@ -129,7 +129,8 @@ "node_modules/@types/validator": { "version": "13.11.9", "resolved": "https://registry.npmjs.org/@types/validator/-/validator-13.11.9.tgz", - "integrity": "sha512-FCTsikRozryfayPuiI46QzH3fnrOoctTjvOYZkho9BTFLCOZ2rgZJHMOVgCOfttjPJcgOx52EpkY0CMfy87MIw==" + "integrity": "sha512-FCTsikRozryfayPuiI46QzH3fnrOoctTjvOYZkho9BTFLCOZ2rgZJHMOVgCOfttjPJcgOx52EpkY0CMfy87MIw==", + "dev": true }, "node_modules/@types/webidl-conversions": { "version": "7.0.3", @@ -345,9 +346,9 @@ } }, "node_modules/cookie": { - "version": "0.5.0", - "resolved": "https://registry.npmjs.org/cookie/-/cookie-0.5.0.tgz", - "integrity": "sha512-YZ3GUyn/o8gfKJlnlX7g7xq4gyO6OSuhGPKaaGssGB2qgDUS0gPgtTvoyZLTt9Ab6dC4hfc9dV5arkvc/OCmrw==", + "version": "0.6.0", + "resolved": "https://registry.npmjs.org/cookie/-/cookie-0.6.0.tgz", + "integrity": "sha512-U71cyTamuh1CRNCfpGY6to28lxvNwPG4Guz/EVjgf3Jmzv0vlDp1atT9eS5dDjMYHucpHbWns6Lwf3BKz6svdw==", "engines": { "node": ">= 0.6" } @@ -468,16 +469,16 @@ } }, "node_modules/express": { - "version": "4.18.3", - "resolved": "https://registry.npmjs.org/express/-/express-4.18.3.tgz", - "integrity": "sha512-6VyCijWQ+9O7WuVMTRBTl+cjNNIzD5cY5mQ1WM8r/LEkI2u8EYpOotESNwzNlyCn3g+dmjKYI6BmNneSr/FSRw==", + "version": "4.19.1", + "resolved": "https://registry.npmjs.org/express/-/express-4.19.1.tgz", + "integrity": "sha512-K4w1/Bp7y8iSiVObmCrtq8Cs79XjJc/RU2YYkZQ7wpUu5ZyZ7MtPHkqoMz4pf+mgXfNvo2qft8D9OnrH2ABk9w==", "dependencies": { "accepts": "~1.3.8", "array-flatten": "1.1.1", "body-parser": "1.20.2", "content-disposition": "0.5.4", "content-type": "~1.0.4", - "cookie": "0.5.0", + "cookie": "0.6.0", "cookie-signature": "1.0.6", "debug": "2.6.9", "depd": "2.0.0", @@ -925,9 +926,9 @@ } }, "node_modules/mongoose": { - "version": "8.2.1", - "resolved": "https://registry.npmjs.org/mongoose/-/mongoose-8.2.1.tgz", - "integrity": "sha512-UgZZbXSJH0pdU936qj3FyVI+sBsMoGowFnL5R/RYrA50ayn6+ZYdVr8ehsRgNxRcMYwoNld5XzHIfkFRJTePEw==", + "version": "8.2.2", + "resolved": "https://registry.npmjs.org/mongoose/-/mongoose-8.2.2.tgz", + "integrity": "sha512-6sMxe1d3k/dBjiOX4ExNTNOP0g1x0iq8eXyg+ttgIXM3HLnQ0IUyXRwVVAPFFY6O4/8uYN5dB0Ec72FrexbPpw==", "dependencies": { "bson": "^6.2.0", "kareem": "2.5.1", diff --git a/package.json b/package.json index b5b1ba1..35cf627 100644 --- a/package.json +++ b/package.json @@ -14,15 +14,15 @@ "author": "", "license": "ISC", "dependencies": { - "@types/validator": "^13.11.9", "dotenv": "^16.4.5", - "express": "^4.18.3", - "mongoose": "^8.2.1", + "express": "^4.19.1", + "mongoose": "^8.2.2", "node-cache": "^5.1.2", "validator": "^13.11.0" }, "devDependencies": { "@types/express": "^4.17.21", + "@types/validator": "^13.11.9", "nodemon": "^3.1.0" } } diff --git a/src/app.ts b/src/app.ts index d535f96..aeb49ad 100644 --- a/src/app.ts +++ b/src/app.ts @@ -1,18 +1,24 @@ -import express from "express"; // Import Express module -import userRoute from "./routes/user.js"; // Assuming user.js is in a subfolder named routes +import express from "express"; import NodeCache from "node-cache" +import bodyParser from "body-parser" -const app = express(); // Create an Express application instance +const app = express(); export const myCache = new NodeCache -app.get("/", (req, res) => { // Route handler for the root path ("/") - res.send("API is working with api/v1"); // Send a response message +app.use(bodyParser.json()); +app.use(bodyParser.urlencoded({ extended: true })); + +app.get("/", (req, res) => { + res.send("API is working with api/v1"); }); + +import userRoute from "./routes/user.js"; +import orderRoute from "./routes/order.js" -app.use("/api/v1/user", userRoute); // Mount the user routes at the specified path - +app.use("/api/v1/user", userRoute); +app.use("/api/v1/order", orderRoute); export {app} \ No newline at end of file diff --git a/src/controllers/product.ts b/src/controllers/product.ts new file mode 100644 index 0000000..fd6710c --- /dev/null +++ b/src/controllers/product.ts @@ -0,0 +1,108 @@ +import { myCache } from "../app.js"; +import { Product } from "../models/produc.js"; +import { ApiError } from "../utils/ApiError.js"; +import { asyncHandler } from "../utils/asyncHandler.js"; +import { NewUserRequestBody } from "../types/types.js"; +import { Request } from "express"; +import {rm } from "fs" +import { invalidateCache } from "../utils/features.js"; + + +export const getlatestProducts = asyncHandler(async (req, res, next) => { + let products; + + if (myCache.has("latest-products")) + products = JSON.parse(myCache.get("latest-products") as string); + else { + products = await Product.find({}).sort({ createdAt: -1 }).limit(5); + myCache.set("latest-products", JSON.stringify(products)); + } + + return res.status(200).json({ + success: true, + products, + }); + }); + + export const getAllCategories = asyncHandler(async (req, res, next) => { + let categories; + + if (myCache.has("categories")) + categories = JSON.parse(myCache.get("categories") as string); + else { + categories = await Product.distinct("category"); + myCache.set("categories", JSON.stringify(categories)); + } + + return res.status(200).json({ + success: true, + categories, + }); + }); + + export const getAdminProducts = asyncHandler(async (req, res, next) => { + let products; + if (myCache.has("all-products")) + products = JSON.parse(myCache.get("all-products") as string); + else { + products = await Product.find({}); + myCache.set("all-products", JSON.stringify(products)); + } + + return res.status(200).json({ + success: true, + products, + }); + }); + + export const getSingleProduct = asyncHandler(async (req, res, next) => { + let product; + const id = req.params.id; + if (myCache.has(`product-${id}`)) + product = JSON.parse(myCache.get(`product-${id}`) as string); + else { + product = await Product.findById(id); + + if (!product) return next(new ApiError(404,"Product Not Found")); + + myCache.set(`product-${id}`, JSON.stringify(product)); + } + + return res.status(200).json({ + success: true, + product, + }); + }); + + export const newProduct = asyncHandler( + async(req: Request<{}, {}, NewUserRequestBody>, res, next)=>{ + const { name, price, stock, category } = req.body; + const photo = req.file; + + if(!photo) throw new ApiError(400, "Please add photo") + + if(!photo || !price || !stock || !category){ + rm(photo.path, ()=>{ + console.log("Deleted"); + + }) + throw new ApiError(400, "Abe Sare Fields Dal ", 400) + } + + await Product.create({ + name, + price, + stock, + category: category.toLowerCase(), + photo: photo.path, + }) + + invalidateCache({product: true, admin: true}) + + return res.status(201).json({ + success: true, + message: "Product Created Successfully" + }) + + } + ) \ No newline at end of file