Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/LINKODE-16-BE-API-스터디룸-삭제-API' i…
Browse files Browse the repository at this point in the history
…nto LINKODE-16-BE-API-스터디룸-삭제-API
  • Loading branch information
Mouon committed Jul 5, 2024
2 parents a88d6cd + ccea837 commit d2a86c2
Show file tree
Hide file tree
Showing 10 changed files with 181 additions and 25 deletions.
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;
}
}
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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ public enum BaseExceptionResponseStatus implements ResponseStatus{
* 1000: 요청 성공 (OK)
*/
SUCCESS(1000,HttpStatus.OK.value(), "요청에 성공하였습니다."),
FAILURE(2000, HttpStatus.BAD_REQUEST.value(), "요청에 실패하였습니다.");

FAILURE(2000, HttpStatus.BAD_REQUEST.value(), "요청에 실패하였습니다."),
INVALID_TOKEN(2000, HttpStatus.OK.value(), "유효하지 않은 토큰입니다."),
ALREADY_EXIST_MEMBER(3000, HttpStatus.OK.value(), "이미 존재하는 회원입니다.");

private final int code;
private final int status;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
.csrf(CsrfConfigurer<HttpSecurity>::disable)
.authorizeHttpRequests(requests ->
requests
.requestMatchers("/login", "/oauth2/redirect").permitAll() // 이 URL은 모두에게 허용
.requestMatchers("/login", "/oauth2/redirect","/user/avatar").permitAll() // 이 URL은 모두에게 허용
.anyRequest().authenticated() // 그 외의 모든 요청은 인증 필요
)
.sessionManagement(sessionManagement ->
Expand Down
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!";
}

}
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);
}

}
}
10 changes: 8 additions & 2 deletions src/main/java/com/linkode/api_server/domain/Member.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.linkode.api_server.domain;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.linkode.api_server.domain.Avatar;
import com.linkode.api_server.domain.Data;
import com.linkode.api_server.domain.memberstudyroom.MemberStudyroom;
import com.linkode.api_server.domain.base.BaseStatus;
import com.linkode.api_server.domain.base.BaseTime;
Expand Down Expand Up @@ -62,4 +60,12 @@ public Member(Long memberId, String githubId, String nickname, String color,Base
this.status = status;
this.avatar = avatar;
}

public Member(String githubId, String nickname, Avatar avatar, String color, BaseStatus status) {
this.githubId = githubId;
this.nickname = nickname;
this.color = color;
this.avatar = avatar;
this.status = status;
}
}
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;
}
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 src/main/java/com/linkode/api_server/service/MemberService.java
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);
}
}
}

0 comments on commit d2a86c2

Please sign in to comment.