Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d6f7280
commit 55dcdf7
Showing
5 changed files
with
201 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,5 @@ | |
|
||
module.exports = [ | ||
require('./v4'), | ||
require('./v5'), | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()), | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; |