Commit 0c8dcb6b authored by earndt's avatar earndt

[W5D5] (ArndtED) Adds methods for finding address by id or email

parent 97379c3e
package service.student.studentaddressservice.repository; package service.student.studentaddressservice.repository;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
import service.student.studentaddressservice.model.StudentAddress; import service.student.studentaddressservice.model.StudentAddress;
@Repository @Repository
public interface StudentAddressRepository extends JpaRepository<StudentAddress, Long> { public interface StudentAddressRepository extends JpaRepository<StudentAddress, Long> {
@Query("select u from StudentAddress u where u.email = ?1")
StudentAddress findByEmail(String email);
} }
package service.student.studentaddressservice.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import service.student.studentaddressservice.model.StudentAddress;
import service.student.studentaddressservice.repository.StudentAddressRepository;
import java.util.Optional;
@Service
public class StudentAddressService {
@Autowired
StudentAddressRepository studentAddressRepository;
public Optional<StudentAddress> getStudentAddressById(long addressId){
return studentAddressRepository.findById(addressId);
}
public Optional<StudentAddress> getStudentAddressByEmail(String email){
return Optional.ofNullable(studentAddressRepository.findByEmail(email));
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment