[No Audio]
In this video, we're going to explore coding
of a repository in the data layer.
So as we saw previously, the LoginController
doesn't actually know how to do authentication.
Instead, it has a reference
to an AuthenticationService.
At runtime, it delegates to the AuthenticationService
to get the job of authentication done.
And similarly as well, if we went into
AuthenticationService, you could also imagine here that
there'd be a similar thing as well. So you can
imagine there'd be a private,
[No Audio]
say a UserRepository.
[No Audio]
And the AuthenticationService might also
[No Audio]
use that UserRepository
to validate that the username is valid, and to
look at the actual password of the user.
So you can imagine, for example, something like this
userRepository.findByUsername, passing in username,
and that might return a user, for example, like so,
[No Audio]
and then here instead of being the hardcoded password,
it could be user.getPassword, that kind of thing.
So I can plug those into create class User
[No Audio]
find it by username, create that method too
[No Audio]
and for getPassword we can
imagine having a user
[No Audio]
which has username, private String password
[No Audio]
with the constructor, and then off
of there a couple of getters as well.
And we'll just hardcode these in here for now.
Just create a quick map,
[No Audio]
Map String, that's the username to a User
[No Audio]
new User
[No Audio]
username: "matt"
[No Audio]
password: "letmein",
[No Audio]
"frank",
[No Audio]
then myPassword, whatever.
We've just basically got two users which
we can look things up for.
So then just return
[No Audio]
users.get of the username passed in.
And by the way, since we're putting things into a
HashMap, these users, we also need to make sure
we've got a hashcode and equals method.
[No Audio]
So let's just generate those now
[No Audio]
accepting the defaults.
that's about it for now. There we go.
This needs to be Java7 now as well,
[No Audio]
so that we can use Objects.hash.
So there it is. We're in a good state now.
We've coded up our controller, our service, and now
our repository, and we've defined a domain object
that's our userObject, which you can use and pass back
to the AuthenticationService in the business layer.
So we're well on the way now with this code,
there's just one more thing we need
to do before we can get to unit testing it.
And that's to make things a little bit
tidier, which we'll do in the next video.