-
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.
added api server to github repo. added empty md file to write in a mo…
…ment
- Loading branch information
Showing
33,735 changed files
with
2,803,508 additions
and
2,434 deletions.
The diff you're trying to view is too large. We only load the first 3000 changed files.
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,45 @@ | ||
# API Server | ||
|
||
To install and start the API server, run the following commands in this directory: | ||
|
||
* `npm install` | ||
* `node server` | ||
|
||
## Using The Server | ||
|
||
### Include An Authorization Header | ||
|
||
All requests should use an **Authorization header** to work with your own data: | ||
|
||
```js | ||
fetch( | ||
url, | ||
{ | ||
headers: { 'Authorization': 'whatever-you-want' } | ||
} | ||
) | ||
``` | ||
|
||
### Comment Counts | ||
Posts retrieved in a list or individually now contain comment counts in the format `post: { commentCount: 0 }`. This should make it easier to display the number of comments a post has without having to call the comments endpoint for each post. This count is updated whenever a comment is added or deleted via the `POST /comments` or `DELETE /comments/:id` endpoints. | ||
|
||
### API Endpoint | ||
|
||
The following endpoints are available: | ||
|
||
| Endpoints | Usage | Params | | ||
|-----------------|----------------|----------------| | ||
| `GET /categories` | Get all of the categories available for the app. List is found in `categories.js`. Feel free to extend this list as you desire. | | | ||
| `GET /:category/posts` | Get all of the posts for a particular category. | | | ||
| `GET /posts` | Get all of the posts. Useful for the main page when no category is selected. | | | ||
| `POST /posts` | Add a new post. | **id** - UUID should be fine, but any unique id will work <br> **timestamp** - [Timestamp] Can in whatever format you like, you can use `Date.now()` if you like. <br> **title** - [String] <br> **body** - [String] <br> **author** - [String] <br> **category** - Any of the categories listed in `categories.js`. Feel free to extend this list as you desire. | | ||
| `GET /posts/:id` | Get the details of a single post. | | | ||
| `POST /posts/:id` | Used for voting on a post. | **option** - [String]: Either `"upVote"` or `"downVote"`. | | ||
| `PUT /posts/:id` | Edit the details of an existing post. | **title** - [String] <br> **body** - [String] | | ||
| `DELETE /posts/:id` | Sets the deleted flag for a post to 'true'. <br> Sets the parentDeleted flag for all child comments to 'true'. | | | ||
| `GET /posts/:id/comments` | Get all the comments for a single post. | | | ||
| `POST /comments` | Add a comment to a post. | **id** - Any unique ID. As with posts, UUID is probably the best here. <br> **timestamp** - [Timestamp] Get this however you want. <br> **body** - [String] <br> **author** - [String] <br> **parentId** - Should match a post id in the database. | | ||
| `GET /comments/:id` | Get the details for a single comment. | | | ||
| `POST /comments/:id` | Used for voting on a comment. | **option** - [String]: Either `"upVote"` or `"downVote"`. | | ||
| `PUT /comments/:id` | Edit the details of an existing comment. | **timestamp** - timestamp. Get this however you want. <br> **body** - [String] | | ||
| `DELETE /comments/:id` | Sets a comment's deleted flag to `true`. | | |
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,41 @@ | ||
const clone = require('clone') | ||
const config = require('./config') | ||
|
||
let db = {} | ||
|
||
const defaultData = { | ||
categories: [ | ||
{ | ||
name: 'react', | ||
path: 'react' | ||
}, | ||
{ | ||
name: 'redux', | ||
path: 'redux' | ||
}, | ||
{ | ||
name: 'udacity', | ||
path: 'udacity' | ||
} | ||
] | ||
} | ||
|
||
function getData (token) { | ||
//Each token has it's own copy of the DB. The token in this case is like an app id. | ||
let data = db[token] | ||
//This populates the default user data if there isn't any in the db. | ||
if (data == null) { | ||
data = db[token] = clone(defaultData) | ||
} | ||
return data | ||
} | ||
|
||
function getAll (token) { | ||
return new Promise((res) => { | ||
res(getData(token)) | ||
}) | ||
} | ||
|
||
module.exports = { | ||
getAll | ||
} |
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,132 @@ | ||
const clone = require('clone') | ||
const posts = require('./posts') | ||
|
||
let db = {} | ||
|
||
const defaultData = { | ||
"894tuq4ut84ut8v4t8wun89g": { | ||
id: '894tuq4ut84ut8v4t8wun89g', | ||
parentId: "8xf0y6ziyjabvozdd253nd", | ||
timestamp: 1468166872634, | ||
body: 'Hi there! I am a COMMENT.', | ||
author: 'thingtwo', | ||
voteScore: 6, | ||
deleted: false, | ||
parentDeleted: false | ||
}, | ||
"8tu4bsun805n8un48ve89": { | ||
id: '8tu4bsun805n8un48ve89', | ||
parentId: "8xf0y6ziyjabvozdd253nd", | ||
timestamp: 1469479767190, | ||
body: 'Comments. Are. Cool.', | ||
author: 'thingone', | ||
voteScore: -5, | ||
deleted: false, | ||
parentDeleted: false | ||
} | ||
} | ||
|
||
function getData (token) { | ||
let data = db[token] | ||
if (data == null) { | ||
data = db[token] = clone(defaultData) | ||
} | ||
return data | ||
} | ||
|
||
function getByParent (token, parentId) { | ||
return new Promise((res) => { | ||
let comments = getData(token) | ||
let keys = Object.keys(comments) | ||
filtered_keys = keys.filter(key => comments[key].parentId === parentId && !comments[key].deleted) | ||
res(filtered_keys.map(key => comments[key])) | ||
}) | ||
} | ||
|
||
function get (token, id) { | ||
return new Promise((res) => { | ||
const comments = getData(token) | ||
res( | ||
comments[id].deleted || comments[id].parentDeleted | ||
? {} | ||
: comments[id] | ||
) | ||
}) | ||
} | ||
|
||
function add (token, comment) { | ||
return new Promise((res) => { | ||
let comments = getData(token) | ||
|
||
comments[comment.id] = { | ||
id: comment.id, | ||
timestamp: comment.timestamp, | ||
body: comment.body, | ||
author: comment.author, | ||
parentId: comment.parentId, | ||
voteScore: 1, | ||
deleted: false, | ||
parentDeleted: false | ||
} | ||
|
||
posts.incrementCommentCounter(token, comment.parentId, 1) | ||
res(comments[comment.id]) | ||
}) | ||
} | ||
|
||
function vote (token, id, option) { | ||
return new Promise((res) => { | ||
let comments = getData(token) | ||
comment = comments[id] | ||
switch(option) { | ||
case "upVote": | ||
comment.voteScore = comment.voteScore + 1 | ||
break | ||
case "downVote": | ||
comment.voteScore = comment.voteScore - 1 | ||
break | ||
default: | ||
console.log(`comments.vote received incorrect parameter: ${option}`) | ||
} | ||
res(comment) | ||
}) | ||
} | ||
|
||
function disableByParent (token, post) { | ||
return new Promise((res) => { | ||
let comments = getData(token) | ||
keys = Object.keys(comments) | ||
filtered_keys = keys.filter(key => comments[key].parentId === post.id) | ||
filtered_keys.forEach(key => comments[key].parentDeleted = true) | ||
res(post) | ||
}) | ||
} | ||
|
||
function disable (token, id) { | ||
return new Promise((res) => { | ||
let comments = getData(token) | ||
comments[id].deleted = true | ||
posts.incrementCommentCounter(token, comments[id].parentId, -1) | ||
res(comments[id]) | ||
}) | ||
} | ||
|
||
function edit (token, id, comment) { | ||
return new Promise((res) => { | ||
let comments = getData(token) | ||
for (prop in comment) { | ||
comments[id][prop] = comment[prop] | ||
} | ||
res(comments[id]) | ||
}) | ||
} | ||
|
||
module.exports = { | ||
get, | ||
getByParent, | ||
add, | ||
vote, | ||
disableByParent, | ||
disable, | ||
edit | ||
} |
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,2 @@ | ||
exports.port = process.env.PORT || 3001 | ||
exports.origin = process.env.ORIGIN || `http://localhost:${exports.port}` |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.