Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Emmanuel Prado #504

Open
wants to merge 21 commits into
base: master
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
node_modules
.DS_Store
.vscode
.env
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,9 @@ Once your MVP has been approved, you have been given a feature list that the cli
- Gate your favorite feature behind the _premium_ paywall

You will notice that this repository does not have any starter code. This is on purpose. You are to start from scratch using any files you have built throughout your time here at Lambda School as reference.

---

## Trello Link for Project Manager:

https://github.com/Emmanium/back-end-project-week.git
100 changes: 100 additions & 0 deletions api/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
const express = require('express');

const cors = require('cors');

const notes = require('../notes/notesModel.js');

const server = express();

server.use(express.json(), cors());

//test change


// Display a list of notes
server.get('/note/get/all', async (req, res) => {
try {
const rows = await notes.getAll();

res.status(200).json(rows);
} catch (err) {
res.status(500).json({ error: 'Database go boom' });
}

})

// Create a note with a title and content
server.post('/note/create', async (req, res) => {
try {
const noteData = req.body;

if (noteData.title && noteData.textBody) {
const count = await notes.insert(noteData);

res.status(201).json(count);

} else {
res.status(422).json({ error: 'Missing data' });
}
} catch (err) {
res.status(500).json({ error: 'Database go boom' });
}

})

// View an existing note
server.get('/note/view/:id', async (req, res) => {
try {
const { id } = req.params;
const note = await notes.findById(id);

if (note.length > 0) {
res.status(200).json(note);
} else {
res.status(404).json({ error: `Note doesn't exist` });
}

} catch (err) {
res.status(500).json({ error: 'Database go boom' });
}

})

// Edit an existing note
server.put('/note/edit/:id', async (req, res) => {
try {
const noteChanges = req.body;
const { id } = req.params;

if (noteChanges.title && noteChanges.textBody) {
const array = await notes.update(id, noteChanges);
res.status(200).json(array[0]);
} else {
res.status(422).json({ error: "Body missing info" });
}

} catch (err) {
res.status(500).json({ error: 'Database go boom' });
}

})

// Delete an existing note
server.delete('/note/delete/:id', async (req, res) => {
try {
const { id } = req.params;

if (id) {
const count = await notes.remove(id);
res.status(200).json({ message: `${count} note successfully deleted` });
} else {
res.status(404).json({ error: `Note doesn't exist` })
}

} catch (err) {
res.status(500).json({ error: 'Database go boom' });
}

})

module.exports = server;
121 changes: 121 additions & 0 deletions api/server.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
const request = require('supertest');

const server = require('./server.js');
const db = require('../data/dbConfig');

describe('the route handlers', () => {

describe('GET /note/get/all', () => {

afterEach(async () => {
await db('notes').truncate();
});

it('responds with 200', async () => {
const response = await request(server).get('/note/get/all');

expect(response.status).toBe(200);
})

it('sends correct responds object', async () => {
const response = await request(server).get('/note/get/all');

expect(response.body).toEqual([]);
})

})

describe('POST /note/create', () => {

afterEach(async () => {
await db('notes').truncate();
});

it('responds with 201 when body is correct', async () => {
const body = { title: 'FakeNote', textBody: 'FakeBody' };
const response = await request(server).post('/note/create').send(body);

expect(response.status).toBe(201);
})

it('responds with 422 when body is missing data', async () => {
const body = {};
const response = await request(server).post('/note/create').send(body);

expect(response.status).toBe(422);
})

})

describe('GET /note/view/:id', () => {

afterEach(async () => {
await db('notes').truncate();
});

it(`responds with 404 when ID isn't available`, async () => {
const response = await request(server).get('/note/view/1');

expect(response.status).toBe(404);
})

it('responds with 200 when ID exists', async () => {
const body = { title: 'FakeNote', textBody: 'FakeBody' };
await request(server).post('/note/create').send(body);
const response = await request(server).get('/note/view/1');

expect(response.status).toBe(200);
})

})

describe('PUT /note/edit/:id', () => {

afterEach(async () => {
await db('notes').truncate();
});

it('responds with 200 when successfully updated', async () => {
const body = { title: 'FakeNote', textBody: 'FakeBody' };
const updatedBody = { title: 'UpdatedNote', textBody: 'UpdatedBody' };

await request(server).post('/note/create').send(body);
const updatedResponse = await request(server).put('/note/edit/1').send(updatedBody);

expect(updatedResponse.status).toBe(200);
})

it('responds with 422 when body is missing info', async () => {
const body = { title: 'FakeNote', textBody: 'FakeBody' };
const updatedBody = {};
await request(server).post('/note/create').send(body);
const updatedResponse = await request(server).put('/note/edit/1').send(updatedBody);

expect(updatedResponse.status).toBe(422);
})

})

describe('DELETE /note/delete/:id', () => {

afterEach(async () => {
await db('notes').truncate();
});

/* it('responds with 404 if ID is not valid', async () => {
const response = await request(server).delete('/note/delete/:id');

expect(response.status).toBe(404);
}) */

it('responds with 200 when succesfully deleted', async () => {
const body = { title: 'FakeNote', textBody: 'FakeBody' };
await request(server).post('/note/create').send(body);
const response = await request(server).delete('/note/delete/1');

expect(response.status).toBe(200);
})

})

})
6 changes: 6 additions & 0 deletions data/dbConfig.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const knex = require('knex');
const config = require('../knexfile.js');

const dbEnv = process.env.DB_ENV || 'development'; // change to NODE_ENV when deploying to Heroku

module.exports = knex(config[dbEnv]);
13 changes: 13 additions & 0 deletions data/migrations/20190212191223_notes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

exports.up = function(knex, Promise) {
return knex.schema.createTable('notes', tbl => {
tbl.increments();
// small character limit for title
tbl.string('title', 60).notNullable();
tbl.string('textBody').notNullable();
})
};

exports.down = function(knex, Promise) {
return knex.schema.dropTableIfExists('notes');
};
8 changes: 8 additions & 0 deletions data/migrations/20190223154916_notes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

exports.up = function(knex, Promise) {

};

exports.down = function(knex, Promise) {

};
Binary file added data/notes.db3
Binary file not shown.
28 changes: 28 additions & 0 deletions data/seeds/001-Notes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

exports.seed = function(knex, Promise) {
// Deletes ALL existing entries
return knex('notes').truncate()
.then(function () {
// Inserts seed entries
return knex('notes').insert([
{title: 'Hello Friends', textBody: `If you're reading this... I love you!`},
{title: 'Add more body to the music', textBody: `Holy moly, measure 34 could really use some fattening up. Get to it!`},
{title: 'Lambda billionare strikes again', textBody: `Wow, another truly breathtaking story of the rise of nobody to somebody under Capatilism.`},
{title: 'Yet another unique title', textBody: `It's getting tiring writing these... better not to put too much thought into it. It's only seed data! Make what's necessary and move on. Take care to not burnout.`},
{title: 'Phew that Voidz show was amaze-balls', textBody: `Amazin! They brought out Air for the encore! In the middle of a twenty-eight minute remix of their hit single Cherry Blossom Girl`},
{title: 'Testing 123', textBody: `Hey it's another test note here`},
{title: 'Is this work??', textBody: `Why doesn't this show up correctly?`},
{title: 'Formatting all messed up', textBody: `Wow what's going on?`},
{title: 'Stop Polluting Notes!', textBody: `Please please PLEASE take care to post only appropriate content. This is a shared database. Pick your words wisely as it reflects on your character and fellow digital student body.`},
{title: 'Make Notes Great Again!', textBody: `WHO NEEDS censorship?! Aren't you SICK of the NOTES SWAMP too?! We need to keep NOTES tidy and EXCLUSIVE. No more silly test note messages or nonsense. Make NOTES DB FREE AGAIN!!`},
{title: `Okay that's enough`, textBody: `If you're reading this... I genuinely put effort into writing some of these. It's fun!`},
{title: 'Hola', textBody: `If you're reading this... te quiero mucho! Quedate un rato.`},
{title: 'From me to the Universe', textBody: `If you're reading this... I love you!`},
{title: 'Pls Respond', textBody: `I miss it when we used to talk every night. Remember how we had the energy to stay up making up topics to talk about. Just you and me from 12 to dawn. It felt cute, young, and pink. Maybe romance is that way.`},
{title: 'What color are you today?', textBody: `I'm black... per usual.`},
{title: 'Starting to see a theme here', textBody: `The Clash`},
{title: 'What a great transition', textBody: `Listen to the way the music breaks and then re-convenes, what a masterful transition.`},
{title: 'Long Distance Call', textBody: `EP Phone Home!`},
]);
});
};
Binary file added data/test.db3
Binary file not shown.
8 changes: 8 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require('dotenv').config();

const server = require('./api/server.js');

const port = process.env.PORT || 5000;
server.listen(port, () => {
console.log(`\n** Server up on PORT ${port} **\n`);
})
45 changes: 45 additions & 0 deletions knexfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Update with your config settings.

module.exports = {

development: {
client: 'sqlite3',
connection: {
filename: './data/notes.db3'
},
useNullAsDefault: true,
migrations: {
directory: './data/migrations'
},
seeds: {
directory: './data/seeds'
}
},

testing: {
client: 'sqlite3',
connection: {
filename: './data/test.db3'
},
useNullAsDefault: true,
migrations: {
directory: './data/migrations'
},
seeds: {
directory: './data/migrations'
}
},

production: {
client: 'pg',
connection: process.env.DATABASE_URL,
useNullAsDefault: true,
migrations: {
directory: './data/migrations'
},
seeds: {
directory: './data/seeds'
}
},

};
36 changes: 36 additions & 0 deletions notes/notesModel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const db = require('../data/dbConfig.js');

module.exports = {
getAll,
insert,
findById,
remove,
update
};

function getAll() {
return db('notes');
}

async function insert(note) {
return db('notes')
.insert(note);
}

function findById(id) {
return db('notes')
.where('id', id);
}

async function remove(id) {
return db('notes')
.where('id', id)
.del();
}

async function update(id, changes) {
return db('notes')
.where('id', id)
.update(changes)
.then(count => (count > 0 ? this.findById(id): null));
}
Loading