Skip to content

Commit

Permalink
feat: 양수가 아닌 id로 특정 방문 기록 삭제를 시도할 때 예외 처리 기능 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
devhoya97 committed Jul 22, 2024
1 parent 295c97c commit 5eefdf6
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,10 @@ public ResponseEntity<ExceptionResponse> handleDateTimeParseException(DateTimePa
return ResponseEntity.badRequest()
.body(new ExceptionResponse(HttpStatus.BAD_REQUEST.toString(), "올바르지 않은 날짜 형식입니다."));
}

@ExceptionHandler(InvalidIdException.class)
public ResponseEntity<ExceptionResponse> handleInvalidIdException(InvalidIdException e) {
return ResponseEntity.badRequest()
.body(new ExceptionResponse(HttpStatus.BAD_REQUEST.toString(), e.getMessage()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.staccato.exception;

public class InvalidIdException extends RuntimeException {
public InvalidIdException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.staccato.exception.InvalidIdException;
import com.staccato.visit.service.VisitService;

import lombok.RequiredArgsConstructor;
Expand All @@ -14,10 +15,15 @@
@RequestMapping("/visits")
@RequiredArgsConstructor
public class VisitController {
private static final int MIN_ID = 1;

private final VisitService visitService;

@DeleteMapping("{visitId}")
public ResponseEntity<Void> deleteById(@PathVariable Long visitId) {
if (visitId < MIN_ID) {
throw new InvalidIdException("방문 기록 식별자는 양수로 이루어져야 합니다.");
}
visitService.deleteById(visitId);
return ResponseEntity.noContent().build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,15 @@ void deleteById() {
.then().log().all()
.statusCode(204);
}

@DisplayName("1보다 작은 id로 Visit 삭제를 시도하면 Bad Request를 반환한다.")
@Test
void deleteByIdFail() {
RestAssured.given().log().all()
.header("Authorization", "1")
.contentType(ContentType.JSON)
.when().delete("/visits/0")
.then().log().all()
.statusCode(400);
}
}

0 comments on commit 5eefdf6

Please sign in to comment.