1 | package finki.diplomska.tripplanner.web;
|
---|
2 |
|
---|
3 | import finki.diplomska.tripplanner.models.*;
|
---|
4 | import finki.diplomska.tripplanner.service.*;
|
---|
5 | import org.springframework.stereotype.Controller;
|
---|
6 | import org.springframework.ui.Model;
|
---|
7 | import org.springframework.web.bind.annotation.*;
|
---|
8 |
|
---|
9 | import java.util.ArrayList;
|
---|
10 | import java.util.List;
|
---|
11 | import java.util.Optional;
|
---|
12 |
|
---|
13 | @Controller
|
---|
14 | @RequestMapping(value = "/create")
|
---|
15 | public class CreatePlannerController {
|
---|
16 |
|
---|
17 | private final LocationService locationService;
|
---|
18 | private final PlannerService plannerService;
|
---|
19 | private final CityService cityService;
|
---|
20 | private final CountryService countryService;
|
---|
21 | private final CategoryService categoryService;
|
---|
22 | private final CompanionService companionService;
|
---|
23 | private final RegionService regionService;
|
---|
24 |
|
---|
25 | public CreatePlannerController(LocationService locationService, PlannerService plannerService, CityService cityService, CountryService countryService, CategoryService categoryService, CompanionService companionService, RegionService regionService) {
|
---|
26 | this.locationService = locationService;
|
---|
27 | this.plannerService = plannerService;
|
---|
28 | this.cityService = cityService;
|
---|
29 | this.countryService = countryService;
|
---|
30 | this.categoryService = categoryService;
|
---|
31 | this.companionService = companionService;
|
---|
32 | this.regionService = regionService;
|
---|
33 | }
|
---|
34 |
|
---|
35 | @GetMapping
|
---|
36 | public String createPlanner(Model model) {
|
---|
37 | return "create-planner";
|
---|
38 | }
|
---|
39 |
|
---|
40 | @PostMapping(value = "/planner")
|
---|
41 | public String newPlanner(@RequestParam String plandesc ,
|
---|
42 | @RequestParam String planname,
|
---|
43 | @RequestParam(required = false) List<Long> locationList) {
|
---|
44 | List<Location> locations = new ArrayList<>();
|
---|
45 | if (!(locationList == null)) {
|
---|
46 | for (long id : locationList) {
|
---|
47 | Location location = locationService.getById(id);
|
---|
48 | locations.add(location);
|
---|
49 | }
|
---|
50 | this.plannerService.createPlannerWithRequestParams(plandesc, planname, locations);
|
---|
51 | } else {
|
---|
52 | this.plannerService.createPlannerWithRequestParams(plandesc, planname, null);
|
---|
53 | }
|
---|
54 | return "redirect:/create/planner";
|
---|
55 | }
|
---|
56 |
|
---|
57 |
|
---|
58 | @GetMapping(value = "/planner")
|
---|
59 | public String showPlanners(Model model) {
|
---|
60 | List<Planner> plannerList = plannerService.getAllPlaners();
|
---|
61 | model.addAttribute("plannerList",plannerList);
|
---|
62 | return "create-homepage";
|
---|
63 | }
|
---|
64 |
|
---|
65 | @GetMapping(value = "/planner/{id}")
|
---|
66 | public String editShow(@PathVariable Long id, Model model) {
|
---|
67 | Optional<Planner> detailsAboutPlanner = this.plannerService.findById(id);
|
---|
68 | model.addAttribute("detailsAboutPlanner", detailsAboutPlanner);
|
---|
69 | return "edit-planner";
|
---|
70 | }
|
---|
71 |
|
---|
72 | @PostMapping(value = "/planner/{id}")
|
---|
73 | public String editsave(@PathVariable Long id,
|
---|
74 | @RequestParam(required = false) String plandesc,
|
---|
75 | @RequestParam(required = false) String planname) {
|
---|
76 |
|
---|
77 | this.plannerService.editPlannerWithRequestParams(id, plandesc, planname, null);
|
---|
78 |
|
---|
79 | return "redirect:/create/planner";
|
---|
80 | }
|
---|
81 |
|
---|
82 | @GetMapping(value = "/locations")
|
---|
83 | public String homepage(Model model){
|
---|
84 | List<City> cities = this.cityService.findAll();
|
---|
85 | List<Country> countries = this.countryService.findAll();
|
---|
86 | List<Category> categories = this.categoryService.findAll();
|
---|
87 | List<Companion> companions = this.companionService.findAll();
|
---|
88 | List<Region> regions = this.regionService.findAll();
|
---|
89 | model.addAttribute("cities", cities);
|
---|
90 | model.addAttribute("countries", countries);
|
---|
91 | model.addAttribute("categories",categories);
|
---|
92 | model.addAttribute("companions",companions);
|
---|
93 | model.addAttribute("regions",regions);
|
---|
94 | return "homepage";
|
---|
95 | }
|
---|
96 |
|
---|
97 | @PostMapping(value = "/trip/locations")
|
---|
98 | public String allLocations(Model model, @RequestParam(required = false) String locName,
|
---|
99 | @RequestParam(required = false) String companion,
|
---|
100 | @RequestParam(required = false) String region,
|
---|
101 | @RequestParam(required = false) List<String> categories,
|
---|
102 | @RequestParam(required = false) int numberOfDays) {
|
---|
103 | List<Location> countryLocations = this.locationService.scheduleLocations(locName, companion, region, categories, numberOfDays);
|
---|
104 | List<Planner> getAllPlanners = this.plannerService.getAllPlaners();
|
---|
105 |
|
---|
106 | if(locName.equals("Macedonia")){
|
---|
107 | model.addAttribute("countryLocations", countryLocations);
|
---|
108 | return "trip-locations-country";
|
---|
109 | }else{
|
---|
110 | model.addAttribute("imgUtil", new ImageUtil());
|
---|
111 | model.addAttribute("getAllPlanners",getAllPlanners);
|
---|
112 | model.addAttribute("countryLocations", countryLocations);
|
---|
113 | return "trip-locations";
|
---|
114 | }
|
---|
115 | }
|
---|
116 |
|
---|
117 | @GetMapping(value = "/trip/locations/{id}")
|
---|
118 | public String seeDetailsAboutLocation(@PathVariable Long id, Model model){
|
---|
119 | Optional<Location> location = this.locationService.findById(id);
|
---|
120 | model.addAttribute("location", location);
|
---|
121 | return "location";
|
---|
122 | }
|
---|
123 |
|
---|
124 | }
|
---|