From c25d669fe94868aa730e8a38e84c0a288ae8320b Mon Sep 17 00:00:00 2001 From: catarak Date: Sat, 18 Jun 2016 18:33:49 -0400 Subject: [PATCH] move ajax requests to /api --- server/controllers/project.controller.js | 27 +++++++++++++- server/controllers/session.controller.js | 5 --- server/controllers/user.controller.js | 5 --- server/routes/project.routes.js | 4 +++ server/routes/server.routes.js | 24 +++++++++++++ server/routes/session.routes.js | 2 -- server/routes/user.routes.js | 2 -- server/server.js | 10 ++++-- shared/redux/actions/index.js | 46 ++++++++++++++++-------- shared/redux/actions/user.js | 2 +- 10 files changed, 94 insertions(+), 33 deletions(-) create mode 100644 server/routes/server.routes.js diff --git a/server/controllers/project.controller.js b/server/controllers/project.controller.js index 9f9c6c01..f1735184 100644 --- a/server/controllers/project.controller.js +++ b/server/controllers/project.controller.js @@ -4,7 +4,11 @@ export function createProject(req, res) { if (req.user) { Project.create({ user: req.user._id, - file: {} + name: req.body.name, + file: { + name: req.body.file.name, + content: req.body.file.content + } }, function(err, newProject) { if (err) { return res.json({success: false}) } return res.json({ @@ -19,4 +23,25 @@ export function createProject(req, res) { } else { res.json({success: false}); } +} + +export function updateProject(req, res) { + Project.update({_id: req.params.project_id}, + { + $set: req.body + }, function(err, updatedProject) { + if (err) { return res.json({success: false}) } + return res.json({ + id: updatedProject._id, + name: updatedProject.name, + file: { + name: updatedProject.file.name, + content: updatedProject.file.content + } + }); + }); +} + +export function getProject(req, res) { + } \ No newline at end of file diff --git a/server/controllers/session.controller.js b/server/controllers/session.controller.js index be145524..d9096434 100644 --- a/server/controllers/session.controller.js +++ b/server/controllers/session.controller.js @@ -3,11 +3,6 @@ import passport from 'passport' import path from 'path' -export function newSession(req, res, next) { - //eventually, it would be cool to have some isomorphic rendering - res.sendFile(path.resolve(__dirname + '/../../index.html')); -} - export function destroySession(req, res) { } diff --git a/server/controllers/user.controller.js b/server/controllers/user.controller.js index 0aff0670..8fc8a4e6 100644 --- a/server/controllers/user.controller.js +++ b/server/controllers/user.controller.js @@ -2,11 +2,6 @@ import User from '../models/user' import passport from 'passport' import path from 'path' -export function newUser(req, res) { - //eventually, it would be cool to have some isomorphic rendering - res.sendFile(path.resolve(__dirname + '/../../index.html')); -} - export function createUser(req, res, next) { const user = new User({ username: req.body.username, diff --git a/server/routes/project.routes.js b/server/routes/project.routes.js index 3649b359..480030e7 100644 --- a/server/routes/project.routes.js +++ b/server/routes/project.routes.js @@ -5,4 +5,8 @@ const router = new Router(); router.route('/projects').post(ProjectController.createProject); +router.route('/projects/:project_id').put(ProjectController.updateProject); + +router.route('/projects/:project_id').get(ProjectController.getProject); + export default router; \ No newline at end of file diff --git a/server/routes/server.routes.js b/server/routes/server.routes.js new file mode 100644 index 00000000..ad50100b --- /dev/null +++ b/server/routes/server.routes.js @@ -0,0 +1,24 @@ +import {Router} from 'express' +const router = new Router(); +import path from 'path' + +// this is intended to be a temporary file +// until i figure out isomorphic rendering + +router.route('/').get(function(req, res) { + res.sendFile(path.resolve(__dirname + '/../../index.html')); +}); + +router.route('/signup').get(function(req, res) { + res.sendFile(path.resolve(__dirname + '/../../index.html')); +}); + +router.route('/projects/:project_id').get(function(req, res) { + res.sendFile(path.resolve(__dirname + '/../../index.html')); +}); + +router.route('/login').get(function(req, res) { + res.sendFile(path.resolve(__dirname + '/../../index.html')); +}); + +export default router; \ No newline at end of file diff --git a/server/routes/session.routes.js b/server/routes/session.routes.js index f6e7ae81..c94bee6b 100644 --- a/server/routes/session.routes.js +++ b/server/routes/session.routes.js @@ -4,8 +4,6 @@ import passport from 'passport'; const router = new Router(); -router.route('/login').get(SessionController.newSession); - router.route('/login').post(SessionController.createSession); router.route('/logout').get(SessionController.destroySession); diff --git a/server/routes/user.routes.js b/server/routes/user.routes.js index cbc1c7d5..47c26571 100644 --- a/server/routes/user.routes.js +++ b/server/routes/user.routes.js @@ -2,8 +2,6 @@ import { Router } from 'express'; import * as UserController from '../controllers/user.controller'; const router = new Router(); -router.route('/signup').get(UserController.newUser); - router.route('/signup').post(UserController.createUser); export default router; diff --git a/server/server.js b/server/server.js index 5623a943..13dbf6b6 100644 --- a/server/server.js +++ b/server/server.js @@ -25,6 +25,7 @@ import serverConfig from './config'; import users from './routes/user.routes'; import sessions from './routes/session.routes'; import projects from './routes/project.routes'; +import serverRoutes from './routes/server.routes'; //Body parser, cookie parser, sessions, serve public assets @@ -49,9 +50,12 @@ app.use(session({ })); app.use(passport.initialize()); app.use(passport.session()); -app.use('/', users); -app.use('/', sessions); -app.use('/', projects); +app.use('/api', users); +app.use('/api', sessions); +app.use('/api', projects); +//this is supposed to be TEMPORARY -- until i figure out +// isomorphic rendering +app.use('/', serverRoutes); const passportConfig = require('./config/passport'); diff --git a/shared/redux/actions/index.js b/shared/redux/actions/index.js index 49c1b246..bcf56bab 100644 --- a/shared/redux/actions/index.js +++ b/shared/redux/actions/index.js @@ -1,7 +1,7 @@ import * as ActionTypes from '../constants/constants'; import axios from 'axios' -const ROOT_URL = location.href.indexOf('localhost') > 0 ? 'http://localhost:8000' : '/'; +const ROOT_URL = location.href.indexOf('localhost') > 0 ? 'http://localhost:8000/api/' : '/api'; export function updateFile(name, content) { return { @@ -54,21 +54,39 @@ export function decreaseFont() { } export function saveProject() { - // return function(dispatch) { - // let projectValues = {}; - // axios.put(`${ROOT_URL}/projects`, projectValues, {withCredentials: true}) - // .then(response => { - // dispatch({ - // type: ActionTypes.PROJECT_SAVE_SUCCESS - // }); - // }) - // .catch(response => dispatch({ - // type: ActionTypes.PROJECT_SAVE_FAIL - // })); - // } return function(dispatch, getState) { var state = getState(); - + var formParams = Object.assign({}, state.project); + formParams.file = state.file; + if (state.id) { + axios.put(`${ROOT_URL}/projects/${state.id}`, formParams, {withCredentials: true}) + .then(response => { + dispatch({ + type: ActionTYpes.PROJECT_SAVE_SUCCESS + }) + .catch(response => dispatch({ + type: ActionTypes.PROJECT_SAVE_FAIL + })); + }) + } + else { + axios.post(`${ROOT_URL}/projects`, formParams, {withCredentials: true}) + .then(response => { + browserHistory.push('/' + response.data.id); + dispatch({ + type: ActionTypes.NEW_PROJECT, + name: response.data.name, + id: response.data.id, + file: { + name: response.data.file.name, + content: response.data.file.content + } + }); + }) + .catch(response => dispatch({ + type: ActionTypes.PROJECT_SAVE_FAIL + })); + } } } diff --git a/shared/redux/actions/user.js b/shared/redux/actions/user.js index 8651e1d6..d08d5d52 100644 --- a/shared/redux/actions/user.js +++ b/shared/redux/actions/user.js @@ -3,7 +3,7 @@ import { browserHistory } from 'react-router' import axios from 'axios' -const ROOT_URL = location.href.indexOf('localhost') > 0 ? 'http://localhost:8000' : '/'; +const ROOT_URL = location.href.indexOf('localhost') > 0 ? 'http://localhost:8000/api/' : '/api'; export function signUpUser(formValues) { return function(dispatch) {