Skip to content

Commit

Permalink
feat: Create PetService class
Browse files Browse the repository at this point in the history
  • Loading branch information
PabloAsencio committed Nov 7, 2021
1 parent 0f3228a commit d79e9c1
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/main/java/com/udacity/jdnd/course3/critter/pet/PetService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.udacity.jdnd.course3.critter.pet;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class PetService {

private final PetRepository petRepository;

public PetService(PetRepository petRepository) {
this.petRepository = petRepository;
}

public Pet savePet(Pet pet) {
return petRepository.save(pet);
}

public Pet getPetById(Long petId) {
return petRepository.findById(petId).orElseThrow(RuntimeException::new);
}

public List<Pet> getAllPets() {
List<Pet> pets = new ArrayList<>();
petRepository.findAll().forEach(pets::add);
return pets;
}

public List<Pet> getPetsByOwner(Long ownerId) {
return petRepository.findAllByOwnerId(ownerId);
}
}

0 comments on commit d79e9c1

Please sign in to comment.