Restful API 방식으로 좌석 예약 서비스를 구현해보겠습니다.
사용가 요청 보내는 방식은 "/seat/{좌석번호}" URI로 보냅니다.

사용자가 위의 URI 형식으로 요청을 보낼 시,
GET 요청을 하면 좌석정보(예약 정보까지) 갖고오고,
POST 요청을 보내면 좌석 예약,
PUT 요청을 보내면 좌석 연장,
DELETE 요청을 보내면 좌석반납을 진행합니다.
먼저 Controller 코드 입니다.
이 Controller 에는 RestController라는 어노테이션을 붙였습니다.
먼저 좌석 예약을 하는 메서드 입니다.
Post방식으로 받기 때문에 어노테이션을 PostMapping 을 선언해줬습니다.
URI에 좌서번호가 담겨오기 때문에 @Pathvariable를 통해 seatId를 갖고옵니다 .
@PostMapping(value= "/seat/{seatId}", produces = "application/json; charset=UTF-8")
public ResponseEntity<String> seatReservation(
Principal principal,
@PathVariable int seatId) throws Exception {
Long studentNumber = (long) Integer.parseInt(principal.getName());
if (principal != null) {
int checkReservation = this.seatReservationService.checkReservation(studentNumber);
if (checkReservation == 1) {
return new ResponseEntity<String>("자리는 하나만 예약할 수 있습니다. ",HttpStatus.BAD_REQUEST);
}
int rowCnt = this.seatReservationService.seatReservationService(studentNumber, seatId);
return rowCnt == 1 ? new ResponseEntity<String>("열람실 예약에 성공하였습니다.", HttpStatus.CREATED)
: new ResponseEntity<String>("열람실 예약에 실패하였습니다.", HttpStatus.INTERNAL_SERVER_ERROR);
}
return new ResponseEntity<String>("로그인 후 이용 가능합니다.", HttpStatus.UNAUTHORIZED);
this.seatReservationService.seatReservation(studentNumber, seatId);
return new ResponseEntity<String>("열람실 예약에 성공하였습니다.", HttpStatus.CREATED);
}
if문을 통해 에러 발생시 ResponseEntitiy로 에러코드와 에러메시지를 응답하는 형식을 갖고 있습니다.
두번째로 좌석을 연장하는 Put요청입니다. 어노테이션은 PutMapping을 붙였습니다.
@PutMapping("/seat/{connectionseq}")
public ResponseEntity<String> extensionSeat(
@PathVariable int connectionseq
, Principal principal){
int studentNumber = Integer.parseInt(principal.getName());
int rowCnt = this.seatReservationService.seatExtensionService(connectionseq, studentNumber);
return rowCnt == 1? new ResponseEntity<String>("Update Success", HttpStatus.OK) : new ResponseEntity<String>(HttpStatus.INTERNAL_SERVER_ERROR);
}
세번째로 좌석을 반납하는 DELETE 요청입니다. 어노테이션은 DeleteMapping을 붙였습니다.
@DeleteMapping("/seat/{connectionseq}")
public ResponseEntity<String> returnSeat(
@PathVariable int connectionseq
, Principal principal){
int studentNumber = Integer.parseInt(principal.getName());
int rowCnt = this.seatReservationService.seatReturnService(connectionseq, studentNumber);
return rowCnt == 1? new ResponseEntity<String>("Update Success", HttpStatus.OK) : new ResponseEntity<String>(HttpStatus.INTERNAL_SERVER_ERROR);
}
이렇게 RestFul Api 서버를 구축해 좌석 예약 서비스를 구현했습니다.
'Projects > 열람실 & 도서관 프로젝트' 카테고리의 다른 글
[Spring](도서관/열람실 프로젝트) 좌석 예약 서비스 개선기 (0) | 2024.04.22 |
---|