From f51fb148c79c89698fd5e7b9f667bac6758bf168 Mon Sep 17 00:00:00 2001 From: yvette-luong <63530290+yvette-luong@users.noreply.github.com> Date: Mon, 16 Nov 2020 13:13:58 -0800 Subject: [PATCH 1/4] first 5 endpoints works locally --- api/config.js | 4 +- api/server.js | 10 +++++ data/expat.db3 | Bin 36864 -> 36864 bytes knexfile.js | 2 +- posts/posts-model.js | 55 +++++++++++++++++--------- posts/posts-router.js | 88 +++++++++++++++++++++++++++++++++++++----- 6 files changed, 127 insertions(+), 32 deletions(-) diff --git a/api/config.js b/api/config.js index 00dd1bc..726f965 100644 --- a/api/config.js +++ b/api/config.js @@ -1,5 +1,5 @@ // read configuration dynamically <-- `development` inside knexfile.js -require('dotenv').config(); +// require('dotenv').config(); const knex = require("knex"); const knexfile = require('../knexfile'); @@ -8,6 +8,6 @@ const knexfile = require('../knexfile'); const enviroment = process.env.NODE_ENV || "development"; -const config = knexfile(enviroment); +const config = knexfile[enviroment]; module.exports = knex(config); diff --git a/api/server.js b/api/server.js index 8821067..4f2f2fb 100644 --- a/api/server.js +++ b/api/server.js @@ -1,15 +1,25 @@ +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 server = express(); +//middleware server.use(cors()); server.use(express.json()); server.use('/',helmet()); server.use('/', morgan('--API testing for Expat Journal BuildWeek--')) +//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 4dc6f091e2ab2281c3378da9513a93ff81ebcdc9..e5455d661bd034667cbb5111e7ad780199c2cd2e 100644 GIT binary patch delta 1723 zcmaJ>O-vI(6rN@6zulpf7T8i*5TXQ0DGOGD3dS@h3T+WcP#~p&4OXG8umnvMm>7cz z2O)dV$U#nO;(sw<^tC1NDR4^p{&^qQ~N1v4{do{H%a@)cIX1v=iOsQzvN7%58 z@C7b(a>w?^He_WjedbrDA>)?WVBNHTFjg9tStE0xzA^*v7U^c{fVYBBLj#G1qe+MYg`xjaE}L56ET|for%s{SYZR-mPPlrNjM#OIr=B63$BljAg!8YzU zH;udSPrQupaZkDLcoX;I-Rw8*D1B>Rc|$^h!hKozXW6gWE7RcwICDuE`<<6%2 z4!hVzU$#qna;v;-*Ri|Y?zUzH%SJ6!dM1xUjCwRX*(NQe#L;cyrM3}1zpack0BEG$ zVlbRyuQ1Bz9{%hJXc)`Do%qZ|Ioq5q8hVp1J9Ptf&xo;CU5aM!{6x5%)!85-^kgR8j_J&G3q%H&2jEk^Uro_;$R z!Itp1Gw?s=Z|6V4zYJ(eIX{aWvm@i={XSBhEV9h@j4(S`nZe>ceo~Vk#cOUBZaB$5 Vae~gK0)BzbLI!8~C*SE;0RTw^LWuwX 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 From 1180104f5d95269841b2fd1de489bc6bdb666f78 Mon Sep 17 00:00:00 2001 From: yvette-luong <63530290+yvette-luong@users.noreply.github.com> Date: Mon, 16 Nov 2020 13:15:09 -0800 Subject: [PATCH 2/4] Update expat.db3 --- data/expat.db3 | Bin 28672 -> 36864 bytes 1 file changed, 0 insertions(+), 0 deletions(-) diff --git a/data/expat.db3 b/data/expat.db3 index e3c0e525678170eb4c6170065d549136580e3adf..e5455d661bd034667cbb5111e7ad780199c2cd2e 100644 GIT binary patch literal 36864 zcmeI5Pi))P9mgNV=${>nZCyuo9M_z&W)U3OqW)OYJi~PzbxYP|apRyVxy zDkL>qmu*ZoWWX*x4$EP;VZaVWfet9p>&`uH7*Omo^ip6LhGE!Y2)5s&L`qa7#~B8; z$bNJrlJ6hCzt8)==H~7jib=%-O>fDjm?6&*jw7#%A|d1T@;X|^z^~V{AjZAfkupRqr?Apx2*^j0_nfeR@a6Q>NxXIa>h7(Qc=h^~(rvLa zHho3Z)P7V&ykw_grBxEvK{yr@w2 z%@oy6vnjT9r6ucK@sM^!xnpXITGeTbs^(>}a-isjXT} zIT(&h;67p7eR?LDirI90#|{aN^_|BP_Z+8JJ~)nDP9<^g@)_G}ew?>q+R^q~(2LE@ zHxAQ@Uc>5R7$7k1UE8hRQ75WVqpB-yQ_<96AMkowyP=s{xuZ9SlXx7-L)%f|rEU94 zpEhe(HQJ;wP~6$RxwCzx#6mjlnmh^>AF+ z886InNthqn)%=u=Qt4fl4jOafMj#YlS>fKcB^q~|F!n7Q6#5$GlipL_BWxt{5^L+1 zA!ow+1{Y>U-dh`I_J}zFPnngGSliBlShw>y(TBX94(krUi}q!++Hh$>fLjq|Q-K~) zWHrZ*5^QW95I!Qpr|^aw5ihgKmter2_OL^fCP{L5@8J#?4-|8=s%!0wN1IYgDqOs~SxE6e;KGC~Ee=U>boi)& z+$UslNQ3VZ!jJghkl9(A0$kiT)8V=qLN@YNS-hroVC`d5ya9{qV4WN+Nn&o{2Jf-8 zZFZz-`X&@-?Z+Wsla$vne16RdL=Ku`wg_7&KE$@(fqt&a)vPuN!l5VrBn zNo=KItoZ^w1){d9Lj-%B;4QZ96uhurAAvZuI94>lQ^i5bx_`3QqRc-qDrOOASKiynP(jym%rGDk@69pG0wZo^Zw*hjf_F2tZh z|9PWhio-1mJaE#gCK0S zhGpUIf^`2(xOxV`+9vH@YgE7W+JXGm zuJYYGrS5mG)QXb_PW^hbN*q|a1wL4Qojv>GRP|=H4>~KQ@NB{{S0VIF~kN^@u0!RP}AOR$R1pFoNG9M!I@wp|V zNol)lDmtxoRzgDX3i@j*UJVH#0VIF~kN^@u0!RP}AOR$R1dsp{_-_eB_&JWv U81OUv66a111kUnHQFk8TKQ7VeR{#J2 delta 388 zcmZozz|`=7ae}m<69WSSI}pQw&O{w!Nhbz9`6--w z^0^sbO}@k(!~&ECF*Lav&Dhw*b#)n=xJAIDx zHF`0#GuStBHl~JTWEM@H=qEZk#$Sj#Gf$x;BUJ$^<|<)gX=q_&Y-DO=XsVlHYG|I4 zYHpNlVw5tu(M?OhT&>X7!k{sYiJif*k-JH9a*v-B$dt*gep{Gb7}+P=`X2#1gTI}D z|1p0%{}KLWKxdTmv&b AO8@`> From 7b4e91395b894422d31ea86c7200ceadfe395d66 Mon Sep 17 00:00:00 2001 From: yvette-luong <63530290+yvette-luong@users.noreply.github.com> Date: Mon, 16 Nov 2020 13:56:43 -0800 Subject: [PATCH 3/4] Update expat.db3 --- data/expat.db3 | Bin 36864 -> 36864 bytes 1 file changed, 0 insertions(+), 0 deletions(-) diff --git a/data/expat.db3 b/data/expat.db3 index 5d5b16c60a1425b18302bbb72e6fc495c2b062c9..e5455d661bd034667cbb5111e7ad780199c2cd2e 100644 GIT binary patch delta 2057 zcmaJ?O>7%Q6rS^QMwJ0^?UibhGAq;4>kDXtZ)s>LKJO;Dh%?5Noqlf+K#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Sv9Y9yF;9+U>U}2xU)8{x}qZczfgMA}sV`@l7X3^w{ zexj3O{DrtP^At)lQWcwpz*clufxtk;>_xMSHOqty3w}sh-k$tkQ{}He=_}dxyAM>~KAK_mHbVfNpiyX5f z From 88de56cba5b53903de2015ded7d342faffbeda23 Mon Sep 17 00:00:00 2001 From: yvette-luong <63530290+yvette-luong@users.noreply.github.com> Date: Mon, 16 Nov 2020 14:24:37 -0800 Subject: [PATCH 4/4] update |GET users/:id/posts| --- users/users-router.js | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 users/users-router.js diff --git a/users/users-router.js b/users/users-router.js new file mode 100644 index 0000000..b900886 --- /dev/null +++ b/users/users-router.js @@ -0,0 +1,35 @@ +const router = require("express").Router(); + +const Users = require("./users-model"); +const restricted = require("../auth/auth-middleware"); + + +router.get("/", restricted, (req, res) => { + Users.find() + .then(users => { + res.status(200).json(users); + }) + .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