Skip to content

Commit

Permalink
fix conflict in launch endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
jakewmeyer committed Apr 18, 2020
1 parent 7d6121f commit 2a9901b
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 74 deletions.
6 changes: 6 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@

FROM node:alpine

WORKDIR /app

COPY package.json package-lock.json ./

RUN npm install --production

COPY . .

ENV NODE_ENV=production

LABEL maintainer="jakewmeyer@gmail.com"
Expand Down
5 changes: 3 additions & 2 deletions middleware/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ module.exports = () => {
let redis;
let redisAvailable = false;

if (process.env.REDIS_URL) {
redis = new Redis(process.env.REDIS_URL);
if (process.env.SPACEX_REDIS) {
redis = new Redis(process.env.SPACEX_REDIS);
} else {
redis = new Redis();
}
Expand All @@ -25,6 +25,7 @@ module.exports = () => {
});
redis.on('connect', () => {
redisAvailable = true;
console.log('Redis ready');
});

/**
Expand Down
148 changes: 76 additions & 72 deletions services/v4/launches/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,146 +7,150 @@ const router = new Router({
prefix: '/launches',
});

// Get all launches
router.get('/', async (ctx) => {
ctx.state.cache = 20;
//
// Convenience Endpoints
//

// Get past launches
router.get('/past', async (ctx) => {
try {
const result = await Launch.find({});
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 one launch
router.get('/:id', async (ctx) => {
ctx.state.cache = 20;
// Get upcoming launches
router.get('/upcoming', async (ctx) => {
try {
const result = await Launch.findById(ctx.params.id);
if (!result) {
ctx.throw(404);
}
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);
}
});

// Query launches
router.post('/query', async (ctx) => {
ctx.state.cache = 20;
const { query = {}, options = {} } = ctx.request.body;
// Get latest launch
router.get('/latest', async (ctx) => {
try {
const result = await Launch.paginate(query, options);
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);
}
});

// Create a launch
router.post('/', auth, async (ctx) => {
// Get next launch
router.get('/next', async (ctx) => {
try {
const core = new Launch(ctx.request.body);
await core.save();
ctx.status = 201;
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);
}
});

// Update a launch
router.patch('/:id', auth, async (ctx) => {
//
// Standard Endpoints
//

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

// Delete a launch
router.delete('/:id', auth, async (ctx) => {
// Get one launch
router.get('/:id', async (ctx) => {
ctx.state.cache = 20;
try {
await Launch.findByIdAndDelete(ctx.params.id);
const result = await Launch.findById(ctx.params.id);
if (!result) {
ctx.throw(404);
}
ctx.status = 200;
ctx.body = result;
} catch (error) {
ctx.throw(400, error.message);
}
});

//
// Convenience Endpoints
//

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

// Get upcoming launches
router.get('/upcoming', async (ctx) => {
// Create a launch
router.post('/', auth, async (ctx) => {
try {
const result = await Launch.find({
upcoming: true,
}, null, {
sort: {
flight_number: 'asc',
},
});
ctx.status = 200;
ctx.body = result;
const core = new Launch(ctx.request.body);
await core.save();
ctx.status = 201;
} catch (error) {
ctx.throw(400, error.message);
}
});

// Get latest launch
router.get('/latest', async (ctx) => {
// Update a launch
router.patch('/:id', auth, async (ctx) => {
try {
const result = await Launch.findOne({
upcoming: false,
}, null, {
sort: {
flight_number: 'desc',
},
await Launch.findByIdAndUpdate(ctx.params.id, ctx.request.body, {
runValidators: true,
});
ctx.status = 200;
ctx.body = result;
} catch (error) {
ctx.throw(400, error.message);
}
});

// Get next launch
router.get('/next', async (ctx) => {
// Delete a launch
router.delete('/:id', auth, async (ctx) => {
try {
const result = await Launch.findOne({
upcoming: true,
}, null, {
sort: {
flight_number: 'asc',
},
});
await Launch.findByIdAndDelete(ctx.params.id);
ctx.status = 200;
ctx.body = result;
} catch (error) {
ctx.throw(400, error.message);
}
Expand Down

0 comments on commit 2a9901b

Please sign in to comment.