Skip to content

Commit 55dcdf7

Browse files
committed
Add v5 route for crew schema change
1 parent d6f7280 commit 55dcdf7

File tree

5 files changed

+201
-10
lines changed

5 files changed

+201
-10
lines changed

models/launches.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,16 @@ const launchSchema = new mongoose.Schema({
9999
}],
100100
},
101101
crew: [{
102-
type: mongoose.ObjectId,
103-
ref: 'Crew',
102+
_id: false,
103+
crew: {
104+
type: mongoose.ObjectId,
105+
ref: 'Crew',
106+
default: null,
107+
},
108+
role: {
109+
type: String,
110+
default: null,
111+
},
104112
}],
105113
ships: [{
106114
type: mongoose.ObjectId,

routes/launches/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22

33
module.exports = [
44
require('./v4'),
5+
require('./v5'),
56
];
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/* eslint-disable no-underscore-dangle */
2+
const buildCrew = (launch) => launch.crew.map((crew) => {
3+
if (crew?.crew) {
4+
return crew?.crew;
5+
}
6+
return crew;
7+
});
8+
9+
module.exports = async (payload) => {
10+
if (Array.isArray(payload)) {
11+
return payload.map((launch) => ({
12+
...launch.toObject(),
13+
crew: buildCrew(launch.toObject()),
14+
}));
15+
}
16+
if (Array.isArray(payload?.docs)) {
17+
const docs = payload.docs.map((launch) => ({
18+
...launch.toObject(),
19+
crew: buildCrew(launch.toObject()),
20+
}));
21+
return {
22+
...payload,
23+
docs,
24+
};
25+
}
26+
return {
27+
...payload.toObject(),
28+
crew: buildCrew(payload.toObject()),
29+
};
30+
};

routes/launches/v4/index.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
const Router = require('koa-router');
22
const { Launch } = require('../../../models');
33
const { auth, authz, cache } = require('../../../middleware');
4+
const transformResponse = require('./_transform-response');
45

56
const router = new Router({
6-
prefix: '/(v4|latest)/launches',
7+
prefix: '/v4/launches',
78
});
89

910
//
@@ -21,7 +22,7 @@ router.get('/past', cache(20), async (ctx) => {
2122
},
2223
});
2324
ctx.status = 200;
24-
ctx.body = result;
25+
ctx.body = await transformResponse(result);
2526
} catch (error) {
2627
ctx.throw(400, error.message);
2728
}
@@ -38,7 +39,7 @@ router.get('/upcoming', cache(20), async (ctx) => {
3839
},
3940
});
4041
ctx.status = 200;
41-
ctx.body = result;
42+
ctx.body = await transformResponse(result);
4243
} catch (error) {
4344
ctx.throw(400, error.message);
4445
}
@@ -55,7 +56,7 @@ router.get('/latest', cache(20), async (ctx) => {
5556
},
5657
});
5758
ctx.status = 200;
58-
ctx.body = result;
59+
ctx.body = await transformResponse(result);
5960
} catch (error) {
6061
ctx.throw(400, error.message);
6162
}
@@ -72,7 +73,7 @@ router.get('/next', cache(20), async (ctx) => {
7273
},
7374
});
7475
ctx.status = 200;
75-
ctx.body = result;
76+
ctx.body = await transformResponse(result);
7677
} catch (error) {
7778
ctx.throw(400, error.message);
7879
}
@@ -87,7 +88,7 @@ router.get('/', cache(20), async (ctx) => {
8788
try {
8889
const result = await Launch.find({});
8990
ctx.status = 200;
90-
ctx.body = result;
91+
ctx.body = await transformResponse(result);
9192
} catch (error) {
9293
ctx.throw(400, error.message);
9394
}
@@ -100,7 +101,7 @@ router.get('/:id', cache(20), async (ctx) => {
100101
ctx.throw(404);
101102
}
102103
ctx.status = 200;
103-
ctx.body = result;
104+
ctx.body = await transformResponse(result);
104105
});
105106

106107
// Query launches
@@ -109,7 +110,7 @@ router.post('/query', cache(20), async (ctx) => {
109110
try {
110111
const result = await Launch.paginate(query, options);
111112
ctx.status = 200;
112-
ctx.body = result;
113+
ctx.body = await transformResponse(result);
113114
} catch (error) {
114115
ctx.throw(400, error.message);
115116
}

routes/launches/v5/index.js

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
const Router = require('koa-router');
2+
const { Launch } = require('../../../models');
3+
const { auth, authz, cache } = require('../../../middleware');
4+
5+
const router = new Router({
6+
prefix: '/(v5|latest)/launches',
7+
});
8+
9+
//
10+
// Convenience Endpoints
11+
//
12+
13+
// Get past launches
14+
router.get('/past', cache(20), async (ctx) => {
15+
try {
16+
const result = await Launch.find({
17+
upcoming: false,
18+
}, null, {
19+
sort: {
20+
flight_number: 'asc',
21+
},
22+
});
23+
ctx.status = 200;
24+
ctx.body = result;
25+
} catch (error) {
26+
ctx.throw(400, error.message);
27+
}
28+
});
29+
30+
// Get upcoming launches
31+
router.get('/upcoming', cache(20), async (ctx) => {
32+
try {
33+
const result = await Launch.find({
34+
upcoming: true,
35+
}, null, {
36+
sort: {
37+
flight_number: 'asc',
38+
},
39+
});
40+
ctx.status = 200;
41+
ctx.body = result;
42+
} catch (error) {
43+
ctx.throw(400, error.message);
44+
}
45+
});
46+
47+
// Get latest launch
48+
router.get('/latest', cache(20), async (ctx) => {
49+
try {
50+
const result = await Launch.findOne({
51+
upcoming: false,
52+
}, null, {
53+
sort: {
54+
flight_number: 'desc',
55+
},
56+
});
57+
ctx.status = 200;
58+
ctx.body = result;
59+
} catch (error) {
60+
ctx.throw(400, error.message);
61+
}
62+
});
63+
64+
// Get next launch
65+
router.get('/next', cache(20), async (ctx) => {
66+
try {
67+
const result = await Launch.findOne({
68+
upcoming: true,
69+
}, null, {
70+
sort: {
71+
flight_number: 'asc',
72+
},
73+
});
74+
ctx.status = 200;
75+
ctx.body = result;
76+
} catch (error) {
77+
ctx.throw(400, error.message);
78+
}
79+
});
80+
81+
//
82+
// Standard Endpoints
83+
//
84+
85+
// Get all launches
86+
router.get('/', cache(20), async (ctx) => {
87+
try {
88+
const result = await Launch.find({});
89+
ctx.status = 200;
90+
ctx.body = result;
91+
} catch (error) {
92+
ctx.throw(400, error.message);
93+
}
94+
});
95+
96+
// Get one launch
97+
router.get('/:id', cache(20), async (ctx) => {
98+
const result = await Launch.findById(ctx.params.id);
99+
if (!result) {
100+
ctx.throw(404);
101+
}
102+
ctx.status = 200;
103+
ctx.body = result;
104+
});
105+
106+
// Query launches
107+
router.post('/query', cache(20), async (ctx) => {
108+
const { query = {}, options = {} } = ctx.request.body;
109+
try {
110+
const result = await Launch.paginate(query, options);
111+
ctx.status = 200;
112+
ctx.body = result;
113+
} catch (error) {
114+
ctx.throw(400, error.message);
115+
}
116+
});
117+
118+
// Create a launch
119+
router.post('/', auth, authz('launch:create'), async (ctx) => {
120+
try {
121+
const launch = new Launch(ctx.request.body);
122+
await launch.save();
123+
ctx.status = 201;
124+
} catch (error) {
125+
ctx.throw(400, error.message);
126+
}
127+
});
128+
129+
// Update a launch
130+
router.patch('/:id', auth, authz('launch:update'), async (ctx) => {
131+
try {
132+
await Launch.findByIdAndUpdate(ctx.params.id, ctx.request.body, {
133+
runValidators: true,
134+
});
135+
ctx.status = 200;
136+
} catch (error) {
137+
ctx.throw(400, error.message);
138+
}
139+
});
140+
141+
// Delete a launch
142+
router.delete('/:id', auth, authz('launch:delete'), async (ctx) => {
143+
try {
144+
await Launch.findByIdAndDelete(ctx.params.id);
145+
ctx.status = 200;
146+
} catch (error) {
147+
ctx.throw(400, error.message);
148+
}
149+
});
150+
151+
module.exports = router;

0 commit comments

Comments
 (0)