Skip to content

Commit

Permalink
Initial node code
Browse files Browse the repository at this point in the history
  • Loading branch information
desongyu committed Jul 2, 2019
0 parents commit 0804fa6
Show file tree
Hide file tree
Showing 11 changed files with 3,132 additions and 0 deletions.
23 changes: 23 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# production
/build

# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local

npm-debug.log*
yarn-debug.log*
yarn-error.log*
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
## Available Scripts

In the project directory, you can run:

### `npm run dev`
1 change: 1 addition & 0 deletions data/posts.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"id":"5f525c10-9c5f-11e9-8741-9f31125355ae","role":"VP","hireDate":"2019-07-02T00:21:34.773Z","firstName":"s","lastName":"b","quote1":"You had me at meat tornado.","quote2":"A steak pun is a rare medium well done."},{"id":"95056a50-9c69-11e9-8741-9f31125355ae","role":"CEO","hireDate":"2019-07-03T16:00:00.000Z","firstName":"v","lastName":"v","quote1":"I hate everything.","quote2":"Did you hear about the bread factory burning down? They say the business is toast."},{"id":"25c9e460-9c6c-11e9-8741-9f31125355ae","role":"CEO","hireDate":"2019-07-01T16:00:00.000Z","firstName":"t","lastName":"t","quote1":"Fishing relaxes me. It's like yoga, except I still get to kill something.","quote2":"Have you heard of the band 1023MB? They haven't got a gig yet."}]
35 changes: 35 additions & 0 deletions helpers/helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const fs = require('fs')
const path = require("path");
const uuidv1 = require('uuid/v1');

const getNewId = (array) => {
return uuidv1();
}
const newDate = () => new Date().toString()

function mustBeInArray(array, id) {
return new Promise((resolve, reject) => {
const row = array.find(r => r.id === id)
if (!row) {
reject({
message: 'ID is not valid or doesn\'t exist',
status: 404
})
}
resolve(row)
})
}
function writeJSONFile(filename, content) {
fs.writeFileSync(path.resolve(__dirname, filename), JSON.stringify(content), 'utf8', (err) => {
if (err) {
console.log(err)
}
})
}

module.exports = {
getNewId,
newDate,
mustBeInArray,
writeJSONFile
}
12 changes: 12 additions & 0 deletions helpers/middlewares.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

function checkFieldsPost(req, res, next) {
const { firstName, lastName } = req.body
if (firstName && lastName) {
next()
} else {
res.status(400).json({ message: 'fields are not good' })
}
}
module.exports = {
checkFieldsPost
}
10 changes: 10 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

const express = require('express')
const morgan = require('morgan')
const app = express()

app.use(morgan('tiny'))
app.listen('5000')
app.use(express.json())
app.use(express.urlencoded({ extended: true }))
app.use(require('./routes/index.routes'))
70 changes: 70 additions & 0 deletions models/post.model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
const filename = '../data/posts.json'
let posts = require(filename)
const helper = require('../helpers/helper.js')

function getPosts() {
return new Promise((resolve, reject) => {
if (posts.length === 0) {
reject({
message: 'no posts available',
status: 202
})
}
resolve(posts)
})
}

function getPost(id) {
return new Promise((resolve, reject) => {
helper.mustBeInArray(posts, id)
.then(post => resolve(post))
.catch(err => reject(err))
})
}

function insertPost(newPost) {

return new Promise((resolve, reject) => {
const id = { id: helper.getNewId(posts) }
newPost = { ...id, ...newPost }
posts.push(newPost)
helper.writeJSONFile(filename, posts)
resolve(newPost)
})
}

function updatePost(id, newPost) {
return new Promise((resolve, reject) => {
helper.mustBeInArray(posts, id)
.then(post => {
const index = posts.findIndex(p => p.id === post.id)
id = { id: post.id }
posts[index] = { ...id, ...newPost }
helper.writeJSONFile(filename, posts)
resolve(posts[index])
})
.catch(err => reject(err))
})
}

function deletePost(id) {
return new Promise((resolve, reject) => {
helper.mustBeInArray(posts, id)
.then(() => {
posts = posts.filter(p => {
return p.id !== id
})
helper.writeJSONFile(filename, posts)
resolve()
})
.catch(err => reject(err))
})
}

module.exports = {
insertPost,
getPosts,
getPost,
updatePost,
deletePost
}
Loading

0 comments on commit 0804fa6

Please sign in to comment.