Skip to content

Commit

Permalink
better checking of rental creation in web UI
Browse files Browse the repository at this point in the history
  • Loading branch information
Venca24 committed Jan 12, 2017
1 parent ec9edba commit ccb1065
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions web/src/main/java/controllers/RentalController.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,15 @@ public String submitEdit(@PathVariable long id,
}

@RequestMapping(value = "/new", method = RequestMethod.GET)
public String newRental(Model model) {
model.addAttribute("rental", new RentalCreateDTO());
public String newRental(@ModelAttribute("rental") RentalCreateDTO rentalDTO, Model model) {
if(rentalDTO == null)
{
model.addAttribute("rental", new RentalCreateDTO());
}
else
{
model.addAttribute("rental", rentalDTO);
}
return "rental/new";
}

Expand All @@ -133,6 +140,7 @@ public String submitNew(@ModelAttribute("rental") RentalCreateDTO rentalDTO,
else
{
redirectAttributes.addFlashAttribute("alert_danger", "\"Date from\" cannot be empty.");
redirectAttributes.addFlashAttribute("rental", rentalDTO);
return "redirect:new";
}

Expand All @@ -142,13 +150,15 @@ public String submitNew(@ModelAttribute("rental") RentalCreateDTO rentalDTO,
else
{
redirectAttributes.addFlashAttribute("alert_danger", "\"Date to\" cannot be empty.");
redirectAttributes.addFlashAttribute("rental", rentalDTO);
return "redirect:new";
}

// checks if "date to" doesn't precede "date from"
if(from != null && to != null && from.after(to))
{
redirectAttributes.addFlashAttribute("alert_danger", "\"Date to\" cannot precede \"Date from\".");
redirectAttributes.addFlashAttribute("rental", rentalDTO);
return "redirect:new";
}

Expand All @@ -166,6 +176,7 @@ public String submitNew(@ModelAttribute("rental") RentalCreateDTO rentalDTO,
if(price < 0)
{
redirectAttributes.addFlashAttribute("alert_danger", "Price can't be negative.");
redirectAttributes.addFlashAttribute("rental", rentalDTO);
return "redirect:new";
}

Expand All @@ -177,7 +188,6 @@ public String submitNew(@ModelAttribute("rental") RentalCreateDTO rentalDTO,
}

rentalFacade.createRental(rental);
model.addAttribute("rental", rental);

redirectAttributes.addFlashAttribute("alert_success", "Rental details saved successfully.");
return "redirect:list";
Expand Down

0 comments on commit ccb1065

Please sign in to comment.