@PreAuthorize("hasRole('ROLE_USER')") @RequestMapping(method = {RequestMethod.GET, RequestMethod.HEAD}) public String createTrip(Model model) { Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); User user = userService.getByLogin(authentication.getName()); List<PassengerNodePoint> points = passengerNodePointService.findAll(); points.size(); List<Integer> listCount20 = new ArrayList<>(); for (int i = 1; i < 21; i++) { listCount20.add(i); } model.addAttribute("adress", "createTrip"); model.addAttribute("user", user); model.addAttribute("list", listCount20); model.addAttribute("points", points); model.addAttribute("trip", new Trip()); return "createTrip"; }
@PreAuthorize("hasRole('ROLE_USER')") @RequestMapping(method = RequestMethod.POST) public String saveTrip(@ModelAttribute Trip trip, String startDate) throws ParseException { Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); User driver = userService.getByLogin(authentication.getName()); DateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy HH:mm"); Date date = dateFormat.parse(startDate); long time = date.getTime(); Timestamp start = new Timestamp(time); trip.setStartTime(start); trip.setDriver(driver); List<PassengerNodePoint> points = trip.getPassengerNodePoints(); Iterator iterator = points.iterator(); while (iterator.hasNext()) { PassengerNodePoint point = (PassengerNodePoint) iterator.next(); if (point.getId() == null) { iterator.remove(); } } tripService.save(trip); return "redirect:/user/showTrips"; }