-
Notifications
You must be signed in to change notification settings - Fork 20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add api endpoints #281
Merged
Merged
add api endpoints #281
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
12f3410
add /promises/parse and user-scoped /promises/create api endpoints
chrisbutler 8808989
re-orgainize express-related code into /server directory
chrisbutler b4880f7
split api endpoints into users and promises in api/v1
chrisbutler 1abf1f7
refactor browser endpoints to api/v1/frontend
chrisbutler File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
require('babel-register') | ||
require('babel-polyfill') | ||
require('./app/index') | ||
require('./server/app/index') |
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,5 @@ | ||
import './calculate' | ||
import './colors' | ||
import './format' | ||
import './path' | ||
import './utils' |
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
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
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,83 @@ | ||
import { Router } from 'express' | ||
|
||
import { Users } from '../../models/' | ||
import log from '../../../lib/logger' | ||
import { parsePromise, parsePromiseWithIp } from '../../../lib/parse/promise' | ||
|
||
const api = Router() | ||
|
||
api.post('/promises/parse/', (req, res) => { | ||
const { | ||
promise = {}, | ||
urtext, | ||
username, | ||
timezone, | ||
} = req.body | ||
|
||
const parsedPromise = parsePromise({ promise, urtext, username, timezone }) | ||
|
||
if (!parsedPromise) { | ||
resp.send(400) | ||
} else { | ||
res.send(parsedPromise) | ||
} | ||
}) | ||
|
||
api.post('/promises/create/', (req, res) => { | ||
const { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Identical blocks of code found in 2 locations. Consider refactoring. |
||
promise = {}, | ||
urtext, | ||
username, | ||
timezone, | ||
} = req.body | ||
|
||
const parsedPromise = parsePromise({ promise, urtext, username, timezone }) | ||
|
||
if (!parsedPromise) { | ||
res.send(400) | ||
} else { | ||
Users.findOne({ | ||
where: { | ||
username, | ||
} | ||
}).then(user => { | ||
if (user) { | ||
user.createPromise(parsedPromise) | ||
.then(function(prom) { | ||
log.info('promise created via POST', prom, req.body) | ||
res.status(201).send(prom) | ||
}) | ||
.catch((reason) => res.status(400).send(reason)) | ||
} | ||
}) | ||
} | ||
}) | ||
|
||
api.put('/promises/:username/:urtext', ({ ip, ...req }, res) => { | ||
const { | ||
urtext, | ||
username, | ||
} = req.params | ||
|
||
Users.findOne({ | ||
where: { | ||
username, | ||
} | ||
}).then(async(user) => { | ||
if (user) { | ||
const parsedPromise = await parsePromiseWithIp({ urtext, username, ip }) | ||
|
||
if (parsedPromise) { | ||
user.createPromise(parsedPromise) | ||
.then(function(prom) { | ||
log.info('promise created via PUT', prom) | ||
return res.status(201).send(prom) | ||
}) | ||
.catch((reason) => res.status(400).send(reason)) | ||
} | ||
} | ||
return res.status(400) | ||
}) | ||
}) | ||
|
||
export default api |
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,23 @@ | ||
import { Router } from 'express' | ||
|
||
import { Users } from '../../models/' | ||
import log from '../../../lib/logger' | ||
import { APP_DOMAIN } from '../../app/config' | ||
|
||
const api = Router() | ||
|
||
api.get('/users/create/:username', (req, res) => { | ||
const { username } = req.params | ||
|
||
if (username) { | ||
Users.create({ username }) | ||
.then(() => { | ||
log.info('user created', username) | ||
res.redirect(`//${username}.${APP_DOMAIN}`) | ||
}) | ||
} else { | ||
res.redirect('/') | ||
} | ||
}) | ||
|
||
export default api |
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,46 @@ | ||
import app from './express' | ||
import { ALLOW_ADMIN_ACTIONS, ENVIRONMENT } from '../app/config' | ||
import { Promises } from '../models/' | ||
import { seed, importJson } from '../db/seed' | ||
import cache from '../db/cache' | ||
|
||
import FrontendApi from '../api/v1/frontend' | ||
import PromiseApi from '../api/v1/promises' | ||
import UserApi from '../api/v1/users' | ||
|
||
// TODO: https://github.com/Vincit/objection.js/tree/master/examples/express-ts | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TODO found |
||
|
||
// Client-side/browser API | ||
app.use('/', FrontendApi) | ||
|
||
// "REST" endpoints | ||
app.use('/_s/api/v1', PromiseApi) | ||
app.use('/_s/api/v1', UserApi) | ||
|
||
// Utils | ||
|
||
// calculate and store reliability for each user | ||
app.get('/cache', (req, resp) => { | ||
cache() | ||
resp.redirect('/') | ||
}) | ||
|
||
if (ENVIRONMENT !== 'production' || ALLOW_ADMIN_ACTIONS) { | ||
// insert promises.json into db | ||
app.get('/import', (req, resp) => { | ||
importJson() | ||
resp.redirect('/') | ||
}) | ||
|
||
// drop db and repopulate | ||
app.get('/reset', (req, resp) => { | ||
seed() | ||
resp.redirect('/') | ||
}) | ||
|
||
// removes all entries from the promises table | ||
app.get('/empty', (req, resp) => { | ||
Promises.destroy({ where: {} }) | ||
resp.redirect('/') | ||
}) | ||
} |
File renamed without changes.
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
File renamed without changes.
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Identical blocks of code found in 2 locations. Consider refactoring.