-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from Linkode2024/LINKODE-30-BE-API-캐릭터최초설정
�Feat : 캐릭터최초설정(회원가입)
- Loading branch information
Showing
10 changed files
with
182 additions
and
24 deletions.
There are no files selected for viewing
20 changes: 20 additions & 0 deletions
20
src/main/java/com/linkode/api_server/common/exception/MemberException.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 com.linkode.api_server.common.exception; | ||
|
||
import com.linkode.api_server.common.response.status.ResponseStatus; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
public class MemberException extends RuntimeException { | ||
|
||
private final ResponseStatus exceptionStatus; | ||
|
||
public MemberException(ResponseStatus exceptionStatus) { | ||
super(exceptionStatus.getMessage()); | ||
this.exceptionStatus = exceptionStatus; | ||
} | ||
|
||
public MemberException(ResponseStatus exceptionStatus, String message) { | ||
super(message); | ||
this.exceptionStatus = exceptionStatus; | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
...java/com/linkode/api_server/common/exception_handler/MemberExceptionControllerAdvice.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 com.linkode.api_server.common.exception_handler; | ||
|
||
import com.linkode.api_server.common.exception.MemberException; | ||
import com.linkode.api_server.common.response.BaseErrorResponse; | ||
import jakarta.annotation.Priority; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.bind.annotation.ResponseStatus; | ||
import org.springframework.web.bind.annotation.RestControllerAdvice; | ||
|
||
@Slf4j | ||
@Priority(0) | ||
@RestControllerAdvice | ||
public class MemberExceptionControllerAdvice { | ||
@ResponseStatus(HttpStatus.BAD_REQUEST) | ||
@ExceptionHandler(MemberException.class) | ||
public BaseErrorResponse handle_MemberException(MemberException e) { | ||
log.error("[handle_MemberException]", e); | ||
return new BaseErrorResponse(e.getExceptionStatus(), e.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
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
35 changes: 35 additions & 0 deletions
35
src/main/java/com/linkode/api_server/controller/LoginController.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,35 @@ | ||
package com.linkode.api_server.controller; | ||
|
||
import com.linkode.api_server.common.response.BaseResponse; | ||
import com.linkode.api_server.dto.member.LoginResponse; | ||
import com.linkode.api_server.service.LoginService; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestHeader; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@Slf4j | ||
@RequiredArgsConstructor | ||
public class LoginController { | ||
|
||
private final LoginService loginService; | ||
|
||
/** | ||
* 소셜 로그인 | ||
*/ | ||
@GetMapping("/oauth2/redirect") | ||
public BaseResponse<LoginResponse> githubLogin(@RequestParam String code) { | ||
log.info("[MemberController.githubLogin]"); | ||
return new BaseResponse<>(loginService.githubLogin(code)); | ||
} | ||
|
||
@GetMapping("/test") | ||
public String test(@RequestHeader("authorization") String authorization){ | ||
|
||
return "success!"; | ||
} | ||
|
||
} |
40 changes: 20 additions & 20 deletions
40
src/main/java/com/linkode/api_server/controller/MemberController.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 |
---|---|---|
@@ -1,35 +1,35 @@ | ||
package com.linkode.api_server.controller; | ||
|
||
import com.linkode.api_server.common.response.BaseResponse; | ||
import com.linkode.api_server.dto.member.LoginResponse; | ||
import com.linkode.api_server.service.LoginService; | ||
import com.linkode.api_server.common.response.status.BaseExceptionResponseStatus; | ||
import com.linkode.api_server.dto.member.CreateAvatarRequest; | ||
import com.linkode.api_server.service.MemberService; | ||
import com.linkode.api_server.util.JwtProvider; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestHeader; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.RestController; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
@RestController | ||
@Slf4j | ||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("/user") | ||
public class MemberController { | ||
|
||
private final LoginService loginService; | ||
@Autowired | ||
private final MemberService memberService; | ||
|
||
@Autowired | ||
private final JwtProvider jwtProvider; | ||
|
||
/** | ||
* 소셜 로그인 | ||
* 캐릭터 생성(회원가입) | ||
*/ | ||
@GetMapping("/oauth2/redirect") | ||
public BaseResponse<LoginResponse> githubLogin(@RequestParam String code) { | ||
log.info("[MemberController.githubLogin]"); | ||
return new BaseResponse<>(loginService.githubLogin(code)); | ||
} | ||
|
||
@GetMapping("/test") | ||
public String test(@RequestHeader("authorization") String authorization){ | ||
|
||
return "success!"; | ||
@PostMapping("/avatar") | ||
public BaseResponse<Void> createAvatar(@RequestBody CreateAvatarRequest createAvatarRequest) { | ||
log.info("[MemberController.createAvatar]"); | ||
memberService.createAvatar(createAvatarRequest); | ||
return new BaseResponse<>(null); | ||
} | ||
|
||
} | ||
} |
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
17 changes: 17 additions & 0 deletions
17
src/main/java/com/linkode/api_server/dto/member/CreateAvatarRequest.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 com.linkode.api_server.dto.member; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
public class CreateAvatarRequest { | ||
|
||
/** | ||
* 캐릭터 생성 (회원 가입) | ||
*/ | ||
private String githubId; | ||
private String nickname; | ||
private Long avatarId; | ||
private String color; | ||
} |
9 changes: 9 additions & 0 deletions
9
src/main/java/com/linkode/api_server/repository/AvatarRepository.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 com.linkode.api_server.repository; | ||
|
||
import com.linkode.api_server.domain.Avatar; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface AvatarRepository extends JpaRepository<Avatar, Long> { | ||
|
||
|
||
} |
46 changes: 46 additions & 0 deletions
46
src/main/java/com/linkode/api_server/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,46 @@ | ||
package com.linkode.api_server.service; | ||
|
||
import com.linkode.api_server.common.exception.MemberException; | ||
import com.linkode.api_server.common.response.status.BaseExceptionResponseStatus; | ||
import com.linkode.api_server.domain.Avatar; | ||
import com.linkode.api_server.domain.Member; | ||
import com.linkode.api_server.domain.base.BaseStatus; | ||
import com.linkode.api_server.dto.member.CreateAvatarRequest; | ||
import com.linkode.api_server.repository.AvatarRepository; | ||
import com.linkode.api_server.repository.MemberRepository; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import static com.linkode.api_server.common.response.status.BaseExceptionResponseStatus.ALREADY_EXIST_MEMBER; | ||
|
||
@Slf4j | ||
@Service | ||
@RequiredArgsConstructor | ||
public class MemberService { | ||
|
||
private final MemberRepository memberRepository; | ||
private final AvatarRepository avatarRepository; | ||
|
||
/** | ||
* 캐릭터 생성(회원가입) | ||
*/ | ||
@Transactional | ||
public void createAvatar(CreateAvatarRequest createAvatarRequest){ | ||
log.info("[MemberService.createAvatar]"); | ||
String githubId = createAvatarRequest.getGithubId(); | ||
if(memberRepository.existsByGithubIdAndStatus(githubId, BaseStatus.ACTIVE)){ | ||
throw new MemberException(ALREADY_EXIST_MEMBER); | ||
} else{ | ||
String nickname = createAvatarRequest.getNickname(); | ||
Long avatarId = createAvatarRequest.getAvatarId(); | ||
Avatar avatar = avatarRepository.findById(avatarId) | ||
.orElseThrow(()-> new IllegalArgumentException("Invalid avatarId: " + avatarId)); | ||
String color = createAvatarRequest.getColor(); | ||
|
||
Member member = new Member(githubId, nickname, avatar, color, BaseStatus.ACTIVE); | ||
memberRepository.save(member); | ||
} | ||
} | ||
} |