Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -17,14 +18,16 @@

#### `.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
- 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)

Expand Down
11 changes: 5 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,12 @@ 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!');
console.error('Houston, we have a problem! You must include flags.');
}
8 changes: 3 additions & 5 deletions lib/input.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -17,19 +16,18 @@ class Input {
this.catName = message[1];
}
validate() {
const ops = ['a', 'add'];
const ops = ['a', 'add', 'l', 'list', 'd', 'delete', 'u', 'update'];
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;
}

}


Expand Down
38 changes: 38 additions & 0 deletions lib/model/notes-collection.js
Original file line number Diff line number Diff line change
@@ -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,
};
4 changes: 2 additions & 2 deletions lib/model/notes-schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
68 changes: 40 additions & 28 deletions lib/notes.js
Original file line number Diff line number Diff line change
@@ -1,50 +1,62 @@
'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');
const collection = require('./model/notes-collection');

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) {
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 (action.includes(this.action)) {
case 'add' || 'a':
Notes.add;
break;
case 'list' || 'l':
Notes.list;
break;
case 'delete' || 'd':
Notes.delete;
break;
default:
console.log('Uh Oh! We have a problem!');
break;
switch (this.action) {
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);
collection.create(this);
}

list() {
console.log('listing notes');
collection.get(this);
}

delete() {
console.log('deleting note');
collection.remove(this);
}

newRequest.save()
.then(results => console.log('saving: ', results))
.catch(err => console.log('ERROR'));
update() {
console.log('updating note');
collection.update(this);
}
}

Expand Down