Skip to content

Commit

Permalink
Merge branch 'dev' of https://github.com/ne-o-rdinary/back into feat/…
Browse files Browse the repository at this point in the history
…getAnswer
  • Loading branch information
hcg0127 committed Nov 23, 2024
2 parents 055f93a + 959f9a4 commit 5666513
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 7 deletions.
1 change: 1 addition & 0 deletions src/main/java/com/example/demo/base/ApiResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,5 @@ public static <T> ApiResponse<T> of(BaseCode code, T result) {
public static <T> ApiResponse<T> onFailure(String code, String message, T data) {
return new ApiResponse<>(false, code, message, data);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public enum ErrorStatus implements BaseErrorCode {
BAD_REQUEST(HttpStatus.BAD_REQUEST, "COMMON400", "잘못된 요청입니다."),
UNAUTHORIZED(HttpStatus.UNAUTHORIZED, "COMMON401", "인증이 필요합니다."),
FORBIDDEN(HttpStatus.FORBIDDEN, "COMMON403", "접근이 금지되었습니다."),
INVALID_ID_TYPE(HttpStatus.BAD_REQUEST, "COMMON400", "적합하지 않은 ID 타입입니다"),

// s3 관련 에러
FILE_UPLOAD_FAILED(HttpStatus.INTERNAL_SERVER_ERROR, "FILE4001", "파일 업로드 실패"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
Expand Down Expand Up @@ -95,4 +96,17 @@ private ResponseEntity<ApiResponse<Object>> buildErrorResponse(ErrorReasonDTO er
ApiResponse<Object> body = ApiResponse.onFailure(errorReason.getCode(), errorReason.getMessage(), null);
return ResponseEntity.status(errorReason.getHttpStatus()).body(body);
}

@ExceptionHandler(HttpMessageNotReadableException.class)
public ResponseEntity<ApiResponse<?>> handleHttpMessageNotReadable(
HttpMessageNotReadableException ex) {
String errorMessage = "유효하지 않은 요청 형식입니다. ID는 숫자만 입력해야 합니다.";

return ResponseEntity.status(HttpStatus.BAD_REQUEST)
.body(ApiResponse.onFailure(
"COMMON400",
errorMessage,
null
));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.example.demo.base.ApiResponse;
import com.example.demo.base.code.status.exception.ErrorStatus;
import com.example.demo.base.code.status.exception.GeneralException;
import com.example.demo.domain.dto.s3.FileResponseDTO;
import com.example.demo.domain.dto.s3.FileUploadRequestDTO;
import com.example.demo.domain.dto.s3.FileUploadResponseDTO;
import com.example.demo.domain.entity.s3.FileUploadEntity;
Expand Down Expand Up @@ -59,27 +60,31 @@ public ApiResponse<FileUploadResponseDTO> uploadFile(
}

@GetMapping("/urls")
public ApiResponse<List<String>> getFileUrls(@RequestParam String folderName) {
public ApiResponse<List<FileResponseDTO>> getFileUrls(@RequestParam String folderName) {
try {
// SecurityContext에서 사용자 ID 추출
String userId = (String) SecurityContextHolder.getContext().getAuthentication().getPrincipal();

// DB에서 해당 조건에 맞는 파일 URL 조회
// DB에서 해당 조건에 맞는 파일 정보 조회
List<FileUploadEntity> fileUploadEntities = fileUploadRepository.findByUserTokenAndFolderName(userId, folderName);

// 파일이 없으면 에러 처리
if (fileUploadEntities.isEmpty()) {
throw new GeneralException(ErrorStatus.NOT_FOUND.getReasonHttpStatus());
}

List<String> fileUrls = fileUploadEntities.stream()
.map(FileUploadEntity::getFileUrl)
// Entity 데이터를 DTO로 변환
List<FileResponseDTO> fileDtos = fileUploadEntities.stream()
.map(entity -> new FileResponseDTO(
entity.getFileUrl()
))
.toList();
return ApiResponse.onSuccess(fileUrls);

return ApiResponse.onSuccess(fileDtos);
} catch (GeneralException ge) {
throw ge;
} catch (Exception e) {
throw new GeneralException(ErrorStatus.FETCH_FAILED.getReasonHttpStatus());
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class LoginController {
* 유저 UUID를 받아서 해당 RefreshToken을 반환하는 API
* @param userId UUID
* @return RefreshToken 정보
*/
@GetMapping("/api/login/{userId}")
public ResponseEntity<ApiResponse<RefreshToken>> login(@PathVariable UUID userId) {
// UUID에 해당하는 RefreshToken을 데이터베이스에서 찾음
Expand All @@ -37,4 +37,5 @@ public ResponseEntity<ApiResponse<RefreshToken>> login(@PathVariable UUID userId
// 정상적으로 토큰을 찾았다면 성공적인 응답 반환
return ResponseEntity.ok(ApiResponse.onSuccess(refreshToken));
}
*/
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
package com.example.demo.domain.dto.answer;

import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Pattern;
import lombok.Getter;

@Getter
public class AnswerRequestDto {

@NotNull
@Min(value = 0, message = "적합하지 않은 ID 타입입니다.")
private Long questionId;

@NotNull
@Pattern(regexp = "^[^0-9]+$", message = "답변은 문자만 입력 가능합니다.")
private String answer;

public AnswerRequestDto(Long questionId, String answer) {
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/com/example/demo/domain/dto/s3/FileResponseDTO
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.example.demo.domain.dto.s3;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
@AllArgsConstructor
public class FileResponseDTO {
private String fileUrl; // 파일 URL
}

0 comments on commit 5666513

Please sign in to comment.