00:00
(upbeat music)
00:02
So let's go about that exact same flow,
00:07
but for our writers and with some new stuff tied in.
00:09
So let's go ahead and create a writer's controller
00:11
with an index and show method bound in.
00:13
So we'll stop our server, node, ace, make controller,
00:17
writers with an index and show method added in.
00:21
Go ahead and boot back up our server
00:22
and jump back into our text editor.
00:24
For the most part, our query for our index page
00:27
is going to remain the same.
00:28
We'll add in some additional stuff to it,
00:30
but it will be relatively similar
00:32
so we can start from it as a base point.
00:34
So let's do const writers equals
00:36
and let's paste in what we've copied.
00:38
Now we'll need to re-import our cinist.
00:40
So I'm just going to type out cin
00:42
and then hit tab to auto import that.
00:43
And instead of where has movies directed,
00:45
this will be where has movies written.
00:47
Then we can return and we need to extract out our view
00:51
from our HTTP context, view, render pages,
00:54
writers slash index.
00:57
We can provide in our writers there.
00:59
Let's get this working
01:00
before we add some additional stuff to it.
01:01
So let's jump back to our routes.
01:03
We can go ahead and copy both of these,
01:05
paste it in since we're going to have
01:06
just writer versions of both of these.
01:08
Hit option command down to get multiple cursors,
01:11
option shift right to select the whole word directors
01:14
and replace that with writers.
01:15
Okay, hit escape to go back to a single cursor,
01:18
jump over to our director's controller here.
01:20
Let's go into the middle of it,
01:21
option command down again to get multiple cursors,
01:24
option left arrow to jump to the start of the word,
01:27
option shift right arrow to jump to the end of the word
01:29
and select the entire word director's controller.
01:32
And let's type out writers controller
01:35
and hit tab to auto import that.
01:36
Lastly, command right arrow to jump to the end of the line,
01:39
option left arrow two times,
01:41
and then just left arrow once more
01:43
with option shift left arrow
01:45
to highlight our whole director's word,
01:47
replacing that with writers.
01:49
If you're not familiar with those keyboard shortcuts,
01:51
it's worth the time to go through that flow
01:53
over and over again so that you get them mentally mapped
01:55
because they really do save you quite a bit of time.
01:57
Okay, so we have our routes, let's create our pages next.
02:00
We can just right click and copy our director's directory,
02:03
right click on pages and paste that in.
02:06
It's gonna be called director's copy, that's okay.
02:08
Hit enter on that so that we can rename it to writers.
02:11
We open that back up, jump into our show page
02:13
because we have director explicitly written right there.
02:16
And let's switch that to writer, switch this to writer.
02:19
And we're gonna go about this a different approach,
02:20
so we will need to switch movies,
02:22
but we'll leave that as is for right now as well.
02:24
Our index page has something similar going on.
02:26
Directors, we'll switch to writers,
02:28
directors to writers yet again, director to writer,
02:31
writers, writer, and writer.
02:34
Okay, there was more on that page than I remembered,
02:36
we should just did a find and replace, but that's okay.
02:38
Lastly, let's jump into our navigation,
02:39
copy our director's href, paste that in,
02:42
switch that to writers and switch this to writers as well.
02:45
So now we should be able to open back up our browser
02:48
and we should have a writer's option up here
02:49
in our navigation that we can click.
02:51
And let's see if we had any differences here.
02:53
A couple, yep.
02:54
So it seems like Jane Dickey is a director,
02:56
but not a writer and Bernie Rogan is a writer,
02:58
but not a director.
02:59
We are working with a pretty small pool of people,
03:01
so there's not gonna be much of a change here,
03:03
but we do see some change, so we know that's working.
03:05
So let's jump back into our writer's controller.
03:07
And now let's say that we want to display
03:09
the number of movies that these writers have written.
03:12
After our where has, we can use a method called with count
03:16
to count the number of movies related to this user
03:18
where they have written the movies.
03:20
And for that, we can use our movies written relationship.
03:22
This will add a count onto the extras object of our writers.
03:26
Our model query builders will always return back
03:29
an instance of the model.
03:31
Any properties that don't exist on that model
03:33
explicitly included inside of our query,
03:36
like our movies written count will be,
03:38
will then be placed on the extras object.
03:40
And just like our where has here,
03:42
we can copy that and add that in as a second argument
03:45
to our count to restrict our movies written count
03:48
to just movies that have already released.
03:50
So if we dive into our writer's index page,
03:53
let's do a span class text slate 400.
03:58
And we'll display this count inside of parentheses
04:00
and we'll do double curly braces here
04:02
so that we can reach into writer dot
04:05
and we'll reach for our dollar sign extras object.
04:08
And there should be a concatenation
04:10
of the relationship name underscore count.
04:13
So that would be movies written underscore count.
04:16
All right, let's see if we got that right.
04:18
Let's jump back into our browser and look at that.
04:20
So we have Alvina Barrows wrote 45 movies
04:23
and we need Doyle 42, 42 there, 35, 34.
04:27
Awesome, so that seems to be working.
04:29
Let's hide our browser back away,
04:30
jump back into our writer's controller.
04:32
Now let's say we also wanted to do with count,
04:35
but where the movie is not yet released.
04:37
So movies coming soon or something like that.
04:40
Well, we can give this specific with count a name
04:43
by inside of the query callback,
04:45
chaining off of it and as,
04:48
and then providing the name in there.
04:49
So we could do something like released count there.
04:53
Let's give that a copy before we save, paste it in,
04:55
switch our query scope to not released for the second count
04:59
and switch the name to not released count.
05:02
Now we'll give that a save so that it formats,
05:04
jump back into our writer's index page.
05:06
And now we need to switch this reference
05:08
from movies written underscore count,
05:10
which is the automatic name that Lucid will provide to it,
05:13
to our manual name of released count.
05:16
And we can add in released comma,
05:18
and now we can do writer.extras.notreleasedcount
05:23
as we'll just say not released there.
05:26
Let's jump back into our browser
05:27
and our released looks the same,
05:29
but we do get back zero across the board for not released.
05:32
So if we dive back into our text editor here,
05:35
go down into our database and our fake cedar.
05:38
Right here we have two movies being created
05:41
with written soon and they should have
05:43
one specific writer attached to it.
05:45
So we should have at least two people
05:47
with a not released movie.
05:48
Let's take a look here.
05:49
So right here, we are limiting our writers
05:52
to just those that have a released movie.
05:55
And within our fake cedar,
05:56
we're creating two specific writers
05:58
for these not released movies.
06:00
So we do wanna get rid of this release specification here
06:03
because that limits our writers to just those
06:05
that have written a movie and that movie has released.
06:08
So if we get rid of our query scope there
06:10
to include those that have not yet released,
06:12
give that a save, jump back into our browser,
06:14
give this a refresh.
06:15
There we go.
06:16
And now we have one, two, three, and four writers
06:21
who don't have a released movie,
06:23
but do have a not released movie.
06:24
So let's jump back into our fake cedar
06:26
to make sure that that is right.
06:27
So we know that we have two here.
06:29
Ah, yeah, post-production is also not released.
06:31
So that would be four.
06:32
Okay, cool.
06:33
So that looks right there.
06:34
So let's jump back into our writer's controller
06:36
and let's go ahead and fill out the show method.
06:38
So again, for this one, we're gonna want view and params.
06:41
And now we can get const writer equals await cinist.
06:46
And this time we're gonna use the cinist query builder
06:49
to find our writer.
06:49
So we can do .where that writer's ID is our params ID.
06:54
If you wanted to, you could also provide this as an object.
06:57
So ID as the column and params.id as the value,
07:01
that will work as well,
07:02
which will limit our cinist down to just the one cinist
07:05
that we're looking for.
07:06
And then we can do first or fail.
07:09
And now we have a query relatively similar to,
07:12
if we scroll up and go to our director's controller,
07:14
this line right here.
07:16
So both of these lines get us the same result.
07:19
However, we can use eager loading,
07:22
so preload to attach the movies that this writer has written.
07:27
And since on our index page,
07:28
we're including those movies
07:30
where they are not quite released,
07:31
we'll leave this as is.
07:33
But if we wanted to limit this preload
07:34
to just movies that were released,
07:36
we could do query, query.apply, scope, scope.released,
07:41
like so to get that result.
07:44
But we'll include those here.
07:45
Okay, let's go ahead and return,
07:47
view, render, pages, writers, show,
07:51
and provide in our writer.
07:53
So now we have all of the information
07:54
on one variable of just writer,
07:56
whereas on our director's controller,
07:58
we had it as director and movies.
08:00
So within our writer's show page,
08:02
we're gonna need to reflect that.
08:03
So instead of just movies here,
08:05
this is going to be writer.moviesWritten.
08:08
And then we can loop over each of the movies
08:10
that they've written and apply their card.
08:12
Okay, so now we should be able to dive
08:14
back into our browser.
08:15
I can't quite recall who we were working with
08:17
on our writer two lessons ago,
08:19
so we'll just click on somebody random here.
08:21
And there we go, we see a number of different movies
08:24
that this person has written.
08:25
If we go back, let's click on somebody who only has one.
08:28
So Bernie Rogan right here, just wrote "Grease," okay.
08:32
So let's click on somebody
08:33
who doesn't have a released movie yet.
08:34
So Johnny here, "Sugar Shack," okay.
08:37
So all of that is looking a-okay.
08:39
[ illegible ]
Join The Discussion! (0 Comments)
Please sign in or sign up for free to join in on the dicussion.
Be the first to Comment!