Practice What You Learned

Intro

In this lab, you'll be deepening your understanding of Mongoose using a Flight model

Similar to what we did in the lesson, you'll start by creating a mongoose-flights project

FYI, your hw will expand upon the mongoose-flights project!




The final version of mongoose-flights will be a deliverable, so be sure you're pacing yourself so you don't fall behind.



Exercises

  1. Create a new folder for this lab called mongoose-flights and initialize a git repo inside of it
  2. Create a seperate remote for this repo on Github to push your commits for this assignment; you will need to send us a URL to your remote when you're ready to turn this assignment in for feedback
  3. Create your package.json and server.js
  4. Create all your basic folders to get started; just as we've done with every express projects ... i.e. models, routes, controllers, and views -- prepare to create additional files (modules) inside of these folders
  5. Create a config/database.js module inside your project that connects to a database named flights -- Be sure to require the module in server.js
  6. Read about Mongoose Built in Validators from the Mongoose Docs here
  7. Create a Flight Model with the following properties:


Property Type Validations Default Value
airline String enum to include 'American', 'Southwest' & 'United' n/a
flightNo Number Required
Between 10 and 9999
n/a
departs Date n/a One year from date created


  1. Implement the following User Stories ("As A User" == AAU):

    • AAU, I want to view a list of all flights (index view) that displays each flight's airline, flight no., and departure date/time
    • AAU, I want to create flights by entering the information on a page (new view) that has a form and submitting it


Hints:




Bonuses

  1. Display the default departure date when displaying the new flight form

    Hints:

  2. In the flight controller's new action, you could create an in-memory flight like this:
    const newFlight = new Flight();
    This in-memory flight doc would have the default departure date set properly based on the logic in the function you assigned to default.
  3. Just like any other data you want to access/display in a view template, that data needs to be passed by the controller action when it calls res.render, however…
  4. Although an input of type="datetime-local" will display a date assigned to its value attribute, that date value needs to be formatted as a string matching this format: yyyy-MM-ddThh:mm (yes, a “T” character is used to separate the date portion from the time portion). One way of obtaining the properly formatted string is to use the toISOString() method and use slice() to return only the first 16 characters, for example:


	const newFlight = new Flight();
	// Obtain the default date
	const dt = newFlight.departs;
	// Format the date for the value attribute of the input
	const departsDate = dt.toISOString().slice(0, 16);
	res.render('flights/new', {departsDate});


  1. Code these additional User Stories:

    • AAU, I want to be able to access each view via a navigation bar at the top of the page with links to:

      • ALL FLIGHTS, and
      • ADD FLIGHT
    • AAU, I want to view the list of flights by departure date in ascending order.
    • AAU, I want the flights in the list to be displayed using red text if the flight's departure date has passed.
  2. Style the index and new views.

Copyright © Per Scholas 2023