-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathproduct-controller.js
More file actions
57 lines (47 loc) · 1.48 KB
/
product-controller.js
File metadata and controls
57 lines (47 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import { request, response } from 'express';
import Product from '../model/product-schema.js';
export const getProducts = async (request, response) => {
try{
let product = await Product.find();
response.json(product);
}catch(error){
response.json({message: error.message});
}
}
export const addProduct = async (request, response) => {
const product = request.body;
const newProduct = new Product(product);
try{
await newProduct.save();
response.json(newProduct);
}catch(error){
response.json({message: error.message});
}
}
export const getProductById = async(request, response) =>{
const id = request.params.id;
try{
const product = await Product.findById(id);
response.json(product);
}catch(error){
response.json({message: error.message});
}
}
export const editProduct = async (request,response) => {
const product = request.body;
const editProduct = new Product(product);
try{
await Product.updateOne({ _id: request.params.id}, editProduct);
response.json(editProduct);
}catch(error) {
response.json({message: error.message});
}
}
export const deleteProduct = async (request, response) => {
try{
await Product.deleteOne({ _id: request.params.id});
response.json("Product Deleted Successfully");
}catch(error) {
response.json({message: error.message});
}
}