Skip to content

Commit

Permalink
merge: conflict 해결 후 feature로 merge 진행
Browse files Browse the repository at this point in the history
- 애플리케이션 실행 정상
- 테스트 실행 정상
  • Loading branch information
Sehee-Lee-01 committed Jan 8, 2024
2 parents 9e4a2c8 + dba9cc0 commit 7444dbc
Show file tree
Hide file tree
Showing 37 changed files with 775 additions and 159 deletions.
4 changes: 2 additions & 2 deletions .idea/codeStyles/codeStyleConfig.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,28 @@
import com.backendoori.ootw.avatar.dto.AvatarItemRequest;
import com.backendoori.ootw.avatar.dto.AvatarItemResponse;
import com.backendoori.ootw.avatar.service.AvatarItemService;
import com.backendoori.ootw.common.validation.Image;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
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.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/api/v1/image")
@RequestMapping("/api/v1/avatar-items")
@RequiredArgsConstructor
public class AvatarItemController {

private final AvatarItemService appearanceService;

@PostMapping
public ResponseEntity<AvatarItemResponse> uploadImage(@RequestPart("file") MultipartFile file,
@RequestBody AvatarItemRequest requestDto) {
AvatarItemResponse avatarItem = appearanceService.uploadItem(file, requestDto);
public ResponseEntity<AvatarItemResponse> uploadImage(@RequestPart @Image MultipartFile file,
@RequestPart @Valid AvatarItemRequest request) {
AvatarItemResponse avatarItem = appearanceService.uploadItem(file, request);

return ResponseEntity.status(HttpStatus.CREATED).body(avatarItem);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ public class AvatarItem {

@Column(name = "type", nullable = false, columnDefinition = "varchar(30)")
@Enumerated(EnumType.STRING)
private Type type;
private ItemType itemType;

@Column(name = "sex", nullable = false, columnDefinition = "tinyint")
private boolean sex;

private AvatarItem(String image, String type, boolean sex) {
this.image = image;
this.type = Type.valueOf(type);
this.itemType = ItemType.valueOf(type);
this.sex = sex;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package com.backendoori.ootw.avatar.domain;

public enum Type {
public enum ItemType {
HAIR, TOP, PANTS, ACCESSORY, SHOES, BACKGROUND
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
package com.backendoori.ootw.avatar.dto;

import com.backendoori.ootw.avatar.domain.ItemType;
import com.backendoori.ootw.common.validation.Enum;
import jakarta.validation.constraints.NotNull;

public record AvatarItemRequest(
@Enum(enumClass = ItemType.class)
String type,
@NotNull
boolean sex
) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public record AvatarItemResponse(
) {

public static AvatarItemResponse from(AvatarItem avatarItem) {
return new AvatarItemResponse(avatarItem.getType().name(),
return new AvatarItemResponse(avatarItem.getItemType().name(),
avatarItem.isSex(),
avatarItem.getImage());
}
Expand Down
33 changes: 33 additions & 0 deletions src/main/java/com/backendoori/ootw/common/AssertUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.backendoori.ootw.common;

import java.util.Objects;
import java.util.function.Supplier;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;

@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class AssertUtil extends Assert {

public static void notBlank(@Nullable String string, String message) {
if (string == null || string.isBlank()) {
throw new IllegalArgumentException(message);
}
}

public static void hasPattern(@Nullable String string, @Nullable String pattern, String message) {
notNull(string, message);

if (!string.matches(Objects.requireNonNull(pattern))) {
throw new IllegalArgumentException(message);
}
}

public static void throwIf(boolean state, Supplier<RuntimeException> exceptionSupplier) {
if (state) {
throw exceptionSupplier.get();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,19 @@
import java.nio.file.Path;
import java.util.concurrent.TimeUnit;
import com.backendoori.ootw.config.MiniOConfig;
import com.backendoori.ootw.exception.ImageUploadException;
import io.minio.GetPresignedObjectUrlArgs;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import io.minio.http.Method;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;


@Slf4j
@Controller
@Service
@RequiredArgsConstructor
public class MiniOImageServiceImpl implements ImageService {

Expand All @@ -39,7 +40,7 @@ public String uploadImage(MultipartFile file) {
.build();
minioClient.putObject(args);
} catch (Exception e) {
log.warn("Exception occurred while saving contents : {}", e.getMessage(), e);
throw new ImageUploadException();
}

return getUrl();
Expand All @@ -56,7 +57,7 @@ private String getUrl() {
.expiry(DURATION, TimeUnit.HOURS)
.build());
} catch (Exception e) {
log.warn("Exception Occurred while getting: {}", e.getMessage(), e);
throw new ImageUploadException();
}

return url;
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/com/backendoori/ootw/common/validation/Enum.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.backendoori.ootw.common.validation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import jakarta.validation.Constraint;
import jakarta.validation.Payload;

@Target(value = {ElementType.PARAMETER, ElementType.FIELD})
@Retention(value = RetentionPolicy.RUNTIME)
@Constraint(validatedBy = EnumValidator.class)
public @interface Enum {

String message() default "유효하지 않은 값입니다 다시 입력해주세요";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
Class<? extends java.lang.Enum<?>> enumClass();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.backendoori.ootw.common.validation;

import java.util.Arrays;
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;

public class EnumValidator implements ConstraintValidator<Enum, String> {

private Enum annotation;

@Override
public void initialize(Enum constraintAnnotation) {
this.annotation = constraintAnnotation;
}

@Override
public boolean isValid(String type, ConstraintValidatorContext context) {
if (type == null) {
return false;
}

return Arrays.stream(this.annotation.enumClass().getEnumConstants())
.anyMatch(e -> e.name().equals(type));
}

}
23 changes: 23 additions & 0 deletions src/main/java/com/backendoori/ootw/common/validation/Image.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.backendoori.ootw.common.validation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import jakarta.validation.Constraint;
import jakarta.validation.Payload;

@Target(value = ElementType.PARAMETER)
@Retention(value = RetentionPolicy.RUNTIME)
@Constraint(validatedBy = ImageValidator.class)
public @interface Image {

String message = "유효하지 않은 이미지를 업로드하였습니다. 다른 이미지를 업로드 해주세요";

String message() default message;

Class<?>[] groups() default {};

Class<? extends Payload>[] payload() default {};

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.backendoori.ootw.common.validation;

import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;
import org.springframework.web.multipart.MultipartFile;

public class ImageValidator implements ConstraintValidator<Image, MultipartFile> {

@Override
public boolean isValid(MultipartFile img, ConstraintValidatorContext context) {
if(img == null || img.isEmpty()){
return false;
}
if(img.getSize() > 10_000_000){
return false;
}
String contentType = img.getContentType();
if(!contentType.startsWith("image")){
return false;
}

return true;
}

}
4 changes: 2 additions & 2 deletions src/main/java/com/backendoori/ootw/config/MiniOConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
import io.minio.MinioClient;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.context.annotation.Configuration;

@Component
@Configuration
@RequiredArgsConstructor
public class MiniOConfig {

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package com.backendoori.ootw.exception;

import java.util.List;
import java.util.NoSuchElementException;
import java.util.Objects;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.support.DefaultMessageSourceResolvable;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.AuthenticationException;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.method.annotation.HandlerMethodValidationException;
Expand All @@ -14,6 +19,44 @@
@RestControllerAdvice
public class GlobalControllerAdvice {

public static final String DEFAULT_MESSAGE = "유효하지 않은 요청 입니다.";

@ExceptionHandler(IllegalArgumentException.class)
public ResponseEntity<ErrorResponse> handleIllegalArgumentException(IllegalArgumentException e) {
ErrorResponse errorResponse = new ErrorResponse(e.getMessage());

return ResponseEntity.status(HttpStatus.BAD_REQUEST)
.body(errorResponse);
}

@ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntity<ErrorResponse> handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
List<FieldError> errors = e.getFieldErrors();
String message = errors.stream()
.map(DefaultMessageSourceResolvable::getDefaultMessage)
.filter(Objects::nonNull)
.findFirst()
.orElse(DEFAULT_MESSAGE);

ErrorResponse errorResponse = new ErrorResponse(message);

return ResponseEntity.status(HttpStatus.BAD_REQUEST)
.body(errorResponse);
}

@ExceptionHandler(HandlerMethodValidationException.class)
public ResponseEntity<ErrorResponse> handlerMethodValidationException(HandlerMethodValidationException e) {
String errorMessage = e.getAllValidationResults()
.get(0)
.getResolvableErrors()
.get(0)
.getDefaultMessage();
ErrorResponse errorResponse = new ErrorResponse(errorMessage);

return ResponseEntity.status(HttpStatus.BAD_REQUEST)
.body(errorResponse);
}

@ExceptionHandler(AuthenticationException.class)
public ResponseEntity<ErrorResponse> handleAuthenticationException(AuthenticationException e) {
ErrorResponse errorResponse = new ErrorResponse(e.getMessage());
Expand All @@ -38,12 +81,11 @@ public ResponseEntity<ErrorResponse> handleDuplicateKeyException(DuplicateKeyExc
.body(errorResponse);
}

@ExceptionHandler(HandlerMethodValidationException.class)
public ResponseEntity<ErrorResponse> handleHandlerMethodValidationException(
HandlerMethodValidationException e) {
ErrorResponse errorResponse = new ErrorResponse(e.getReason());
@ExceptionHandler(ImageUploadException.class)
public ResponseEntity<ErrorResponse> handleImageUploadException(ImageUploadException e) {
ErrorResponse errorResponse = new ErrorResponse(e.getMessage());

return ResponseEntity.status(HttpStatus.BAD_REQUEST)
return ResponseEntity.status(HttpStatus.UNPROCESSABLE_ENTITY)
.body(errorResponse);
}

Expand Down
Loading

0 comments on commit 7444dbc

Please sign in to comment.