diff --git a/api/config.js b/api/config.js index 31219cd..6e205f6 100644 --- a/api/config.js +++ b/api/config.js @@ -1,3 +1,5 @@ +// read configuration dynamically <-- `development` inside knexfile.js +// require('dotenv').config(); const knex = require("knex"); diff --git a/api/server.js b/api/server.js index aae92bb..058842b 100644 --- a/api/server.js +++ b/api/server.js @@ -1,18 +1,28 @@ +require('dotenv').config(); const express = require('express'); const cors = require('cors'); const helmet = require('helmet'); const morgan = require ("morgan"); +//router +const postsRouter = require('../posts/posts-router') + +//server const authRouter = require('../auth/auth-router') const server = express(); +//middleware server.use(cors()); server.use(express.json()); server.use('/',helmet()); server.use('/', morgan('--API testing for Expat Journal BuildWeek--')) server.use('/api/auth', authRouter) +//routes +server.use('/api/posts', postsRouter) + + server.get('/', (req, res) =>{ res.status(200).json({server: 'Expat Journal BuildWeek'}) }) diff --git a/data/expat.db3 b/data/expat.db3 index 5d5b16c..e5455d6 100644 Binary files a/data/expat.db3 and b/data/expat.db3 differ diff --git a/knexfile.js b/knexfile.js index d2d4345..d7c92b7 100644 --- a/knexfile.js +++ b/knexfile.js @@ -42,7 +42,7 @@ module.exports = { production: { connection: pgConnection, - useNullAsDefault: true, + // useNullAsDefault: true, // client: 'postgresql', client: "pg", migrations: { diff --git a/posts/posts-model.js b/posts/posts-model.js index 6263425..020baa0 100644 --- a/posts/posts-model.js +++ b/posts/posts-model.js @@ -1,33 +1,50 @@ -const db = require('../api/config') +const db = require("../api/config"); module.exports = { - find, - findBy, - findById, - insert, - update, - remove + find, + findBy, + findById, + insert, + update, + remove, +}; +//Find all posts data +function find() { + return db("posts").select( + "id", + "user_id", + "title", + "description", + "photo_url" + ); } -function find(){ +//Find posts by id +function findById(id) { + return db("posts") + .select("id", "user_id", "title", "description", "photo_url") + .where({ id }) + .first(); } -function findBy(){ - -} - -function findById(){ - +//insert new post into db +function insert(newPost) { + return db("posts") + .insert(newPost).returning(["title", "description", "photo_url"]); } -function insert(){ +//Find posts by filter +function findBy(filter) { + return db("posts").where(filter); } -function update(){ - +//update new infor of pj to db +function update(changes, id) { + return db("posts").where({ id }).update(changes); //updates the record with 'changes' where the id matches } -function remove(){ - +//remove post from database +function remove(id) { + return db("posts").where({ id }).del(); } diff --git a/posts/posts-router.js b/posts/posts-router.js index 97543be..5fa9467 100644 --- a/posts/posts-router.js +++ b/posts/posts-router.js @@ -4,30 +4,98 @@ const model = require('./posts-model') //GET all posts router.get('/', (req,res) =>{ - + model.find() + .then(posts=>{ + res.status(200).json({notification:'Information of posts are successfully retrieved', postsdata: posts}) + }) + .catch(err=>{ + res.status(500).json({notification:'Failed to retrieve information of posts', error: err.message}) + }) }) //GET posts by id router.get('/:id', (req,res) =>{ - + const id = req.params.id; + model + .findById(id, "posts") + .then((post) => + res.status(200).json({ + message: "This is information of post with specific ID", + post, + }) + ) + .catch((err) => res.status(500).json({ status: 'Failed to retrieve information of post BY ID', errorMess: err.message })); }) //GET posts by users id -router.get('/users/:id/posts', (req,res)=>{ - -}) +// router.get('/users/:id/posts', (req,res)=>{ -//POST a new post -router.post('/', (req,res)=>{ +// }) +// //POST a new post +router.post('/', (req,res)=>{ + let newPost = req.body; + model.insert(newPost).then(newPost=>{ + res.status(200).json({notification :"New Post is updated", newPost: newPost}) + }) + .catch(err=>{ + console.log(err) + res.status(500).json({notification: 'Failed To Add New Post', postSent: newPost, errorMess:err.message}) + }) }) -//UPDATE a post by id -router.put('/', (req,res)=>{ +//UPDATE a post by id +router.put('/:id', (req,res)=>{ + const { id } = req.params; + const changes = req.body; + + model + .findById(id, "posts") + .then((post) => { + if (post) { + model.update(changes, id).then((updated) => { + res + .status(201) + .json({ + success: "Post information is updated", + ...changes, + id: post.id, + UpdatingInformation: updated, + }); + }); + } else { + res + .status(401) + .json({ + message: `Could Not Find Object With ID: ${id}`, + errorMess: err.message, + }); + } + }) + .catch((err) => { + console.log(err); + res + .status(500) + .json({ message: "Failed To Update Project", errormessage: err }); + }); }) //DELETE a post by id router.delete('/:id', (req,res)=>{ - + model.remove(req.params.id) + .then((num) => { + if (num === 1) { + res.status(200).json({ succesmessage: "This post is successfully deleted"}).end(); + } else { + res.status(404).json({ message: "Failed to delete the post" }).end(); + } + }) + .catch((err) => { + res + .status(500) + .json({ message: "Error in delete post", error: err.message }); + }); }) + +module.exports = router; \ No newline at end of file diff --git a/users/users-router.js b/users/users-router.js index bd65f51..98de7bd 100644 --- a/users/users-router.js +++ b/users/users-router.js @@ -2,6 +2,7 @@ const router = require("express").Router(); const Users = require("./users-model"); const restricted = require("../auth/auth-middleware"); +const Posts = require("../posts/posts-model"); router.get("/", restricted, (req, res) => { @@ -12,4 +13,24 @@ router.get("/", restricted, (req, res) => { .catch(err => res.send(err)); }); +//GET user's posts +router.get('/:id/posts', (req, res) => { + Users.findById(req.params.id) + .then(user =>{ + if(user){ + Posts.findBy({user_id: user.id}).then(posts =>{ + res.status(200).json({notification:`Information of ${user.username} post`, information: posts}) + }) + .catch(err =>{ + res.status(401).json({message: `Failed To Find Posts For: ${user.username}`, errMessage: err.message}) + }) + } else{ + res.status(401).json({message: `Failed To Find User With ID: ${req.params.id}`}) + } + }) + .catch(err => { + res.status(500).json({message: 'Failed To Get Users Posts', errMessage:err.message}) + }) +}) + module.exports = router; \ No newline at end of file