Skip to content

Commit

Permalink
get all pets for specific pet owner operation is implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
ituitis20-karadagd20 committed Jan 1, 2024
1 parent 16ca7a6 commit 87847fa
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/api/pets")
Expand All @@ -29,12 +30,17 @@ public ResponseEntity<PetDTO> getPetById(@PathVariable Integer id) {
}
}

// @GetMapping
// public ResponseEntity<List<PetDTO>> getAllPets() {
// List<PetDTO> petDTOList = petService.getAllPets();
// return new ResponseEntity<>(petDTOList, HttpStatus.OK);
// }
//
@GetMapping("/all/{petOwnerId}")
public ResponseEntity<List<PetDTO>> getAllPetsForPetOwner(@PathVariable Integer petOwnerId) {
List<PetDTO> response = petService.getAllPetsForPetOwner(petOwnerId);

if (!response.isEmpty()) {
return new ResponseEntity<>(response, HttpStatus.OK);
} else {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
}

@PostMapping
public ResponseEntity<PetDTO> savePet(@RequestBody CreateOrUpdatePetDTO petDTO) {
PetDTO savedPet = petService.postPet(petDTO);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
package com.production.ehayvanbackendapi.Repositories;

import com.production.ehayvanbackendapi.Entities.Appointment;
import com.production.ehayvanbackendapi.Entities.Pet;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

@Repository
public interface PetRepository extends JpaRepository<Pet, Integer> {
@Query(value="SELECT *FROM Pet WHERE Pet.pet_ownerid = :pet_ownerid",nativeQuery = true)
public Optional<List<Pet>> getAllPetsForPetOwner(@Param("pet_ownerid") int pet_ownerid);
}
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,21 @@ public PetDTO deletePet(Integer id){
return null;
}

// public List<PetDTO> getAllPets() {
// List<Pet> pets = petRepository.findAll();
// return petMapper.mapList(pets, PetDTO.class);
// }
public List<PetDTO> getAllPetsForPetOwner(Integer petOwnerId) {
Optional<List<Pet>> petsOptional = petRepository.getAllPetsForPetOwner(petOwnerId);

if (petsOptional.isPresent()) {
List<PetDTO> petDtoList = new ArrayList<>();
for (Pet pet : petsOptional.get()) {
petDtoList.add(petMapper.convertToDto(pet));
}
return petDtoList;
} else {
return null;
}
}



// Other service methods
}

0 comments on commit 87847fa

Please sign in to comment.