forked from SchoolOfCode/national-project-week-jlan-r15
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
102 lines (91 loc) · 2.26 KB
/
app.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 express from "express";
import "dotenv/config";
import cors from "cors";
const app = express();
app.use(cors());
const PORT = process.env.PORT;
import {
getLinks,
updateLinkByID,
getLinkByID,
createLink,
deleteLinkByID,
//getLinksByQuery,
} from "./models/functions.js";
app.use(express.static("public"));
app.use(express.json());
/*our front-end */
app.get("/", function (req, res) {
res.json({ message: "its working" });
});
//
// app.get("/links", async function (req, res) {
// console.log(req.query, req.params);
// const { name, topic, title } = req.query;
// if (topic) {
// const data = await getLinksByQuery(topic);
// console.log("test", data);
// return res.json({ success: true, payload: data });
// }
// if (name) {
// const linkName = await getLinkByID(name);
// return res.json({ success: true, payload: linkName });
// }
// if (title) {
// const data = await getLinksByQuery(null, title);
// return res.json({ success: true, payload: data });
// } else {
// return res.json({
// success: false,
// payload: null,
// });
// }
// });
app.get("/links/:id", async function (req, res) {
console.log(req.params.id);
var linkByID = await getLinkByID(req.params.id);
if (linkByID) {
return res.json({
success: true,
payload: linkByID,
});
} else {
return res.json({
success: false,
});
}
});
app.get("/links", async function (req, res) {
const allLinks = await getLinks();
res.json({
success: true,
payload: allLinks,
});
});
app.post("/links", async function (req, res) {
let newLinks = req.body;
//newLinks = await createLink();
const result = await createLink(newLinks);
return res.json({
success: true,
//payload: newLinks,
payload: result,
});
});
app.put("/links/:id", async function (req, res) {
let updatedLink = await updateLinkByID(req.params.id, req.body);
return res.json({
success: true,
payload: updatedLink,
});
});
app.delete("/links/:id", async function (req, res) {
const deletedLink = await deleteLinkByID(req.params.id);
return res.json({
success: true,
payload: deletedLink,
});
});
app.listen(PORT, () => {
console.log(`Example app listening at http://localhost:${PORT}`);
});