Skip to content

Commit

Permalink
[FEAT] success, error response 추가 #7
Browse files Browse the repository at this point in the history
  • Loading branch information
eeddiinn committed Jul 8, 2024
1 parent e71cea1 commit 5680342
Show file tree
Hide file tree
Showing 15 changed files with 134 additions and 26 deletions.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package sopt.univoice.domain.universityData.entity;

public class CollegeDepartment {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package sopt.univoice.domain.universityData.entity;

public class Department {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package sopt.univoice.domain.universityData.entity;

public class University {
}
15 changes: 15 additions & 0 deletions src/main/java/sopt/univoice/infra/common/dto/ErrorResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package sopt.univoice.infra.common.dto;

import sopt.univoice.infra.common.exception.message.ErrorMessage;

public record ErrorResponse(
int status,
String message
) {
public static ErrorResponse of(int status, String message) {
return new ErrorResponse(status, message);
}
public static ErrorResponse of(ErrorMessage errorMessage) {
return new ErrorResponse(errorMessage.getStatus(), errorMessage.getMessage());
}
}
14 changes: 14 additions & 0 deletions src/main/java/sopt/univoice/infra/common/dto/SuccessMessage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package sopt.univoice.infra.common.dto;

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

@Getter
@AllArgsConstructor
public enum SuccessMessage {

;
private final int status;
private final String message;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package sopt.univoice.infra.common.dto;

public record SuccessStatusResponse<T>(
int status,
String message,
T data
) {
public static <T> SuccessStatusResponse<T> of(SuccessMessage successMessage, T data) {
return new SuccessStatusResponse<>(successMessage.getStatus(), successMessage.getMessage(), data);
}
public static SuccessStatusResponse<Void> of(SuccessMessage successMessage) {
return new SuccessStatusResponse<>(successMessage.getStatus(), successMessage.getMessage(), null);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package sopt.univoice.infra.common.exception;

import jakarta.persistence.EntityNotFoundException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import sopt.univoice.infra.common.dto.ErrorResponse;
import sopt.univoice.infra.common.exception.message.BusinessException;

@RestControllerAdvice
public class GlobalExceptionHandler {

@ExceptionHandler(EntityNotFoundException.class)
protected ResponseEntity<ErrorResponse> handleEntityNotFoundException(EntityNotFoundException e) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(ErrorResponse.of(HttpStatus.NOT_FOUND.value(), e.getMessage()));
}

@ExceptionHandler(BusinessException.class)
protected ResponseEntity<ErrorResponse> handleBusinessException(BusinessException e) {
return ResponseEntity.status(HttpStatus.CONFLICT).body(ErrorResponse.of(e.getErrorMessage()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package sopt.univoice.infra.common.exception;

import sopt.univoice.infra.common.exception.message.BusinessException;
import sopt.univoice.infra.common.exception.message.ErrorMessage;

public class NotFoundException extends BusinessException {
public NotFoundException(ErrorMessage errorMessage) {
super(errorMessage);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package sopt.univoice.infra.common.exception.message;

import sopt.univoice.infra.common.exception.message.ErrorMessage;

public class BusinessException extends RuntimeException {
private ErrorMessage errorMessage;

public BusinessException(ErrorMessage errorMessage) {
super(errorMessage.getMessage());
this.errorMessage = errorMessage;
}

public ErrorMessage getErrorMessage() {
return errorMessage;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package sopt.univoice.infra.common.exception.message;

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

@AllArgsConstructor
@Getter
public enum ErrorMessage {

;
private final int status;
private final String message;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package sopt.univoice.external;
package sopt.univoice.infra.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
Expand Down Expand Up @@ -41,8 +41,8 @@ public Region getRegion() {
@Bean
public S3Client getS3Client() {
return S3Client.builder()
.region(getRegion())
.credentialsProvider(systemPropertyCredentialsProvider())
.build();
.region(getRegion())
.credentialsProvider(systemPropertyCredentialsProvider())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package sopt.univoice.external;
package sopt.univoice.infra.external;


import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
Expand All @@ -7,6 +8,7 @@
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.PutObjectRequest;
import software.amazon.awssdk.services.s3.model.DeleteObjectRequest;
import sopt.univoice.infra.config.AwsConfig;

import java.io.IOException;
import java.util.Arrays;
Expand Down Expand Up @@ -35,11 +37,11 @@ public String uploadImage(String directoryPath, MultipartFile image) throws IOEx
validateFileSize(image);

PutObjectRequest request = PutObjectRequest.builder()
.bucket(bucketName)
.key(key)
.contentType(image.getContentType())
.contentDisposition("inline")
.build();
.bucket(bucketName)
.key(key)
.contentType(image.getContentType())
.contentDisposition("inline")
.build();

RequestBody requestBody = RequestBody.fromBytes(image.getBytes());
s3Client.putObject(request, requestBody);
Expand All @@ -50,9 +52,9 @@ public void deleteImage(String key) throws IOException {
final S3Client s3Client = awsConfig.getS3Client();

s3Client.deleteObject((DeleteObjectRequest.Builder builder) ->
builder.bucket(bucketName)
.key(key)
.build()
builder.bucket(bucketName)
.key(key)
.build()
);
}

Expand All @@ -77,4 +79,4 @@ private void validateFileSize(MultipartFile image) {
}
}

}
}

0 comments on commit 5680342

Please sign in to comment.