Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
A great place to start when architecting an API is the model. Let's build out what we will be using.
This video doesn't have any notes.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
Well, it is that time again.
0:00
It is time to define our model.
0:01
So we know we will have
a course model object, and
0:03
we also know we will have
a review model object.
0:06
Now each course will
possibly have many reviews.
0:09
Now you might recognize this type of
model relationship as one to many.
0:13
One course, many reviews.
0:17
Let's do some modeling.
0:19
Okay so let's make a new IntelliJ
project and let's make it Gradle based.
0:22
So I'm gonna click Create New Project and
0:25
I'm gonna make sure that
Gradle is checked over here.
0:28
And I'm gonna click Next.
0:31
And for the Group ID,
let's make that com.teamtreehouse.courses.
0:34
And for the artifact we will
call this course reviews.
0:39
Right, we're writing reviews for courses.
0:42
Click next.
0:44
We're going to go ahead and make sure
that we check the create directories for
0:45
empty content routes automatically.
0:48
Iām going to click next.
0:49
And we will keep it in the same
place in the home directory.
0:52
Click finish.
0:55
Okay, now let's create our model object.
0:58
So if you open up over here under
the source directory main Java,
1:00
we'll right click.
1:04
Choose New, Java Class.
1:05
Then we wanna blow that all the way out.
1:07
We wanna say
com.teamtreehouse.courses.model and
1:09
first we'll build a thing called
Course in the model package.
1:15
Okay, so our database that we're going to
eventually use is going to use an internal
1:20
identifier for a primary key.
1:24
But this is common amongst just
about every one of these things.
1:26
So we're gonna make
a thing here called id.
1:30
And this is common practice,
it's either a long or an integer.
1:31
So we'll make an id, and,
what else will a course have?
1:35
A course will have a name.
1:38
We'll also have a URL, right?
1:41
So, it'll be the name of the course and
the URL of where to find it.
1:44
All right.
So let's go ahead and
1:47
generate our constructor.
1:49
So I'm going to go Code Generate and
again that's Command N on a Mac
1:51
and we are going to
generate a Constructor.
1:58
And for now let's just leave the ID out.
2:03
Right.
We can just make a new course with name in
2:04
url, because it's an integer it will just
be zero until it gets set properly.
2:06
Okay and then let's go ahead and
let's add our getters and our setters. So,
2:11
we'll generate getters, and setters,
and we will generate for all of them.
2:14
That's fine.
2:20
And, let's go ahead, and while we're here,
let's also do the equals, and hash code.
2:21
Might as well, right?
2:25
It's gonna write the code for
us, might as well let it.
2:26
So, we're just gonna use
the IntelliJ default.
2:28
We will use all of those.
2:31
Those are all expected in
the fields that are non null.
2:33
They're both not null right?
2:38
We should always have those.
2:39
Okay, and now very similarly,
let's make our review object.
2:43
So we'll do new, Java Class and
we're in that same model directory, model.
2:46
And we'll say review and
again this will also have an id.
2:52
So I'll do private int id.
2:56
And for now,
let's just store the parent course's id.
2:59
You know you never want to go over
board unless you end up needing it.
3:03
Like we could actually put
a course in here, but for now,
3:06
let's just stick with the id.
3:09
That's what we're going to do, right?
3:10
We're just going to use this id.
3:12
So remember, keep it simple,
smarty pants, K-I-S-S.
3:13
All right.
3:18
So it's private int, and it's a course id.
3:18
And that's going to refer to the course
that we are writing a review about.
3:22
So we've got a rating.
3:25
And we also have a comment here.
3:28
Okay and very similarly let's go ahead and
let's do our generating.
3:33
We'll generate a constructor.
3:37
Lets make the course id required,
but not the id itself.
3:39
We'll let the dao objects take
care of that later here in a bit.
3:42
So and now we will make getters and
setters.
3:46
Let's get for all of them.
3:50
And also let's make the equals and hash
code when we're in here just to play nice.
3:52
Okay those should all be included,
3:58
all those should be included
the non null field.
4:01
Yeah comments could probably be null.
4:05
All right.
4:07
So now you can see that the review
has a reference to the course id
4:09
simply by the way of the id.
4:13
Now, we have made it required for
creation.
4:15
You can't create a review
without having a course id.
4:19
All right, so we just created our plain,
old Java objects, and now,
4:22
we need to create a way
to serialize those.
4:26
I had mentioned that we are going to
be using SQL, and more specifically,
4:28
we'll embrace the classical design
pattern of data access objects, or dao.
4:31
Let's get that all ready to rock
right after this quick break.
4:36
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up