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
23 changes: 23 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "node-2",
"version": "1.0.0",
"description": "In this project you'll build an API that with the following entities",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/boomcamp/node-2.git"
},
"keywords": [],
"author": "",
"license": "ISC",
"bugs": {
"url": "https://github.com/boomcamp/node-2/issues"
},
"homepage": "https://github.com/boomcamp/node-2#readme",
"dependencies": {
"express": "^4.17.1"
}
}
60 changes: 60 additions & 0 deletions server/controller/profileController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
module.exports = {
create:(req, res) => {
const db = req.app.get('db')
const { email, password } = req.body;
let user = {
id: db.users.id,
password,
email
}
let profile = {
userId: db.profiles.id,
thumbnail: null,
about: ''
}
db.users.data.push(user)
db.users.id++;
db.profiles.data.push(profile)
db.profiles.id++
res.status(201).json(user)
},
read:(req, res) => {},
update:(req, res) => {
const db = req.app.get('db');
const toUpdate = db.profiles.data.find(p => p.userId === parseInt(req.params.userId));
console.log(toUpdate)
if (toUpdate) {
const { thumbnail, about } = req.body;

Object.assign(toUpdate, {
...(thumbnail && { thumbnail }),
...(about && { about }),
});
return res.status(200).send(toUpdate);
}
res.status(200).json(toUpdate);
},
post: (req, res) => {
const db = req.app.get('db');
const { post } = req.body;
let posts = {
userId: db.posts.id,
post: post
}
db.posts.data.push(posts)
db.posts.id++;
res.status(201).json(posts)
},
comment: (req, res) =>{
const db = req.app.get('db');
const { comment } = req.body;
let comments = {
userId: db.comments.id,
comment: comment
}
db.comments.data.push(comments)
db.comments.id++;
res.status(201).json(comments);
},
delete:(req, res) => {}
}
41 changes: 41 additions & 0 deletions server/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const express = require('express')
const profile = require('./controller/profileController')
const app = express()
const db = {
users: {
id: 0,
data: [],
},
profiles: {
id: 0,
data: [],
},
posts: {
id: 0,
data: [],
},
comments: {
id: 0,
data: [],
},
}

app.set('db', db)
app.use(express.json())

const port = 3001

app.post('/api/sign-up', profile.create)
app.get('/debug', (req, res) => {
res.status(200).json(req.app.get('db'))
})
app.patch('/api/patch/:userId', profile.update)
app.post('/api/post/:postId', profile.post)
app.post('/comments/:commentId', profile.comment)

app.listen(port, err =>{
if(err){
console.log(`Unable to d(-,-)b to Port ${port}`)
}
console.log(`Listening to Port ${port}`)
})