Skip to content
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
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 45 additions & 65 deletions README.md
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
Copy link
Contributor

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.

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
Copy link
Contributor

Choose a reason for hiding this comment

The 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.
6 changes: 4 additions & 2 deletions models/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
const mongoose = require('mongoose');
require('dotenv').config();
// TODO: include all model files here (and export models together below)
const testModels = require('./test');
const pokemonModels = require('./pokemon');
const trainerModels = require('./trainer');

// connect to Mongo DB
mongoose.connection.openUri(process.env.MONGODB_URI || process.env.DB_CONN, {}, function(err, conn) {
Expand All @@ -14,5 +15,6 @@ mongoose.connection.openUri(process.env.MONGODB_URI || process.env.DB_CONN, {},

module.exports = {
// TODO: add references to all models here
Test: testModels.Test
Pokemon: pokemonModels.PokemonData,
Trainer: trainerModels.TrainerData,
};
27 changes: 27 additions & 0 deletions models/pokemon.js
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,
};
22 changes: 0 additions & 22 deletions models/test.js

This file was deleted.

24 changes: 24 additions & 0 deletions models/trainer.js
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,
};
Loading