From b8a61a8fd2a85be9e37511d0f0e77ee640590167 Mon Sep 17 00:00:00 2001 From: josdem Date: Mon, 21 Oct 2024 09:19:12 -0600 Subject: [PATCH 1/6] #377 Listing only vaccines in status pending --- .../java/com/josdem/vetlog/controller/PetController.java | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/josdem/vetlog/controller/PetController.java b/src/main/java/com/josdem/vetlog/controller/PetController.java index cb817982..ff67f923 100644 --- a/src/main/java/com/josdem/vetlog/controller/PetController.java +++ b/src/main/java/com/josdem/vetlog/controller/PetController.java @@ -20,7 +20,9 @@ import com.josdem.vetlog.command.PetCommand; import com.josdem.vetlog.enums.PetStatus; import com.josdem.vetlog.enums.PetType; +import com.josdem.vetlog.enums.VaccinationStatus; import com.josdem.vetlog.model.User; +import com.josdem.vetlog.model.Vaccination; import com.josdem.vetlog.service.BreedService; import com.josdem.vetlog.service.LocaleService; import com.josdem.vetlog.service.PetService; @@ -30,6 +32,8 @@ import jakarta.servlet.http.HttpServletRequest; import jakarta.validation.Valid; import java.io.IOException; +import java.util.List; + import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; @@ -182,7 +186,10 @@ public ModelAndView delete(@RequestParam("uuid") String uuid) { private ModelAndView fillPetAndImageUrl(ModelAndView modelAndView) { var user = userService.getCurrentUser(); var pets = petService.getPetsByUser(user); - pets.forEach(pet -> pet.setVaccines(vaccinationService.getVaccinationsByPet(pet))); + pets.forEach(pet -> { + final List pendingVaccines = vaccinationService.getVaccinationsByPet(pet).stream().filter(vaccination -> vaccination.getStatus().equals(VaccinationStatus.PENDING)).toList(); + pet.setVaccines(pendingVaccines); + }); modelAndView.addObject("pets", pets); modelAndView.addObject(GCP_IMAGE_URL, gcpUrl + imageBucket + "/"); modelAndView.addObject("defaultImage", defaultImage); From 88e05a1f92d365885b427af38febdc9f417481ae Mon Sep 17 00:00:00 2001 From: josdem Date: Mon, 21 Oct 2024 09:21:10 -0600 Subject: [PATCH 2/6] #377 Listing only vaccines in status pending in vet search --- .../com/josdem/vetlog/controller/PetController.java | 5 +++-- .../com/josdem/vetlog/controller/VetController.java | 10 +++++++++- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/josdem/vetlog/controller/PetController.java b/src/main/java/com/josdem/vetlog/controller/PetController.java index ff67f923..b6863e90 100644 --- a/src/main/java/com/josdem/vetlog/controller/PetController.java +++ b/src/main/java/com/josdem/vetlog/controller/PetController.java @@ -33,7 +33,6 @@ import jakarta.validation.Valid; import java.io.IOException; import java.util.List; - import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; @@ -187,7 +186,9 @@ private ModelAndView fillPetAndImageUrl(ModelAndView modelAndView) { var user = userService.getCurrentUser(); var pets = petService.getPetsByUser(user); pets.forEach(pet -> { - final List pendingVaccines = vaccinationService.getVaccinationsByPet(pet).stream().filter(vaccination -> vaccination.getStatus().equals(VaccinationStatus.PENDING)).toList(); + final List pendingVaccines = vaccinationService.getVaccinationsByPet(pet).stream() + .filter(vaccination -> vaccination.getStatus().equals(VaccinationStatus.PENDING)) + .toList(); pet.setVaccines(pendingVaccines); }); modelAndView.addObject("pets", pets); diff --git a/src/main/java/com/josdem/vetlog/controller/VetController.java b/src/main/java/com/josdem/vetlog/controller/VetController.java index 2f1f1ae3..979aed54 100644 --- a/src/main/java/com/josdem/vetlog/controller/VetController.java +++ b/src/main/java/com/josdem/vetlog/controller/VetController.java @@ -17,10 +17,13 @@ package com.josdem.vetlog.controller; import com.josdem.vetlog.command.UsernameCommand; +import com.josdem.vetlog.enums.VaccinationStatus; +import com.josdem.vetlog.model.Vaccination; import com.josdem.vetlog.service.PetService; import com.josdem.vetlog.service.UserService; import com.josdem.vetlog.service.VaccinationService; import jakarta.validation.Valid; +import java.util.List; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; @@ -63,7 +66,12 @@ ModelAndView search(@Valid UsernameCommand command) { var modelAndView = new ModelAndView("vet/list"); var user = userService.getByUsername(command.getUsername()); var pets = petService.getPetsByUser(user); - pets.forEach(pet -> pet.setVaccines(vaccinationService.getVaccinationsByPet(pet))); + pets.forEach(pet -> { + final List pendingVaccines = vaccinationService.getVaccinationsByPet(pet).stream() + .filter(vaccination -> vaccination.getStatus().equals(VaccinationStatus.PENDING)) + .toList(); + pet.setVaccines(pendingVaccines); + }); modelAndView.addObject("pets", pets); modelAndView.addObject("gcpImageUrl", gcpUrl + imageBucket + "/"); modelAndView.addObject("defaultImage", defaultImage); From 1820ef1eb1fe1da0731462968937eaa190b49461 Mon Sep 17 00:00:00 2001 From: josdem Date: Mon, 21 Oct 2024 09:22:02 -0600 Subject: [PATCH 3/6] #377 Setting release version --- build.gradle | 2 +- src/main/java/com/josdem/vetlog/controller/PetController.java | 2 +- src/main/java/com/josdem/vetlog/controller/VetController.java | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/build.gradle b/build.gradle index b47960b2..a38fc980 100644 --- a/build.gradle +++ b/build.gradle @@ -24,7 +24,7 @@ ext { } group = 'com.josdem.vetlog' -version = '1.9.4' +version = '2.0.0' configurations { compileOnly { diff --git a/src/main/java/com/josdem/vetlog/controller/PetController.java b/src/main/java/com/josdem/vetlog/controller/PetController.java index b6863e90..0e4670e0 100644 --- a/src/main/java/com/josdem/vetlog/controller/PetController.java +++ b/src/main/java/com/josdem/vetlog/controller/PetController.java @@ -186,7 +186,7 @@ private ModelAndView fillPetAndImageUrl(ModelAndView modelAndView) { var user = userService.getCurrentUser(); var pets = petService.getPetsByUser(user); pets.forEach(pet -> { - final List pendingVaccines = vaccinationService.getVaccinationsByPet(pet).stream() + final var pendingVaccines = vaccinationService.getVaccinationsByPet(pet).stream() .filter(vaccination -> vaccination.getStatus().equals(VaccinationStatus.PENDING)) .toList(); pet.setVaccines(pendingVaccines); diff --git a/src/main/java/com/josdem/vetlog/controller/VetController.java b/src/main/java/com/josdem/vetlog/controller/VetController.java index 979aed54..b2c238d0 100644 --- a/src/main/java/com/josdem/vetlog/controller/VetController.java +++ b/src/main/java/com/josdem/vetlog/controller/VetController.java @@ -67,7 +67,7 @@ ModelAndView search(@Valid UsernameCommand command) { var user = userService.getByUsername(command.getUsername()); var pets = petService.getPetsByUser(user); pets.forEach(pet -> { - final List pendingVaccines = vaccinationService.getVaccinationsByPet(pet).stream() + final var pendingVaccines = vaccinationService.getVaccinationsByPet(pet).stream() .filter(vaccination -> vaccination.getStatus().equals(VaccinationStatus.PENDING)) .toList(); pet.setVaccines(pendingVaccines); From b719dece87d14e55bfbc8f0831afa988da2540b8 Mon Sep 17 00:00:00 2001 From: josdem Date: Mon, 21 Oct 2024 09:22:46 -0600 Subject: [PATCH 4/6] #377 Formatting code --- src/main/java/com/josdem/vetlog/controller/PetController.java | 2 -- src/main/java/com/josdem/vetlog/controller/VetController.java | 2 -- 2 files changed, 4 deletions(-) diff --git a/src/main/java/com/josdem/vetlog/controller/PetController.java b/src/main/java/com/josdem/vetlog/controller/PetController.java index 0e4670e0..73ed8854 100644 --- a/src/main/java/com/josdem/vetlog/controller/PetController.java +++ b/src/main/java/com/josdem/vetlog/controller/PetController.java @@ -22,7 +22,6 @@ import com.josdem.vetlog.enums.PetType; import com.josdem.vetlog.enums.VaccinationStatus; import com.josdem.vetlog.model.User; -import com.josdem.vetlog.model.Vaccination; import com.josdem.vetlog.service.BreedService; import com.josdem.vetlog.service.LocaleService; import com.josdem.vetlog.service.PetService; @@ -32,7 +31,6 @@ import jakarta.servlet.http.HttpServletRequest; import jakarta.validation.Valid; import java.io.IOException; -import java.util.List; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; diff --git a/src/main/java/com/josdem/vetlog/controller/VetController.java b/src/main/java/com/josdem/vetlog/controller/VetController.java index b2c238d0..7553d1fd 100644 --- a/src/main/java/com/josdem/vetlog/controller/VetController.java +++ b/src/main/java/com/josdem/vetlog/controller/VetController.java @@ -18,12 +18,10 @@ import com.josdem.vetlog.command.UsernameCommand; import com.josdem.vetlog.enums.VaccinationStatus; -import com.josdem.vetlog.model.Vaccination; import com.josdem.vetlog.service.PetService; import com.josdem.vetlog.service.UserService; import com.josdem.vetlog.service.VaccinationService; import jakarta.validation.Valid; -import java.util.List; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; From ddc3aca0e52a928ee5602d7f58c05619e5a485ec Mon Sep 17 00:00:00 2001 From: josdem Date: Mon, 21 Oct 2024 09:40:27 -0600 Subject: [PATCH 5/6] 377 Refacroting to introduce a method to return vaccines by status --- .../vetlog/controller/PetController.java | 5 +--- .../vetlog/controller/VetController.java | 5 +--- .../vetlog/service/VaccinationService.java | 3 +++ .../service/impl/VaccinationServiceImpl.java | 8 ++++++ .../service/VaccinationServiceTest.java | 27 ++++++++++++------- 5 files changed, 31 insertions(+), 17 deletions(-) diff --git a/src/main/java/com/josdem/vetlog/controller/PetController.java b/src/main/java/com/josdem/vetlog/controller/PetController.java index 73ed8854..0898d129 100644 --- a/src/main/java/com/josdem/vetlog/controller/PetController.java +++ b/src/main/java/com/josdem/vetlog/controller/PetController.java @@ -184,10 +184,7 @@ private ModelAndView fillPetAndImageUrl(ModelAndView modelAndView) { var user = userService.getCurrentUser(); var pets = petService.getPetsByUser(user); pets.forEach(pet -> { - final var pendingVaccines = vaccinationService.getVaccinationsByPet(pet).stream() - .filter(vaccination -> vaccination.getStatus().equals(VaccinationStatus.PENDING)) - .toList(); - pet.setVaccines(pendingVaccines); + pet.setVaccines(vaccinationService.getVaccinesByStatus(pet, VaccinationStatus.PENDING)); }); modelAndView.addObject("pets", pets); modelAndView.addObject(GCP_IMAGE_URL, gcpUrl + imageBucket + "/"); diff --git a/src/main/java/com/josdem/vetlog/controller/VetController.java b/src/main/java/com/josdem/vetlog/controller/VetController.java index 7553d1fd..e57a8eb9 100644 --- a/src/main/java/com/josdem/vetlog/controller/VetController.java +++ b/src/main/java/com/josdem/vetlog/controller/VetController.java @@ -65,10 +65,7 @@ ModelAndView search(@Valid UsernameCommand command) { var user = userService.getByUsername(command.getUsername()); var pets = petService.getPetsByUser(user); pets.forEach(pet -> { - final var pendingVaccines = vaccinationService.getVaccinationsByPet(pet).stream() - .filter(vaccination -> vaccination.getStatus().equals(VaccinationStatus.PENDING)) - .toList(); - pet.setVaccines(pendingVaccines); + pet.setVaccines(vaccinationService.getVaccinesByStatus(pet, VaccinationStatus.PENDING)); }); modelAndView.addObject("pets", pets); modelAndView.addObject("gcpImageUrl", gcpUrl + imageBucket + "/"); diff --git a/src/main/java/com/josdem/vetlog/service/VaccinationService.java b/src/main/java/com/josdem/vetlog/service/VaccinationService.java index 14faee3b..637c3617 100644 --- a/src/main/java/com/josdem/vetlog/service/VaccinationService.java +++ b/src/main/java/com/josdem/vetlog/service/VaccinationService.java @@ -16,6 +16,7 @@ package com.josdem.vetlog.service; +import com.josdem.vetlog.enums.VaccinationStatus; import com.josdem.vetlog.model.Pet; import com.josdem.vetlog.model.Vaccination; import java.util.List; @@ -24,4 +25,6 @@ public interface VaccinationService { void save(Pet pet); List getVaccinationsByPet(Pet pet); + + List getVaccinesByStatus(Pet pet, VaccinationStatus status); } diff --git a/src/main/java/com/josdem/vetlog/service/impl/VaccinationServiceImpl.java b/src/main/java/com/josdem/vetlog/service/impl/VaccinationServiceImpl.java index 07941c7d..12fd0dc6 100644 --- a/src/main/java/com/josdem/vetlog/service/impl/VaccinationServiceImpl.java +++ b/src/main/java/com/josdem/vetlog/service/impl/VaccinationServiceImpl.java @@ -17,6 +17,7 @@ package com.josdem.vetlog.service.impl; import com.josdem.vetlog.enums.PetType; +import com.josdem.vetlog.enums.VaccinationStatus; import com.josdem.vetlog.exception.BusinessException; import com.josdem.vetlog.model.Pet; import com.josdem.vetlog.model.Vaccination; @@ -54,4 +55,11 @@ public void save(Pet pet) { public List getVaccinationsByPet(Pet pet) { return vaccinationRepository.findAllByPet(pet); } + + @Override + public List getVaccinesByStatus(Pet pet, VaccinationStatus status) { + return getVaccinationsByPet(pet).stream() + .filter(vaccination -> vaccination.getStatus().equals(status)) + .toList(); + } } diff --git a/src/test/java/com/josdem/vetlog/service/VaccinationServiceTest.java b/src/test/java/com/josdem/vetlog/service/VaccinationServiceTest.java index 418b1430..00dca58f 100644 --- a/src/test/java/com/josdem/vetlog/service/VaccinationServiceTest.java +++ b/src/test/java/com/josdem/vetlog/service/VaccinationServiceTest.java @@ -16,11 +16,13 @@ package com.josdem.vetlog.service; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.never; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; import com.josdem.vetlog.enums.PetType; import com.josdem.vetlog.enums.VaccinationStatus; @@ -36,6 +38,7 @@ import java.time.LocalDate; import java.time.LocalDateTime; import java.util.HashMap; +import java.util.List; import java.util.Map; import lombok.extern.slf4j.Slf4j; import org.junit.jupiter.api.BeforeEach; @@ -55,22 +58,16 @@ class VaccinationServiceTest { @Mock private VaccinationRepository vaccinationRepository; - private DogVaccinationStrategy dogVaccinationStrategy; - - private CatVaccinationStrategy catVaccinationStrategy; - - private Map vaccinationStrategies; - private final Pet pet = new Pet(); @BeforeEach void setup() { MockitoAnnotations.openMocks(this); - dogVaccinationStrategy = new DogVaccinationStrategy(vaccinationRepository); - catVaccinationStrategy = new CatVaccinationStrategy(vaccinationRepository); + DogVaccinationStrategy dogVaccinationStrategy = new DogVaccinationStrategy(vaccinationRepository); + CatVaccinationStrategy catVaccinationStrategy = new CatVaccinationStrategy(vaccinationRepository); - vaccinationStrategies = new HashMap<>(); + Map vaccinationStrategies = new HashMap<>(); vaccinationStrategies.put(PetType.DOG, dogVaccinationStrategy); vaccinationStrategies.put(PetType.CAT, catVaccinationStrategy); @@ -108,4 +105,16 @@ void shouldNotSaveVaccinationDueToNotOldEnough(TestInfo testInfo) { verify(vaccinationRepository, never()) .save(new Vaccination(null, any(), LocalDate.now(), VaccinationStatus.PENDING, pet)); } + + @Test + @DisplayName("getting vaccines in Pending status") + void shouldGetVaccinesInPendingStatus(TestInfo testInfo) { + log.info("Test: {}", testInfo.getDisplayName()); + when(vaccinationRepository.findAllByPet(pet)) + .thenReturn(List.of( + new Vaccination(1L, "DA2PP", LocalDate.now(), VaccinationStatus.PENDING, pet), + new Vaccination(2L, "Deworming", LocalDate.now(), VaccinationStatus.APPLIED, pet))); + final var vaccinesInPendingStatus = vaccinationService.getVaccinesByStatus(pet, VaccinationStatus.PENDING); + assertEquals(1, vaccinesInPendingStatus.size()); + } } From 98aacd39f9d38267fdbf13c398f679c6d7e29f5f Mon Sep 17 00:00:00 2001 From: josdem Date: Mon, 21 Oct 2024 09:45:08 -0600 Subject: [PATCH 6/6] #377 Avoid unused code --- src/main/java/com/josdem/vetlog/controller/PetController.java | 4 +--- src/main/java/com/josdem/vetlog/controller/VetController.java | 4 +--- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/josdem/vetlog/controller/PetController.java b/src/main/java/com/josdem/vetlog/controller/PetController.java index 0898d129..e5e8df7f 100644 --- a/src/main/java/com/josdem/vetlog/controller/PetController.java +++ b/src/main/java/com/josdem/vetlog/controller/PetController.java @@ -183,9 +183,7 @@ public ModelAndView delete(@RequestParam("uuid") String uuid) { private ModelAndView fillPetAndImageUrl(ModelAndView modelAndView) { var user = userService.getCurrentUser(); var pets = petService.getPetsByUser(user); - pets.forEach(pet -> { - pet.setVaccines(vaccinationService.getVaccinesByStatus(pet, VaccinationStatus.PENDING)); - }); + pets.forEach(pet -> pet.setVaccines(vaccinationService.getVaccinesByStatus(pet, VaccinationStatus.PENDING))); modelAndView.addObject("pets", pets); modelAndView.addObject(GCP_IMAGE_URL, gcpUrl + imageBucket + "/"); modelAndView.addObject("defaultImage", defaultImage); diff --git a/src/main/java/com/josdem/vetlog/controller/VetController.java b/src/main/java/com/josdem/vetlog/controller/VetController.java index e57a8eb9..bdbcae6d 100644 --- a/src/main/java/com/josdem/vetlog/controller/VetController.java +++ b/src/main/java/com/josdem/vetlog/controller/VetController.java @@ -64,9 +64,7 @@ ModelAndView search(@Valid UsernameCommand command) { var modelAndView = new ModelAndView("vet/list"); var user = userService.getByUsername(command.getUsername()); var pets = petService.getPetsByUser(user); - pets.forEach(pet -> { - pet.setVaccines(vaccinationService.getVaccinesByStatus(pet, VaccinationStatus.PENDING)); - }); + pets.forEach(pet -> pet.setVaccines(vaccinationService.getVaccinesByStatus(pet, VaccinationStatus.PENDING))); modelAndView.addObject("pets", pets); modelAndView.addObject("gcpImageUrl", gcpUrl + imageBucket + "/"); modelAndView.addObject("defaultImage", defaultImage);