Skip to content

Commit

Permalink
Listing all pet type is implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
ituitis20-karadagd20 committed Jan 1, 2024
1 parent 3f201d1 commit 10ad072
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -26,6 +28,18 @@ public ResponseEntity<PetTypeDTO> getPetTypeById(@PathVariable Integer id) {
}
}

@GetMapping("/all")
public ResponseEntity<List<PetTypeDTO>> getAllPetTypes() {
List<PetTypeDTO> 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


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<PetTypeDTO> getAllPetTypes() {
List<PetType> petTypeList = petTypeRepository.findAll();
List<PetTypeDTO> petTypeDtoList = new ArrayList<>();

for (PetType petType : petTypeList) {
petTypeDtoList.add(petTypeMapper.convertToDto(petType));
}

return petTypeDtoList;
}
}

0 comments on commit 10ad072

Please sign in to comment.