-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
155 lines (139 loc) · 3.22 KB
/
Copy pathserver.js
File metadata and controls
155 lines (139 loc) · 3.22 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
144
145
146
147
148
149
150
151
152
153
154
155
import Koa from 'koa';
import Router from 'koa-router';
// import convert from 'koa-convert';
import cors from 'koa-cors';
import bodyParser from 'koa-body-parser';
import mongoose from 'mongoose';
import * as OrderLoader from './src/loaders/OrderLoader';
import * as IssueLoader from './src/loaders/IssueLoader';
mongoose.connect('mongodb://localhost/fullstack-rest');
const PORT = process.env.PORT || 5000;
const app = new Koa();
const router = new Router();
const koaOptions = {
origin: '*',
};
router.get('/', async (ctx) => {
ctx.body = {
status: 'success',
message: 'server is on!',
};
});
router.get('/orders', async (ctx) => {
try {
const orders = await OrderLoader.loadAll();
ctx.body = {
status: 'success',
data: orders,
};
} catch (err) {
console.log(err);
}
});
router.get('/order/:id', async (ctx) => {
if (!ctx.params || !ctx.params.id) {
ctx.status = 400;
ctx.body = {
status: 'error',
message: 'Invalid order id',
};
return;
}
try {
const order = await OrderLoader.load({ id: ctx.params.id });
ctx.body = {
status: 'success',
data: order,
};
} catch (err) {
ctx.status = 500;
ctx.body = {
status: 'error',
message: 'Something went wrong while trying to get the order!',
};
}
});
router.put('/orderEdit/:id', async (ctx) => {
if (!ctx.params || !ctx.params.id) {
ctx.status = 400;
ctx.body = {
status: 'error',
message: 'Invalid order id',
};
return;
}
try {
const order = await OrderLoader.edit({ id: ctx.params.id, values: ctx.request.body });
ctx.body = {
status: 'success',
data: order,
};
} catch (err) {
ctx.status = 500;
ctx.body = {
status: 'error',
message: 'Something went wrong while trying to edit the order!',
};
}
});
router.post('/orderAdd', async (ctx) => {
if (!ctx.request || !ctx.request.body) {
ctx.status = 400;
ctx.body = {
status: 'error',
message: 'Invalid order data',
};
return;
}
try {
const order = await OrderLoader.add({ values: ctx.request.body });
if (!order) {
ctx.status = 400;
ctx.body = {
status: 'error',
message: 'Invalid order data',
};
return;
}
ctx.body = {
status: 'success',
data: order,
};
} catch (err) {
ctx.status = 500;
ctx.body = {
status: 'error',
message: 'Something went wrong while trying to add the order!',
};
}
});
router.get('/issues/:id', async (ctx) => {
if (!ctx.params || !ctx.params.id) {
ctx.status = 400;
ctx.body = {
status: 'error',
message: 'Invalid order id',
};
return;
}
try {
const issues = await IssueLoader.loadByOrder({ orderId: ctx.params.id });
ctx.body = {
status: 'success',
data: issues,
};
} catch (err) {
ctx.status = 500;
ctx.body = {
status: 'error',
message: 'Something went wrong while trying to get the issues!',
};
}
});
// app.use(router.routes()).use(router.allowedMethods());
app.use(cors(koaOptions));
app.use(bodyParser());
app.use(router.routes());
app.listen(PORT, () => {
console.info('Server running on port %s', PORT);
});