From f9df0b42d518bbf14ae4678d68faabfe6d723bc7 Mon Sep 17 00:00:00 2001 From: d-d-wolfe Date: Thu, 11 Jun 2020 11:38:03 -0700 Subject: [PATCH 1/3] Got add, list and delete working --- index.js | 9 ++++---- lib/input.js | 8 +++---- lib/model/notes-schema.js | 4 ++-- lib/notes.js | 46 +++++++++++++++++++++++++-------------- 4 files changed, 39 insertions(+), 28 deletions(-) diff --git a/index.js b/index.js index b8b6f8b..dc99065 100644 --- a/index.js +++ b/index.js @@ -17,12 +17,11 @@ db.on('open', () => { const input = new Input; -// console.log(input); - + if (input.validate(input) === true) { - let addNotes = new Notes(input); - addNotes.execute(input); - addNotes.add(input); + let noteAction = new Notes(input); + noteAction.execute(input); + // addNotes.add(input); } else { console.error('Houston, we have a problem!'); diff --git a/lib/input.js b/lib/input.js index aa5d445..2ae1988 100644 --- a/lib/input.js +++ b/lib/input.js @@ -8,7 +8,6 @@ let action = (Object.keys(args).slice(1)); let message = (Object.values(args).slice(1)); - class Input { constructor() { this.action = action[0]; @@ -17,19 +16,18 @@ class Input { this.catName = message[1]; } validate() { - const ops = ['a', 'add']; + const ops = ['a', 'add', 'l', 'list', 'd', 'delete']; let validOp = false; let validString = false; const emptyString = ''; if (ops.includes(this.action)) validOp = true; - if (ops.includes(this.category)) validOp = true; + // if (ops.includes(this.category)) validOp = true; if (this.payload !== emptyString) validString = true; - if (this.catName !== emptyString) validString = true; + // if (this.catName !== emptyString) validString = true; return validOp && validString; } - } diff --git a/lib/model/notes-schema.js b/lib/model/notes-schema.js index 345ba5a..d68190f 100644 --- a/lib/model/notes-schema.js +++ b/lib/model/notes-schema.js @@ -2,9 +2,9 @@ const mongoose = require('mongoose'); -const requestSchema = new mongoose.Schema({ +const noteSchema = new mongoose.Schema({ category: {type: String, required: true}, note: {type: String, required: true}, }); -module.exports = mongoose.model('request', requestSchema); +module.exports = mongoose.model('request', noteSchema); diff --git a/lib/notes.js b/lib/notes.js index e038016..da01150 100644 --- a/lib/notes.js +++ b/lib/notes.js @@ -1,14 +1,9 @@ 'use strict'; -const mongoose = require('mongoose'); -const Request = require('../lib/model/notes-schema.js'); +// const mongoose = require('mongoose'); +const Note = require('../lib/model/notes-schema.js'); -mongoose.connect(process.env.MONGODB_ATLAS_URI, { - useNewUrlParser: true, - useUnifiedTopology: true, -}); -const newRequest = new Request({ url: 'http//localhost:3000', method: 'POST' }); class Notes { constructor(input) { @@ -21,15 +16,18 @@ class Notes { execute() { const action = ['add', 'a', 'list', 'l', 'delete', 'd']; console.log(this.action); - switch (action.includes(this.action)) { - case 'add' || 'a': - Notes.add; + switch (this.action) { + case 'add': + case 'a': + this.add(); break; - case 'list' || 'l': - Notes.list; + case 'list': + case 'l': + this.list(); break; - case 'delete' || 'd': - Notes.delete; + case 'delete': + case 'd': + this.delete(); break; default: console.log('Uh Oh! We have a problem!'); @@ -38,14 +36,30 @@ class Notes { } add() { - let note = new Notes(this.action, this.payload, this.category, this.catName); + // let note = new Notes(this.action, this.payload, this.category, this.catName); // console.log(new Notes('add', 'testing')); console.log('Adding note: ' + this.payload); + const newNote = new Note( { note: this.payload, category: this.catName } ); - newRequest.save() + newNote.save() .then(results => console.log('saving: ', results)) .catch(err => console.log('ERROR')); } + + list() { + console.log('listing notes'); + Note.find() + .then(results => console.log('All our notes', results)) + .catch(err => console.log('Cant find anything')); + } + + delete() { + console.log('deleting note'); + Note.findByIdAndDelete(this.payload) + .then(() => console.log('Its GONE!')) + .catch(() => console.log('no delete')); + } + } From 1f20933305af0fd94392c6018029aa24ddeb41e7 Mon Sep 17 00:00:00 2001 From: d-d-wolfe Date: Thu, 11 Jun 2020 11:54:31 -0700 Subject: [PATCH 2/3] Added info to readme and updated an error message --- README.md | 11 ++++++----- index.js | 2 +- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index fbcec42..63cd5a4 100644 --- a/README.md +++ b/README.md @@ -17,14 +17,15 @@ #### `.env` requirements (where applicable) -i.e. - -- `PORT` - Port Number -- `MONGODB_URI` - URL to the running mongo instance/db +- `PORT` - 3000 +- `MONGODB_ATLAS_URI` - URL to the running mongo instance/db #### How to initialize/run your application (where applicable) -- `node index.js -a 'Hello world'` +- `node index.js` must include flags listed below +- use flags `-a` or `--add` combined with a note in "" to add a note +- use flags `-l` or `--list` to list the notes in the db +- use flags `-d` or `--delete` combined with the id of a note to delete that note #### How to use your library (where applicable) diff --git a/index.js b/index.js index dc99065..67c5870 100644 --- a/index.js +++ b/index.js @@ -24,5 +24,5 @@ if (input.validate(input) === true) { // addNotes.add(input); } else { - console.error('Houston, we have a problem!'); + console.error('Houston, we have a problem! You must include flags.'); } \ No newline at end of file From f0f87113453978a988891a0dce785e8ee5d4768a Mon Sep 17 00:00:00 2001 From: d-d-wolfe Date: Thu, 11 Jun 2020 20:08:29 -0700 Subject: [PATCH 3/3] modularized the code, added update function --- README.md | 2 ++ lib/input.js | 2 +- lib/model/notes-collection.js | 38 ++++++++++++++++++++++ lib/notes.js | 60 +++++++++++++++++------------------ 4 files changed, 70 insertions(+), 32 deletions(-) diff --git a/README.md b/README.md index 63cd5a4..f524e4b 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,7 @@ - [submission PR 1](https://github.com/wolfes-401-advanced-javascript/notes/pull/1) - [submission PR 2](https://github.com/wolfes-401-advanced-javascript/notes/pull/2) - [submission PR 3](https://github.com/wolfes-401-advanced-javascript/notes/pull/3) +- [submission PR 4](https://github.com/wolfes-401-advanced-javascript/notes/pull/4) - [ci/cd](http://xyz.com) (GitHub Actions) - [back-end server url](http://xyz.com) (when applicable) - [front-end application](http://xyz.com) (when applicable) @@ -26,6 +27,7 @@ - use flags `-a` or `--add` combined with a note in "" to add a note - use flags `-l` or `--list` to list the notes in the db - use flags `-d` or `--delete` combined with the id of a note to delete that note +- use flags `-u` or `--update` combined with the id and what you want the note to say, to update the note (not working yet) #### How to use your library (where applicable) diff --git a/lib/input.js b/lib/input.js index 2ae1988..b132f6c 100644 --- a/lib/input.js +++ b/lib/input.js @@ -16,7 +16,7 @@ class Input { this.catName = message[1]; } validate() { - const ops = ['a', 'add', 'l', 'list', 'd', 'delete']; + const ops = ['a', 'add', 'l', 'list', 'd', 'delete', 'u', 'update']; let validOp = false; let validString = false; const emptyString = ''; diff --git a/lib/model/notes-collection.js b/lib/model/notes-collection.js index e69de29..10d2e84 100644 --- a/lib/model/notes-collection.js +++ b/lib/model/notes-collection.js @@ -0,0 +1,38 @@ +'use strict'; + +const schema = require('./notes-schema'); + +function create(input) { + let newNote = new schema( { note: input.payload, category: input.catName } ); + console.log('this is a new note', newNote); + newNote.save() + .then(results => console.log('saving: ', results)) + .catch(err => console.log('ERROR')); +} + +function get(input) { + schema.find() + .then(results => console.log('All our notes', results)) + .catch(err => console.log('Cant find anything')); +} + +// delete is a reserved word, so I used remove instead. +function remove(input) { + schema.findByIdAndDelete(input.payload) + .then(() => console.log('Its GONE!')) + .catch(() => console.log('no delete')); +} + +function update(input) { + schema.findByIdAndUpdate(input.id, input.payload, { + new: true }) + .then(() => console.log('Updated!')) + .catch(() => console.log('Update error!')); +} + +module.exports = { + create, + get, + remove, + update, +}; \ No newline at end of file diff --git a/lib/notes.js b/lib/notes.js index da01150..002686c 100644 --- a/lib/notes.js +++ b/lib/notes.js @@ -2,7 +2,7 @@ // const mongoose = require('mongoose'); const Note = require('../lib/model/notes-schema.js'); - +const collection = require('./model/notes-collection'); class Notes { @@ -10,56 +10,54 @@ class Notes { this.action = input.action; this.payload = input.payload; this.category = input.category; - this.catName = input.catName; + this.catName = input.catName || 'General'; } execute() { - const action = ['add', 'a', 'list', 'l', 'delete', 'd']; + const action = ['add', 'a', 'list', 'l', 'delete', 'd', 'u', 'update']; console.log(this.action); switch (this.action) { - case 'add': - case 'a': - this.add(); - break; - case 'list': - case 'l': - this.list(); - break; - case 'delete': - case 'd': - this.delete(); - break; - default: - console.log('Uh Oh! We have a problem!'); - break; + case 'add': + case 'a': + this.add(); + break; + case 'list': + case 'l': + this.list(); + break; + case 'delete': + case 'd': + this.delete(); + break; + case 'update': + case 'u': + this.update(); + break; + default: + console.log('Uh Oh! We have a problem!'); + break; } } add() { - // let note = new Notes(this.action, this.payload, this.category, this.catName); - // console.log(new Notes('add', 'testing')); console.log('Adding note: ' + this.payload); - const newNote = new Note( { note: this.payload, category: this.catName } ); - - newNote.save() - .then(results => console.log('saving: ', results)) - .catch(err => console.log('ERROR')); + collection.create(this); } list() { console.log('listing notes'); - Note.find() - .then(results => console.log('All our notes', results)) - .catch(err => console.log('Cant find anything')); + collection.get(this); } delete() { console.log('deleting note'); - Note.findByIdAndDelete(this.payload) - .then(() => console.log('Its GONE!')) - .catch(() => console.log('no delete')); + collection.remove(this); } + update() { + console.log('updating note'); + collection.update(this); + } }