diff --git a/src/main/java/org/springframework/samples/petclinic/owner/Pet.java b/src/main/java/org/springframework/samples/petclinic/owner/Pet.java index 99ca428dc..1945f9b67 100644 --- a/src/main/java/org/springframework/samples/petclinic/owner/Pet.java +++ b/src/main/java/org/springframework/samples/petclinic/owner/Pet.java @@ -30,7 +30,6 @@ import jakarta.persistence.FetchType; import jakarta.persistence.JoinColumn; import jakarta.persistence.ManyToOne; import jakarta.persistence.OneToMany; -import jakarta.persistence.OneToOne; import jakarta.persistence.OrderBy; import jakarta.persistence.Table; @@ -58,21 +57,6 @@ public class Pet extends NamedEntity { @JoinColumn(name = "pet_id") @OrderBy("date ASC") private final Set visits = new LinkedHashSet<>(); - - @OneToOne(mappedBy ="pet",cascade = CascadeType.ALL) - private PetAttribute petAttribute; - - @ManyToOne(fetch=FetchType.LAZY) - @JoinColumn(name="owner_id") - private Owner owner; - - public Owner getOwner() { - return owner; - } - - public void setOwner(Owner owner) { - this.owner = owner; - } public void setBirthDate(LocalDate birthDate) { this.birthDate = birthDate; @@ -98,13 +82,4 @@ public class Pet extends NamedEntity { getVisits().add(visit); } - public PetAttribute getPetAttribute() { - return petAttribute; - } - - public void setPetAttribute(PetAttribute petAttribute) { - this.petAttribute = petAttribute; - } - - } diff --git a/src/main/java/org/springframework/samples/petclinic/owner/PetAttribute.java b/src/main/java/org/springframework/samples/petclinic/owner/PetAttribute.java deleted file mode 100644 index b6a82a38d..000000000 --- a/src/main/java/org/springframework/samples/petclinic/owner/PetAttribute.java +++ /dev/null @@ -1,60 +0,0 @@ -package org.springframework.samples.petclinic.owner; - -import org.springframework.samples.petclinic.model.BaseEntity; - -import jakarta.persistence.Entity; -import jakarta.persistence.JoinColumn; -import jakarta.persistence.OneToOne; -import jakarta.persistence.Table; - -@Entity -@Table (name="pet_attributes") -public class PetAttribute extends BaseEntity{ - - - @OneToOne - @JoinColumn(name="pet_id") - private Pet pet; - - //@NotBlank(message="Temperament is required") - private String temperament; - - private Double length; - - private Double weight; - - public Pet getPet() { - return pet; - } - - public void setPet(Pet pet) { - this.pet = pet; - } - - public String getTemperament() { - return temperament; - } - - public void setTemperament(String temperament) { - this.temperament = temperament; - } - - public Double getLength() { - return length; - } - - public void setLength(Double length) { - this.length = length; - } - - public Double getWeight() { - return weight; - } - - public void setWeight(Double weight) { - this.weight = weight; - } - - - -} diff --git a/src/main/java/org/springframework/samples/petclinic/owner/PetController.java b/src/main/java/org/springframework/samples/petclinic/owner/PetController.java index f1cd4ca78..56707d99f 100644 --- a/src/main/java/org/springframework/samples/petclinic/owner/PetController.java +++ b/src/main/java/org/springframework/samples/petclinic/owner/PetController.java @@ -95,15 +95,12 @@ class PetController { @GetMapping("/pets/new") public String initCreationForm(Owner owner, ModelMap model) { Pet pet = new Pet(); - PetAttribute petAttribute= new PetAttribute(); - pet.setPetAttribute(petAttribute); - pet.getPetAttribute().setPet(pet); owner.addPet(pet); return VIEWS_PETS_CREATE_OR_UPDATE_FORM; } @PostMapping("/pets/new") - public String processCreationForm(Owner owner, @Valid Pet pet,BindingResult result, + public String processCreationForm(Owner owner, @Valid Pet pet, BindingResult result, RedirectAttributes redirectAttributes) { if (StringUtils.hasText(pet.getName()) && pet.isNew() && owner.getPet(pet.getName(), true) != null) @@ -119,7 +116,6 @@ class PetController { } owner.addPet(pet); - pet.getPetAttribute().setPet(pet); this.owners.save(owner); redirectAttributes.addFlashAttribute("message", "New Pet has been Added"); return "redirect:/owners/{ownerId}"; diff --git a/src/main/java/org/springframework/samples/petclinic/owner/PetViewController.java b/src/main/java/org/springframework/samples/petclinic/owner/PetViewController.java deleted file mode 100644 index aa29574c9..000000000 --- a/src/main/java/org/springframework/samples/petclinic/owner/PetViewController.java +++ /dev/null @@ -1,92 +0,0 @@ -package org.springframework.samples.petclinic.owner; - -import java.util.List; -import java.util.Optional; - -import org.springframework.data.domain.Page; -import org.springframework.data.domain.PageRequest; -import org.springframework.data.domain.Pageable; -import org.springframework.stereotype.Controller; -import org.springframework.ui.Model; -import org.springframework.validation.BindingResult; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.ModelAttribute; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.servlet.ModelAndView; - -@Controller -public class PetViewController { - - private final PetViewRepository petViewRepository; - - public PetViewController(PetViewRepository petViewRepository) { - this.petViewRepository = petViewRepository; - } - - @ModelAttribute("pet") - public Pet findPet(@PathVariable(name = "petId", required = false) Integer petId) { - return petId == null ? new Pet() - : this.petViewRepository.findById(petId) - .orElseThrow(() -> new IllegalArgumentException("Pet not found with id: " + petId - + ". Please ensure the ID is correct " + "and the pet exists in the database.")); - } - - @GetMapping("/pets/find") - public String initFindForm() { - return "pets/findPets"; - } - - @GetMapping("/pets") - public String processFindForm(@RequestParam(defaultValue = "1") int page, Pet pet, BindingResult result, - Model model) { - // allow parameterless GET request for /pets to return all records - if (pet.getName() == null) { - pet.setName(""); // empty string signifies broadest possible search - } - - // find owners by last name - Page petsResults = findPaginatedForPetsName(page, pet.getName()); - if (petsResults.isEmpty()) { - // no owners found - result.rejectValue("Name", "notFound", "not found"); - return "pets/findPets"; - } - - if (petsResults.getTotalElements() == 1) { - // 1 owner found - pet = petsResults.iterator().next(); - return "redirect:/pets/" + pet.getId(); - } - - // multiple owners found - return addPaginationModel(page, model, petsResults); - } - - private String addPaginationModel(int page, Model model, Page paginated) { - List listPets = paginated.getContent(); - model.addAttribute("currentPage", page); - model.addAttribute("totalPages", paginated.getTotalPages()); - model.addAttribute("totalItems", paginated.getTotalElements()); - model.addAttribute("listPets", listPets); - return "pets/petsList"; - } - private Page findPaginatedForPetsName(int page, String name) { - int pageSize = 5; - Pageable pageable = PageRequest.of(page - 1, pageSize); - return petViewRepository.findByNameStartingWith(name, pageable); - } - - @GetMapping("/pets/{petId}") - public ModelAndView showPet(@PathVariable("petId") int petId) { - ModelAndView mav = new ModelAndView("pets/petDetails"); - Optional optionalPet = this.petViewRepository.findPetByWithOwnerAndAttribute(petId); - Pet pet = optionalPet.orElseThrow(() -> new IllegalArgumentException( - "Pet not found with id: " + petId + ". Please ensure the ID is correct ")); - - mav.addObject(pet); - mav.addObject(pet.getOwner()); - //mav.addObject(pet.getPetAttribute()); - return mav; - } -} diff --git a/src/main/java/org/springframework/samples/petclinic/owner/PetViewRepository.java b/src/main/java/org/springframework/samples/petclinic/owner/PetViewRepository.java deleted file mode 100644 index 2068fb175..000000000 --- a/src/main/java/org/springframework/samples/petclinic/owner/PetViewRepository.java +++ /dev/null @@ -1,18 +0,0 @@ -package org.springframework.samples.petclinic.owner; - -import java.util.Optional; - -import org.springframework.data.domain.Page; -import org.springframework.data.domain.Pageable; -import org.springframework.data.jpa.repository.JpaRepository; -import org.springframework.data.jpa.repository.Query; -import org.springframework.data.repository.query.Param; - -public interface PetViewRepository extends JpaRepository{ - - Page findByNameStartingWith(String name, Pageable pageable); - - - @Query("SELECT p from Pet p LEFT JOIN FETCH p.owner LEFT JOIN FETCH p.petAttribute WHERE p.id= :id") - Optional findPetByWithOwnerAndAttribute(@Param("id") int id); -} diff --git a/src/main/resources/db/h2/data.sql b/src/main/resources/db/h2/data.sql index 8aa135879..f232b1361 100644 --- a/src/main/resources/db/h2/data.sql +++ b/src/main/resources/db/h2/data.sql @@ -51,8 +51,3 @@ INSERT INTO visits VALUES (default, 7, '2013-01-01', 'rabies shot'); INSERT INTO visits VALUES (default, 8, '2013-01-02', 'rabies shot'); INSERT INTO visits VALUES (default, 8, '2013-01-03', 'neutered'); INSERT INTO visits VALUES (default, 7, '2013-01-04', 'spayed'); - -INSERT INTO pet_attributes VALUES (default,2,'Friendly',35.5,12.0); -INSERT INTO pet_attributes VALUES (default,9,'social',25.5,10.0); - - diff --git a/src/main/resources/db/h2/schema.sql b/src/main/resources/db/h2/schema.sql index a3a6c0b52..4a6c322cb 100644 --- a/src/main/resources/db/h2/schema.sql +++ b/src/main/resources/db/h2/schema.sql @@ -5,7 +5,6 @@ DROP TABLE visits IF EXISTS; DROP TABLE pets IF EXISTS; DROP TABLE types IF EXISTS; DROP TABLE owners IF EXISTS; -DROP TABLE pet_attributes IF EXISTS; CREATE TABLE vets ( @@ -63,13 +62,3 @@ CREATE TABLE visits ( ); ALTER TABLE visits ADD CONSTRAINT fk_visits_pets FOREIGN KEY (pet_id) REFERENCES pets (id); CREATE INDEX visits_pet_id ON visits (pet_id); - -CREATE TABLE pet_attributes( - id INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, - pet_id INTEGER, - temperament VARCHAR(255), - length DOUBLE, - weight DOUBLE -); -ALTER TABLE pet_attributes ADD CONSTRAINT fk_pets_attribute FOREIGN KEY (pet_id) REFERENCES pets (id); - diff --git a/src/main/resources/messages/messages.properties b/src/main/resources/messages/messages.properties index b747bec1c..7b8b5dfd8 100644 --- a/src/main/resources/messages/messages.properties +++ b/src/main/resources/messages/messages.properties @@ -16,8 +16,6 @@ owners=Owners addOwner=Add Owner findOwner=Find Owner findOwners=Find Owners -findPet=Find Pet -findPets=Find Pets updateOwner=Update Owner vets=Veterinarians name=Name @@ -44,11 +42,7 @@ new=New addVisit=Add Visit editPet=Edit Pet ownerInformation=Owner Information -petInformation=Pet Information visitDate=Visit Date editOwner=Edit Owner addNewPet=Add New Pet petsAndVisits=Pets and Visits -petAttributeWeight=Weight -petAttributeLength=Length -petAttributeTemp=Temperament diff --git a/src/main/resources/templates/fragments/layout.html b/src/main/resources/templates/fragments/layout.html index 2a74f5d6f..7c5cd0d86 100644 --- a/src/main/resources/templates/fragments/layout.html +++ b/src/main/resources/templates/fragments/layout.html @@ -48,11 +48,6 @@ Find owners -
  • - - Find pets -
  • -
  • Veterinarians diff --git a/src/main/resources/templates/owners/ownerDetails.html b/src/main/resources/templates/owners/ownerDetails.html index c7c2c781f..cc175cd13 100644 --- a/src/main/resources/templates/owners/ownerDetails.html +++ b/src/main/resources/templates/owners/ownerDetails.html @@ -54,12 +54,6 @@
    Type
    -
    Temperament
    -
    -
    Length
    -
    -
    Weight
    -
    diff --git a/src/main/resources/templates/pets/createOrUpdatePetForm.html b/src/main/resources/templates/pets/createOrUpdatePetForm.html index 51318d8c5..396963d41 100644 --- a/src/main/resources/templates/pets/createOrUpdatePetForm.html +++ b/src/main/resources/templates/pets/createOrUpdatePetForm.html @@ -20,9 +20,6 @@ - - -
    diff --git a/src/main/resources/templates/pets/findPets.html b/src/main/resources/templates/pets/findPets.html deleted file mode 100644 index 1625022d0..000000000 --- a/src/main/resources/templates/pets/findPets.html +++ /dev/null @@ -1,35 +0,0 @@ - - - - - - -

    Find Pets

    - -
    -
    -
    - -
    - - -
    -

    Error

    -
    -
    -
    -
    -
    -
    -
    - -
    -
    - - -
    - - - - - \ No newline at end of file diff --git a/src/main/resources/templates/pets/petDetails.html b/src/main/resources/templates/pets/petDetails.html deleted file mode 100644 index 2dfeba5ae..000000000 --- a/src/main/resources/templates/pets/petDetails.html +++ /dev/null @@ -1,68 +0,0 @@ - - - - - - -

    Pet Information

    - -
    - -
    - -
    - -
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    Name
    Birth Date
    Type
    Temperament
    Length
    Weight
    Owner
    - -
    -
    - - - - - - - \ No newline at end of file diff --git a/src/main/resources/templates/pets/petsList.html b/src/main/resources/templates/pets/petsList.html deleted file mode 100644 index bf69978eb..000000000 --- a/src/main/resources/templates/pets/petsList.html +++ /dev/null @@ -1,63 +0,0 @@ - - - - - - -

    Pets

    - - - - - - - - - - - - - - - - - -
    NameBirth DateTypeTemperamentLengthWeight
    - - - - - - -
    -
    - Pages: - [ - - [[${i}]] - [[${i}]] - - - - - - - - - - - - - - - - - - -
    - - - \ No newline at end of file