Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
SeongJoon-K committed Dec 20, 2023
2 parents 63deb11 + 5db6389 commit 5e91ea3
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 34 deletions.
2 changes: 2 additions & 0 deletions src/main/java/flirting/demo/common/StatusCode.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package flirting.demo.common;

import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
Expand All @@ -19,6 +20,7 @@ public enum StatusCode {
SNOWFLAKE_NOT_ENOUGH(HttpStatus.INTERNAL_SERVER_ERROR, "N506", "There is no enough snowflakes")
;

@JsonIgnore
private final HttpStatus httpStatus;
private final String statusCode;
private final String statusMessage;
Expand Down
15 changes: 2 additions & 13 deletions src/main/java/flirting/demo/controller/InvitationController.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ public ResponseEntity<Object> getInvitationList(@PathVariable("inviterId") Long
// Todo: member id, group id로 초대장 조회

HttpHeaders httpheaders = new HttpHeaders();
try {
Invitation invitation = invitationService.createInvitation(inviterId, groupId);
Long memberCnt = invitationService.getMemberCnt(groupId);
InvitationResponse invitationResponse = InvitationResponse.builder()
Expand All @@ -43,17 +42,12 @@ public ResponseEntity<Object> getInvitationList(@PathVariable("inviterId") Long
),
httpheaders, HttpStatus.OK
);
}catch (RuntimeException e) {
return new ResponseEntity<>(
new ApiStatus(StatusCode.INTERNAL_SERVER_ERROR, e.getMessage()),
httpheaders, HttpStatus.INTERNAL_SERVER_ERROR);
}

}

@PostMapping(value = "", produces = "application/json")
public ResponseEntity<Object> accept(@RequestBody InvitationAcceptRequest invitationAcceptRequest){
HttpHeaders httpHeaders = new HttpHeaders();
try {
InvitationAcceptResponse invitation = invitationService.acceptInvitation(invitationAcceptRequest);
return new ResponseEntity<>(
new ResponseData(
Expand All @@ -62,11 +56,6 @@ public ResponseEntity<Object> accept(@RequestBody InvitationAcceptRequest invita
),
httpHeaders, HttpStatus.OK
);
}catch (RuntimeException e) {
return new ResponseEntity<>(
new ApiStatus(StatusCode.INTERNAL_SERVER_ERROR, e.getMessage()),
httpHeaders, HttpStatus.INTERNAL_SERVER_ERROR
);
}

}
}
14 changes: 7 additions & 7 deletions src/main/java/flirting/demo/controller/VoteController.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public ResponseEntity<Object> getGuessData(@PathVariable("memberId") Long member
@PutMapping(value = "/guess", produces = "application/json")
public ResponseEntity<Object> guess(@RequestBody VoteGuessRequest voteGuessRequest) {
HttpHeaders httpHeaders = new HttpHeaders();
try {
// try {
Long memberId = voteGuessRequest.getMemberId();
Long selecteMemberId = voteGuessRequest.getSelectedMemberId();
boolean isCorrect = voteService.getIsCorrect(voteGuessRequest);
Expand All @@ -136,11 +136,11 @@ public ResponseEntity<Object> guess(@RequestBody VoteGuessRequest voteGuessReque
), httpHeaders, HttpStatus.OK
);

}catch (CustomException e) {
return new ResponseEntity<>(
new ApiStatus(StatusCode.INTERNAL_SERVER_ERROR, e.getMessage()),
httpHeaders, HttpStatus.INTERNAL_SERVER_ERROR
);
}
// }catch (CustomException e) {
// return new ResponseEntity<>(
// new ApiStatus(StatusCode.INTERNAL_SERVER_ERROR, e.getMessage()),
// httpHeaders, HttpStatus.INTERNAL_SERVER_ERROR
// );
// }
}
}
2 changes: 1 addition & 1 deletion src/main/java/flirting/demo/entity/Invitation.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class Invitation {
private Member sender;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "receiver_id", nullable = false)
@JoinColumn(name = "receiver_id")
private Member receiver;

@ManyToOne(fetch = FetchType.LAZY)
Expand Down
36 changes: 36 additions & 0 deletions src/main/java/flirting/demo/exception/GlobalExceptionHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package flirting.demo.exception;

import flirting.demo.common.CustomException;
import flirting.demo.common.StatusCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

@ExceptionHandler(value = { Exception.class })
@ResponseBody
public ResponseEntity<?> handlerException(Exception e) {
log.error("Handing Exception : {}", e.getMessage());
e.printStackTrace();
return new ResponseEntity<>(
StatusCode.INTERNAL_SERVER_ERROR,
HttpStatus.INTERNAL_SERVER_ERROR
);
}

@ExceptionHandler(value = { CustomException.class })
@ResponseBody
public ResponseEntity<?> handlerCustomException(CustomException e) {
log.error("Handing CustomException : {}", e.getErrorCode().getStatusMessage());
return new ResponseEntity<>(
e.getErrorCode(),
e.getErrorCode().getHttpStatus()
);
}
}
19 changes: 6 additions & 13 deletions src/main/java/flirting/demo/service/InvitationService.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,30 +22,26 @@ public class InvitationService {
private final GroupRepository groupRepository;

public Invitation createInvitation(Long inviterId, Long groupId) {
try {
// Todo: get memer id -> invitation 생성 후 저장 ...
// Todo: 동일 member id, group id인 멤버 이미 존재하면 오류 반환
Group group = groupRepository.getReferenceById(groupId);
Member inviter = memberRepository.getReferenceById(inviterId);

System.out.println("get group and inviter");
Invitation invitation = Invitation.builder()
.sender(inviter)
.group(group)
.build();
System.out.println("build invitation");
// Invitation _invitation = invitationRepository.save(invitation);
System.out.println("save invitation");
return invitation;

Invitation _invitation = invitationRepository.save(invitation);
return _invitation;
}
catch (RuntimeException e) {
throw new RuntimeException("사용자의 초대장을 조회할 수 없습니다.");
}
}
public Long getMemberCnt(Long groupId) {
return memberRepository.getMemberCnt(groupId);
}

public InvitationAcceptResponse acceptInvitation(InvitationAcceptRequest invitationAcceptRequest) {
try {
Long receiverId = invitationAcceptRequest.getMemberId();
Long groupId = invitationAcceptRequest.getGroupId();
Long inviterId = invitationAcceptRequest.getInviterId();
Expand Down Expand Up @@ -86,9 +82,6 @@ public InvitationAcceptResponse acceptInvitation(InvitationAcceptRequest invitat
return _invitation;
}else throw new RuntimeException("이미 가입된 그룹입니다.");
// Todo: custom runtime exception 만들기
}
catch (RuntimeException e) {
throw new RuntimeException("초대 수락 실패");
}

}
}

0 comments on commit 5e91ea3

Please sign in to comment.