Skip to content

Commit

Permalink
chore: 반환값 ResponseEntity로 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
k000927 committed Jul 31, 2024
1 parent 0ebf49b commit 7bec1e1
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,21 @@
import JGS.CasperEvent.domain.event.service.eventService.LotteryEventService;
import JGS.CasperEvent.global.response.CustomResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/event/lottery")
public class LotteryEventController {

private final LotteryEventService lotteryEventService;

@Autowired
public LotteryEventController(LotteryEventService lotteryEventService) {
this.lotteryEventService = lotteryEventService;
}
private LotteryEventService lotteryEventService;

@PostMapping
public CustomResponse<GetCasperBot> postCasperBot(@RequestBody String body) {
return CustomResponse.create(lotteryEventService.postCasperBot(body));
public ResponseEntity<CustomResponse<GetCasperBot>> postCasperBot(@CookieValue String userData,
@RequestBody String body) {

return new ResponseEntity<>(CustomResponse.create(lotteryEventService.postCasperBot(userData, body)), HttpStatus.CREATED);
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
package JGS.CasperEvent.domain.health.api;

import JGS.CasperEvent.global.response.CustomResponse;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import static JGS.CasperEvent.global.response.CustomResponse.response;

@RestController
@RequestMapping("/health")
public class HealthController {

@GetMapping
public CustomResponse<Boolean> health(){
return CustomResponse.success(true);
public ResponseEntity<CustomResponse<Boolean>> health(){
return new ResponseEntity<>(CustomResponse.success(true), HttpStatus.OK);
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package JGS.CasperEvent.global.error.exception;

public class CustomException extends RuntimeException{
public class CustomException extends RuntimeException {
private final ErrorCode errorCode;

public CustomException(String message, ErrorCode errorCode) {
super(message);
this.errorCode = errorCode;
}

public ErrorCode getErrorCode() {
return errorCode;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,8 @@ public enum ErrorCode {
ErrorCode(int status) {
this.status = status;
}

public int getStatus(){
return status;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package JGS.CasperEvent.global.response;

import JGS.CasperEvent.global.error.exception.CustomException;

public record CustomErrorResponse(String message) {
public static CustomErrorResponse returnError(CustomException e){
return new CustomErrorResponse(e.getMessage());
}

public static CustomErrorResponse returnError(String message){
return new CustomErrorResponse(message);
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package JGS.CasperEvent.global.response;

public record CustomResponse<T> (int statusCode, String message, T result){
public record CustomResponse<T>(String message, T result) {
public static <T> CustomResponse<T> response(String message, T result) {
return new CustomResponse<>(message, result);
}

public static <T> CustomResponse<T> success(T result){
return new CustomResponse<>(200, "요청에 성공하였습니다.", result);
return new CustomResponse<>("요청에 성공했습니다.", result);
}

public static <T> CustomResponse<T> create(T result){
return new CustomResponse<>(201, "생성에 성공하였습니다", result);
return new CustomResponse<>("생성에 성공했습니다.", result);
}
}

0 comments on commit 7bec1e1

Please sign in to comment.