Skip to content

Commit

Permalink
added edit function to server
Browse files Browse the repository at this point in the history
  • Loading branch information
Ashhm committed Aug 29, 2017
1 parent dda273a commit 39ed852
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 26 deletions.
2 changes: 1 addition & 1 deletion client/components/NotesGrid.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export class NotesGrid extends React.Component {
title={note.title}
onDelete={this.props.onNoteDelete.bind(null, note)}
color={note.color}
date={`${note.createdAt.slice(11,19)} ${note.createdAt.slice(0,10)}`}
date={`${note.createdAt.slice(10} {note.createdAt.slice(0,10)}`}
>
{note.text}
</Note>
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
"webpack-devserver": "webpack-dev-server --debug --hot --devtool eval-source-map --output-pathinfo --watch --colors --inline --content-base public --port 8090 --host 0.0.0.0"
},
"dependencies": {
"axios": "^0.16.2",
"body-parser": "^1.17.2",
"cors": "^2.8.4",
"events": "^1.1.1",
Expand All @@ -19,6 +18,7 @@
"mongoose": "^4.11.7",
"react": "^15.6.1",
"react-dom": "^15.6.1",
"react-icons": "^2.2.5",
"react-masonry-component": "^5.0.7",
"superagent": "^3.6.0"
},
Expand Down
18 changes: 13 additions & 5 deletions server/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import cors from 'cors';

import {serverPort} from './etc/config.json';

import * as db from '../server/utils/DataBaseUtils';
import dbUtils from "./utils/DataBaseUtils";

db.setUpConnection();
dbUtils.setUpConnection();

const app = express();

Expand All @@ -15,15 +15,23 @@ app.use(bodyParser.json());
app.use(cors({origin: '*'}));

app.get('/notes', (req, res) => {
db.listNotes().then(data => res.send(data));
dbUtils.listNotes().then(data => res.send(data)).catch(err => console.log(err));
});

app.post('/notes', (req, res) => {
db.createNote(req.body).then(data => res.send(data));
if (req.body.id){
dbUtils.updateNote(req.body).then(data => res.send(data)).catch(err => console.log(err));
} else {
dbUtils.createNote(req.body).then(data => res.send(data)).catch(err => console.log(err));
}
});

app.delete('/notes/:id', (req, res) => {
db.deleteNote(req.params.id).then(data => res.send(data));
dbUtils.deleteNote(req.params.id).then(data => res.send(data)).catch(err => console.log(err));
});

app.put('/notes/:id', (req, res) => {
dbUtils.updateNote(req.params.id, req.body).then(data => res.send(data)).catch(err => console.log(err));
});


Expand Down
48 changes: 29 additions & 19 deletions server/utils/DataBaseUtils.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,38 @@
import mongoose from 'mongoose';
import '../models/note';
import config from '../../etc/config.json';
import config from '../etc/config.json';

const Note = mongoose.model('Note');

export function setUpConnection() {
mongoose.connect(`mongodb://${config.db.host}:${config.db.port}/${config.db.name}`, { useMongoClient: true });
}
const dbUtils = {

export function listNotes() {
return Note.find();
}
setUpConnection: () => {
mongoose.connect(`mongodb://${config.db.host}:${config.db.port}/${config.db.name}`, {useMongoClient: true});
},

export function createNote(data) {
const note = new Note({
title: data.title,
text: data.text,
color: data.color,
createdAt: new Date()
});
listNotes: () => {
return Note.find();
},

return note.save();
}
createNote: (data) => {
const note = new Note({
title: data.title,
text: data.text,
color: data.color,
createdAt: new Date()
});

export function deleteNote(id) {
return Note.findById(id).remove();
}
return note.save();
},

deleteNote: (id) => {
return Note.findById(id).remove();
},

//for mongo _id = Object(id). $set gonna modify only matched fields
updateNote: (data) => {
return Note.update({_id: Object(data.id)},{$set: data});
}
};

export default dbUtils;

0 comments on commit 39ed852

Please sign in to comment.