Skip to content

Latest commit

 

History

History
140 lines (101 loc) · 5.43 KB

lesson-plan.md

File metadata and controls

140 lines (101 loc) · 5.43 KB

Lesson plan

> Focus on having lots of in class exercises.

> DONT teach everything, let the students investigate topics on their own aswell!

> Focus on how to read documentation, google answers and google errors!!

> Teach towards the students being able to solve the homework

Remember to add the code you wrote in the class to the relevant class branch's class work folder. If the branch has not been created just create and push it :) If you dont have access, write to one from the core team. You can see an example below!

To find examples of what teachers have taught before go to the class branches in the classwork folder, Fx class 07

If you find anything that could be improved then please create a pull request! We welcome changes, so please get involved if you have any ideas!!!


Code inspiration

Executing queries

const knex = require("../database");

router.get("/", async (request, response) => {
  try {
    const titles = await knex("meals").select("title");
    console.log(titles);
  } catch (error) {
    throw error;
  }
});

Phonebook database

Create a phonebook database with contacts:

CREATE TABLE `phonebook`.`contacts` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `name` VARCHAR(45) NULL,
  `phonenumber` VARCHAR(45) NULL,
  PRIMARY KEY (`id`));
// import knex
const knex = require("../database");

// Select using knex
try {
  const contacts = await knex("contacts").select("*").where({ id: 1 });
} catch (error) {
  throw error;
}


// insert using knex
const contact = {
  name: "benjamin",
  phonenumber: "12345678"
};

await knex("contacts").insert(contact);

Phonebook api

Create the following api

Url Verb Functionality Example
api/contacts/ GET Returns all contacts GET api/contacts/
api/contacts/ POST Adds a new contact POST api/contacts/
api/contacts/{id} GET Returns contact by id GET api/contacts/2
api/contacts/{id} PUT Updates the contact by id PUT api/contacts/2
api/contacts/{id} DELETE Deletes the contact by id DELETE contacts/2

Exercises

Concerts api

Lets create an api for concerts

Connecting the database

Create a concerts table in mysql. It should have these columns: title, band, venue, createdDate, performanceDate and price

Using node and the knex lib:

  • INSERT three new concerts
  • GET all concerts
  • DELETE a specific concert

Routing

Create the following routes

Url Verb Functionality Example
api/concerts/ GET Returns all concerts GET api/concerts/
api/concerts/ POST Adds a new concert POST api/concerts/
api/concerts/{id} GET Returns concert by id GET api/concerts/2
api/concerts/{id} PUT Updates the concert by id PUT api/concerts/2
api/concerts/{id} DELETE Deletes the concert by id DELETE concerts/2

Query parameters

Parameter Description Data type Example
maxPrice Get concerts that has a price smaller than maxPrice Number /concerts?maxPrice=160
title Get concerts that partially match a title. Metallic will match the concert with the title Metallica in Parken String api/concerts?title=metallic
createdAfter Get concerts that has been created after the date Date api/concerts?createdAfter=2019-04-05
band Get concerts with a specific band String api/concerts?band=metallica

Use the api using postman

Using post man insert some concerts, get some concerts using query parameters, delete some concerts and update some concerts