-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
115 lines (98 loc) · 3.38 KB
/
Copy pathserver.js
File metadata and controls
115 lines (98 loc) · 3.38 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
//Author: Victor A.
//Las updated: 2022-08-23
require("dotenv").config();
const mongoDBConnectionString = process.env.MONGODB_CONNECTION_STRING;
const HTTP_PORT = process.env.PORT || 3000;
const express= require("express");
const bodyParser = require("body-parser");
const cors = require("cors");
const dataService = require("./data-services.js");
//const mongoose = require("mongoose");
const data = dataService(mongoDBConnectionString);
const app = express();
// Add support for incoming JSON entities
app.use(bodyParser.json());
// Add support for CORS
app.use(cors());
// ************* API Routes************
// POST /api/sales (NOTE: This route must read the contents of the request body)
app.post("/api/sales",(req,res)=>{
data.addNewSale(req.body)
.then((data)=>{
res.json({message:"Sale " + data + " added succesfully"});
console.log("ADDED SUCCESFULLY: " + data);
})
.catch((err)=>{
console.log("THERE WAS AN ERROR");
res.status(500).end();
})
});
//GET ALL
// GET /api/sales (NOTE: This route must accept the numeric query parameters "page" and "perPage", ie: /api/sales?page=1&perPage=5 )
app.get("/api/sales",(req,res)=>{
data.getAllSales().then((data)=>{
console.log("LOADED SUCCESFULLY!");
res.json(data);
})
.catch((err)=>{
console.log("DID NOT LOAD BECAUSE: ");
res.status(500).end();
})
});
//GET ONE WITH ID
// GET /api/sales (NOTE: This route must accept a numeric route parameter, ie: /api/sales/5bd761dcae323e45a93ccfe8)
app.get("/api/sales/:_id", (req,res) => {
data.getSalesById(req.params._id).then((data)=>{
if(data.length > 0){
console.log("GET ONE LOADED SUCCESFULLY");
res.json(data);
}else{
console.log("GET ONE LOADED SUCCESFULLY BUT NOT MATCH");
res.status(404).end();
}
})
.catch((err)=>{
console.log("GET ONE DID NOT LOAD");
res.status(500).end();
})
});
// PUT /api/sales (NOTE: This route must accept a numeric route parameter, ie: /api/sales/5bd761dcae323e45a93ccfe8 as well as read the contents of the request body)
app.put("/api/sales/:_id", (req, res) => {
data.updateSaleById(req.params._id, req.body)
.then((data)=>{
console.log("UPDATED SUCCESSFULLY");
res.json({"message": data });
})
.catch((err)=>{
console.log("NOT UPDATED");
res.status(500).end();
})
});
// DELETE /api/sales (NOTE: This route must accept a numeric route parameter, ie: /api/sales/5bd761dcae323e45a93ccfe8)
app.delete("/api/sales/:_id", (req, res) => {
data.deleteSaleById(req.params._id).then((data)=>{
if(data.length > 0){
console.log("DELETED SUCCESFULLY");
// res.json(data);
res.json({"message": data });
}else{
console.log("DELETION LOADED SUCCESFULLY BUT NOT MATCH");
res.status(404).end();
}
})
.catch((err)=>{
console.log("NOT DELETED");
res.status(500).end();
})
});
// Catch-All 404 error
app.use((req, res) => {
res.status(404).end();
});
// ************* Initialize the Service & Start the Server**********************
data.initialize().then(()=>{
app.listen(HTTP_PORT, ()=>{console.log(`server listening on: ${HTTP_PORT}`)});
}).catch((err)=>{
console.log("unable to start the server: ", err.message);
process.exit();
});