-
Notifications
You must be signed in to change notification settings - Fork 8
/
WebController.java
150 lines (123 loc) · 5.27 KB
/
WebController.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package ac.uk.bristol.cs.santa.grotto.controllers;
import ac.uk.bristol.cs.santa.grotto.business.GeoLookup;
import ac.uk.bristol.cs.santa.grotto.business.UserRepository;
import ac.uk.bristol.cs.santa.grotto.business.UserService;
import ac.uk.bristol.cs.santa.grotto.business.data.*;
import ac.uk.bristol.cs.santa.grotto.business.dto.AccountDTO;
import ac.uk.bristol.cs.santa.grotto.business.route.Location;
import ac.uk.bristol.cs.santa.grotto.business.route.LocationRoutePlanning;
import ac.uk.bristol.cs.santa.grotto.rest.GrottoDTO;
import com.google.maps.errors.ApiException;
import org.apache.poi.ss.formula.functions.T;
import org.modelmapper.ModelMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.repository.support.SimpleJpaRepository;
import org.springframework.data.util.Pair;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import javax.validation.Valid;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Optional;
import java.util.stream.Collectors;
/**
* Created by csxds on 24/11/2017.
*/
@Controller
public class WebController extends WebMvcConfigurerAdapter {
private static final Logger LOG = LoggerFactory.getLogger(WebController.class);
@Autowired
private UserService userService;
@Autowired
private UserRepository userRepository;
/**
* view controllers without logic
*
* @param registry
*/
@Override
public void addViewControllers(ViewControllerRegistry registry) {
// registry.addViewController("/contact");
registry.addViewController("/").setViewName("index");
registry.addViewController("/login");
registry.addViewController("/terms").setViewName("terms");
}
@RequestMapping("/login-error.html")
public String loginError(Model model) {
model.addAttribute("loginError", true);
return "login";
}
@Autowired
private GeoLookup geoLookup;
@RequestMapping(value = "/api/geolookup", method = RequestMethod.POST)
@ResponseBody
public String geolookup(@RequestBody String address) throws InterruptedException, ApiException, IOException {
return String.valueOf(geoLookup.latLngFromAddress(address));
}
@Autowired
private LocationRoutePlanning routePlanning;
@RequestMapping(value = "/api/grottoroute", method = RequestMethod.POST)
@ResponseBody
public String grottoRoute(@RequestBody GrottoDTO[] grottos) throws InterruptedException, ApiException, IOException {
ArrayList<Location> locations = new ArrayList<>();
for (GrottoDTO grotto : grottos) {
Pair<Double, Double> latLong = geoLookup.latLngFromAddress(grotto.getAddress());
locations.add(new Location(grotto.getName(), latLong.getFirst(), latLong.getSecond()));
}
ArrayList<Location> tour = routePlanning.computeOptimalTour(locations, 1000);
return tour.stream()
.map(Location::getName)
.collect(Collectors.joining(","));
}
@Autowired
private ContactRequestRepository contactRepository;
@GetMapping("/contact")
public String showContactForm(ContactRequest contact) {
return "contact";
}
@PostMapping(value = "/contact")
public String submitContact(@Valid ContactRequest contact, BindingResult binding, RedirectAttributes attr) {
if (binding.hasErrors()) {
return "/contact";
}
contactRepository.save(contact);
attr.addFlashAttribute("message", "Thank you for your message. We'll be in touch ASAP");
return "redirect:/contact";
}
private ModelMapper modelMapper = new ModelMapper();
//
@Secured({"ROLE_ADMIN"})
@PostMapping("/api/account")
public ResponseEntity<?> create(@RequestBody AccountDTO account) {
if (account.getPassword() == null) {
return new ResponseEntity<>("password for account not included in DTO", HttpStatus.BAD_REQUEST);
}
User destination = null;
ResponseEntity response = null;
if (account.getUserId() != null) {
Optional<User> oldUser = userRepository.findById(account.getUserId());
if (oldUser.isPresent()) {
destination = oldUser.get();
modelMapper.map(account, destination);
} else {
return new ResponseEntity<>("user id not found or password not set", HttpStatus.BAD_REQUEST);
}
} else {
destination = modelMapper.map(account, User.class);
}
// save user
LOG.info("creating/updating user account " + destination.getUsername());
userService.saveUser(destination);
return new ResponseEntity<>("OK", HttpStatus.CREATED);
}
}