
# WDI-PANTHALASSA
---
Title: Mongoose Store
Type: Homework
Duration: Two days
Creator: Thom Page
Course: WDIr-Panthalassa
Competencies: Full CRUD in Express with Mongoose
---
# MONGOOSE STORE
Make a product inventory manager with full CRUD using Mongoose.
## APP
###Index page
Your app should have an index page where
- all the products are displayed
- the images link to the product's **show** page
- and there should be a link to add a new product.

###Show page
Your show page should display a product with
- a link back to the products
- a link to edit the product (goes to the edit page)
- a delete button that deletes
- and the number of items remaining in stock.
There will be a BUY button. The BUY button will reduce the number of items in stock by 1 each time it's pressed.


If the quantity of your item is zero, the show page should say 'OUT OF STOCK' instead of saying how many are remaining. (Hint: conditionals in `ejs`). On the edit page, make sure you can set the quantity to zero if you want so that you can test if this is working.
The BUY button should also not be rendered if the quantity of the item is zero.

###Edit page and New page
These views should render forms and submit to the appropriate routes.
###Redirects:
- The *create* route should redirect to the index
- The *delete* route should redirect to the index
- The *update* route will redirect back to the product's **show** page.
- For the **Primary Bonus** the BUY button will go to a route that redirects back to the product's **show** page
# DIRECTIONS
- Set up Express with MVC architecture with the appropriate folders for models, views, and controllers.
- You will need the seven RESTful routes to start. Test that they are accessible with Postman or cURL. Don't worry about what the BUY button does or where it goes just yet. Just set up your regular RESTful stuff.
- You will need to make a Mongoose Schema in a `products.js` file for your products. The schema should have:
```
name: String,
description: String,
img: String,
price: Number,
qty: Number
```
- Make sure you connect to your Mongo server in server.js
```
mongoose.connect('mongodb://localhost/mongoose_store');
```
- **If you have mongoose.connect in your file, Your Node server will crash if you aren't running `mongod`**
- Make sure your controller can access your model:
```
var Product = require('../models/products');
```
For testing purposes, especially for having quick access to those wacky Mongo ids, maybe think about having a route `/json` that `res.sends` an index of all the data in json format so that you can copy/paste ids into your url bar or cURL or what-have-you.
# Buy Button
After you have your full CRUD app working, it's time to break/extend RESTful conventions according to your own lights. The app needs a buy button. It's up to you to make your own routes to facilitate it.
If a product is in stock (the qty is above 0), the **show** page should have a `BUY` button. If the product is out of stock, it should not have this button.
When the `BUY` button is pressed, it will make a request to update the qty of the product (decrease it by 1).
- What route should the `BUY` request go to? Maybe it could go to its own route.
- Since it `updates` the product, should it go to a `PUT` route?
- Do you need to send any data through to the route? You will need the id, but that is likely all you'll need.
- Can you edit the qty value just in the route? `product.qty -= 1`?
- Will you have to `product.save()` if you do this?
# Commits
When you get to any of the following milestones, commit your work and include the number of your commit within the message.