-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathMainController.java
190 lines (153 loc) · 6.06 KB
/
MainController.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package ac.uk.bristol.cs.santa.grotto;
import ac.uk.bristol.cs.santa.grotto.business.GeoLookup;
import ac.uk.bristol.cs.santa.grotto.business.data.*;
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.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.util.Pair;
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.security.Principal;
import java.util.ArrayList;
import java.util.stream.Collectors;
/**
* Created by csxds on 24/11/2017.
*/
@Controller
public class MainController extends WebMvcConfigurerAdapter {
private static final Logger logger = LoggerFactory.getLogger(MainController.class);
@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;
@Autowired
private GrottoRepository grottoRepository;
@Autowired
private EventRepository eventRepository;
@GetMapping("/grotto/add")
public String addGrotto(Model model) {
model.addAttribute("grotto", new Grotto());
return "grotto_form";
}
@PostMapping("/grotto")
public String submitGrotto(@ModelAttribute Grotto grotto) {
grottoRepository.save(grotto);
return "grotto_view";
}
@GetMapping("/grotto/{id}")
public String viewGrotto(@PathVariable Long id, Model model) {
model.addAttribute("grotto", grottoRepository.findOne(id));
return "grotto_view";
}
@GetMapping("/event/add")
public String addEvent(Model model) {
model.addAttribute("event", new Event());
model.addAttribute("grottos", grottoRepository.findAll());
return "event_form";
}
@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";
}
@PostMapping("/event")
public String submitEvent(@ModelAttribute Event event) {
eventRepository.save(event);
return "event_view";
}
@GetMapping("/event/{id}")
public String viewEvent(@PathVariable Long id, Model model) {
model.addAttribute("event", eventRepository.findOne(id));
return "event_view";
}
@GetMapping("/eventbooking/add")
public String addEventBooking(Model model) {
model.addAttribute("eventbooking", new EventBooking());
model.addAttribute("events", eventRepository.findAll());
return "eventbooking_form";
}
@Autowired
private
EventBookingRepository eventBookingRepository;
/**
* Create booking for logged in user.
* <p>
* We get the logged in user from Spring and then look up the UserAccount for this user.
*
* @param eventbooking
* @param principal
* @param model
* @return
*/
@PostMapping("/eventbooking")
public String submitEventBooking(@ModelAttribute EventBooking eventbooking, Principal principal, Model model) {
UserAccount userAccount = userRepository.findByUserName(principal.getName());
eventbooking.setUserAccount(userAccount);
eventBookingRepository.save(eventbooking);
model.addAttribute("eventbooking", eventbooking);
return "eventbooking_view";
}
@GetMapping("/eventbooking/{id}")
public String viewEventBooking(@PathVariable Long id, Model model) {
model.addAttribute("eventbooking", eventBookingRepository.findOne(id));
return "eventbooking_view";
}
@RequestMapping(value = "/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 = "/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(","));
}
}