-
Notifications
You must be signed in to change notification settings - Fork 15
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
James Tang Custom API homework w/ README.md #12
Open
TangJames
wants to merge
9
commits into
wdi-atx-12:master
Choose a base branch
from
TangJames: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
9 commits
Select commit
Hold shift + click to select a range
820ee6d
ready for heroku deploy attempt #1
f02d9f5
heroku deploy attempt #2
50fb480
ver2
05ec4a0
ver4 THIS ONE WORKS
19385b0
ver 5
44c23a4
heroku push
da2568e
ver 10 WORKING
7ac0d62
working copy final ver 30
37ad90a
readme final
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 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,85 +1,65 @@ | ||
# <img src="https://cloud.githubusercontent.com/assets/7833470/10423298/ea833a68-7079-11e5-84f8-0a925ab96893.png" width="60"> Personal API - Weekend Lab | ||
TABLE OF CONTENTS | ||
1. DESCRIPTION OF THE API | ||
2. URL TO THE API | ||
3. METHODS AND ENDPOINTS OF THE API | ||
4. REQUIREMENTS AND BASIC STRUCTURE FOR THE API | ||
|
||
It's time to have some fun and play with the technologies you've learned in the past week. Your goal is to start your own API. | ||
|
||
Your API can be related to whatever you like, but its Mongo DB must have at least 2 schemas/models that are connected by reference. | ||
|
||
For example, if your API is for keeping track of various hotels, it would need to reference another type of object besides hotels. You might choose to provide more information about the cities your hotels are found in beyond a simple string like "Austin" or "Orlando". Your `City` schema/model could contain information about the city iself like city name, state, local attractions, and of course a list of related hotels connected by reference. | ||
|
||
### Your API should have: | ||
|
||
* Well-organized **JSON API** Endpoints | ||
* The ability to **read** (`GET`) each type of resource **as a collection** | ||
* The ability to **read** (`GET`) each type of resource **by specific ID** | ||
* The ability to **create** (`POST`) a new resource of each type | ||
* At least one **update** (`PUT`) endpoint | ||
* At least one **delete** (`DELETE`) endpoint | ||
|
||
Try to start by completing the schema/model and implementing endpoints for only one type of resource and expand from there to include other schemas/models/endpoints. | ||
|
||
Fork and clone this repo. You can use the placeholder code in this repo as a starting point. | ||
|
||
## Step 0: Deploy to Heroku | ||
|
||
Before we start coding, let's prepare to deploy our API on Heroku so other developers can access our API from any computer. | ||
|
||
You can find full instructions here: [Deploying Express Apps to Heroku](https://github.com/SF-WDI-LABS/shared_modules/blob/master/how-to/heroku-mean-stack-deploy.md) | ||
--------------------------------------------------------------------------------------------------- | ||
1. Description of what your API does. | ||
This API is a built datebase to track Pokemon trainers and their respective Pokemon. | ||
Example: Ash Ketchum -- Pikachu. | ||
|
||
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!): | ||
2. The URL to the database is https://peaceful-sands-51099.herokuapp.com/ | ||
|
||
```bash | ||
# git add changed-files | ||
# git commit -m "detailed description of what I changed" | ||
git push heroku master | ||
heroku open | ||
``` | ||
3. Maps out the API endpoints (method + path combos) | ||
The API end points are '/trainer', '/pokemon', '/trainer:id/', '/pokemon:id' | ||
|
||
It's common for code to break "in production" even when it works fine "in development" on your computer (due to different environment variables, missing dependenies, etc). Do your best to debug! Let us know if you get stuck on deployment issues. | ||
In the '/trainer' endpoint is where all the trainer data is stored. Name of the trainer, location of where the trainer is from and what Pokemon the Trainer has. | ||
In the '/trainer:id' endpoint is where you can find the specific trainer through their unique ID. | ||
In the '/pokemon' endpoint is where all the information on the Pokemon is stored. Name of the Pokemon, type, number in the Pokedex, ability, height, weight, and all base stats. | ||
In the '/pokemon:id' endpoint you can find all information on a specific Pokemon based off their unique ID. | ||
|
||
## Step 1: Verify the Test Routes in Postman | ||
The expected responses for these endpoints is when you use any CRUD operation the data is able to be manipulated in the database. You can add onto the existing data in the database, edit the data, make new data, or even delete all the data. The way you want to use the database is up to the user. | ||
|
||
Since we're building a JSON API with no front-end (yet), Postman will come in handy for testing our work. | ||
4. The basic properties of the schema model are as follows. | ||
PokemonSchema | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Put this into a code block using backticks. PokemonSchema {
name: String,
// etc...
} |
||
name: String, | ||
pokedexNumber: String, | ||
pokemonType: String, | ||
specialAbility: String, | ||
height: String, | ||
weight: String | ||
|
||
Make sure you've **installed dependencies** and added a `.env` file with your **Mongo DB credentials**. Then **run the server** with the placeholder code. | ||
baseStats: { | ||
hp: Number, | ||
attack: Number, | ||
defense: Number, | ||
specialAttack: Number, | ||
specialDefense: Number, | ||
speed: Number | ||
|
||
Use Postman to verify that the `GET /test` and `POST /test` endpoints work, confirming that our server can connect to our Mongo DB and complete operations on demand. For the `POST` endpoint, you can simply submit an object with a `name` property, and then it should appear in your next `GET /test` request: | ||
And for Trainers | ||
name: { | ||
type: String, | ||
unique: true, | ||
required: true | ||
|
||
```json | ||
{ | ||
"name": "test item 1" | ||
} | ||
``` | ||
location: { | ||
type: String, | ||
unique: true, | ||
required: true | ||
|
||
## Step 2: Build Your Main Schema/Model | ||
pokemon: [{ | ||
type: mongoose.Schema.Types.ObjectId, | ||
ref: 'Pokemon' | ||
|
||
Next, go to the `models/` folder and add a new file for your main schema/model. For now we'll create it without a reference to any other schemas/models. | ||
|
||
Be sure to export the model from the file and then modify `models/index.js`. It should require the file you just created and then export the models from it so they can be used more directly while preparing responses for our routes. | ||
|
||
## Step 3: Create Your Primary Endpoints | ||
|
||
Now go to the `routes/` folder and create a new file to handle your endpoints for the primary model. It should contain function definitions for handling various endpoints (`GET`, `POST`, etc) related to your primary model. | ||
|
||
Start with the `GET` method for retrieving all matching resources. Then add a function for adding new resources (`POST`). Then add a function for retireving a specific resource by ID (`GET`, with path parameter). | ||
|
||
Make sure that for each function, you modify `server.js` to register the functions as endpoint/route handlers. | ||
|
||
## Step 4: Check Your Work in Postman | ||
|
||
Open up Postman with your new code running on `localhost`. Hit your new `GET` and `POST` endpoints a few times to verify everything is working as expected. | ||
|
||
If it's all working, add/commit your changes to git. Push to Github to backup your code. Push to Heroku so other people can use your newly created endpoints. | ||
|
||
Return to Postman and test again, making sure that your code is working "in production" on the URL Heroku provided for your API server. | ||
|
||
## Step 5: Add Your Other Schemas/Models/Endpoints | ||
|
||
Now it's time to expand on what you've done. Piece by piece, add the other schemas/models and endpoints you planned. | ||
|
||
Be sure to test your endpoints as you go in Postman. Also don't forget to commit your changes and deploy to Heroku from time to time. | ||
|
||
## Options for Extra Challenge | ||
|
||
- **Add even more schemas/models/enpoints.** Thinking of more detailed information is usually fairly easy, but the code to support it gets more difficult the more detail is added. | ||
- **Add support for query parameters** to limit/filter responses on your endpoints that retrieve all resources in a collection. For example, you might limit items based on rating, time/date range, tags, etc. | ||
- **Document all the endpoints of your API** in a markdown file. What does each endpoint do? How should a new user of your API get started? | ||
I have left the following in the examples for you because those are the requirements to make this database. Inside the folder there will also be dependencies in the package.json folder. Do not forget to download those. |
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
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,27 @@ | ||
const mongoose = require('mongoose'); | ||
|
||
const pokemonSchema = new mongoose.Schema({ | ||
pokemonData: { | ||
name: String, | ||
pokedexNumber: String, | ||
pokemonType: String, | ||
specialAbility: String, | ||
height: String, | ||
weight: String | ||
}, | ||
baseStats: { | ||
hp: Number, | ||
attack: Number, | ||
defense: Number, | ||
specialAttack: Number, | ||
specialDefense: Number, | ||
speed: Number | ||
}, | ||
}); | ||
|
||
|
||
const PokemonData = mongoose.model('PokemonData', pokemonSchema); | ||
|
||
module.exports = { | ||
PokemonData: PokemonData, | ||
}; |
This file was deleted.
Oops, something went wrong.
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,24 @@ | ||
const mongoose = require('mongoose'); | ||
|
||
const trainerSchema = new mongoose.Schema({ | ||
name: { | ||
type: String, | ||
unique: true, | ||
required: true | ||
}, | ||
location: { | ||
type: String, | ||
unique: true, | ||
required: true | ||
}, | ||
pokemon: [{ | ||
type: mongoose.Schema.Types.ObjectId, | ||
ref: 'Pokemon' | ||
}], | ||
}); | ||
|
||
const TrainerData = mongoose.model('TrainerData', trainerSchema); | ||
|
||
module.exports = { | ||
TrainerData: TrainerData, | ||
}; |
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 want to add
#
to make these headings instead of plain text.