-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadminController.js
102 lines (90 loc) · 2.28 KB
/
adminController.js
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import initAdminModel from "../models/adminModel.js";
import { initDBConnection } from "../../db/db-connection.js";
const {
getAllItem,
addItem,
toggleActiveStatus,
updateItem,
getMessage,
deleteMessage,
} = initAdminModel(initDBConnection());
// import getAdminModel from "../models/adminModel.js";
// const {
// getAllItem,
// addItem,
// toggleActiveStatus,
// updateItem,
// getMessage,
// deleteMessage,
// } = getAdminModel()
export const getItemController = async (req, res) => {
try {
const item = await getAllItem();
return res.json(item);
} catch (error) {
console.log(error);
return res.status(400).json({ error });
}
};
// Post request
export const addItemController = async (req, res) => {
const categoryid = Number(req.body.categoryid);
console.log(categoryid);
const newItem = {
itemName: req.body.itemname,
price: req.body.price,
categoryid: categoryid,
};
try {
const item = await addItem(newItem);
res.json(item.rows[0]);
} catch (error) {
console.log(error);
return res.status(400).json({ error });
}
};
export const toggleStatusController = async (req, res) => {
const id = Number.parseInt(req.params.id, 10);
console.log(id);
try {
const item = await toggleActiveStatus(id);
res.json(item.rows[0]);
} catch (error) {
console.log(error);
return res.status(400).json({ error });
}
};
// update item request
export const updateItemController = async (req, res) => {
const id = Number.parseInt(req.params.id, 10);
const items = req.body;
console.log(items);
try {
const item = await updateItem(id, items);
res.json(item.rows[0]);
} catch (error) {
console.log(error);
return res.status(400).json({ error });
}
};
// Get request Message
export const getMessageController = async (req, res) => {
try {
const { rows: messages } = await getMessage();
return res.json(messages);
} catch (error) {
console.log(error);
return res.status(400).json({ error });
}
};
// Delete request
export const deleteMessageController = async (req, res) => {
const id = parseInt(req.params.id);
try {
await deleteMessage(id);
res.send({ status: "success" });
} catch (error) {
console.log(error);
return res.status(400).json({ error });
}
};