-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHome.java
52 lines (41 loc) · 1.52 KB
/
Home.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
40
41
42
43
44
45
46
47
48
49
50
51
52
import com.fasterxml.jackson.databind.ObjectMapper;
import spark.ModelAndView;
import spark.Request;
import spark.Response;
import spark.template.thymeleaf.ThymeleafTemplateEngine;
import java.util.HashMap;
import java.util.Map;
import static spark.Spark.*;
public class Home {
static int buhariVotes = 0;
static int jonathanVotes = 0;
static int kachikwuVotes = 0;
public static void main(String[] args) {
port(8080);
staticFiles.location("/templates");
get("/home", Home::welcome, new ThymeleafTemplateEngine());
post("/vote", (req, res) -> {
//ObjectMapper provides functionality for reading and writing JSON
ObjectMapper objectMapper = new ObjectMapper();
Vote votePojo = objectMapper.readValue(req.body(), Vote.class);
vote(votePojo);
return "Vote successful";
});
}
private static ModelAndView welcome(Request req, Response res) {
Map<String, Object> params = new HashMap<>();
params.put("Jonathan", jonathanVotes);
params.put("Buhari", buhariVotes);
params.put("Kachikwu", kachikwuVotes);
return new ModelAndView(params, "welcome");
}
private static void vote(Vote vote) {
if (vote.getName().equalsIgnoreCase("Buhari")) {
buhariVotes++;
} else if (vote.getName().equalsIgnoreCase("Jonathan")) {
jonathanVotes++;
} else if (vote.getName().equalsIgnoreCase("Kachikwu")) {
kachikwuVotes++;
}
}
}