2023.12.16 - [Spring/[PROJECT] 도서 대여 프로그램] - 💛 책 대출 등록하기 💛
💛 책 대출 등록하기 💛
Controller → Service → Repository 🔖 Controller ☑️ 책의 대출 정보가 담긴 데이터 (addRentDto) 가 들어온다 ☑️ 책이 이미 대출 중인지, 대출 가능한 상태인지 확인한다 (BookStatus) ☑️ 대출이 가능하면
alsrudalsrudalsrud.tistory.com
📗 Controller
☑️ 반납하고자 하는 책의 ID 를 입력받는다
☑️ Rent 테이블에서 삭제하는 service 메서드를 호출한다 (deleteRent)
...
public class RentController {
private final RentService rentService;
...
// 책 반납
@DeleteMapping("/{id}")
public void deleteRent(@PathVariable("id") Long id) {
rentService.deleteRent(id);
}
}
📗 Service
☑️ 대출했던 책의 상태를 다시 변경한다 : UNAVAILABLE → AVAILABLE
☑️ Rent 테이블에서 해당 ID 를 가진 레코드를 삭제한다
...
public class RentService {
private final RentRepository rentRepository;
private final MemberRepository memberRepository;
private final BookRepository bookRepository;
...
// 책 반납
public void deleteRent(Long id) {
// BookStatus 변경 : UNAVAILABLE -> AVAILABLE
Rent rent = rentRepository.findById(id).orElseThrow(RuntimeException::new);
Book book = bookRepository.findById(rent.getBook().getId()).orElseThrow(RuntimeException::new);
book.changeBookStatus(BookStatus.AVAILABLE);
// rent table 에서 해당 id 삭제
rentRepository.deleteById(id);
}
}
📗 Repository
☑️ findById, deleteById 메서드를 호출하기 위해 JpaRepository 인터페이스를 상속받는다
...
public interface RentRepository extends JpaRepository<Rent, Long> {}
💡TEST💡
· Postman

'Spring > [P] 도서 대여 프로그램' 카테고리의 다른 글
🩷 좋아요 기능 (추가, 취소) 🩷 (0) | 2023.12.16 |
---|---|
💛 대출한 책 조회하기 💛 (0) | 2023.12.16 |
💛 책 대출 등록하기 💛 (0) | 2023.12.16 |
💚 등록된 책 조회하기 (전체 조회, 선택 조회) 💚 (0) | 2023.12.16 |
💚 책 등록하기 💚 (0) | 2023.12.12 |