Skip to content

Commit

Permalink
Refactor: 케이크샵 등록 시, 외부 링크 추가
Browse files Browse the repository at this point in the history
Refactor: 케이크샵 등록 시, 외부 링크 추가
  • Loading branch information
YongsHub authored Jun 27, 2024
2 parents 8398528 + 97290bb commit d345783
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR;

import java.sql.SQLException;
import java.util.List;
import java.util.HashMap;
import java.util.Map;

import jakarta.servlet.http.HttpServletRequest;

Expand All @@ -13,7 +14,6 @@
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
Expand Down Expand Up @@ -41,7 +41,7 @@ public ResponseEntity<ApiResponse<Void>> handleCakkException(CakkException excep
if (exception.getReturnCode().equals(ReturnCode.EXTERNAL_SERVER_ERROR)) {
slackService.sendSlackForError(exception, request);
}
logger.debug(exception.getMessage());
logger.error(exception.getMessage());
return getResponseEntity(BAD_REQUEST, ApiResponse.fail(exception.getReturnCode()));
}

Expand All @@ -51,20 +51,27 @@ public ResponseEntity<ApiResponse<Void>> handleCakkException(CakkException excep
MethodArgumentTypeMismatchException.class
})
public ResponseEntity<ApiResponse<Void>> handleRequestException(Exception exception) {
logger.debug(exception.getMessage());
logger.error(exception.getMessage());
return getResponseEntity(BAD_REQUEST, ApiResponse.fail(ReturnCode.WRONG_PARAMETER));
}

@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
public ResponseEntity<ApiResponse<Void>> handleMethodNotSupportedException(HttpRequestMethodNotSupportedException exception) {
logger.debug(exception.getMessage());
logger.error(exception.getMessage());
return getResponseEntity(BAD_REQUEST, ApiResponse.fail(ReturnCode.METHOD_NOT_ALLOWED));
}

@ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntity<ApiResponse<List<FieldError>>> badRequestExHandler(BindingResult bindingResult) {
logger.debug(bindingResult.getFieldErrors().get(0).toString());
return getResponseEntity(BAD_REQUEST, ApiResponse.fail(ReturnCode.WRONG_PARAMETER, bindingResult.getFieldErrors()));
public ResponseEntity<ApiResponse<Map<String, String>>> badRequestExHandler(MethodArgumentNotValidException exception) {
Map<String, String> errors = new HashMap<>();
exception.getBindingResult().getAllErrors().forEach((error) -> {
String fieldName = ((FieldError) error).getField();
String errorMessage = error.getDefaultMessage();
errors.put(fieldName, errorMessage);
});

logger.error("Validation failed: {}", errors);
return getResponseEntity(BAD_REQUEST, ApiResponse.fail(ReturnCode.WRONG_PARAMETER, errors));
}

@ExceptionHandler(value = {
Expand All @@ -73,7 +80,7 @@ public ResponseEntity<ApiResponse<List<FieldError>>> badRequestExHandler(Binding
})
public ResponseEntity<ApiResponse<String>> handleServerException(SQLException exception, HttpServletRequest request) {
slackService.sendSlackForError(exception, request);
logger.debug(exception.getMessage());
logger.error(exception.getMessage());
return getResponseEntity(INTERNAL_SERVER_ERROR, ApiResponse.error(ReturnCode.INTERNAL_SERVER_ERROR, exception.getMessage()));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.cakk.api.dto.request.link;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Size;

import com.cakk.common.enums.LinkKind;

public record ShopLinkParam(
@NotBlank
LinkKind linkKind,
@NotBlank @Size(min = 1, max = 200)
String linkPath
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@
import jakarta.validation.constraints.Size;

import com.cakk.api.annotation.OperationDay;
import com.cakk.api.dto.request.link.ShopLinkParam;
import com.cakk.api.dto.request.operation.ShopOperationParam;


public record CreateShopRequest(
@Size(max = 20)
String businessNumber,
@NotNull @OperationDay
@OperationDay
List<ShopOperationParam> operationDays,
@NotBlank @Size(max = 30)
String shopName,
Expand All @@ -28,6 +29,8 @@ public record CreateShopRequest(
@NotNull @Min(-90) @Max(90)
Double latitude,
@NotNull @Min(-180) @Max(180)
Double longitude
Double longitude,
@NotNull
List<ShopLinkParam> links
) {
}
41 changes: 41 additions & 0 deletions cakk-api/src/main/java/com/cakk/api/mapper/LinkMapper.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package com.cakk.api.mapper;


import java.util.List;
import java.util.stream.Collectors;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;

import com.cakk.api.dto.request.link.ShopLinkParam;
import com.cakk.common.enums.LinkKind;
import com.cakk.domain.mysql.entity.shop.CakeShop;
import com.cakk.domain.mysql.entity.shop.CakeShopLink;

@NoArgsConstructor(access = AccessLevel.PRIVATE)
Expand Down Expand Up @@ -39,4 +44,40 @@ public static CakeShopLink supplyCakeShopLinkByWeb(String web) {
.linkPath(web)
.build();
}

public static CakeShopLink supplyCakeShopLinkByWeb(CakeShop cakeShop, String web) {
return CakeShopLink.builder()
.linkKind(LinkKind.WEB)
.linkPath(web)
.cakeShop(cakeShop)
.build();
}

public static CakeShopLink supplyCakeShopLinkByInstagram(CakeShop cakeShop, String instagram) {
return CakeShopLink.builder()
.linkKind(LinkKind.INSTAGRAM)
.linkPath(instagram)
.cakeShop(cakeShop)
.build();
}

public static CakeShopLink supplyCakeShopLinkByKakao(CakeShop cakeShop, String kakao) {
return CakeShopLink.builder()
.linkKind(LinkKind.KAKAOTALK)
.linkPath(kakao)
.cakeShop(cakeShop)
.build();
}

public static List<CakeShopLink> supplyCakeShopLinksBy(CakeShop cakeShop, List<ShopLinkParam> links) {
return links.stream().map(link -> {
if (link.linkKind() == LinkKind.WEB) {
return supplyCakeShopLinkByWeb(cakeShop, link.linkPath());
} else if (link.linkKind() == LinkKind.INSTAGRAM) {
return supplyCakeShopLinkByInstagram(cakeShop, link.linkPath());
} else {
return supplyCakeShopLinkByKakao(cakeShop, link.linkPath());
}
}).collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.cakk.api.dto.response.shop.CakeShopOwnerResponse;
import com.cakk.api.dto.response.shop.CakeShopSearchResponse;
import com.cakk.api.dto.response.shop.CakeShopSimpleResponse;
import com.cakk.api.mapper.LinkMapper;
import com.cakk.api.mapper.PointMapper;
import com.cakk.api.mapper.ShopMapper;
import com.cakk.domain.mysql.bo.CakeShops;
Expand All @@ -33,6 +34,7 @@
import com.cakk.domain.mysql.dto.param.shop.UpdateShopAddressParam;
import com.cakk.domain.mysql.dto.param.user.CertificationParam;
import com.cakk.domain.mysql.entity.shop.CakeShop;
import com.cakk.domain.mysql.entity.shop.CakeShopLink;
import com.cakk.domain.mysql.entity.shop.CakeShopOperation;
import com.cakk.domain.mysql.entity.user.BusinessInformation;
import com.cakk.domain.mysql.entity.user.User;
Expand All @@ -57,8 +59,9 @@ public CakeShopCreateResponse createCakeShopByCertification(final CreateShopRequ
final CakeShop cakeShop = ShopMapper.supplyCakeShopBy(request);
final BusinessInformation businessInformation = ShopMapper.supplyBusinessInformationBy(request, cakeShop);
final List<CakeShopOperation> cakeShopOperations = ShopMapper.supplyCakeShopOperationsBy(cakeShop, request.operationDays());
final List<CakeShopLink> cakeShopLinks = LinkMapper.supplyCakeShopLinksBy(cakeShop, request.links());

final CakeShop result = cakeShopWriter.createCakeShop(cakeShop, cakeShopOperations, businessInformation);
final CakeShop result = cakeShopWriter.createCakeShop(cakeShop, cakeShopOperations, businessInformation, cakeShopLinks);

return ShopMapper.supplyCakeShopCreateResponseBy(result);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.cakk.api.validator;

import static java.util.Objects.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -15,8 +17,11 @@ public class OperationValidator implements ConstraintValidator<OperationDay, Lis

@Override
public boolean isValid(List<ShopOperationParam> operationParams, ConstraintValidatorContext context) {
Map<Days, Boolean> days = new HashMap<>();
if (isNull(operationParams)) {
return false;
}

Map<Days, Boolean> days = new HashMap<>();
for (ShopOperationParam operationParam : operationParams) {
if (days.containsKey(operationParam.operationDay())) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ void searchDetailById2() {
void createCakeShop() {
//given
CreateShopRequest request = getCreateShopRequestFixture();
when(cakeShopWriter.createCakeShop(any(CakeShop.class), anyList(), any(BusinessInformation.class)))
when(cakeShopWriter.createCakeShop(any(CakeShop.class), anyList(), any(BusinessInformation.class), anyList()))
.thenReturn(getCakeShopFixture());

//when
Expand All @@ -192,7 +192,7 @@ void createCakeShop() {
//verify
assertThat(response.cakeShopId()).isNotNull();
verify(cakeShopWriter, times(1))
.createCakeShop(any(CakeShop.class), anyList(), any(BusinessInformation.class));
.createCakeShop(any(CakeShop.class), anyList(), any(BusinessInformation.class), anyList());
}

@TestWithDisplayName("userId와 cakeShopId가 존재한다면, 해당 userId의 사용자는 Owner로 승격된다")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@

import com.cakk.domain.mysql.annotation.Writer;
import com.cakk.domain.mysql.entity.shop.CakeShop;
import com.cakk.domain.mysql.entity.shop.CakeShopLink;
import com.cakk.domain.mysql.entity.shop.CakeShopOperation;
import com.cakk.domain.mysql.entity.user.BusinessInformation;
import com.cakk.domain.mysql.repository.jpa.BusinessInformationJpaRepository;
import com.cakk.domain.mysql.repository.jpa.CakeShopJpaRepository;
import com.cakk.domain.mysql.repository.jpa.CakeShopLinkJpaRepository;
import com.cakk.domain.mysql.repository.jpa.CakeShopOperationJpaRepository;

@Writer
Expand All @@ -19,14 +21,17 @@ public class CakeShopWriter {
private final BusinessInformationJpaRepository businessInformationJpaRepository;
private final CakeShopJpaRepository cakeShopJpaRepository;
private final CakeShopOperationJpaRepository cakeShopOperationJpaRepository;
private final CakeShopLinkJpaRepository cakeShopLinkJpaRepository;

public CakeShop createCakeShop(
final CakeShop cakeShop,
final List<CakeShopOperation> cakeShopOperations,
final BusinessInformation businessInformation) {
final BusinessInformation businessInformation,
final List<CakeShopLink> cakeShopLinks) {
final CakeShop result = cakeShopJpaRepository.save(cakeShop);
cakeShopOperationJpaRepository.saveAll(cakeShopOperations);
businessInformationJpaRepository.save(businessInformation);
cakeShopLinkJpaRepository.saveAll(cakeShopLinks);
return result;
}
}

0 comments on commit d345783

Please sign in to comment.