-
Notifications
You must be signed in to change notification settings - Fork 1
/
api.js
45 lines (37 loc) · 903 Bytes
/
api.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
const express = require("express");
const app = express.Router();
const db = require("./db");
const funcs = require("./helpers");
const errHandler = (req, res, next) => {
const ID = req.params.id;
if (Number.isNaN(parseInt(ID))) {
return res.status(400).send("ID parametresi yanlış girildi");
}
const isExist = funcs.findDiaryIndex(db.diaryData, ID);
if (isExist == -1) {
return res.status(204).send("no contentt");
}
next();
};
app.get("", (req, res) => {
res.json({
status: "ok",
data: db.diaryData,
});
});
app.post("", (req, res) => {
const newDiary = db.addDiary(req);
res.json({
stat: "eklendi",
data: newDiary,
});
});
app.delete("/:id", errHandler, (req, res) => {
db.deleteDiary(req);
res.json({ data: db.diaryData });
});
app.patch("/:id", errHandler, (req, res) => {
db.updateDiary(req);
res.send();
});
module.exports = app;