Today we're going to make our first full CRUD back-end.
I'll be referring to this mini project through these instructions : https://github.com/DevMountain/mini-sql-node-massive
Get your basic server working. Follow the steps from the mini-project to setup a server :
- Require and setup express and get it listening on a port
- Require MassiveJS and Connect to a database (make a new one, or use one you've already made).
- Add db to our express app, put the massive instance on there and export our app (see the mini-project)
We are going to be working with a single table, products. It's schema will look something like this:
- ID : Primary key number
- Name : string
- Description: string
- Price: number
- Imageurl : string
-
Create a folder called db and place 5 files in there:
- create_product
- read_products
- read_product
- update_product
- delete_product
-
Create a working SQL query for each one. You can use pgAdmin to test your queries against the database.
create_product
This query will need to take the 4 parameters defined in the schema and insert a record into the database.
read_products
This query will get all products in the table and return only the name, price, and image url
read_product
This query will take in an id and return all data for that product
update_product
This query will take in an id and a new description. Find the product with the id and update it's description with the new description.
delete_product
This query will take in an id. Find and delete the product with the id.
- Create a productsCtrl.js
- Export an object with 5 functions
- Create, GetOne, GetAll, Update, Delete
- At the top of the controller (outside the object), get the db object off of our express app (see mini-project)
- Inside of Create use the create_product query
- Inside of GetAll use the read_products query
- Inside of GetOne, use the read_product query
- Inside of Update, use the update_product query
- Inside of Delete, use the delete_product query
- Export an object with 5 functions
- Create some endpoints on express. Create one for each query type.
Sample Urls
GET /api/products
GET /api/product/:productId
PUT /api/product/:productId?desc=....
POST /api/product
DELETE /api/product/:productId
- Get any information you need off of the query parameters, query, or body and pass them into your controller
- You will need to add parameters in your controller functions to receive these values.
- You should have a working crud app. Use postman to insert a few records, modify them, query them, and delete them to make sure it's all working.
- Create an angular front end to interact with your app.
- Use express static to serve up your angular files from a public folder
- Create a single view that can insert, read, update, and delete products.
- Create a 2nd page that just reads the products and displays them in a more pretty way (like Jane.com or amazon).
© DevMountain LLC, 2016. Unauthorized use and/or duplication of this material without express and written permission from DevMountain, LLC is strictly prohibited. Excerpts and links may be used, provided that full and clear credit is given to DevMountain with appropriate and specific direction to the original content.