forked from yustudio/react-employee-node
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 0804fa6
Showing
11 changed files
with
3,132 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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."}] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.