From 2c5cf162840792e33be069217187ec0aeb00179f Mon Sep 17 00:00:00 2001 From: Admin Date: Tue, 1 Jul 2025 10:34:40 +0530 Subject: [PATCH] vikram-Ajabe -Software Engineer Assignment --- .../petclinic/owner/PetAttributes.java | 78 +++++++++++ .../owner/PetAttributesController.java | 79 +++++++++++ .../owner/PetAttributesRepository.java | 14 ++ .../petclinic/owner/PetAttributesService.java | 9 ++ .../owner/PetAttributesServiceImpl.java | 29 ++++ .../petclinic/owner/PetRepository.java | 11 ++ src/main/resources/application.properties | 6 + src/main/resources/db/h2/schema.sql | 10 ++ .../resources/messages/messages.properties | 16 +++ .../resources/templates/fragments/layout.html | 129 +++++++++--------- .../messages_ru.properties | 0 .../templates/petAttributes/form.html | 38 ++++++ .../templates/petAttributes/list.html | 48 +++++++ .../templates/petAttributes/petAttribute.html | 57 ++++++++ .../templates/petAttributes/search.html | 45 ++++++ 15 files changed, 507 insertions(+), 62 deletions(-) create mode 100644 src/main/java/org/springframework/samples/petclinic/owner/PetAttributes.java create mode 100644 src/main/java/org/springframework/samples/petclinic/owner/PetAttributesController.java create mode 100644 src/main/java/org/springframework/samples/petclinic/owner/PetAttributesRepository.java create mode 100644 src/main/java/org/springframework/samples/petclinic/owner/PetAttributesService.java create mode 100644 src/main/java/org/springframework/samples/petclinic/owner/PetAttributesServiceImpl.java create mode 100644 src/main/java/org/springframework/samples/petclinic/owner/PetRepository.java rename src/main/resources/{messages => templates}/messages_ru.properties (100%) create mode 100644 src/main/resources/templates/petAttributes/form.html create mode 100644 src/main/resources/templates/petAttributes/list.html create mode 100644 src/main/resources/templates/petAttributes/petAttribute.html create mode 100644 src/main/resources/templates/petAttributes/search.html diff --git a/src/main/java/org/springframework/samples/petclinic/owner/PetAttributes.java b/src/main/java/org/springframework/samples/petclinic/owner/PetAttributes.java new file mode 100644 index 000000000..39792ada5 --- /dev/null +++ b/src/main/java/org/springframework/samples/petclinic/owner/PetAttributes.java @@ -0,0 +1,78 @@ +package org.springframework.samples.petclinic.owner; + + +import org.springframework.samples.petclinic.model.BaseEntity; + +import jakarta.persistence.*; + +@Entity +@Table(name = "pet_attributes") +public class PetAttributes extends BaseEntity { + + private static final long serialVersionUID = 1L; + + @Column(name = "petId", nullable = false, unique = true) + private Long petId; + + @Column(name = "temperament") + private String temperament; + + @Column(name = "length") + private Double length; + + @Column(name = "weight") + private Double weight; + + @Column(name = "energy_level") + private Integer energyLevel; + + // Getters and Setters + + public Long getPetId() { + return petId; + } + + public void setPetId(Long petId) { + this.petId = petId; + } + + 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; + } + + public Integer getEnergyLevel() { + return energyLevel; + } + + public void setEnergyLevel(Integer energyLevel) { + this.energyLevel = energyLevel; + } + + @Override + public String toString() { + return "PetAttributes [petId=" + petId + ", temperament=" + temperament + ", length=" + length + ", weight=" + + weight + ", energyLevel=" + energyLevel + "]"; + } + + +} diff --git a/src/main/java/org/springframework/samples/petclinic/owner/PetAttributesController.java b/src/main/java/org/springframework/samples/petclinic/owner/PetAttributesController.java new file mode 100644 index 000000000..5ecf58403 --- /dev/null +++ b/src/main/java/org/springframework/samples/petclinic/owner/PetAttributesController.java @@ -0,0 +1,79 @@ +package org.springframework.samples.petclinic.owner; + +import java.util.List; + +import org.springframework.beans.factory.annotation.Autowired; +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.web.bind.annotation.*; +import org.springframework.web.servlet.mvc.support.RedirectAttributes; + +@Controller +@RequestMapping("/petAttributes") +public class PetAttributesController { + + private final PetAttributesRepository petAttributesRepository; + + private final PetAttributesService petAttributesService; + + @Autowired + public PetAttributesController(PetAttributesRepository petAttributesRepository,PetAttributesService petAttributesService) { + this.petAttributesRepository = petAttributesRepository; + this.petAttributesService=petAttributesService; + } + + @GetMapping("/all") + public String list(Model model) { + model.addAttribute("listAttributes", petAttributesRepository.findAll()); + return "petAttributes/list"; + } + + @GetMapping("/new") + public String showCreateForm(Model model) { + model.addAttribute("petAttributes", new PetAttributes()); + return "petAttributes/form"; + } + + @PostMapping("/save") + public String save(@ModelAttribute PetAttributes petAttributes, RedirectAttributes redirectAttributes) { + petAttributesRepository.save(petAttributes); + redirectAttributes.addFlashAttribute("successMessage", "Saved successfully!"); + return "redirect:/petAttributes/all"; + } + + @GetMapping("/edit/{id}") + public String showEditForm(@PathVariable("id") Integer id, Model model) { + PetAttributes attr = petAttributesRepository.findById(id) + .orElseThrow(() -> new IllegalArgumentException("Invalid ID: " + id)); + model.addAttribute("petAttributes", attr); + return "petAttributes/form"; + } + @GetMapping("/search") + public String showSearchForm() { + return "petAttributes/search"; + } + + @GetMapping(value = "/search", params = "petId") + public String searchByPetId(@RequestParam("petId") Integer petId, Model model) { + PetAttributes result = petAttributesService.findByPetId(petId); + if (result != null) { + model.addAttribute("listAttributes", List.of(result)); + } else { + model.addAttribute("listAttributes", List.of()); + model.addAttribute("error", "No results for Pet ID: " + petId); + } + return "petAttributes/search"; + } + + + @PostMapping("/delete/{id}") + public String delete(@PathVariable("id") Integer id, RedirectAttributes redirectAttributes) { + petAttributesRepository.deleteById(id); + redirectAttributes.addFlashAttribute("successMessage", "Deleted successfully!"); + return "redirect:/petAttributes/all"; + } + +} diff --git a/src/main/java/org/springframework/samples/petclinic/owner/PetAttributesRepository.java b/src/main/java/org/springframework/samples/petclinic/owner/PetAttributesRepository.java new file mode 100644 index 000000000..531b48ff3 --- /dev/null +++ b/src/main/java/org/springframework/samples/petclinic/owner/PetAttributesRepository.java @@ -0,0 +1,14 @@ +package org.springframework.samples.petclinic.owner; + +import java.util.List; + +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.repository.CrudRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface PetAttributesRepository extends JpaRepository { + + PetAttributes findByPetId(int petId); + +} diff --git a/src/main/java/org/springframework/samples/petclinic/owner/PetAttributesService.java b/src/main/java/org/springframework/samples/petclinic/owner/PetAttributesService.java new file mode 100644 index 000000000..c53b7298d --- /dev/null +++ b/src/main/java/org/springframework/samples/petclinic/owner/PetAttributesService.java @@ -0,0 +1,9 @@ +package org.springframework.samples.petclinic.owner; + +public interface PetAttributesService { + + PetAttributes save(PetAttributes attributes); + + PetAttributes findByPetId(int petId); + +} \ No newline at end of file diff --git a/src/main/java/org/springframework/samples/petclinic/owner/PetAttributesServiceImpl.java b/src/main/java/org/springframework/samples/petclinic/owner/PetAttributesServiceImpl.java new file mode 100644 index 000000000..923e8204a --- /dev/null +++ b/src/main/java/org/springframework/samples/petclinic/owner/PetAttributesServiceImpl.java @@ -0,0 +1,29 @@ +package org.springframework.samples.petclinic.owner; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +@Service +public class PetAttributesServiceImpl implements PetAttributesService { + + private final PetAttributesRepository petAttributesRepository; + + @Autowired + public PetAttributesServiceImpl(PetAttributesRepository repository) { + this.petAttributesRepository = repository; + } + + @Override + @Transactional + public PetAttributes save(PetAttributes attributes) { + return petAttributesRepository.save(attributes); + } + + @Override + @Transactional(readOnly = true) + public PetAttributes findByPetId(int petId) { + return petAttributesRepository.findByPetId(petId); + } + +} diff --git a/src/main/java/org/springframework/samples/petclinic/owner/PetRepository.java b/src/main/java/org/springframework/samples/petclinic/owner/PetRepository.java new file mode 100644 index 000000000..f25567f08 --- /dev/null +++ b/src/main/java/org/springframework/samples/petclinic/owner/PetRepository.java @@ -0,0 +1,11 @@ +package org.springframework.samples.petclinic.owner; + +import org.springframework.data.repository.CrudRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface PetRepository extends CrudRepository { + + Pet findById(int id); + +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 6ed985654..a2825283d 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,4 +1,5 @@ # database init, supports mysql too + database=h2 spring.sql.init.schema-locations=classpath*:db/${database}/schema.sql spring.sql.init.data-locations=classpath*:db/${database}/data.sql @@ -23,3 +24,8 @@ logging.level.org.springframework=INFO # Maximum time static resources should be cached spring.web.resources.cache.cachecontrol.max-age=12h + +spring.h2.console.enabled=true + +# Optional: Change the console path (default is /h2-console) +spring.h2.console.path=/h2-console diff --git a/src/main/resources/db/h2/schema.sql b/src/main/resources/db/h2/schema.sql index 4a6c322cb..ee4d9d93e 100644 --- a/src/main/resources/db/h2/schema.sql +++ b/src/main/resources/db/h2/schema.sql @@ -62,3 +62,13 @@ 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 NOT NULL, + temperament VARCHAR(255), + length INTEGER, + weight INTEGER, + energy_level INTEGER +); + diff --git a/src/main/resources/messages/messages.properties b/src/main/resources/messages/messages.properties index 7b8b5dfd8..accd13b63 100644 --- a/src/main/resources/messages/messages.properties +++ b/src/main/resources/messages/messages.properties @@ -46,3 +46,19 @@ visitDate=Visit Date editOwner=Edit Owner addNewPet=Add New Pet petsAndVisits=Pets and Visits +petTypes=Pet Types +addPetAttributes=Add Pet Attributes +save=Save +petTypeAdded=Pet Type Added Successfully. +pet.attributes=Pet Attributes +pet.id=Pet ID +temperament=Temperament +length=Length +weight=Weight +energy.level=Energy Level +pages=Pages +petAttributes=Pet Attributes + + + + diff --git a/src/main/resources/templates/fragments/layout.html b/src/main/resources/templates/fragments/layout.html index 7c5cd0d86..f7161eeea 100644 --- a/src/main/resources/templates/fragments/layout.html +++ b/src/main/resources/templates/fragments/layout.html @@ -4,84 +4,89 @@ - - - - - - PetClinic :: a Spring Framework demonstration - - + + + + + + PetClinic :: a Spring Framework demonstration + + - -
-
+ - +
+
+
+
+
+ +
+
+
+
+
-
-
-
-
-
- -
-
-
- - - - + diff --git a/src/main/resources/messages/messages_ru.properties b/src/main/resources/templates/messages_ru.properties similarity index 100% rename from src/main/resources/messages/messages_ru.properties rename to src/main/resources/templates/messages_ru.properties diff --git a/src/main/resources/templates/petAttributes/form.html b/src/main/resources/templates/petAttributes/form.html new file mode 100644 index 000000000..cbc3d8fa2 --- /dev/null +++ b/src/main/resources/templates/petAttributes/form.html @@ -0,0 +1,38 @@ + + + +

Pet Attributes

+ +
+ + + + + + + + + + +
+
+ + + +
+ + +
+ + +
+ + +
+ + + Cancel +
+ + diff --git a/src/main/resources/templates/petAttributes/list.html b/src/main/resources/templates/petAttributes/list.html new file mode 100644 index 000000000..46557013d --- /dev/null +++ b/src/main/resources/templates/petAttributes/list.html @@ -0,0 +1,48 @@ + + + +

Pet Attributes

+ + Add New + Search by Pet ID + + + + + + + + + + + + + + + + + + + + + + + + + +
Pet IDTemperamentLengthWeightEnergy LevelActions
100Calm25.010.04 + Edit | + + +
+ +
+
+ +
+ + diff --git a/src/main/resources/templates/petAttributes/petAttribute.html b/src/main/resources/templates/petAttributes/petAttribute.html new file mode 100644 index 000000000..e40fd654e --- /dev/null +++ b/src/main/resources/templates/petAttributes/petAttribute.html @@ -0,0 +1,57 @@ + + + + + + +

Veterinarians

+ + + + + + + + + + + + + + +
NameSpecialties
+ none +
+
+ Pages: + [ + + [[${i}]] + [[${i}]] + + + + + + + + + + + + + + + + + + +
+ + + \ No newline at end of file diff --git a/src/main/resources/templates/petAttributes/search.html b/src/main/resources/templates/petAttributes/search.html new file mode 100644 index 000000000..fcb70fed5 --- /dev/null +++ b/src/main/resources/templates/petAttributes/search.html @@ -0,0 +1,45 @@ + + + + +

Search by Pet ID

+ +
+
+ + +
+ +
+ +
+ +
+

Results

+ + + + + + + + + + + + + + + + + + + + + +
Pet IDTemperamentLengthWeightEnergy Level
+
+
+