-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathIndexController.java
More file actions
50 lines (40 loc) · 1.64 KB
/
IndexController.java
File metadata and controls
50 lines (40 loc) · 1.64 KB
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
40
41
42
43
44
45
46
47
48
49
50
package com.unbabel.challenge.controller;
import com.unbabel.challenge.model.Message;
import com.unbabel.challenge.repositories.MessageRepository;
import org.apache.commons.lang.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class IndexController {
@Autowired
MessageRepository messageRepository;
@Value("${java.challenge.company}")
private String company; // Reads this value from Spring properties file
/**
* Accepts get requests to the "/" url, generates random messages
* and renders them in thymeleaf template (index.html).
* @param model inject objects into thymeleaf template
* @return generated html page using thymeleaf
*/
@GetMapping("/")
public String main(Model model) {
generateNMessages(10);
//set variables to be used in thymeleaf template
model.addAttribute("company", company);
model.addAttribute("messages", messageRepository.findAll());
return "index"; //thymeleaf template name (index -> index.html)
}
/**
* Deletes all messages and inserts new random ones
* @param repetitions number of times to loop
*/
private void generateNMessages(int repetitions) {
messageRepository.deleteAll(); //clean all random str
for (int i = 0; i < repetitions; i++) {
messageRepository.save(new Message(RandomStringUtils.randomAlphabetic(30)));
}
}
}