Skip to content

Commit

Permalink
Merge pull request #7 from lotteon2/dev-user-service
Browse files Browse the repository at this point in the history
Dev user service
  • Loading branch information
nowgnas authored Dec 14, 2023
2 parents 04d8b79 + 91e9d25 commit 9064395
Show file tree
Hide file tree
Showing 14 changed files with 133 additions and 52 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.bit.lotte.flower.user.common.exception;

import com.bit.lotte.flower.user.social.exception.SocialUserDomainException;
import com.bit.lotte.flower.user.store.exception.StoreUserDomainException;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
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;

@RestControllerAdvice
public class GlobalExceptionHandler {

@ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntity<Map<String, List<String>>> handleValidationErrors(
MethodArgumentNotValidException ex) {
List<String> errors = ex.getBindingResult().getFieldErrors()
.stream().map(FieldError::getDefaultMessage).collect(Collectors.toList());
return new ResponseEntity<>(getErrorsMap(errors), new HttpHeaders(), HttpStatus.BAD_REQUEST);
}

private Map<String, List<String>> getErrorsMap(List<String> errors) {
Map<String, List<String>> errorResponse = new HashMap<>();
errorResponse.put("errors", errors);
return errorResponse;
}



@ExceptionHandler(SocialUserDomainException.class)
public ResponseEntity<Map<String, List<String>>> emailCodeException(
SocialUserDomainException ex) {
List<String> errors = Collections.singletonList(ex.getMessage());
return new ResponseEntity<>(getErrorsMap(errors), new HttpHeaders(), HttpStatus.BAD_REQUEST);
}
@ExceptionHandler(StoreUserDomainException.class)
public ResponseEntity<Map<String, List<String>>> storeManagerException(
StoreUserDomainException ex) {
List<String> errors = Collections.singletonList(ex.getMessage());
return new ResponseEntity<>(getErrorsMap(errors), new HttpHeaders(), HttpStatus.BAD_REQUEST);
}



@ExceptionHandler(Exception.class)
public final ResponseEntity<Map<String, List<String>>> handleGeneralExceptions(Exception ex) {
List<String> errors = Collections.singletonList(ex.getMessage());
return new ResponseEntity<>(getErrorsMap(errors), new HttpHeaders(),
HttpStatus.INTERNAL_SERVER_ERROR);
}

@ExceptionHandler(RuntimeException.class)
public final ResponseEntity<Map<String, List<String>>> handleRuntimeExceptions(
RuntimeException ex) {
List<String> errors = Collections.singletonList(ex.getMessage());
return new ResponseEntity<>(getErrorsMap(errors), new HttpHeaders(),
HttpStatus.INTERNAL_SERVER_ERROR);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public class UserDataDto {
private String nickname;
private String phoneNumber;
private String email;
private String profileImage;



}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;


@FeignClient(name = "get-user-likes-cnt", value = "${service.likes.domain}")
@FeignClient(name = "get-user-likes-cnt", url = "${service.likes.domain}")
public interface UserLikesFeignRequest {

@GetMapping("/likes-cnt")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;

@FeignClient(name = "get-user-usable-coupon-cnt", value = "${service.coupon.domain}")
@FeignClient(name = "get-user-usable-coupon-cnt", url = "${service.coupon.domain}")
public interface UserUsableCouponCntFeignRequest {
@GetMapping("/coupons/count")
ResponseEntity<Long> getUsableUserCouponCnt(@RequestHeader Long userId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public static UserLoginDataResponse createUserLoginCommandBySocialUser(String pr

public static UserDataDto socialUserToUserMyPageDataResponse(SocialUser socialUser) {
return UserDataDto.builder()
.email(socialUser.getEmail()).nickname(socialUser.getNickname())
.email(socialUser.getEmail()).nickname(socialUser.getNickname()).profileImage(socialUser.getProfileImage())
.phoneNumber(socialUser.getPhoneNumber()).build();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.bit.lotte.flower.user.store.http.feign;

import com.bit.lotte.flower.user.common.valueobject.BaseId;
import com.bit.lotte.flower.user.store.http.feign.dto.UpdateStoreManagerPendingStausDto;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.RequestBody;

@FeignClient(name = "init-storemanager-status",value = "${service.auth.domain}")
public interface InitStoreManagerStatusPendingFeignRequest<ID extends BaseId> {
public void publish(@RequestHeader ID userId);
@FeignClient(name = "init-storemanager-status",url = "${service.auth.domain}")
public interface InitStoreManagerStatusPendingFeignRequest {
@PatchMapping("/admin/store-manager")
public void publish(@RequestBody UpdateStoreManagerPendingStausDto dto);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.bit.lotte.flower.user.store.http.feign.dto;

import javax.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Builder
@AllArgsConstructor
@NoArgsConstructor
@Getter
public class UpdateStoreManagerPendingStausDto {

@NotNull
private final String status = "ROLE_STORE_MANAGER_PENDING";
@NotNull
private Long storeManagerId;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.bit.lotte.flower.user.common.valueobject.UserId;
import com.bit.lotte.flower.user.store.http.feign.InitStoreManagerStatusPendingFeignRequest;
import com.bit.lotte.flower.user.store.http.feign.dto.UpdateStoreManagerPendingStausDto;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

Expand All @@ -10,11 +11,10 @@
public class InitStoreManagerAuthorizationPublisherImpl implements
InitStoreManagerAuthorizationPublisher<UserId> {

private final InitStoreManagerStatusPendingFeignRequest<UserId> request;

private final InitStoreManagerStatusPendingFeignRequest request;

@Override
public void publish(UserId storeManagerId) {
request.publish(storeManagerId);
request.publish(new UpdateStoreManagerPendingStausDto(storeManagerId.getValue()));
}
}
27 changes: 8 additions & 19 deletions src/main/resources/application-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,12 @@ spring:
config:
activate:
on-profile: dev
import: optional:configserver:http://localhost:8888
import: optional:configserver:http://config-service:8888

jpa:
show-sql: true
hibernate:
ddl-auto: update

feign:
client:
config:
default:
connectTimeout: 5000
readTimeout: 5000
loggerLevel: basic

service:
coupon:
domain : ${service.coupon.domain}
likes:
domain : ${servive.likes.domain}
management:
endpoints:
web:
exposure:
include:
- "refresh"
- "bus-refresh"
22 changes: 9 additions & 13 deletions src/main/resources/application-local.yml
Original file line number Diff line number Diff line change
@@ -1,30 +1,26 @@

server:
port: 8600

spring:
config:
activate:
on-profile: local
datasource:
url: jdbc:mysql://localhost:3306/bb-user-service?serverTimezone=Asia/Seoul
url: jdbc:mysql://localhost:3306/develop?serverTimezone=Asia/Seoul&useUnicode=true&characterEncoding=utf8
username: root
password: k1651227
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
jpa:
hibernate:
ddl-auto: update
show-sql: true

feign:
client:
default:
url: http://localhost:8082
config:
default:
connectTimeout: 5000
readTimeout: 5000
loggerLevel: basic

service:
coupon:
domain : ${service.coupon.domain}
domain : http://localhost:9999
likes:
domain : ${servive.likes.domain}
domain : http://localhost:8888
auth:
domain : http://localhost:9000
2 changes: 1 addition & 1 deletion src/main/resources/application-prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ spring:
jpa:
show-sql: true
hibernate:
ddl-auto: update
ddl-auto: none

feign:
client:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.bit.lotte.flower.user.store.entity.StoreManager;
import com.bit.lotte.flower.user.store.exception.StoreUserDomainException;
import com.bit.lotte.flower.user.store.repository.StoreManagerJpaRepository;
import com.bit.lotte.flower.user.store.service.FindStoreMangerByLongIdService;
import com.bit.lotte.flower.user.store.service.StoreManagerLoginResponseService;
import java.util.Optional;
import org.junit.jupiter.api.DisplayName;
Expand All @@ -25,10 +26,13 @@
@ExtendWith(MockitoExtension.class)
class StoreManagerLoginResponseTest {

@InjectMocks
StoreManagerLoginResponseService loginResponseService;
@Mock
StoreManagerJpaRepository repository;
@Mock
FindStoreMangerByLongIdService findStoreMangerByLongIdService;
@InjectMocks
StoreManagerLoginResponseService loginResponseService;

private final Long validStoreManagerId = 1L;


Expand All @@ -40,7 +44,7 @@ private StoreManager initUser() {
@DisplayName("스토어 매니저 로그인시 유저가 존재할 때 유저 네임 반환")
@Test
void UserNameResponse_WhenUserIsExist_GetUserName() {
when(repository.findById(validStoreManagerId)).thenReturn(Optional.of(initUser()));
when(findStoreMangerByLongIdService.findByLongId(validStoreManagerId)).thenReturn(initUser());

StoreManagerLoginResponse response = loginResponseService.getStoreManagerResponse(
validStoreManagerId);
Expand All @@ -51,8 +55,7 @@ void UserNameResponse_WhenUserIsExist_GetUserName() {
@Test
void UserNameResponse_WhenUserIsExist_ThrowStoreUserDomainException() {

when(repository.findById(anyLong())).thenReturn(Optional.empty());

when(findStoreMangerByLongIdService.findByLongId(validStoreManagerId)).thenThrow(StoreUserDomainException.class);
assertThrows(StoreUserDomainException.class, () -> {
loginResponseService.getStoreManagerResponse(validStoreManagerId);
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.bit.lotte.flower.user.store.service;
package com.bit.lotte.flower.user.store.service.mypage;


import static org.junit.jupiter.api.Assertions.assertNotNull;
Expand All @@ -11,6 +11,7 @@
import com.bit.lotte.flower.user.store.entity.StoreManager;
import com.bit.lotte.flower.user.store.exception.StoreUserDomainException;
import com.bit.lotte.flower.user.store.repository.StoreManagerJpaRepository;
import com.bit.lotte.flower.user.store.service.FindStoreMangerByLongIdService;
import java.util.Optional;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.bit.lotte.flower.user.store.service;
package com.bit.lotte.flower.user.store.service.mypage;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.any;
Expand All @@ -14,6 +14,8 @@
import com.bit.lotte.flower.user.store.entity.StoreManager;
import com.bit.lotte.flower.user.store.exception.StoreUserDomainException;
import com.bit.lotte.flower.user.store.repository.StoreManagerJpaRepository;
import com.bit.lotte.flower.user.store.service.FindStoreMangerByLongIdService;
import com.bit.lotte.flower.user.store.service.StoreManagerServiceImpl;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
Expand Down

0 comments on commit 9064395

Please sign in to comment.