Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

카페 생성 API 오류 수정 #170

Merged
merged 1 commit into from
Feb 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions src/main/java/shop/cazait/domain/cafe/api/CafeController.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package shop.cazait.domain.cafe.api;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import shop.cazait.domain.cafe.dto.GetCafeRes;
Expand All @@ -30,14 +34,18 @@ public class CafeController {

private final CafeService cafeService;

@PostMapping(value = "/add/master/{masterId}", consumes = {"multipart/form-data"})
@PostMapping(value = "/add/master/{masterId}", consumes = {MediaType.APPLICATION_JSON_VALUE, MediaType.MULTIPART_FORM_DATA_VALUE})
@ApiOperation(value = "카페 등록", notes = "master가 카페를 등록한다.")
@ApiImplicitParam(name = "masterId", value = "마스터 ID")
public SuccessResponse<String> addCafe(@PathVariable Long masterId,
@RequestPart @Valid PostCafeReq postCafeReq,
@RequestPart(required = false) List<MultipartFile> imageFiles) throws JsonProcessingException {
cafeService.addCafe(masterId, postCafeReq, imageFiles);
@RequestParam String json,
@RequestPart(value = "cafeImages", required = false) List<MultipartFile> cafeImage) throws JsonProcessingException {

ObjectMapper objectMapper = new ObjectMapper().registerModule(new JavaTimeModule());
PostCafeReq postCafeReq = objectMapper.readValue(json, new TypeReference<>() {});
cafeService.addCafe(masterId, postCafeReq, cafeImage);
return new SuccessResponse<>("카페 등록 완료");

}

@GetMapping("/all/user/{userId}")
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/shop/cazait/domain/cafe/dto/GetCafeRes.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class GetCafeRes {
@Schema(description = "이름", example = "롬곡")
private String name;
@JsonProperty
@Schema(description = "위치", example = "서울시 중구")
@Schema(description = "위치", example = "서울특별시 광진구 군자동 광나루로17길 18")
private String address;
@JsonProperty
@Schema(description = "경도", example = "127.543215")
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/shop/cazait/domain/cafe/dto/GetCafesRes.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class GetCafesRes {
@Schema(description = "이름", example = "롬곡")
private String name;
@JsonProperty
@Schema(description = "위치", example = "서울시 중구")
@Schema(description = "위치", example = "서울특별시 광진구 군자동 광나루로17길 18")
private String address;
@JsonProperty
@Schema(description = "경도", example = "127.543215")
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/shop/cazait/domain/cafe/dto/PostCafeReq.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
import javax.validation.constraints.NotBlank;

@Schema(description = "카페 정보 등록 및 수정 Request : 카페 등록 및 수정 시 필요한 정보")
@Getter
@Data
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class PostCafeReq {

@Schema(description = "이름", example = "롬곡")
@NotBlank(message = "카페 이름을 입력해주세요.")
private String name;

@Schema(description = "주소", example = "서울시 중구")
@Schema(description = "주소", example = "서울특별시 광진구 군자동 광나루로17길 18")
@NotBlank(message = "카페 위치를 입력해주세요.")
private String address;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package shop.cazait.global.error.exception;

import org.springframework.validation.BindingResult;
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 shop.cazait.domain.cafe.exception.CafeException;
Expand All @@ -10,6 +13,7 @@
import shop.cazait.domain.review.exception.ReviewException;
import shop.cazait.domain.user.exception.UserException;
import shop.cazait.global.common.dto.response.FailResponse;
import shop.cazait.global.error.status.ErrorStatus;

@RestControllerAdvice
public class GlobalExceptionHandler {
Expand Down Expand Up @@ -54,10 +58,21 @@ protected FailResponse handlerReviewException(ReviewException exception) {
return new FailResponse(exception.getError());
}

@ExceptionHandler({ ValidException.class })
protected FailResponse handleValidException(ValidException exception) {
@ExceptionHandler({ MethodArgumentNotValidException.class })
protected FailResponse handleValidException(MethodArgumentNotValidException exception) {
BindingResult bindingResult = exception.getBindingResult();
StringBuilder description = new StringBuilder();

return new FailResponse(exception.getError(), exception.getDescription().toString());
for (FieldError fieldError : bindingResult.getFieldErrors()) {
description.append("[");
description.append(fieldError.getField());
description.append("](은)는");
description.append(fieldError.getDefaultMessage());
description.append(" 입력된 값: [");
description.append(fieldError.getRejectedValue());
description.append("]\n");
}
return new FailResponse(ErrorStatus.INVALID_REQUEST, description.toString());

}

Expand Down