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) {
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) {
}

View File

@ -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) {
}

View File

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

View File

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

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();
router.route('/login').get(SessionController.newSession);
router.route('/login').post(SessionController.createSession);
router.route('/logout').get(SessionController.destroySession);

View File

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

View File

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

View File

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

View File

@ -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) {