Skip to content

inittial build #295

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

Open
wants to merge 2 commits into
base: main
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
41 changes: 20 additions & 21 deletions api/posts/posts-model.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const db = require('../../data/db-config');
const db = require("../../data/db-config");

module.exports = {
find,
Expand All @@ -12,47 +12,46 @@ module.exports = {
};

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

function findById(id) {
return db('posts').where({ id: Number(id) }).first()
return db("posts")
.where({ id: Number(id) })
.first();
}

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

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

function remove(id) {
return db('posts')
.where('id', Number(id))
.del();
return db("posts").where("id", Number(id)).del();
}

function findPostComments(postId) {
return db('comments')
.join('posts', 'posts.id', 'post_id')
.select('comments.*', 'title as post')
.where('post_id', postId);
return db("comments")
.join("posts", "posts.id", "post_id")
.select("comments.*", "title as post")
.where("post_id", postId);
}

function findCommentById(id) {
return db('comments')
.join('posts', 'posts.id', 'post_id')
.select('comments.*', 'title as post')
.where('comments.id', id).first();
return db("comments")
.join("posts", "posts.id", "post_id")
.select("comments.*", "title as post")
.where("comments.id", id)
.first();
}

function insertComment(comment) {
return db('comments')
return db("comments")
.insert(comment)
.then(ids => ({ id: ids[0] }));
.then((ids) => ({ id: ids[0] }));
}
125 changes: 125 additions & 0 deletions api/posts/posts-router.js
Original file line number Diff line number Diff line change
@@ -1 +1,126 @@
// implement your posts router here
const model = require("./posts-model");
const express = require("express");
const router = express.Router();

//get all posts
router.get("/", (req, res) => {
model
.find()
.then((posts) => {
res.status(200).json(posts);
})
.catch(() => {
res
.status(500)
.json({ message: "The posts information could not be retrieved" });
});
});

//get posts by id
router.get("/:id", (req, res) => {
const { id } = req.params;
model
.findById(id)
.then((post) => {
if (!post) {
res
.status(404)
.json({ message: "The post with the specified ID does not exist" });
} else {
res.status(200).json(post);
}
})
.catch(() => {
res
.status(500)
.json({ message: "The post information could not be retrieved" });
});
});

//post new post
router.post("/", (req, res) => {
const newPost = req.body;
model
.insert(newPost)
.then(() => {
if (!newPost.title || !newPost.contents) {
res
.status(400)
.json({ message: "Please provide title and contents for the post" });
} else {
res.status(201).json(newPost);
}
})
.catch(() => {
res.status(500).json({
message: "There was an error while saving the post to the database",
});
});
});

//update a post
router.put("/:id", async (req, res) => {
const { id } = req.params;
const postChanges = req.body;

try {
if (!postChanges.title || !postChanges.contents) {
res
.status(400)
.json({ message: "Please provide title and contents for the post" });
} else {
const updatedPost = model.update(id, postChanges);
if (!updatedPost) {
res
.status(404)
.json({ message: "The post with the specified ID does not exist" });
} else {
res.status(200).json(updatedPost);
}
}
} catch {
res
.status(500)
.json({ message: "The post information could not be modified" });
}
});

//delete post
router.delete("/:id", async (req, res) => {
const { id } = req.params;

try {
const removed = model.remove(id);
if (!removed) {
res
.status(404)
.json({ message: "The post with the specified ID does not exist" });
} else {
res.status(201).json(removed);
}
} catch {
res.status(500).json({ message: "The post could not be removed" });
}
});

//get all of the comments
router.get("/:id/comments", async (req, res) => {
const { id } = req.params;
try {
const postComms = model.findPostComments(id);
if (!postComms) {
res
.status(404)
.json({ message: "The post with the specified ID does not exist" });
} else {
res.status(200).json(postComms);
}
} catch {
res
.status(500)
.json({ message: "The comments information could not be retrieved" });
}
});

module.exports = router;
9 changes: 9 additions & 0 deletions api/server.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,11 @@
// implement your server here
// require your posts router and connect it here

const express = require("express");
const server = express();
const postRouter = require("./posts/posts-router");

server.use(express.json());
server.use("/api/posts", postRouter);

module.exports = server;
Binary file modified data/database.db3
Binary file not shown.
5 changes: 5 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
// require your server and launch it here
const server = require("./api/server");

server.listen(9990, () => {
console.log("server running on 9990");
});
Loading