From 7a9ab828ad5321a83693e7ca14705c8205545d11 Mon Sep 17 00:00:00 2001 From: franco Date: Mon, 6 Jan 2020 10:07:35 +0800 Subject: [PATCH] inital commit --- package.json | 23 ++++++++++ server/controller/profileController.js | 60 ++++++++++++++++++++++++++ server/index.js | 41 ++++++++++++++++++ 3 files changed, 124 insertions(+) create mode 100644 package.json create mode 100644 server/controller/profileController.js create mode 100644 server/index.js diff --git a/package.json b/package.json new file mode 100644 index 0000000..1552951 --- /dev/null +++ b/package.json @@ -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" + } +} diff --git a/server/controller/profileController.js b/server/controller/profileController.js new file mode 100644 index 0000000..860a709 --- /dev/null +++ b/server/controller/profileController.js @@ -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) => {} +} \ No newline at end of file diff --git a/server/index.js b/server/index.js new file mode 100644 index 0000000..38caa93 --- /dev/null +++ b/server/index.js @@ -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}`) +}) \ No newline at end of file