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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions api/config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// read configuration dynamically <-- `development` inside knexfile.js
// require('dotenv').config();
const knex = require("knex");


Expand Down
10 changes: 10 additions & 0 deletions api/server.js
Original file line number Diff line number Diff line change
@@ -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'})
})
Expand Down
Binary file modified data/expat.db3
Binary file not shown.
2 changes: 1 addition & 1 deletion knexfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ module.exports = {

production: {
connection: pgConnection,
useNullAsDefault: true,
// useNullAsDefault: true,
// client: 'postgresql',
client: "pg",
migrations: {
Expand Down
55 changes: 36 additions & 19 deletions posts/posts-model.js
Original file line number Diff line number Diff line change
@@ -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();
}
88 changes: 78 additions & 10 deletions posts/posts-router.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
21 changes: 21 additions & 0 deletions users/users-router.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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;