From fbf2f34adb3e37973603358c2aeaacfd030f4902 Mon Sep 17 00:00:00 2001 From: antmarcab4 Date: Sat, 28 Mar 2020 21:13:38 +0100 Subject: [PATCH] =?UTF-8?q?arreglada=20validaci=C3=B3n=20de=20time?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../petclinic/web/HairdressingController.java | 12 ++++-- .../petclinic/web/HairdressingValidator.java | 41 +++++++++++++++---- .../pets/createOrUpdateHairdressingForm.jsp | 2 +- 3 files changed, 43 insertions(+), 12 deletions(-) diff --git a/src/main/java/org/springframework/samples/petclinic/web/HairdressingController.java b/src/main/java/org/springframework/samples/petclinic/web/HairdressingController.java index e18f0b5bdaa..f36b0c151c5 100644 --- a/src/main/java/org/springframework/samples/petclinic/web/HairdressingController.java +++ b/src/main/java/org/springframework/samples/petclinic/web/HairdressingController.java @@ -144,9 +144,15 @@ public String processNewHairdressingForm(@PathVariable("petId") int petId, @Path return "pets/createOrUpdateHairdressingForm"; } else { - this.petService.saveHairdressing(hairdressing); - System.out.println("\n\n\n\n Estos son los hairdressings que hay: \n\n\n\n"+this.petService.findPetById(petId).getHairdressings()+"\n\n\n\n"); - return "redirect:/owners/"+ ownerId; + if (hairdressingService.countHairdressingsByDateAndTime(hairdressing.getDate(), hairdressing.getTime()) != 0){ + result.rejectValue("time", "", "This time isn't available, please select another"); + return "pets/createOrUpdateHairdressingForm"; + + }else { + this.petService.saveHairdressing(hairdressing); + System.out.println("\n\n\n\n Estos son los hairdressings que hay: \n\n\n\n"+this.petService.findPetById(petId).getHairdressings()+"\n\n\n\n"); + return "redirect:/owners/"+ ownerId; + } } } diff --git a/src/main/java/org/springframework/samples/petclinic/web/HairdressingValidator.java b/src/main/java/org/springframework/samples/petclinic/web/HairdressingValidator.java index 49fffdc6652..337e67e37ed 100644 --- a/src/main/java/org/springframework/samples/petclinic/web/HairdressingValidator.java +++ b/src/main/java/org/springframework/samples/petclinic/web/HairdressingValidator.java @@ -15,9 +15,13 @@ */ package org.springframework.samples.petclinic.web; +import java.time.LocalDate; + import org.springframework.beans.factory.annotation.Autowired; import org.springframework.samples.petclinic.model.Hairdressing; import org.springframework.samples.petclinic.model.Pet; +import org.springframework.samples.petclinic.model.TipoCuidado; +import org.springframework.samples.petclinic.repository.HairdressingRepository; import org.springframework.samples.petclinic.service.HairdressingService; import org.springframework.validation.Errors; import org.springframework.validation.Validator; @@ -36,20 +40,41 @@ public class HairdressingValidator implements Validator { private static final String REQUIRED = "required"; -// @Autowired -// HairdressingService hairdressingService; + @Autowired HairdressingService hairdressingService; + @Autowired + HairdressingRepository hairdressingRepo; @Override public void validate(Object obj, Errors errors) { Hairdressing h = (Hairdressing) obj; - - - - - if (hairdressingService.countHairdressingsByDateAndTime(h.getDate(), h.getTime()) != 0){ - errors.rejectValue("time", REQUIRED + "This time isn't available, please select another", REQUIRED + "This time isn't available, please select another"); + //date + if (h.getDate() == null){ + errors.rejectValue("date", REQUIRED, REQUIRED); + }else if(h.getDate().isBefore(LocalDate.now().plusDays(1))) { + errors.rejectValue("date", "The appointment has to be at least for tomorrow", "The appointment has to be at least for tomorrow"); + } + //description + if (h.getDescription().isEmpty()){ + errors.rejectValue("description", REQUIRED, REQUIRED); + }if (h.getDescription().length()>20){ + errors.rejectValue("description", "Must not be longer than 20 characters", "Must not be longer than 20 characters"); + } + //care type + if (h.getCuidado() == null){ + errors.rejectValue("cuidado", REQUIRED, REQUIRED); + }else if(h.getCuidado() != TipoCuidado.ESTETICA && h.getCuidado() != TipoCuidado.PELUQUERIA) { + errors.rejectValue("cuidado", "Select a valid care type", "Select a valid care type"); + } + System.out.println("\n\n\n\n··········Hora: " + h.getTime()); + System.out.println("\n\n\n\n··········Fecha: " + h.getDate()); + //time + if (h.getTime() == null){ + errors.rejectValue("time", REQUIRED, REQUIRED); } +// else if (hairdressingService.countHairdressingsByDateAndTime(h.getDate(), h.getTime()) != 0){ +// errors.rejectValue("time", REQUIRED + "This time isn't available, please select another", REQUIRED + "This time isn't available, please select another"); +// } } /** diff --git a/src/main/webapp/WEB-INF/jsp/pets/createOrUpdateHairdressingForm.jsp b/src/main/webapp/WEB-INF/jsp/pets/createOrUpdateHairdressingForm.jsp index ce5dd361e46..424ff35216f 100644 --- a/src/main/webapp/WEB-INF/jsp/pets/createOrUpdateHairdressingForm.jsp +++ b/src/main/webapp/WEB-INF/jsp/pets/createOrUpdateHairdressingForm.jsp @@ -58,7 +58,7 @@ Description Tipo de cuidado - +