Skip to content

Commit

Permalink
[refactor] : ApiResponse 구현 및 meetings 적용
Browse files Browse the repository at this point in the history
  • Loading branch information
TaetaetaE01 committed Aug 5, 2024
1 parent 79730f7 commit 472a12d
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import com.example.bigbrotherbe.domain.meetings.entity.Meetings;
import com.example.bigbrotherbe.domain.meetings.service.MeetingsService;

import com.example.bigbrotherbe.global.exception.enums.SuccessCode;
import com.example.bigbrotherbe.global.exception.response.ApiResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
Expand All @@ -18,6 +20,8 @@

import java.util.List;

import static com.example.bigbrotherbe.global.exception.enums.SuccessCode.SUCCESS;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/big-brother/meetings")
Expand All @@ -26,44 +30,44 @@ public class MeetingsController {
private final MeetingsService meetingsService;

@PostMapping
public ResponseEntity<Void> registerMeetings(@RequestPart(value = "meetingsRegisterRequest") MeetingsRegisterRequest meetingsRegisterRequest,
@RequestPart(value = "file", required = false) List<MultipartFile> multipartFiles) {
public ResponseEntity<ApiResponse<Void>> registerMeetings(@RequestPart(value = "meetingsRegisterRequest") MeetingsRegisterRequest meetingsRegisterRequest,
@RequestPart(value = "file", required = false) List<MultipartFile> multipartFiles) {
meetingsService.registerMeetings(meetingsRegisterRequest, multipartFiles);
return ResponseEntity.ok().build();
return ResponseEntity.ok(ApiResponse.success(SUCCESS));
}

@PutMapping("/{meetingsId}")
public ResponseEntity<Void> updateMeetings(@PathVariable("meetingsId") Long meetingsId,
@RequestPart(value = "meetingsUpdateRequest") MeetingsUpdateRequest meetingsUpdateRequest,
@RequestPart(value = "file", required = false) List<MultipartFile> multipartFiles) {
public ResponseEntity<ApiResponse<Void>> updateMeetings(@PathVariable("meetingsId") Long meetingsId,
@RequestPart(value = "meetingsUpdateRequest") MeetingsUpdateRequest meetingsUpdateRequest,
@RequestPart(value = "file", required = false) List<MultipartFile> multipartFiles) {
meetingsService.updateMeetings(meetingsId, meetingsUpdateRequest, multipartFiles);
return ResponseEntity.ok().build();
return ResponseEntity.ok(ApiResponse.success(SUCCESS));
}

@DeleteMapping("/{meetingsId}")
public ResponseEntity<Void> deleteMeetings(@PathVariable("meetingsId") Long meetingsId) {
public ResponseEntity<ApiResponse<Void>> deleteMeetings(@PathVariable("meetingsId") Long meetingsId) {
meetingsService.deleteMeetings(meetingsId);
return ResponseEntity.ok().build();
return ResponseEntity.ok(ApiResponse.success(SUCCESS));
}

@GetMapping("/{meetingsId}")
public ResponseEntity<MeetingsResponse> getMeetingsById(@PathVariable("meetingsId") Long MeetingsId) {
public ResponseEntity<ApiResponse<MeetingsResponse>> getMeetingsById(@PathVariable("meetingsId") Long MeetingsId) {
MeetingsResponse meetingsResponse = meetingsService.getMeetingsById(MeetingsId);
return ResponseEntity.ok().body(meetingsResponse);
return ResponseEntity.ok(ApiResponse.success(SUCCESS, meetingsResponse));
}

@GetMapping("all/{affiliationId}")
public ResponseEntity<Page<Meetings>> getMeetingsList(@PathVariable("affiliationId") Long affiliationId,
@RequestParam(name = "page", defaultValue = "0") int page,
@RequestParam(name = "size", defaultValue = "10") int size,
@RequestParam(name = "search", required = false) String search) {
public ResponseEntity<ApiResponse<Page<Meetings>>> getMeetingsList(@PathVariable("affiliationId") Long affiliationId,
@RequestParam(name = "page", defaultValue = "0") int page,
@RequestParam(name = "size", defaultValue = "10") int size,
@RequestParam(name = "search", required = false) String search) {
Page<Meetings> meetingsPage;
Pageable pageable = PageRequest.of(page, size, Sort.by(Sort.Direction.DESC, "id"));
if (search != null && !search.isEmpty()) {
meetingsPage = meetingsService.searchMeetings(affiliationId, search, pageable);
} else {
meetingsPage = meetingsService.getMeetings(affiliationId, pageable);
}
return ResponseEntity.ok().body(meetingsPage);
return ResponseEntity.ok(ApiResponse.success(SUCCESS, meetingsPage));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,14 @@
@Getter
@AllArgsConstructor
public enum ErrorCode {
// SUCCESS
SUCCESS(HttpStatus.OK, "SUCCESS", "요청에 성공하였습니다."),

// MEMBER
UNABLE_TO_SEND_EMAIL(HttpStatus.BAD_REQUEST, "MEMBER-001", "이메일을 보낼 수 없습니다."),
EXIST_EMAIL(HttpStatus.BAD_REQUEST, "MEMBER-002", "이미 존재하는 이메일입니다."),
NO_SUCH_ALGORITHM(HttpStatus.NOT_FOUND, "MEMBER-003", "알고리즘이 없습니다."),
FAIL_LOAD_MEMBER(HttpStatus.EXPECTATION_FAILED, "MEMBER-004", "멤버를 불러오는 데 실패했습니다."),
NO_EXIST_EMAIL(HttpStatus.NOT_FOUND, "MEMBER-005", "존재하지 않는 이메일입니다."),
INVALID_EMAIL_FORMAT(HttpStatus.BAD_REQUEST,"MEMBER-006", "올바르지 않은 이메일 형식입니다."),
INVALID_EMAIL_FORMAT(HttpStatus.BAD_REQUEST, "MEMBER-006", "올바르지 않은 이메일 형식입니다."),

// MEETINGS
NO_EXIST_MEETINGS(HttpStatus.NOT_FOUND, "MEETINGS-001", "존재하지 않는 회의록 입니다."),

Expand All @@ -30,8 +28,8 @@ public enum ErrorCode {
NO_EXIST_AFFILIATION(HttpStatus.NOT_FOUND, "AFFILIATION-001", "존재하지 않는 학생회 입니다."),

// FILE
FAIL_TO_UPLOAD(HttpStatus.INTERNAL_SERVER_ERROR, "FILE-001","파일 업로드에 실패하였습니다."),
FAIL_TO_DELETE(HttpStatus.INTERNAL_SERVER_ERROR, "FILE-002","파일 삭제에 실패하였습니다.");
FAIL_TO_UPLOAD(HttpStatus.INTERNAL_SERVER_ERROR, "FILE-001", "파일 업로드에 실패하였습니다."),
FAIL_TO_DELETE(HttpStatus.INTERNAL_SERVER_ERROR, "FILE-002", "파일 삭제에 실패하였습니다.");

private final HttpStatus httpStatus;
private final String errorCode;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.example.bigbrotherbe.global.exception.enums;

import lombok.AllArgsConstructor;
import lombok.Getter;
import org.springframework.http.HttpStatus;

@Getter
@AllArgsConstructor
public enum SuccessCode {
// SUCCESS
SUCCESS(HttpStatus.OK, "SUCCESS", "요청에 성공하였습니다.");

private final HttpStatus httpStatus;
private final String successCode;
private final String message;
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.example.bigbrotherbe.global.exception.handler;

import com.example.bigbrotherbe.global.exception.BusinessException;
import com.example.bigbrotherbe.global.exception.response.ExceptionResponse;
import com.example.bigbrotherbe.global.exception.response.ApiResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
Expand All @@ -12,11 +12,18 @@
public class GlobalExceptionHandler {

// ErrorCode 내의 에러
// @ExceptionHandler(BusinessException.class)
// protected ResponseEntity<ApiResponse> handleBusinessException(BusinessException e) {
// log.error("BusinessException", e);
// ApiResponse apiResponse = ApiResponse.of(e.getErrorCode());
//
// return ResponseEntity.status(e.getErrorCode().getHttpStatus()).body(apiResponse);
// }

@ExceptionHandler(BusinessException.class)
protected ResponseEntity<ExceptionResponse> handleBusinessException(BusinessException e) {
protected ResponseEntity<ApiResponse<?>> handleBusinessException(BusinessException e) {
log.error("BusinessException", e);
ExceptionResponse exceptionResponse = ExceptionResponse.of(e.getErrorCode());

return ResponseEntity.status(e.getErrorCode().getHttpStatus()).body(exceptionResponse);
ApiResponse<?> apiResponse = ApiResponse.error(e.getErrorCode());
return ResponseEntity.status(e.getErrorCode().getHttpStatus()).body(apiResponse);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.example.bigbrotherbe.global.exception.response;

import com.example.bigbrotherbe.global.exception.enums.ErrorCode;
import com.example.bigbrotherbe.global.exception.enums.SuccessCode;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.*;
import org.springframework.http.HttpStatus;

@Getter
@Builder
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor(access = AccessLevel.PRIVATE)
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ApiResponse<T> {
private HttpStatus status;
private int httpStatusCode;
private String responseCode;
private String resultMsg;
private T data;

// @Builder
// protected ApiResponse(final ErrorCode code) {
// this.status = code.getHttpStatus();
// this.responseCode = code.getErrorCode();
// this.resultMsg = code.getMessage();
// }

public static <T> ApiResponse<T> success(final SuccessCode successCode) {
return ApiResponse.<T>builder()
.status(successCode.getHttpStatus())
.httpStatusCode(successCode.getHttpStatus().value())
.responseCode(successCode.getSuccessCode())
.resultMsg(successCode.getMessage())
.build();
}

public static <T> ApiResponse<T> success(final SuccessCode successCode, final T data) {
return ApiResponse.<T>builder()
.status(successCode.getHttpStatus())
.httpStatusCode(successCode.getHttpStatus().value())
.responseCode(successCode.getSuccessCode())
.resultMsg(successCode.getMessage())
.data(data)
.build();
}

public static ApiResponse<?> error(final ErrorCode errorCode) {
return ApiResponse.builder()
.status(errorCode.getHttpStatus())
.httpStatusCode(errorCode.getHttpStatus().value())
.responseCode(errorCode.getErrorCode())
.resultMsg(errorCode.getMessage())
.build();
}

// public static ApiResponse of(final ErrorCode code) {
// return new ApiResponse(code);
// }
}

This file was deleted.

0 comments on commit 472a12d

Please sign in to comment.