Playing Next Lesson In
seconds

Transcript

  1. At this point, we have over 400 movies on our database and we're currently just displaying them

  2. 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

  3. 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

  4. 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

  5. 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

  6. 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

  7. 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,

  8. 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

  9. 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

  10. 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.

  11. 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

  12. 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.

  13. 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,

  14. 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,

  15. 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.

  16. So row.statusID equals movieStatuses. And set that to released. Let's go ahead and hide our text editor back away.

  17. 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.

  18. 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.

  19. 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,

  20. 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.

  21. 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.

  22. 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

  23. 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.

  24. So we want to do where the status ID is not released. And then we want to do or where release that is null

  25. 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

  26. 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.

  27. 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.

  28. 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

  29. 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.

  30. 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.

  31. 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.

  32. 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.

  33. 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,

  34. 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.

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.

Created by
@tomgobich
Published

Join the Discussion 0 comments

Create a free account to join in on the discussion
robot comment bubble

Be the first to comment!