1 00:00:00,000 --> 00:00:02,650 [No Audio] 2 00:00:02,650 --> 00:00:04,426 In this video, we're going to explore coding 3 00:00:04,426 --> 00:00:06,058 of a repository in the data layer. 4 00:00:06,058 --> 00:00:08,170 So as we saw previously, the LoginController 5 00:00:08,170 --> 00:00:09,958 doesn't actually know how to do authentication. 6 00:00:09,958 --> 00:00:11,134 Instead, it has a reference 7 00:00:11,134 --> 00:00:12,590 to an AuthenticationService. 8 00:00:12,590 --> 00:00:15,152 At runtime, it delegates to the AuthenticationService 9 00:00:15,152 --> 00:00:17,372 to get the job of authentication done. 10 00:00:17,372 --> 00:00:18,800 And similarly as well, if we went into 11 00:00:18,800 --> 00:00:23,744 AuthenticationService, you could also imagine here that 12 00:00:23,744 --> 00:00:25,693 there'd be a similar thing as well. So you can 13 00:00:25,693 --> 00:00:26,660 imagine there'd be a private, 14 00:00:26,660 --> 00:00:29,010 [No Audio] 15 00:00:29,010 --> 00:00:30,630 say a UserRepository. 16 00:00:30,630 --> 00:00:44,070 [No Audio] 17 00:00:44,070 --> 00:00:46,286 And the AuthenticationService might also 18 00:00:46,286 --> 00:00:48,095 [No Audio] 19 00:00:48,095 --> 00:00:49,382 use that UserRepository 20 00:00:49,382 --> 00:00:51,816 to validate that the username is valid, and to 21 00:00:51,816 --> 00:00:53,558 look at the actual password of the user. 22 00:00:53,558 --> 00:00:56,328 So you can imagine, for example, something like this 23 00:00:56,328 --> 00:01:05,010 userRepository.findByUsername, passing in username, 24 00:01:05,010 --> 00:01:09,893 and that might return a user, for example, like so, 25 00:01:09,893 --> 00:01:13,084 [No Audio] 26 00:01:13,084 --> 00:01:14,867 and then here instead of being the hardcoded password, 27 00:01:14,867 --> 00:01:21,084 it could be user.getPassword, that kind of thing. 28 00:01:21,084 --> 00:01:24,734 So I can plug those into create class User 29 00:01:24,734 --> 00:01:28,517 [No Audio] 30 00:01:28,517 --> 00:01:32,034 find it by username, create that method too 31 00:01:32,034 --> 00:01:36,050 [No Audio] 32 00:01:36,050 --> 00:01:37,318 and for getPassword we can 33 00:01:37,318 --> 00:01:39,585 imagine having a user 34 00:01:39,585 --> 00:01:42,481 [No Audio] 35 00:01:42,481 --> 00:01:47,164 which has username, private String password 36 00:01:47,164 --> 00:01:50,364 [No Audio] 37 00:01:50,364 --> 00:01:52,624 with the constructor, and then off 38 00:01:52,624 --> 00:01:56,379 of there a couple of getters as well. 39 00:01:56,379 --> 00:01:59,846 And we'll just hardcode these in here for now. 40 00:01:59,846 --> 00:02:02,496 Just create a quick map, 41 00:02:02,496 --> 00:02:04,629 [No Audio] 42 00:02:04,629 --> 00:02:08,929 Map String, that's the username to a User 43 00:02:08,929 --> 00:02:31,653 [No Audio] 44 00:02:31,653 --> 00:02:32,653 new User 45 00:02:32,653 --> 00:02:35,587 [No Audio] 46 00:02:35,587 --> 00:02:36,704 username: "matt" 47 00:02:36,704 --> 00:02:38,781 [No Audio] 48 00:02:38,781 --> 00:02:41,314 password: "letmein", 49 00:02:41,314 --> 00:02:48,432 [No Audio] 50 00:02:48,432 --> 00:02:49,499 "frank", 51 00:02:49,499 --> 00:03:03,339 [No Audio] 52 00:03:03,339 --> 00:03:05,336 then myPassword, whatever. 53 00:03:05,336 --> 00:03:07,244 We've just basically got two users which 54 00:03:07,244 --> 00:03:09,176 we can look things up for. 55 00:03:09,176 --> 00:03:11,159 So then just return 56 00:03:11,159 --> 00:03:13,159 [No Audio] 57 00:03:13,159 --> 00:03:18,548 users.get of the username passed in. 58 00:03:18,548 --> 00:03:20,276 And by the way, since we're putting things into a 59 00:03:20,276 --> 00:03:23,737 HashMap, these users, we also need to make sure 60 00:03:23,737 --> 00:03:25,511 we've got a hashcode and equals method. 61 00:03:25,511 --> 00:03:29,310 [No Audio] 62 00:03:29,310 --> 00:03:31,027 So let's just generate those now 63 00:03:31,027 --> 00:03:33,943 [No Audio] 64 00:03:33,943 --> 00:03:36,224 accepting the defaults. 65 00:03:36,224 --> 00:03:38,860 that's about it for now. There we go. 66 00:03:38,860 --> 00:03:40,826 This needs to be Java7 now as well, 67 00:03:40,826 --> 00:03:49,269 [No Audio] 68 00:03:49,269 --> 00:03:51,058 so that we can use Objects.hash. 69 00:03:51,058 --> 00:03:53,072 So there it is. We're in a good state now. 70 00:03:53,072 --> 00:03:56,204 We've coded up our controller, our service, and now 71 00:03:56,204 --> 00:03:59,288 our repository, and we've defined a domain object 72 00:03:59,288 --> 00:04:01,856 that's our userObject, which you can use and pass back 73 00:04:01,856 --> 00:04:03,886 to the AuthenticationService in the business layer. 74 00:04:03,886 --> 00:04:05,502 So we're well on the way now with this code, 75 00:04:05,502 --> 00:04:06,656 there's just one more thing we need 76 00:04:06,656 --> 00:04:08,588 to do before we can get to unit testing it. 77 00:04:08,588 --> 00:04:09,824 And that's to make things a little bit 78 00:04:09,824 --> 00:04:11,606 tidier, which we'll do in the next video.