Database Individual Exercises
After completing the code-along activity, work on the exercises below.
Adding a Duration
One of the cool things about the current app setup is that it's easy to add additional fields. In addition to tracking the link and note for a video, track a new piece of data: dur (short for duration).
Form Update
First, update the form so the user can enter a duration when adding a movie.
- Open the index.ejs file
- Find the
<form></form>element - Within the
form, at the bottom, add a newinputelement - Set the
typeattribute of the newinputto be"text" - Set the
nameattribute of the newinputto be"dur" - Set the
placeholderattribute of the newinputto be"Video Duration"
List Update
Next, update the list item to display the duration in addition to the note.
- In the index.ejs file, in the
forloop, in theli, find the mainaelement - Remove all of the current content for the
aelement - In its place, create a new EJS block with
<%=and%> - Within the block, use a template literal to display the current video's note and duration
`${videos[i].note} (${videos[i].dur})`
Run the program, and verify that new videos have a duration!
The magic here is that it is only necessary to update the index.ejs file. That's because the data it adds to the database includes all the data from the form with parsedUrl.query. Any field added to the form with a name will be stored in the DB!
Updating Styles
While not always necessary from a functional perspective, updating the styles for a website can have a huge impact on the user experience. Add some CSS to make the page look and feel a little more interesting. Try to make the following updates:
- Change the font
- Change the background color
- Change the text color
- Make the inputs display on new lines (instead of side-by-side)
- Add a border around the form
- Add some padding and margins as needed
The Ability to Edit
So far, the Video Watchlist app has the ability to Create, Read, and Delete data, but one thing is missing from the full CRUD capabilities: Update!
Add in the ability for a user to edit an existing video in the list.
An Existing ID Form
Start by adding a new form to the index.ejs template. This form should be a lot like the "Add" form, with a couple of key differences. Make the following updates in the index.ejs file.
- Under the existing
</form>, create a new<form></form>element - Set the
actionattribute to"/edit" - Add a
pwithin the form saying "Edit Existing Video" - Under that, add an
input - Give the
inputatypeof"text", anameof"id", and aplaceholderof"Existing ID" - Copy the other
inputelements from the "Add" form into the "Edit" form, including thesubmit - In the
liwithin theforloop, add the videoidtext:<%= videos[i].id %>
Run the program, and verify that each video has an ID. The new form should also appear, but it won't do anything yet...
Handling the Form Request
The next step is to handle requests that come from form submissions. This will be very similar to the way /add requests are handled. Open the app.js file and follow the steps below.
- Under the
addVideofunction, define a new function namededitVideo - Make the function
async, and make it take inrequestandresponse - In the body of the function, create a new variable named
parsedUrl - Set
parsedUrltourl.parse(request.url, true)- This will contain the form data as query parameters
- Under that, create a new variable named
videoObj - Set
videoObjtoparsedUrl.query - Under that, create a new variable named
key - Set
keyto the"id"property ofvideoObj - Now, the
"id"onvideoObjis no longer necessary, so delete it:delete videoObj["id"]; - Under that, call
db.set - Pass in
keyfor the key andvideoObjfor the value - Use the
awaitkeyword beforedb.setto wait for the process to complete - Under that, call
response.redirectand go back home ("/") - Outside of the function definition, find the
app.getstatements - Use another
app.getto hook the"/edit"route to theeditVideofunction
Run the program, and verify that it is possible to edit existing videos by their ID!
Embedding YouTube Videos
Note: this challenge provides little guidance.
One cool thing about HTML is that it's pretty easy to embed YouTube videos! Add some HTML for each video in the list to put the video right on the page.
- Figure out how to extract the video ID from the link provided on the form
- Add the YouTube Video ID to the object sent to the homepage
- Add an
iframeon the homepage with asrcthat points to the embed URL
Something Completely Different - SQL
While SQL will not be specifically covered in the Web 201 course, it may be valuable to learn. Go to SQLBolt to begin. The site explains a bit about relational databases, and then walks through a tutorial of SQL, using interactive exercises for practice.