move ajax requests to /api

This commit is contained in:
catarak 2016-06-18 18:33:49 -04:00
parent 452b46bddd
commit c25d669fe9
10 changed files with 94 additions and 33 deletions

View file

@ -4,7 +4,11 @@ export function createProject(req, res) {
if (req.user) { if (req.user) {
Project.create({ Project.create({
user: req.user._id, user: req.user._id,
file: {} name: req.body.name,
file: {
name: req.body.file.name,
content: req.body.file.content
}
}, function(err, newProject) { }, function(err, newProject) {
if (err) { return res.json({success: false}) } if (err) { return res.json({success: false}) }
return res.json({ return res.json({
@ -20,3 +24,24 @@ export function createProject(req, res) {
res.json({success: false}); 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) {
}

View file

@ -3,11 +3,6 @@ import passport from 'passport'
import path from 'path' 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) { export function destroySession(req, res) {
} }

View file

@ -2,11 +2,6 @@ import User from '../models/user'
import passport from 'passport' import passport from 'passport'
import path from 'path' 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) { export function createUser(req, res, next) {
const user = new User({ const user = new User({
username: req.body.username, username: req.body.username,

View file

@ -5,4 +5,8 @@ const router = new Router();
router.route('/projects').post(ProjectController.createProject); 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; export default router;

View file

@ -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;

View file

@ -4,8 +4,6 @@ import passport from 'passport';
const router = new Router(); const router = new Router();
router.route('/login').get(SessionController.newSession);
router.route('/login').post(SessionController.createSession); router.route('/login').post(SessionController.createSession);
router.route('/logout').get(SessionController.destroySession); router.route('/logout').get(SessionController.destroySession);

View file

@ -2,8 +2,6 @@ import { Router } from 'express';
import * as UserController from '../controllers/user.controller'; import * as UserController from '../controllers/user.controller';
const router = new Router(); const router = new Router();
router.route('/signup').get(UserController.newUser);
router.route('/signup').post(UserController.createUser); router.route('/signup').post(UserController.createUser);
export default router; export default router;

View file

@ -25,6 +25,7 @@ import serverConfig from './config';
import users from './routes/user.routes'; import users from './routes/user.routes';
import sessions from './routes/session.routes'; import sessions from './routes/session.routes';
import projects from './routes/project.routes'; import projects from './routes/project.routes';
import serverRoutes from './routes/server.routes';
//Body parser, cookie parser, sessions, serve public assets //Body parser, cookie parser, sessions, serve public assets
@ -49,9 +50,12 @@ app.use(session({
})); }));
app.use(passport.initialize()); app.use(passport.initialize());
app.use(passport.session()); app.use(passport.session());
app.use('/', users); app.use('/api', users);
app.use('/', sessions); app.use('/api', sessions);
app.use('/', projects); app.use('/api', projects);
//this is supposed to be TEMPORARY -- until i figure out
// isomorphic rendering
app.use('/', serverRoutes);
const passportConfig = require('./config/passport'); const passportConfig = require('./config/passport');

View file

@ -1,7 +1,7 @@
import * as ActionTypes from '../constants/constants'; import * as ActionTypes from '../constants/constants';
import axios from 'axios' 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) { export function updateFile(name, content) {
return { return {
@ -54,21 +54,39 @@ export function decreaseFont() {
} }
export function saveProject() { 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) { return function(dispatch, getState) {
var state = 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
}));
}
} }
} }

View file

@ -3,7 +3,7 @@ import { browserHistory } from 'react-router'
import axios from 'axios' 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) { export function signUpUser(formValues) {
return function(dispatch) { return function(dispatch) {