forked from sf-wdi-25/express_self_api
-
Notifications
You must be signed in to change notification settings - Fork 54
Chen's work on personal API #27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
achentha
wants to merge
15
commits into
SF-WDI-LABS:master
Choose a base branch
from
achentha:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
3d01ebf
Basic HTML and CSS for look and feel of the page
achentha 1650f01
GET /api/birds and POST /api/birds now working
achentha 275fc60
Update & Delete bird are now working
achentha b1d4b46
Modified seed & server. Also style the page.
achentha 9c1f077
Updating profile, README.md, adding more birds into the seed, also up…
achentha 68510b9
renamed a photo
achentha 6559e6a
change a photo
achentha e3e7678
updated seed file
achentha 125a3fe
modified seed.js
achentha 7deff69
added process.exit() call in seed.js so it just does not hang there
achentha 0eca6e4
corrections after Michelle's code review comments
achentha e08d7fc
updated README.md for more information of the app
achentha de9d164
README.md syntax correction
achentha 510637e
yet another syntax correction
achentha 72cc22d
hope this is the last of the syntax fix
achentha File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
| @@ -1 +1,4 @@ | ||
| **/node_modules | ||
| .DS_Store | ||
| public/.DS_Store | ||
| package-lock.json |
This file contains hidden or 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 |
|---|---|---|
| @@ -1,151 +1,22 @@ | ||
| # <img src="https://cloud.githubusercontent.com/assets/7833470/10423298/ea833a68-7079-11e5-84f8-0a925ab96893.png" width="60"> Personal API | ||
| # <img src="https://cloud.githubusercontent.com/assets/7833470/10423298/ea833a68-7079-11e5-84f8-0a925ab96893.png" width="60"> Bird API | ||
|
|
||
| It's time to have some fun and play with the technologies you've learned in the past week. Your goal is to build a API about yourself. Your API will incorporate: | ||
| * Well-documented **JSON API** Endpoints | ||
| * A full set of **REST-ful Routes** (GET, POST, UPDATE, DELETE) | ||
| * At least one **CRUD-able resource** (Create, Read, Update, Destroy) | ||
| * and an `/api/profile` endpoint with some basic **details about you** | ||
| Welcome to the 'My Bird API'. You can use it to create and maintain your bird informations like: | ||
| * bird name | ||
| * bird type | ||
| * comments you want to add about the bird | ||
| * two images you can add for each bird | ||
| * a url link to learn more about this particular bird | ||
|
|
||
| Finally, you will **consume your API** using AJAX and **render the results** to the page using jQuery. You must complete *both* of these portions of the assignment. | ||
| ## Technologies Used | ||
| * bootstrap for styling the HTML page | ||
| * ajax to send asynchronous HTTP request to server | ||
| * express and body-parser to provide the access to the server services | ||
| * Mongo DB and mongoose are used for noSQL DB | ||
|
|
||
| Please fork & clone this repo to get started. | ||
| See more API endpoint information [here](https://dry-scrubland-21249.herokuapp.com/api) | ||
|
|
||
| ## Part 0. Deploy to Heroku | ||
| Before we start coding, our first goal together is to configure our application so that it can be deployed to Heroku (a web application host). | ||
| Some information about myself [Apichai](https://dry-scrubland-21249.herokuapp.com/api/profile) | ||
|
|
||
| Follow the instructions here: [Deploying Express Apps to Heroku](https://github.com/SF-WDI-LABS/shared_modules/blob/master/how-to/heroku-mean-stack-deploy.md) | ||
|
|
||
| As you continue to work on this project, you'll need to remember to push your changes to heroku (just like you would with github!): | ||
|
|
||
| ```bash | ||
| # git add . -A | ||
| # git commit -m "detailed description of what I changed" | ||
| git push heroku master | ||
| heroku open | ||
| ``` | ||
|
|
||
| It's common for code to break "in production" (broken links, different environment, missing dependenies...), even if it worked in development, so do your best to debug! Let us know if you get stuck. | ||
|
|
||
| ## Part 1. Personal API | ||
| Now that we're deployed, it's time to start coding your "personal" api! | ||
|
|
||
| #### Minimum Requirements | ||
|
|
||
| - **Documented API Endpoints** | ||
|
|
||
| - You must document your API endpoints. We really want to know *how* to use your API! And for starters, we need to know what endpoints exist! (Do this step first! _Plan plan plan!_) | ||
| - One cool way to do this is to create an endpoint at `/api` that describes all the available endpoints. We've set you up with an example in `server.js`. Make sure to update it to fill it in with your own information! | ||
| + Here's a good example student `/api` endpoint: | ||
| <img width="500" alt="example api documentation" src="https://cloud.githubusercontent.com/assets/1489337/22841538/dc0b7f26-ef86-11e6-9a56-013bbe51792a.png"> | ||
|
|
||
| + See the [Open API Initiative](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#paths-object-example) for what this looks like in practice. | ||
| - **A Profile Endpoint** (`/api/profile`) that responds with *hard-coded* data: | ||
|
|
||
| + `name` - a string | ||
| + `githubUsername` - a string | ||
| + `githubLink` - a url to your github profile | ||
| + `githubProfileImage` - the url of your github profile image | ||
| + `personalSiteLink` - a link to your personal site. | ||
| + `currentCity` | ||
| + `pets` - an array of your pets | ||
| + e.g. `[{name: "foo", type: "Cat", breed: "Siamese"}, {name: "bar", type: "Dog", breed: "Dalmation"}]` | ||
| + if you do not have any pets, please get creative, or use hobbies instead | ||
| + Please hardcode it! It would be seriously overkill to save a single profile to the database. | ||
| - **At least one resource (mongoose model)** that you can _*CRUD*_ using _*RESTful Routes*_ | ||
| - That means endpoints for `index`, `show`, `create` `update`, `delete`! | ||
| - Here are some ideas: | ||
| * **RECOMMENDED:** `projects` that you have built for this class | ||
| - `_id`, name, description, githubRepoUrl, deployedUrl, screenshot | ||
| * `places` that you've lived or that are important to you | ||
| - `_id`, description, town, state, country, years, gps: {lat, lon}, photo | ||
| * `destinations` you've visited, or `vacations` you're planning | ||
| - `_id`, country, date, duration, photo | ||
| * `books` you've read or love | ||
| - `_id`, title, author, image, releaseDate, characters | ||
| * `movies` or `shows` you like | ||
| - `_id`, title, season, director | ||
| * `portfolioProjects` or `lyrics` you've written | ||
| - `_id`, title, body, date | ||
| * Wish list (e.g. `gifts` or `wishes`) | ||
| - `_id`, description, price, amazonLink | ||
|
|
||
| All API Endpoints must return JSON. | ||
|
|
||
| <!--- You will be able to see if your API is up and running because [this api aggregator](https://api-aggregator.herokuapp.com/) will be able to consume your data. Check back on it to see if it's working for your code! Note: This won't be set up until you send your instructors the name of your Heroku app! ---> | ||
|
|
||
| > **Pro-Tip**: One good strategy is to add the database *last*. Start with your api routes and some hard-coded data. Make sure it's working the way you want before tackling the database layer! | ||
|
|
||
| #### API Stretch Goals | ||
| * Profile info stretch goals | ||
| * Add a `daysOld` field that calculates how many days old you are. | ||
| * Add an `isAwake` field that's only `true` between 8am and 10pm! | ||
| * CRUD resource stretch goals | ||
| * Use query parameters to filter results from one of your CRUD endpoints: | ||
| - e.g. `?limit=2` only return two results | ||
| * Create a `/search` endpoint | ||
| - e.g. `?q=mad+men` only returns tv shows with that in the title | ||
| - e.g. `?type=romance` only returns romance novels | ||
|
|
||
| #### Examples | ||
| An example API for 'Jon Snow' might have endpoints like: | ||
|
|
||
| JSON API Endpoint Response JSON | ||
| ============= ============= | ||
| GET /api/profile { | ||
| name: "Jon Snow", | ||
| githubLink: "http://github.com/u-know-nothing-jon-snow", | ||
| currentCity: "The Wall", | ||
| isAwake: false, | ||
| familyMembers: [ | ||
| { name: 'Arya Stark', relationship: 'sister' }, | ||
| { name: 'Bran Stark', relationship: 'brother' } | ||
| ] | ||
| } | ||
|
|
||
| GET /api/projects [ | ||
| { | ||
| \_id: 2, | ||
| name: 'Defeat the wildlings', | ||
| type: 'quest', | ||
| opponents: [ 'Mance Rayder', 'Lord of Bones'], | ||
| status: 'resolved' | ||
| }, | ||
| { | ||
| \_id: 3, | ||
| name: 'Save the wildlings', | ||
| type: 'campaign', | ||
| opponents: ['the Night Watch', 'the Others'], | ||
| status: 'pending' | ||
| } | ||
| ] | ||
|
|
||
| GET /api/projects?limit=1 [ { \_id: 2, name:'Defeat...' } ] | ||
|
|
||
| GET /api/projects?status=pending | ||
| [ { \_id: 3, name:'Save...' } ] | ||
| GET /api/projects/2 { \_id: 2, name:'Defeat...' } | ||
|
|
||
| POST /api/projects etc | ||
| PUT /api/projects/2 etc | ||
| DELETE /api/projects/2 etc | ||
|
|
||
| Make sure to spend time planning this part out! | ||
|
|
||
| ## Part 2. Personal Dashboard | ||
|
|
||
| #### Minimum Requirements | ||
| Consume the Personal API you just created, and use it to build your own personal dashboard. | ||
|
|
||
| * Create an `index.html` **homepage** that's linked to your main JavaScript and CSS files. | ||
| * Use **jQuery** and **AJAX** to consume your Personal API. | ||
|
|
||
| * Use **Template Strings** and **Array Iteration** to render data to the page. | ||
|
|
||
| * Display **at least one image/gif** that you retrieved from your Personal API. | ||
| * Create **at least one form**, so you can CRUD at least one of your resources. | ||
| * Get rid of that ugly blue background. Style it up! | ||
|
|
||
| <br> | ||
| <br> | ||
|
|
||
| <img src="https://media.giphy.com/media/mWUuD8qPSi5B6/giphy.gif" width="400"> | ||
| ## History | ||
| 09/10/2017 - address Michelle's code review comments | ||
| 09/01/2017 - initial creation |
This file contains hidden or 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,79 @@ | ||
| //DB | ||
| const db = require("../models"); | ||
|
|
||
| //GET /api/birds -- get all birds | ||
| function index(req, res) { | ||
| db.Bird.find({}, function(err, allBirds) { | ||
| if (err) { | ||
| console.log("Failed /api/birds "); | ||
| return; | ||
| } | ||
| res.json(allBirds); | ||
| }) | ||
| } | ||
|
|
||
| //POST /api/birds -- adding a bird | ||
| function create(req, res) { | ||
| db.Bird.create(req.body, function(err, bird) { | ||
| console.log('entering db.Bird.create()'); | ||
| if (err) { | ||
| console.log(`create bird failed: err =${err}`); | ||
| return; | ||
| } | ||
| console.log("create bird success"); | ||
| res.json(bird); | ||
| }); | ||
| } | ||
|
|
||
| function update(req, res) { | ||
| db.Bird.findById(req.params.birdId, function(err, bird){ | ||
| if (err) { | ||
| console.log(`Did not find bird id: ${req.params.birdId} in db`); | ||
| return; | ||
| } | ||
|
|
||
| bird.name = req.body.name; | ||
| bird.type = req.body.type; | ||
| bird.comments = req.body.comments; | ||
|
|
||
| bird.save(function(err, bird) { | ||
| if (err) { | ||
| console.log(`Failed to update bird id: ${bird._id} in db`); | ||
| return; | ||
| } | ||
|
|
||
| res.json(bird); | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| function destroy(req, res) { | ||
| db.Bird.findByIdAndRemove(req.params.birdId, function(err, bird) { | ||
| if (err) { | ||
| console.log(`Failed to delete bird id ${req.params.birdId}`); | ||
| return; | ||
| } | ||
|
|
||
| res.json(bird); | ||
| }); | ||
| } | ||
|
|
||
| function show(req, res) { | ||
| db.Bird.findById(req.params.birdId, function(err, bird) { | ||
| if (err) { | ||
| console.log(`Cannot find bird id ${req.params.birdId} in db`); | ||
| return; | ||
| } | ||
|
|
||
| res.json(bird); | ||
| }); | ||
| } | ||
|
|
||
| //public methods | ||
| module.exports = { | ||
| index: index, | ||
| create: create, | ||
| update: update, | ||
| destroy: destroy, | ||
| show: show, | ||
| } | ||
This file contains hidden or 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,3 @@ | ||
| module.exports = { | ||
| birds: require("./birdsController"), | ||
| } |
This file contains hidden or 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,27 @@ | ||
| // var mongoose = require('mongoose'), | ||
| // Schema = mongoose.Schema; | ||
|
|
||
| // var CampsiteSchema = new Schema({ | ||
| // description: String | ||
| // }); | ||
|
|
||
| // var Campsite = mongoose.model('Campsite', CampsiteSchema); | ||
|
|
||
| // module.exports = Campsite; | ||
|
|
||
| const mongoose = require("mongoose"); | ||
| const Schema = mongoose.Schema; | ||
|
|
||
| let BirdSchema = new Schema({ | ||
| name: String, | ||
| type: String, | ||
| comments: String, | ||
| urlName: String, | ||
| url: String, | ||
| photo1: String, | ||
| photo2: String, | ||
| }); | ||
|
|
||
| let Bird = mongoose.model('Bird', BirdSchema); | ||
|
|
||
| module.exports = Bird; |
This file contains hidden or 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should probably send a response here, not just return from the function; as is, your app.js file would be waiting forever, for a response that would never come, if there were an error. Instead, you could send a 404.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And this goes for all of your error-handling: you should send a response, not just console.log.