게시글 데이터를 데이터베이스에서 삭제
☃️ Controller
@RestController
@RequiredArgsConstructor
@Slf4j
@RequestMapping("/volunteers")
public class VolunteerController {
private final VolunteerService volunteerService;
// 봉사활동 구인글 삭제
@DeleteMapping("/delete/{volunteerId}")
public ResponseEntity<Void> deleteVolunteer(@PathVariable("volunteerId") Long volunteerId) {
volunteerService.deleteVolunteer(volunteerId);
return new ResponseEntity<>(HttpStatus.OK);
}
}
· 삭제할 게시글의 id (volunteerId) 를 PathVariable 로 받기
☃️ Service
@Service
@RequiredArgsConstructor
@Slf4j
@Transactional
public class VolunteerService {
private final VolunteerRepository volunteerRepository;
// 봉사활동 구인글 삭제
public void deleteVolunteer(Long volunteerId) {
getVolunteerOrThrow(volunteerId);
volunteerRepository.deleteById(volunteerId);
}
// 예외 처리 - 존재하는 봉사활동 구인글인지
private Volunteer getVolunteerOrThrow(Long volunteerId) {
return volunteerRepository.findById(volunteerId)
.orElseThrow(() -> new CustomException(ErrorCode.NOT_FOUND_VOLUNTEER));
}
}
· jparepository 기능으로 데이터 삭제하기
☃️ Repository
public interface VolunteerRepository extends JpaRepository<Volunteer, Long> {}
'Spring > [P] 눈치우기 봉사활동 매칭 플랫폼' 카테고리의 다른 글
OpenWeatherMap API 로 매일 날씨 정보 가져오기 (0) | 2024.02.20 |
---|---|
게시글 댓글 작성하기 (대댓글X) (0) | 2024.02.16 |
게시글 작성하기 (0) | 2024.02.15 |
최신 게시글 3개 조회하기 (0) | 2024.02.14 |
게시글 상세 조회하기 (0) | 2024.02.13 |