diff --git a/src/main/java/com/production/ehayvanbackendapi/Controllers/PetTypeController.java b/src/main/java/com/production/ehayvanbackendapi/Controllers/PetTypeController.java index 98b34ed..cf91715 100644 --- a/src/main/java/com/production/ehayvanbackendapi/Controllers/PetTypeController.java +++ b/src/main/java/com/production/ehayvanbackendapi/Controllers/PetTypeController.java @@ -6,6 +6,8 @@ import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; +import java.util.List; + @RestController @RequestMapping("/api/pettypes") public class PetTypeController { @@ -26,6 +28,18 @@ public ResponseEntity getPetTypeById(@PathVariable Integer id) { } } + @GetMapping("/all") + public ResponseEntity> getAllPetTypes() { + List response = petTypeService.getAllPetTypes(); + + if (!response.isEmpty()) { + return new ResponseEntity<>(response, HttpStatus.OK); + } else { + return new ResponseEntity<>(HttpStatus.NOT_FOUND); + } + } + + // no other CRUD operation is necessary diff --git a/src/main/java/com/production/ehayvanbackendapi/Services/PetTypeService.java b/src/main/java/com/production/ehayvanbackendapi/Services/PetTypeService.java index 1d373c9..c5cb530 100644 --- a/src/main/java/com/production/ehayvanbackendapi/Services/PetTypeService.java +++ b/src/main/java/com/production/ehayvanbackendapi/Services/PetTypeService.java @@ -7,6 +7,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import java.util.ArrayList; import java.util.List; @Service @@ -22,4 +23,15 @@ public PetTypeDTO getPetTypeById(Integer id) { PetType petType = petTypeRepository.findById(id).orElse(null); return petType != null ? petTypeMapper.convertToDto(petType) : null; } + + public List getAllPetTypes() { + List petTypeList = petTypeRepository.findAll(); + List petTypeDtoList = new ArrayList<>(); + + for (PetType petType : petTypeList) { + petTypeDtoList.add(petTypeMapper.convertToDto(petType)); + } + + return petTypeDtoList; + } }