This repository was archived by the owner on Jan 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathAccountController.java
40 lines (34 loc) · 1.61 KB
/
AccountController.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package com.bobocode.web.controller;
import com.bobocode.TestDataGenerator;
import com.bobocode.model.Account;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.List;
import java.util.stream.Stream;
import static java.util.stream.Collectors.toList;
/**
* This controller provides endpoint that generates a list of {@link Account} and passes it to the view.
* <p>
* todo: 1. Configure controller that handles requests with url "/accounts"
* todo: 2. Inject a {@link TestDataGenerator}
* todo: 3. Implement a method that handles GET request with param "size" to url "/accounts" and forwards it to the accounts.jsp view
* todo: 4. In this method generate a list of account using data generator and received int value "size"
* todo: 5. Provide a default value "10" for parameter "size"
* todo: 6. Pass the list of accounts to the view using model attribute with name "accountList"
*/
@Controller
@RequestMapping("/accounts")
public class AccountController {
@Autowired
private TestDataGenerator dataGenerator;
@GetMapping
public String getAccounts(@RequestParam(name = "size", defaultValue = "10") int size, Model model) {
List<Account> accounts = Stream.generate(dataGenerator::generateAccount).limit(size).collect(toList());
model.addAttribute("accountList", accounts);
return "accounts";
}
}