Querying Recently Released and Coming Soon Movies
In this lesson, we'll learn how to use the Model Query Builder to query our movies that have been recently released. We'll then do a separate query to get movies that are coming soon.
- Author
- Tom Gobich
- Published
- Mar 15
- Duration
- 4m 59s
Developer, dog lover, and burrito eater. Currently teaching AdonisJS, a fully featured NodeJS framework, and running Adocasts where I post new lessons weekly. Professionally, I work with JavaScript, .Net C#, and SQL Server.
Adocasts
Burlington, KY
Transcript
Querying Recently Released and Coming Soon Movies
-
(upbeat music)
-
At this point, we have over 400 movies on our database
-
and we're currently just displaying them
-
all on our homepage, which leads us to a massive list
-
of movies on our homepage.
-
What would be better is instead,
-
we just display the recently released
-
or maybe those coming most soon on the homepage
-
instead of listing every single movie.
-
So how do we do that?
-
Well, let's hide our browser away
-
and let's jump up in into our movies controller.
-
And we're gonna be working within our index method here.
-
And this is what's rendering out our homepage.
-
Movie.all is going to query every single movie
-
that we have inside of our movies table,
-
which is not quite what we want.
-
Instead of that, let's do const recently released
-
equals, oh wait, movie.query
-
to get an instance of the movie query builder.
-
And this will allow us to build out a more dynamic query
-
to get to the movies that are just most recently released.
-
Now, if you recall back, we created a query scope
-
to check whether or not a movie is released.
-
So we can make use of that directly within here as well.
-
So we can apply scope, scope.released, just like so.
-
And now we're only gonna get back released movies,
-
taking those future dated movies out of the picture.
-
We want the most recently released.
-
So we'll order these by released at date
-
in descending order
-
so that we get the most recent movies first.
-
And then we want to limit the results that we get back.
-
Maybe we'll do nine for right now.
-
Lastly, we can go ahead and get rid of our movies all query
-
and replace the state with recently released.
-
Now we need to jump into our homepage
-
and replace our movies with recently released as well.
-
We can keep the rest of the information the same there.
-
And we'll also switch the title here to an H2.
-
And let's call this recently released, just like so.
-
So we can give that a save.
-
And let's jump back into our browser
-
where now we have recently released movies.
-
And we only see two movies here,
-
which is not quite what I was expecting.
-
So let's go ahead and hide that back away.
-
And let's take a look at our fake seeder here.
-
So dive into here.
-
Here's the two movies that we're seeing.
-
So we know that that factory creation worked okay.
-
If we scroll up a little bit here,
-
the underlying issue is that we didn't overwrite the status
-
whenever we were creating the movies from our data list.
-
So if we take a look at the actual movie factory here,
-
scroll up, the default status ID is writing.
-
So we just created all of those movies as being written.
-
What we wanna do instead is add in the status ID
-
here as well.
-
So row.statusID equals movieStatuses.
-
And set that to released.
-
Let's go ahead and hide our text editor back away.
-
Stop our server, node ace migration.
-
And since we reset last,
-
we should be able to just refresh
-
since everything should now be in the same batch.
-
And then we can do hyphen hyphen seed
-
to have this go ahead and seed our data
-
along with this refresh.
-
Hit enter there, and there we go.
-
So now it reverted, re-migrated, and reseeded everything
-
all in one blow.
-
So we can npm run dev to boot our server back up.
-
Let's jump back into our browser.
-
There we go, that's looking better.
-
Now we have nine movies actually populated.
-
And in addition to that,
-
we also have the first two movies being song titles.
-
These Boots Are Made For Walking is definitely a song,
-
and that's just what they'll do.
-
So let's go ahead and hide that back away.
-
Open back up our text editor,
-
jump back into our movies controller.
-
This query is now a-okay and working as expected.
-
And let's do the inverse of that query as well.
-
So let's do movies coming soon.
-
So const coming soon equals await movie query.
-
So we have a scope for when a movie is released.
-
What would be fantastic is if we had a way to tell
-
if it's not released as well.
-
So let's jump back into our movie model
-
and let's give this scope here a copy and a paste.
-
And we'll just make this nice and vague, not released.
-
So we want to do where the status ID is not released.
-
And then we want to do or where release that is null
-
or where release that is greater than the current date time.
-
So overall, we're saying where the status ID
-
is not released or release that is null
-
or the date time is sometime in the future.
-
Any one of these use cases not being true
-
will indicate a movie as being released,
-
matching what we have in our release query up here.
-
So now we can use this query scope
-
by jumping into our movies controller,
-
going up to where we have our coming soon query
-
and applying the scope, scope.notreleased.
-
And we'll want to order this by the release that yet again.
-
And we'll order that in ascending order.
-
And let's just limit this one to maybe three.
-
Now, our not released is going to include the movie
-
if the release that date is null.
-
But technically for this query,
-
if the release that is null,
-
then it's technically not coming soon because it's not,
-
we don't really know when it's coming.
-
So we want to remove those from this particular query.
-
So we'll do where not null released at.
-
Give that a save.
-
And this will work because we have our not released.
-
If we jump back into our movie model,
-
nested inside of parentheses for the where statement,
-
thanks to us using this callback method
-
into the where clause here.
-
So let's go ahead and provide that
-
into the state of our page.
-
So we have released that and let's do coming soon as well.
-
Jump into the page.
-
Let's give this section here a copy
-
and we'll put coming soon before the recently released.
-
Change the title here to coming soon
-
and change this list to coming soon as well.
-
Give that a save, jump back into our browser.
-
And no, I think we had the H1 specially styled,
-
but yeah, so our headings are there.
-
Coming soon are these three movies right here.
-
And then we have our recently released down here.
-
Awesome, so everything does seem to be working A-okay.
-
Introduction
-
Fundamentals
-
2.0Routes and How To Create Them5m 23s
-
2.1Rendering a View for a Route6m 29s
-
2.2Linking Between Routes7m 51s
-
2.3Loading A Movie Using Route Parameters9m 17s
-
2.4Validating Route Parameters6m 6s
-
2.5Vite and Our Assets6m 38s
-
2.6Setting Up Tailwind CSS9m 5s
-
2.7Reading and Supporting Markdown Content4m 32s
-
2.8Listing Movies from their Markdown Files8m 51s
-
2.9Extracting Reusable Code with Services7m 4s
-
2.10Cleaning Up Routes with Controllers4m 52s
-
2.11Defining A Structure for our Movie using Models9m 38s
-
2.12Singleton Services and the Idea of Caching6m 11s
-
2.13Environment Variables and their Validation4m 16s
-
2.14Improved Caching with Redis10m 44s
-
2.15Deleting Items and Flushing our Redis Cache6m 46s
-
2.16Quick Start Apps with Custom Starter Kits6m 28s
-
2.17Easy Imports with NodeJS Subpath Imports8m 40s
-
-
Building Views with EdgeJS
-
3.0EdgeJS Templating Basics8m 49s
-
3.1HTML Attribute and Class Utilities6m 9s
-
3.2Making A Reusable Movie Card Component10m 24s
-
3.3Component Tags, State, and Props4m 53s
-
3.4Use Slots To Make A Button Component6m 56s
-
3.5Extracting A Layout Component5m 13s
-
3.6State vs Share Data Flow2m 59s
-
3.7Share vs Global Data Flow6m 7s
-
3.8Form Basics and CSRF Protection6m 13s
-
3.9HTTP Method Spoofing HTML Forms3m 3s
-
3.10Easy SVG Icons with Edge Iconify7m 57s
-
-
Database and Lucid ORM Basics
-
4.0Configuring Lucid and our Database Connection4m 3s
-
4.1Understanding our Database Schema9m 35s
-
4.2Introducing and Defining Database Migrations18m 35s
-
4.3The Flow of Migrations8m 28s
-
4.4Introducing Lucid Models5m 43s
-
4.5Defining Our Models6m 49s
-
4.6The Basics of CRUD11m 56s
-
4.7Defining Required Data with Seeders11m 11s
-
4.8Stubbing Fake Data with Model Factories13m 48s
-
4.9Querying Our Movies with the Query Builder15m 30s
-
4.10Unmapped and Computed Model Properties3m 24s
-
4.11Altering Tables with Migrations7m 6s
-
4.12Adding A Profile Model, Migration, Factory, and Controller2m 57s
-
4.13SQL Parameters and Injection Protection9m 19s
-
4.14Reusable Query Statements with Model Query Scopes8m 11s
-
4.15Tapping into Model Factory States9m 15s
-
4.16Querying Recently Released and Coming Soon Movies4m 59s
-
4.17Generating A Unique Movie Slug With Model Hooks7m 59s
-
-
Lucid ORM Relationships
-
5.0Defining One to One Relationships Within Lucid Models5m 49s
-
5.1Model Factory Relationships2m 54s
-
5.2Querying Relationships and Eager Vs Lazy Loading5m 17s
-
5.3Cascading and Deleting Model Relationships5m 16s
-
5.4Defining One to Many Relationships with Lucid Models6m 56s
-
5.5Seeding Movies with One to Many Model Factory Relationships5m 24s
-
5.6Listing A Director's Movies with Relationship Existence Queries8m 41s
-
5.7Listing and Counting a Writer's Movies8m 41s
-
5.8Using Eager and Lazy Loading to Load A Movie's Writer and Director5m 18s
-
5.9Defining Many-To-Many Relationships and Pivot Columns9m 48s
-
5.10Many-To-Many Model Factory Relationships4m 50s
-
5.11A Deep Dive Into Relationship CRUD with Models18m 5s
-
5.12How To Create Factory Relationships from a Pool of Data13m 55s
-
5.13How To Query, Sort, and Filter by Pivot Table Data9m 47s
-
-
Working With Forms
-
6.0Accepting Form Data12m 15s
-
6.1Validating Form Data with VineJS9m 29s
-
6.2Displaying Validation Errors and Validating from our Request7m 16s
-
6.3Reusing Old Form Values After A Validation Error2m 3s
-
6.4Creating An EdgeJS Form Input Component5m 28s
-
6.5Creating A Login Form and Validator5m 1s
-
6.6How To Create A Custom VineJS Validation Rule9m 7s
-
-
Authentication & Middleware
-
The Flow of Middleware7m 49s
-
Authenticating A Newly Registered User4m 14s
-
Checking For and Populating an Authenticated User2m 10s
-
Logging Out An Authenticated User2m 24s
-
Logging In An Existing User6m 54s
-
Remembering A User's Authenticated Session6m 55s
-
Protecting Routes with Auth, Guest, and Admin Middleware5m 36s
-
-
Filtering and Paginating Queries
Join The Discussion! (0 Comments)
Please sign in or sign up for free to join in on the dicussion.
Be the first to Comment!