|
@@ -1,23 +1,35 @@
|
|
|
package es.uv.garcosda.controllers;
|
|
|
|
|
|
import es.uv.garcosda.domain.Meeting;
|
|
|
+import jakarta.validation.ConstraintViolation;
|
|
|
+import jakarta.validation.ConstraintViolationException;
|
|
|
+import jakarta.validation.constraints.NotEmpty;
|
|
|
+import jakarta.validation.constraints.NotNull;
|
|
|
|
|
|
import java.time.LocalDateTime;
|
|
|
import java.time.format.DateTimeFormatter;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.Arrays;
|
|
|
import java.util.List;
|
|
|
+import java.util.Set;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
+import org.springframework.format.annotation.DateTimeFormat;
|
|
|
+import org.springframework.http.HttpStatus;
|
|
|
+import org.springframework.http.ResponseEntity;
|
|
|
import org.springframework.stereotype.Controller;
|
|
|
import org.springframework.ui.Model;
|
|
|
+import org.springframework.validation.annotation.Validated;
|
|
|
+import org.springframework.web.bind.annotation.ExceptionHandler;
|
|
|
import org.springframework.web.bind.annotation.GetMapping;
|
|
|
import org.springframework.web.bind.annotation.PathVariable;
|
|
|
import org.springframework.web.bind.annotation.PostMapping;
|
|
|
import org.springframework.web.bind.annotation.RequestParam;
|
|
|
+import org.springframework.web.bind.annotation.ResponseBody;
|
|
|
import org.springframework.web.servlet.view.RedirectView;
|
|
|
|
|
|
@Controller
|
|
|
+@Validated
|
|
|
public class MeetupController {
|
|
|
|
|
|
DateTimeFormatter format = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm");
|
|
@@ -39,7 +51,9 @@ public class MeetupController {
|
|
|
}
|
|
|
|
|
|
@PostMapping("/")
|
|
|
- public RedirectView addMeeting(Model model, @RequestParam("subject") String subject, @RequestParam("date") String date) {
|
|
|
+ public RedirectView addMeeting(Model model, @RequestParam("subject") @NotNull @NotEmpty String subject,
|
|
|
+ @RequestParam("date") @NotNull @NotEmpty @DateTimeFormat(pattern="dd/MM/yyyy hh:MM") String date) {
|
|
|
+
|
|
|
meetings.add(new Meeting(subject, LocalDateTime.parse(date, this.format)));
|
|
|
model.addAttribute("meetings", this.meetings);
|
|
|
return new RedirectView("/");
|
|
@@ -51,4 +65,15 @@ public class MeetupController {
|
|
|
return new RedirectView("/");
|
|
|
}
|
|
|
|
|
|
+ @ExceptionHandler(ConstraintViolationException.class)
|
|
|
+ @ResponseBody
|
|
|
+ public ResponseEntity<String> validationError(ConstraintViolationException ex) {
|
|
|
+ Set<ConstraintViolation<?>> violations = ex.getConstraintViolations();
|
|
|
+ String msg = "";
|
|
|
+ for(ConstraintViolation<?> v : violations) {
|
|
|
+ msg += v.getPropertyPath().toString() + " : " + v.getMessage() + ", ";
|
|
|
+ }
|
|
|
+ return new ResponseEntity<String>(msg, HttpStatus.BAD_REQUEST);
|
|
|
+ }
|
|
|
+
|
|
|
}
|