Skip to content

Commit

Permalink
Add v5 route for crew schema change
Browse files Browse the repository at this point in the history
  • Loading branch information
jakewmeyer committed Jul 17, 2021
1 parent d6f7280 commit 55dcdf7
Show file tree
Hide file tree
Showing 5 changed files with 201 additions and 10 deletions.
12 changes: 10 additions & 2 deletions models/launches.js
Expand Up @@ -99,8 +99,16 @@ const launchSchema = new mongoose.Schema({
}],
},
crew: [{
type: mongoose.ObjectId,
ref: 'Crew',
_id: false,
crew: {
type: mongoose.ObjectId,
ref: 'Crew',
default: null,
},
role: {
type: String,
default: null,
},
}],
ships: [{
type: mongoose.ObjectId,
Expand Down
1 change: 1 addition & 0 deletions routes/launches/index.js
Expand Up @@ -2,4 +2,5 @@

module.exports = [
require('./v4'),
require('./v5'),
];
30 changes: 30 additions & 0 deletions routes/launches/v4/_transform-response.js
@@ -0,0 +1,30 @@
/* eslint-disable no-underscore-dangle */
const buildCrew = (launch) => launch.crew.map((crew) => {
if (crew?.crew) {
return crew?.crew;
}
return crew;
});

module.exports = async (payload) => {
if (Array.isArray(payload)) {
return payload.map((launch) => ({
...launch.toObject(),
crew: buildCrew(launch.toObject()),
}));
}
if (Array.isArray(payload?.docs)) {
const docs = payload.docs.map((launch) => ({
...launch.toObject(),
crew: buildCrew(launch.toObject()),
}));
return {
...payload,
docs,
};
}
return {
...payload.toObject(),
crew: buildCrew(payload.toObject()),
};
};
17 changes: 9 additions & 8 deletions routes/launches/v4/index.js
@@ -1,9 +1,10 @@
const Router = require('koa-router');
const { Launch } = require('../../../models');
const { auth, authz, cache } = require('../../../middleware');
const transformResponse = require('./_transform-response');

const router = new Router({
prefix: '/(v4|latest)/launches',
prefix: '/v4/launches',
});

//
Expand All @@ -21,7 +22,7 @@ router.get('/past', cache(20), async (ctx) => {
},
});
ctx.status = 200;
ctx.body = result;
ctx.body = await transformResponse(result);
} catch (error) {
ctx.throw(400, error.message);
}
Expand All @@ -38,7 +39,7 @@ router.get('/upcoming', cache(20), async (ctx) => {
},
});
ctx.status = 200;
ctx.body = result;
ctx.body = await transformResponse(result);
} catch (error) {
ctx.throw(400, error.message);
}
Expand All @@ -55,7 +56,7 @@ router.get('/latest', cache(20), async (ctx) => {
},
});
ctx.status = 200;
ctx.body = result;
ctx.body = await transformResponse(result);
} catch (error) {
ctx.throw(400, error.message);
}
Expand All @@ -72,7 +73,7 @@ router.get('/next', cache(20), async (ctx) => {
},
});
ctx.status = 200;
ctx.body = result;
ctx.body = await transformResponse(result);
} catch (error) {
ctx.throw(400, error.message);
}
Expand All @@ -87,7 +88,7 @@ router.get('/', cache(20), async (ctx) => {
try {
const result = await Launch.find({});
ctx.status = 200;
ctx.body = result;
ctx.body = await transformResponse(result);
} catch (error) {
ctx.throw(400, error.message);
}
Expand All @@ -100,7 +101,7 @@ router.get('/:id', cache(20), async (ctx) => {
ctx.throw(404);
}
ctx.status = 200;
ctx.body = result;
ctx.body = await transformResponse(result);
});

// Query launches
Expand All @@ -109,7 +110,7 @@ router.post('/query', cache(20), async (ctx) => {
try {
const result = await Launch.paginate(query, options);
ctx.status = 200;
ctx.body = result;
ctx.body = await transformResponse(result);
} catch (error) {
ctx.throw(400, error.message);
}
Expand Down
151 changes: 151 additions & 0 deletions routes/launches/v5/index.js
@@ -0,0 +1,151 @@
const Router = require('koa-router');
const { Launch } = require('../../../models');
const { auth, authz, cache } = require('../../../middleware');

const router = new Router({
prefix: '/(v5|latest)/launches',
});

//
// Convenience Endpoints
//

// Get past launches
router.get('/past', cache(20), async (ctx) => {
try {
const result = await Launch.find({
upcoming: false,
}, null, {
sort: {
flight_number: 'asc',
},
});
ctx.status = 200;
ctx.body = result;
} catch (error) {
ctx.throw(400, error.message);
}
});

// Get upcoming launches
router.get('/upcoming', cache(20), async (ctx) => {
try {
const result = await Launch.find({
upcoming: true,
}, null, {
sort: {
flight_number: 'asc',
},
});
ctx.status = 200;
ctx.body = result;
} catch (error) {
ctx.throw(400, error.message);
}
});

// Get latest launch
router.get('/latest', cache(20), async (ctx) => {
try {
const result = await Launch.findOne({
upcoming: false,
}, null, {
sort: {
flight_number: 'desc',
},
});
ctx.status = 200;
ctx.body = result;
} catch (error) {
ctx.throw(400, error.message);
}
});

// Get next launch
router.get('/next', cache(20), async (ctx) => {
try {
const result = await Launch.findOne({
upcoming: true,
}, null, {
sort: {
flight_number: 'asc',
},
});
ctx.status = 200;
ctx.body = result;
} catch (error) {
ctx.throw(400, error.message);
}
});

//
// Standard Endpoints
//

// Get all launches
router.get('/', cache(20), async (ctx) => {
try {
const result = await Launch.find({});
ctx.status = 200;
ctx.body = result;
} catch (error) {
ctx.throw(400, error.message);
}
});

// Get one launch
router.get('/:id', cache(20), async (ctx) => {
const result = await Launch.findById(ctx.params.id);
if (!result) {
ctx.throw(404);
}
ctx.status = 200;
ctx.body = result;
});

// Query launches
router.post('/query', cache(20), async (ctx) => {
const { query = {}, options = {} } = ctx.request.body;
try {
const result = await Launch.paginate(query, options);
ctx.status = 200;
ctx.body = result;
} catch (error) {
ctx.throw(400, error.message);
}
});

// Create a launch
router.post('/', auth, authz('launch:create'), async (ctx) => {
try {
const launch = new Launch(ctx.request.body);
await launch.save();
ctx.status = 201;
} catch (error) {
ctx.throw(400, error.message);
}
});

// Update a launch
router.patch('/:id', auth, authz('launch:update'), async (ctx) => {
try {
await Launch.findByIdAndUpdate(ctx.params.id, ctx.request.body, {
runValidators: true,
});
ctx.status = 200;
} catch (error) {
ctx.throw(400, error.message);
}
});

// Delete a launch
router.delete('/:id', auth, authz('launch:delete'), async (ctx) => {
try {
await Launch.findByIdAndDelete(ctx.params.id);
ctx.status = 200;
} catch (error) {
ctx.throw(400, error.message);
}
});

module.exports = router;

0 comments on commit 55dcdf7

Please sign in to comment.