/swe-task-2

potential lamp semiautomatic luminous soap

Primary LanguageJavaScript

Programming Assignment 2 - Express with Knex

Get & setup the repository(assignment)

First clone the repository to your station
git clone https://github.com/PVNETEdu/Software-Engineer---Program-Assignment-2.git

Be sure to add the base repository as one of your remote since I might update README
git remote add projectDescription https://github.com/PVNETEdu/Software-Engineer---Program-Assignment-2.git

To keep updated to of the assignment, update your project
git pull projectDescription master

Create a new branch with your name and start working
git checkout -b <your name>

Check if you are on your branch
git branch

First we need to install all of the npm dependencies run:
npm install

To check everything worked well, go to localhost:3000 to check if you see the web page:
cd bin

  1. run: node www to run the server file
  2. put: localhost:3000 in the browser and check if you see web app works.

Overview

For this assignment, we will try to take the cleaness and the structure of our software to a different level by using knex which provides a more organized style of code and much more efficient backend query rendering due to functional programming.

Table of Contents

Files to complete
Part One: Get Started
Part Two: Configure Mysql
Part Three: finish writing itemModel.js file
Part Four: finish writing userModel.js file
Part Five: Finish four router functions
Part Six: Finish the signupWithItem page
Part Seven: Commit and Submit

Files to complete:

knexfile.js - The express app setup
index.js - The index router with four different routes
userModel.js - The folder that contains your front-end engine there are four files in total needed

Part One: Get Started

You should import it to your IDE of choice WebStorm recommended.

By default, your project should have no errors and contain the following root items:

bin - The folder that includes the starter code on starting server with the port number
database - The folder that includes the knex migrations and the seeds.
models - The folder that all the models which are the database functions.
node_modules - The library for all of the npm packages.
public - The public folder where the page elements like stylesheet, in page javascripts, and images can go to
routes - The folders that have the index routes and user routes that we can use
views - The folder that contains your front-end engine
app.js - The express app setup file
knexfile.js - The knex configuration file.
package.json - This is all the dependencies that NPM will look into when you install or delete packages
package-lock.json - Describes the exact tree that was generated by NPM

Please seek help from me or any course staffs if you are missing any of these files

Part Two: Configure Mysql

Look into knexfile.js file the mysql connection was already done for you! All you have to do is to fill in the username of your localhost mysql account, the password, and the database your tables are stored in.

The database you will be using this time is SWE2 same host, and username as before.

After you have configured the mysql connection, run the following commands: knex migrate:latest
knex seed:run

Go to your database select your database in datagrip by runing: USE SWE2;

and run the show tables command to check if the users and items table are present.

Part Three: finish writing itemModel.js file

Before starting with this task, check the links to learn more about knex:

There are two functions in that javascript file

/*
 * This function should look inside the users table and look for the rows
 * that has the parameter id. return the result row with this function.
 */
async function findItems(id) {
    //TODO start your code right here
}

/*
 * This function should look inside the users table and the items table
 * make a join query that will return you the users json with the item
 * json for the JSON format look in the README direction
 */
async function insertItems(item) {
    //TODO start your code right here
}

The first function takes a parameter id which is the primary key used in the items table. The findItems function should return the row in the items where the id matches items.id make sure to use the db object configured on the top of the file, this will allow you to use functional queries provided by knex.
The second function takes a JSON of item where in this function, you will insert that item json into the table items.

Part Four: finish writing userModel.js file

Now this particular part of the program you will complete two different functions in that file.

/*
 * This function should look inside the users table and look for the rows
 * that has the parameter id. return the result row with this function.
 */
async function findUsers(id) {
    //TODO start your code right here
}

/*
 * This function should look inside the users table and the items table
 * make a join query that will return you the users json with the item
 * json for the JSON format look in the README direction
 */
async function findUsersWithItems(id) {
    //TODO start your code right here
}

/*
 * This function should insert the user with a user parameter
 */
async function insertUser(user) {
    //TODO start your code right here
}

/*
 * This function should insert the parameter into the tables
 * this is trickier than you think the parameter is a JSON object
 * but the way it is build is this JSON also has an array of JSON
 * for items as one of its keys.
 */
async function insertUsersWithItems(userWithItems) {
    //TODO start your code right here
}

Complete the findUser function and insertUser function first
when you are working on the findUsersWithItems function, the function should return a JSON file with the following structure:

{
  email:"someEmail"
  password:"somePassword"
  items:[
    {
       name:"Coke",
       quantity:2,
     },
     {
       name:"Rice",
       quantity:3,
     }
  ]
}

Note: remember the items array should be formed with the foreign key that pointed to the user table which is the parameter in this case. Accomplish this by making the join query first, then reformat the JSON returned to you.

Now you can start working on insertUsersWithItems the parameter JSON you take looks the same as the one that you just worked on in the findUsersWithItems function. Now you are just going to reverse this process. First insert the user object, and then insert the items array. Remember the knex insert function is allowed to insert multiple items. Very lucky!

Part Five: Finish four router functions

There are three different post requests that you will need to implement, the rest was already done for you. It is highly recommended that you go ahead and dig into the views folder and read around all the files and understand how the structure was built for this app. Look at the forms and how they interact with the API routes.

router.post('/insertUser', async function(req, res){
  //TODO your code goes here
});

router.post('/insertUsersWithItems', async function(req, res){
  //TODO your code goes here
});

router.post('/getUserWithItems', async function(req, res){
  //TODO your code goes here
});

These functions should be very easy to implement where you already did all the hardcore backend works in the model folder. Therefore go ahead and import these modules and utilize these functions to complete the above post requests.

Part Six: Finish the signupWithItem page

There is just one more in page javascript function for you to implement, where you need to complete this function for user to add more items to the form.

function addElements(){
    //TODO your code goes here

}

Part Seven: Commit and Submit

Once you have finished all the works and ready to submit your assignment, make a new pull request for merging into master and @ThomasLIARDJAVA me as a reviewee so I will know that your code is ready to be reviewed.

Grade: /100