Skip to content
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
The diff you're trying to view is too large. We only load the first 3000 changed files.
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"git.ignoreLimitWarning": true
}
1 change: 1 addition & 0 deletions Procfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
web: node index.js
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
Trello Link: https://trello.com/b/mj76sFkf/lambda-notesbackend-louis-li

Heroku: https://dashboard.heroku.com/apps/stark-refuge-65834

Netlify: https://quirky-haibt-9c03e3.netlify.com/


# Back End Project Week

This week you will build a backend for a note taking app called "Lambda Notes".
Expand Down
91 changes: 91 additions & 0 deletions data/helpers/postsHelpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
const knex = require('knex');
const knexConfig = require('../../knexfile.js');
const db = knex(knexConfig.development);

module.exports = {
find,
findById,
insert,
update,
remove,
insertTags,
deleteTags,
};

function find() {
return db('posts');
}

//get post's tags
function getPostTags(postId) {
return db('tags')
.join('posttags', 'tags.id', 'posttags.tagId')
.select('tags.tag')
.where('posttags.postId', postId);
}

function findById(id) {
let query = db('posts');
query.where({ id: Number(id) });
const promises = [query, getPostTags(id)]; // [ posts, tags ]
return Promise
.all(promises)
.then((res) => {
let [posts, tags] = res;
let post = posts[0];
post.tags = tags.map(x => x.tag);
return post;
});
}

function deleteTags(postId) {
return db('posttags')
.where('postId', Number(postId))
.del()
}

function insertTags(postId, tags) {
const tagsArr = tags.map(x => {
return { tag: x };
})
tagsArr.map(tag => {
return db('tags')
.where('tag', tag.tag)
.then(tags => {
if (tags.length === 0) {
db('tags').insert(tag)
.then(tagIds => {
insertPostTags(postId, tagIds[0]);
})
} else {
insertPostTags(postId, tags[0].id)
}
})
})
}

function insertPostTags(postId, tagId) {
const postTag = { postId, tagId };
db('posttags')
.insert(postTag)
.then(ids => { id: ids[0] }
)
}

function insert(post) {
return db('posts')
.insert(post)
.then(postId => postId);
}

function update(id, post) {
return db('posts')
.where('id', Number(id))
.update(post)
}

function remove(id) {
return db('posts')
.where('id', Number(id))
.del();
}
31 changes: 31 additions & 0 deletions data/helpers/tagsHelpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const knex = require('knex');
const knexConfig = require('../../knexfile');
const db = knex(knexConfig.development);

module.exports = {
find,
findById,
insert,
remove,
};

function find() {
return db('tags');
}

function findById(id) {
return db('tags')
.where({ id: Number(id) })
}

function insert(tag) {
return db('tags')
.insert(tag)
.then(ids => ({ id: ids[0] }));
}

function remove(id) {
return db('tags')
.where('id', Number(id))
.del();
}
13 changes: 13 additions & 0 deletions data/migrations/20180401213248_add_posts_table.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
exports.up = function(knex) {
return knex.schema.createTable('posts', function(posts) {
posts.increments();

posts.string('title', 255).notNullable();
posts.text('contents').notNullable();
posts.timestamps(true, true);
});
};

exports.down = function(knex) {
return knex.schema.dropTableIfExists('posts');
};
10 changes: 10 additions & 0 deletions data/migrations/20190213123532_create_tags_table.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
exports.up = function (knex) {
return knex.schema.createTable('tags', function (tags) {
tags.increments();
tags.string('tag', 50).notNullable().unique('tag');
});
};

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

exports.up = function (knex, Promise) {
return knex.schema
.createTable('posttags', function (posttags) {
posttags.increments();
posttags
.integer('postId')
.unsigned()
.notNullable()
.references('id')
.inTable('posts');
posttags
.integer('tagId')
.unsigned()
.notNullable()
.references('id')
.inTable('tags');
});
};

exports.down = function (knex, Promise) {
return knex.schema.dropTableIfExists('posttags');
};
Binary file added data/notes.db
Binary file not shown.
152 changes: 152 additions & 0 deletions data/routes/postsRouter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
const express = require("express")
const router = express.Router();
const postDb = require('../helpers/postsHelpers');

//endpoints

//POST
router.post('/posts', (req, res) => {
const { title, contents, tags } = req.body;
const newPost = { title, contents };
const postTags = tags;

//return new post after posting
const findPost = id =>
postDb.findById(id)
.then(post => {
res.status(201)
.json(post)
})

//posting new post
if (title && contents) {
postDb.insert(newPost)
.then(postId => {
postDb.insertTags(postId[0], postTags);
findPost(postId[0]);
})
.catch(err => {
res.status(500)
.json({ error: "There was an error while saving the post to the database" })
})
}
else {
res.status(400)
.json({ errorMessage: "Please provide title and contents for the post." })
}
})

//GET all posts
router.get('/posts', (req, res) => {
postDb.find()
.then(posts => {
res.json(posts);
})
.catch(err => {
res
.status(500)
.json({ error: "The posts information could not be retrieved." })
})
})
//GET one post
router.get('/posts/:id', (req, res) => {
const { id } = req.params;
postDb.findById(id)
.then(post => {
if (post) {
res.json(post);
}
else {
res.status(404)
.json({ message: "The post with the specified ID does not exist." })
}
})
.catch(err => {
res
.status(500)
.json({ message: "failed to get post" })
})
});

//PUT
router.put('/posts/:id', async (req, res) => {
const { id } = req.params;
const { title, contents, tags } = req.body;
const updatedPost = { title, contents };
const updatedTags = tags;

//return updated post after updating
const findPost = id =>
postDb.findById(id)
.then(post => {
res.status(201)
.json(post)
})

//delete all old tags before updating
const deleteOldTags = id => {
postDb.deleteTags(id)
.then(count => {
console.log(count);
})
}
//

//updating
if (title && contents) {
postDb.update(id, updatedPost)
.then(count => {
if (count) {
// deleteOldTags(id);
postDb.deleteTags(id)
.then(count => {
if (count) {
postDb.insertTags(id, updatedTags);
// findPost(id)
;

}
});
}
else {
res.status(404)
.json({ message: "The post with the specified ID does not exist." })
}
})
.catch(err => {
res.status(500)
.json({ error: "The post information could not be modified." })
})
}
else {
res.status(400)
.json({ errorMessage: "Please provide title and contents for the post." })
}

//cannot get the most updated tags
await findPost(id);
})

//DELETE
router.delete('/posts/:id', (req, res) => {
const { id } = req.params;
postDb.remove(id)
.then(count => {
if (count) {
res.json({ message: "Post succesfully deleted." });
}
else {
res.status(404)
.json({ message: "The post with the specified ID does not exist." })
}
})
.catch(err => {
res.status(500)
.json({ error: "The post could not be removed" })
})
})




module.exports = router;
Loading