-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
143 lines (132 loc) · 3.37 KB
/
server.js
File metadata and controls
143 lines (132 loc) · 3.37 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
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
const express = require("express");
const { body, validationResult } = require("express-validator");
const cors = require("cors");
const mongoose = require("mongoose");
require("dotenv").config();
const app = express();
app.use(express.json());
app.use(cors());
let expenses = [];
mongoose
.connect(process.env.MONGO_URL, {
useNewUrlParser: true,
useUnifiedTopology: true,
})
.then(() => console.log("Connected to mongoDB"))
.catch(() => console.log("Could not connect to mongoDB"));
const expenseSchema = mongoose.Schema({
title: { type: String, required: true },
amount: { type: Number, required: true },
date: { type: Date, required: true },
});
const Expense = mongoose.model("Expense", expenseSchema);
app.get("/", async (req, res) => {
const expenses = await Expense.find();
res.json({
data: expenses,
message: "ok",
});
});
app.post(
"/addExpense",
[
body("title").trim().escape().notEmpty().withMessage("Title is required"),
body("amount")
.trim()
.escape()
.notEmpty()
.withMessage("Amount is required")
.isFloat({ min: 0.01 })
.withMessage("Amount must be a number greater than 0"),
body("date", "date Must not be empty")
.trim()
.escape()
.notEmpty()
.withMessage("Date is required")
.isISO8601()
.withMessage("Invalid date format!"),
],
(req, res) => {
const errors = validationResult(req);
if (!errors.isEmpty()) {
return res.status(400).json({
data: null,
error: errors,
message: "Validation error!",
});
}
let newExpense = new Expense({
title: req.body.title,
amount: req.body.amount,
date: req.body.date,
});
newExpense.save();
newExpense = res.status(201).json({
data: newExpense,
message: "Expense added successfully.",
});
}
);
app.put(
"/updateExpense/:id",
[
body("title").trim().escape().notEmpty().withMessage("Title is required"),
body("amount")
.trim()
.escape()
.notEmpty()
.withMessage("Amount is required")
.isFloat({ min: 0.01 })
.withMessage("Amount must be a number greater than 0"),
body("date", "date Must not be empty")
.trim()
.escape()
.notEmpty()
.withMessage("Date is required")
.isISO8601()
.withMessage("Invalid date format!"),
],
async (req, res) => {
const errors = validationResult(req);
if (!errors.isEmpty()) {
return res.status(400).json({
data: null,
error: errors,
message: "Validation error!",
});
}
const expense = await Expense.findByIdAndUpdate(
req.params.id,
{
title: req.body.title,
amount: req.body.amount,
date: req.body.date,
},
{ new: true }
);
if (!expense) {
return res.status(404).json({
data: null,
message: "Expense not found",
});
}
res.status(200).json({
message: "Expense updated successfully!",
});
}
);
app.delete("/deleteExpense/:id", async (req, res) => {
const expense = await Expense.findByIdAndRemove(req.params.id);
if (!expense) {
return res.status(404).json({
data: null,
message: "Expense not found",
});
}
res.status(200).json({
message: "Expense deleted successfully!",
});
});
app.listen(3030, () => {
console.log("Listening on port 3030");
});