-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
feat: 사용자 조회, 탈퇴 API 구현
- Loading branch information
Showing
16 changed files
with
228 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
user-service/src/main/java/kr/mafoo/user/annotation/RequestMemberId.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package kr.mafoo.user.annotation; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target({ElementType.PARAMETER, ElementType.TYPE_PARAMETER}) | ||
public @interface RequestMemberId { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
user-service/src/main/java/kr/mafoo/user/config/MemberIdParameterResolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package kr.mafoo.user.config; | ||
|
||
|
||
import kr.mafoo.user.annotation.RequestMemberId; | ||
import org.springframework.core.MethodParameter; | ||
import org.springframework.web.reactive.BindingContext; | ||
import org.springframework.web.reactive.result.method.HandlerMethodArgumentResolver; | ||
import org.springframework.web.server.ServerWebExchange; | ||
import reactor.core.publisher.Mono; | ||
|
||
public class MemberIdParameterResolver implements HandlerMethodArgumentResolver { | ||
@Override | ||
public boolean supportsParameter(MethodParameter parameter) { | ||
return parameter.getParameterAnnotation(RequestMemberId.class) != null; | ||
} | ||
|
||
@Override | ||
public Mono<Object> resolveArgument(MethodParameter parameter, BindingContext bindingContext, ServerWebExchange exchange) { | ||
String memberId = exchange.getRequest().getHeaders().getFirst("X-MEMBER-ID"); | ||
return Mono.justOrEmpty(memberId); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
user-service/src/main/java/kr/mafoo/user/config/WebExceptionHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package kr.mafoo.user.config; | ||
|
||
import kr.mafoo.user.controller.dto.response.ErrorResponse; | ||
import kr.mafoo.user.exception.DomainException; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.ControllerAdvice; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
|
||
@ControllerAdvice | ||
public class WebExceptionHandler { | ||
@ExceptionHandler(DomainException.class) | ||
public ResponseEntity<ErrorResponse> handleDomainException(DomainException exception) { | ||
return ResponseEntity | ||
.badRequest() | ||
.body(ErrorResponse.fromErrorCode(exception.getErrorCode())); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
user-service/src/main/java/kr/mafoo/user/config/WebFluxConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package kr.mafoo.user.config; | ||
|
||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.web.reactive.config.EnableWebFlux; | ||
import org.springframework.web.reactive.config.WebFluxConfigurer; | ||
import org.springframework.web.reactive.result.method.annotation.ArgumentResolverConfigurer; | ||
|
||
@EnableWebFlux | ||
@Configuration | ||
public class WebFluxConfig implements WebFluxConfigurer { | ||
@Override | ||
public void configureArgumentResolvers(ArgumentResolverConfigurer configurer) { | ||
configurer.addCustomResolver(new MemberIdParameterResolver()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
user-service/src/main/java/kr/mafoo/user/controller/dto/response/ErrorResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package kr.mafoo.user.controller.dto.response; | ||
|
||
import io.swagger.v3.oas.annotations.media.Schema; | ||
import kr.mafoo.user.exception.ErrorCode; | ||
|
||
@Schema(description = "에러 응답") | ||
public record ErrorResponse( | ||
@Schema(description = "에러 코드", example = "ME0001") | ||
String code, | ||
|
||
@Schema(description = "에러 메시지", example = "사용자를 찾을 수 없습니다") | ||
String message | ||
) { | ||
public static ErrorResponse fromErrorCode(ErrorCode errorCode) { | ||
return new ErrorResponse( | ||
errorCode.getCode(), | ||
errorCode.getMessage() | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
user-service/src/main/java/kr/mafoo/user/domain/MemberEntity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package kr.mafoo.user.domain; | ||
|
||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
import org.springframework.data.annotation.CreatedDate; | ||
import org.springframework.data.annotation.Id; | ||
import org.springframework.data.relational.core.mapping.Column; | ||
import org.springframework.data.relational.core.mapping.Table; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
@Getter | ||
@Setter | ||
@NoArgsConstructor | ||
@Table("member") | ||
public class MemberEntity { | ||
@Id | ||
@Column("member_id") | ||
private String id; | ||
|
||
@Column("name") | ||
private String name; | ||
|
||
@CreatedDate | ||
@Column("created_at") | ||
private LocalDateTime createdAt; | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (this == obj) return true; | ||
if (obj == null || getClass() != obj.getClass()) return false; | ||
|
||
MemberEntity that = (MemberEntity) obj; | ||
|
||
return id.equals(that.id); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return id.hashCode(); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
user-service/src/main/java/kr/mafoo/user/exception/DomainException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package kr.mafoo.user.exception; | ||
|
||
import lombok.Getter; | ||
|
||
@Getter | ||
public class DomainException extends RuntimeException { | ||
private final ErrorCode errorCode; | ||
|
||
public DomainException(ErrorCode errorCode) { | ||
super(errorCode.getMessage()); | ||
this.errorCode = errorCode; | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
user-service/src/main/java/kr/mafoo/user/exception/ErrorCode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package kr.mafoo.user.exception; | ||
|
||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
@Getter | ||
@RequiredArgsConstructor | ||
public enum ErrorCode { | ||
MEMBER_NOT_FOUND("ME0001", "사용자를 찾을 수 없습니다"); | ||
private final String code; | ||
private final String message; | ||
} |
7 changes: 7 additions & 0 deletions
7
user-service/src/main/java/kr/mafoo/user/exception/MemberNotFoundException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package kr.mafoo.user.exception; | ||
|
||
public class MemberNotFoundException extends DomainException { | ||
public MemberNotFoundException() { | ||
super(ErrorCode.MEMBER_NOT_FOUND); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
user-service/src/main/java/kr/mafoo/user/repository/MemberRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package kr.mafoo.user.repository; | ||
|
||
import kr.mafoo.user.domain.MemberEntity; | ||
import org.springframework.data.r2dbc.repository.R2dbcRepository; | ||
import reactor.core.publisher.Mono; | ||
|
||
public interface MemberRepository extends R2dbcRepository<MemberEntity, String> { | ||
Mono<Void> deleteMemberById(String memberId); | ||
} |
25 changes: 25 additions & 0 deletions
25
user-service/src/main/java/kr/mafoo/user/service/MemberService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package kr.mafoo.user.service; | ||
|
||
import kr.mafoo.user.domain.MemberEntity; | ||
import kr.mafoo.user.exception.MemberNotFoundException; | ||
import kr.mafoo.user.repository.MemberRepository; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import reactor.core.publisher.Mono; | ||
|
||
@RequiredArgsConstructor | ||
@Service | ||
public class MemberService { | ||
private final MemberRepository memberRepository; | ||
|
||
public Mono<Void> quitMemberByMemberId(String memberId) { | ||
return memberRepository.deleteMemberById(memberId); | ||
} | ||
|
||
public Mono<MemberEntity> getMemberByMemberId(String memberId) { | ||
return memberRepository | ||
.findById(memberId) | ||
.switchIfEmpty(Mono.error(new MemberNotFoundException())); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters