Skip to content

Commit

Permalink
getAllPetOwners for specific vet is implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
ituitis20-karadagd20 committed Jan 1, 2024
1 parent 87847fa commit 60d046c
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/petowners")
public class PetOwnerController {
Expand All @@ -27,6 +29,17 @@ public ResponseEntity<PetOwnerDTO> getPetOwnerById(@PathVariable Integer id) {
}
}

@GetMapping("/forVet/{vetId}")
public ResponseEntity<List<PetOwnerDTO>> getPetOwnersForVeterinarian(@PathVariable Integer vetId) {
List<PetOwnerDTO> response = petOwnerService.getPetOwnersForVeterinarian(vetId);

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

@PostMapping("/newowner")
public ResponseEntity<PetOwnerDTO> savePetOwner(@RequestBody CreateOrUpdatePetOwnerDTO petOwnerDTO) {
PetOwnerDTO savedPetOwner = petOwnerService.postPetOwner(petOwnerDTO);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
package com.production.ehayvanbackendapi.Repositories;

import com.production.ehayvanbackendapi.Entities.Pet;
import com.production.ehayvanbackendapi.Entities.PetOwner;
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 PetOwnerRepository extends JpaRepository<PetOwner, Integer> {
@Query(value="SELECT *FROM pet_owner WHERE pet_owner.vetid = :vetid",nativeQuery = true)
public Optional<List<PetOwner>> getAllPetsForPetOwner(@Param("vetid") int vetid);
}

Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,20 @@ public PetOwnerService(PetOwnerRepository petOwnerRepository, PetOwnerMapper pet
this.petOwnerMapper = petOwnerMapper;
}

public List<PetOwnerDTO> getPetOwnersForVeterinarian(Integer vetId) {
Optional<List<PetOwner>> petOwnersOptional = petOwnerRepository.getAllPetsForPetOwner(vetId);

if (petOwnersOptional.isPresent()) {
List<PetOwnerDTO> petOwnerDtoList = new ArrayList<>();
for (PetOwner petOwner : petOwnersOptional.get()) {
petOwnerDtoList.add(petOwnerMapper.convertToDto(petOwner));
}
return petOwnerDtoList;
} else {
return null;
}
}

public PetOwnerDTO getPetOwnerById(Integer id) {
Optional<PetOwner> petOwner = petOwnerRepository.findById(id);
// Listeler otomatik olarak mapleniyor. Şimdilik bir problem yok.
Expand All @@ -40,7 +54,6 @@ public PetOwnerDTO getPetOwnerById(Integer id) {
}
// Other service methods for updating, deleting pet owners, etc.

// TODO: getAllPetOwners() fonksiyonu oluşturulacak.

public PetOwnerDTO postPetOwner(CreateOrUpdatePetOwnerDTO petOwnerDTO){
// Map the data transfer object data to real object.
Expand Down

0 comments on commit 60d046c

Please sign in to comment.