-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
102 lines (79 loc) · 2.84 KB
/
server.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
/*********************************************************************************
* * WEB422 – Assignment 1
* * I declare that this assignment is my own work in accordance with Seneca Academic Policy.
* * No part of this assignment has been copied manually or electronically from any other source
* * (including web sites) or distributed to other students.
* *
* * Name: Badal Sarkar Student ID: 137226189 Date: January 13, 2020
* * Heroku Link: https://secret-cliffs-75145.herokuapp.com/api/sales?page=1&perPage=5
* ********************************************************************************/
const express = require("express");
const cors = require("cors");
const bodyParser = require("body-parser");
const dataService = require("./modules/data-service.js");
const myData = dataService('');
const app = express();
app.use(cors());
app.use(bodyParser.json());
const HTTP_PORT = process.env.PORT || 8080;
// ************* API Routes
// POST /api/sales (NOTE: This route must read the contents of the request body)
app.post('/api/sales', (req,res)=>{
let data= req.body;
myData.addNewSale(data).
then(msg=>{
res.json(msg);
}).
catch(err=>{
res.json(err);
});
});
// 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)=>{
let query= req.query;
myData.getAllSales(query.page, query.perPage).
then(sales=>{
res.json(sales);
}).
catch(err=>{
res.json(err);
});
});
// GET /api/sales (NOTE: This route must accept a numeric route parameter, ie: /api/sales/5bd761dcae323e45a93ccfe8)
app.get('/api/sales/:id', (req,res)=>{
myData.getSaleById(req.params.id).
then(sale=>{
res.json(sale);
}).
catch(err=>{
res.json(err);
});
});
// 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)=>{
myData.updateSaleById(req.body, req.params.id).
then(data=>{
res.json(data);
}).
catch(err=>{
res.json(err);
});
});
// DELETE /api/sales (NOTE: This route must accept a numeric route parameter, ie: /api/sales/5bd761dcae323e45a93ccfe8)
app.delete('/api/sales/:id', (req,res)=>{
myData.deleteSaleById(req.params.id).
then(data=>{
res.json(data);
}).
catch(err=>{
res.json(err);
});
});
// ************* Initialize the Service & Start the Server
myData.initialize().then(()=>{
app.listen(HTTP_PORT,()=>{
console.log(`server listening on: ${HTTP_PORT}`);
});
}).catch((err)=>{
console.log(err);
});