Skip to content

Commit

Permalink
✨ /api/auth/member API 기능 추가 (#4)
Browse files Browse the repository at this point in the history
* Feat: Member DB 수정

- 한글이름, 영어이름, 기수, 과정 추가
- 과정 표현하는 Enumerate 추가
- 수정에 따른 오류 수정
  - getNickname -> getKoreaName of getEnglishName

Related to: #2

* Rename: Member DB Field 이름 변경

Related to: #2

* Feat: /api/auth/member 관련 DTO 작성

Related to: #2

* Feat: /api/auth/member API 관련 DTO 내용 추가

Related to: #2

* Feat: /api/auth/member 관련 MemberService 추가

Related to: #2

* Feat: /api/auth/member 관련 MemberContoller 추가

Related to: #2

* Feat: CORS Error 수정

Related to: #2

* Fix: RefreshToken 매번 생성 오류 수정

Related to: #2

* Refactor: 코드 리뷰 내용 반영

- 불필요 코드 삭제
- 반환값 오류 수정

Related to: #2
  • Loading branch information
Taejin1221 authored Sep 2, 2024
1 parent 90052aa commit ea9533e
Show file tree
Hide file tree
Showing 15 changed files with 300 additions and 8 deletions.
15 changes: 15 additions & 0 deletions src/main/java/kaboo/kaboo_auth/config/SecurityConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

import kaboo.kaboo_auth.domain.handler.LoginSuccessHandler;
import kaboo.kaboo_auth.domain.jwt.filter.JwtFilter;
Expand Down Expand Up @@ -45,4 +48,16 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {

return http.build();
}

@Bean
public CorsFilter corsFilter() {
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
CorsConfiguration config = new CorsConfiguration();
config.setAllowCredentials(true);
config.addAllowedOriginPattern("*"); // 모든 도메인 허용. 필요에 따라 변경
config.addAllowedHeader("*");
config.addAllowedMethod("*");
source.registerCorsConfiguration("/**", config);
return new CorsFilter(source);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package kaboo.kaboo_auth.controller;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import kaboo.kaboo_auth.domain.dto.response.ResponseDTO;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestControllerAdvice
public class MainControllerAdvice {

@ExceptionHandler({IllegalStateException.class, UsernameNotFoundException.class})
public ResponseEntity<ResponseDTO<?>> exceptionHandler(Exception e) {
log.error("[Kaboo-Auth]: 예외가 발생하였습니다. {}", e.getMessage());
return ResponseEntity.status(HttpStatus.OK)
.body(new ResponseDTO<>(
true,
e.getMessage(),
null
));
}
}
86 changes: 86 additions & 0 deletions src/main/java/kaboo/kaboo_auth/controller/MemberController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package kaboo.kaboo_auth.controller;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import kaboo.kaboo_auth.domain.dto.request.MemberInfoUpdateRequest;
import kaboo.kaboo_auth.domain.dto.response.MemberInfoResponse;
import kaboo.kaboo_auth.domain.dto.response.MemberListResponse;
import kaboo.kaboo_auth.domain.dto.response.ResponseDTO;
import kaboo.kaboo_auth.service.MemberService;
import lombok.RequiredArgsConstructor;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/auth/member")
public class MemberController {

private final MemberService memberService;

@GetMapping("/all")
public ResponseEntity<ResponseDTO<MemberListResponse>> getAllMembers() {
return ResponseEntity.status(HttpStatus.OK)
.body(new ResponseDTO<>(
memberService.getAllMembers()
));
}

@GetMapping("/class/{class}")
public ResponseEntity<ResponseDTO<MemberListResponse>> getClassMembers(
@PathVariable(name = "class") int classNum) {

return ResponseEntity.status(HttpStatus.OK)
.body(new ResponseDTO<>(
memberService.getMembersByClassNum(classNum)
));
}

@GetMapping
public ResponseEntity<ResponseDTO<MemberInfoResponse>> getMemberInfo(
@RequestParam(name = "name", defaultValue = "") String koreaName) {

return ResponseEntity.status(HttpStatus.OK)
.body(new ResponseDTO<>(
memberService.getMemberInfoByKoreaName(koreaName)
));
}

@PostMapping
public ResponseEntity<ResponseDTO<MemberInfoResponse>> updateMemberInfo(
@RequestParam(name = "name", defaultValue = "") String koreaName,
@RequestBody MemberInfoUpdateRequest request) {

return ResponseEntity.status(HttpStatus.OK)
.body(new ResponseDTO<>(
memberService.updateMemberInfoByKoreaName(koreaName, request)
));
}

@GetMapping("/introduce")
public ResponseEntity<ResponseDTO<String>> getMemberIntroduce(
@RequestParam(name = "name", defaultValue = "") String koreaName) {

return ResponseEntity.status(HttpStatus.OK)
.body(new ResponseDTO<>(
memberService.getMemberIntroduceByKoreaName(koreaName)
));
}

@PostMapping("/introduce")
public ResponseEntity<ResponseDTO<String>> updateMemberIntrouce(
@RequestParam(name = "name", defaultValue = "") String koreaName,
@RequestBody String request) {

return ResponseEntity.status(HttpStatus.OK)
.body(new ResponseDTO<>(
memberService.updateMemberIntroduceByKoreaName(koreaName, request)
));
}
}
13 changes: 13 additions & 0 deletions src/main/java/kaboo/kaboo_auth/domain/Course.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package kaboo.kaboo_auth.domain;

public enum Course {
AI("GenAI"),
FULLSTACK("Fullstack"),
CLOUD("Cloud");

private final String course;

Course(String course) {
this.course = course;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public String getUsername() {

@Override
public String getName() {
return member.getNickname();
return member.getKoreaName();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package kaboo.kaboo_auth.domain.dto.request;

import kaboo.kaboo_auth.domain.Course;
import lombok.Getter;

@Getter
public class MemberInfoUpdateRequest {
private String koreaName;
private String englishName;
private int classNum;
private Course course;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package kaboo.kaboo_auth.domain.dto.response;

import kaboo.kaboo_auth.domain.Course;
import kaboo.kaboo_auth.domain.entity.Member;
import lombok.Getter;

@Getter
public class MemberInfoResponse {
private final String koreaName;
private final String englishName;
private final int classNum;
private final Course course;

public MemberInfoResponse(Member member) {
this.koreaName = member.getKoreaName();
this.englishName = member.getEnglishName();
this.classNum = member.getClassNum();
this.course = member.getCourse();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package kaboo.kaboo_auth.domain.dto.response;

import java.util.List;

import kaboo.kaboo_auth.domain.entity.Member;
import lombok.Getter;

@Getter
public class MemberListResponse {
private final int classNum;
private final int memberNum;
private final List<MemberInfoResponse> memberList;

public MemberListResponse(List<Member> members, int classNum) {
this.classNum = classNum;
this.memberNum = members.size();
this.memberList = members.stream()
.map(MemberInfoResponse::new)
.toList();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package kaboo.kaboo_auth.domain.dto.response;

import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
public class ResponseDTO<T> {
private boolean success;
private String message;
private T data;

public ResponseDTO(T data) {
success = true;
message = "요청이 성공적으로 처리되었습니다.";
this.data = data;
}
}
34 changes: 30 additions & 4 deletions src/main/java/kaboo/kaboo_auth/domain/entity/Member.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import kaboo.kaboo_auth.domain.Course;
import kaboo.kaboo_auth.domain.UserRole;
import lombok.Builder;
import lombok.Getter;
Expand All @@ -23,19 +24,44 @@ public class Member {

private String username;
private String email;
private String nickname;
private String koreaName;
private String englishName;
private String password;
private String info;

@Column(columnDefinition = "TEXT")
private String introduce;

private int classNum;

@Enumerated(EnumType.STRING)
private Course course;

@Enumerated(EnumType.STRING)
private UserRole role;

@Builder
public Member(String username, String email, String nickname, String password, UserRole role) {
public Member(String username, String email, String koreaName, String englishName, String password,
String introduce,
int classNum, Course course, UserRole role) {
this.username = username;
this.email = email;
this.nickname = nickname;
this.koreaName = koreaName;
this.englishName = englishName;
this.password = password;
this.introduce = introduce;
this.classNum = classNum;
this.course = course;
this.role = role;
}

public void updateInfo(String koreaName, String englishName, int classNum, Course course) {
this.koreaName = koreaName;
this.englishName = englishName;
this.classNum = classNum;
this.course = course;
}

public void updateIntroduce(String introduce) {
this.introduce = introduce;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse

SecurityContextHolder.getContext().setAuthentication(authentication);
chain.doFilter(request, response);

return;
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package kaboo.kaboo_auth.repository;

import java.util.List;
import java.util.Optional;

import org.springframework.data.jpa.repository.JpaRepository;
Expand All @@ -8,4 +9,8 @@

public interface MemberRepository extends JpaRepository<Member, Long> {
Optional<Member> findByUsername(String username);

List<Member> findByClassNum(int classNum);

Optional<Member> findByKoreaName(String koreaName);
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public OAuth2User loadUser(OAuth2UserRequest userRequest) throws OAuth2Authentic
String rawPassword = username + passwordPostfix;
member = Member.builder()
.username(username)
.nickname(nickname)
.koreaName(nickname)
.email(email)
.password(passwordEncoder.encode(rawPassword))
.role(UserRole.ROLE_USER)
Expand Down
47 changes: 47 additions & 0 deletions src/main/java/kaboo/kaboo_auth/service/MemberService.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
package kaboo.kaboo_auth.service;

import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import kaboo.kaboo_auth.domain.dto.request.MemberInfoUpdateRequest;
import kaboo.kaboo_auth.domain.dto.response.MemberInfoResponse;
import kaboo.kaboo_auth.domain.dto.response.MemberListResponse;
import kaboo.kaboo_auth.domain.entity.Member;
import kaboo.kaboo_auth.repository.MemberRepository;
import lombok.RequiredArgsConstructor;
Expand All @@ -16,4 +22,45 @@ public Member getMemberByUsername(String username) {
new IllegalStateException("존재하지 않는 ID 입니다.")
);
}

public MemberListResponse getAllMembers() {
List<Member> members = memberRepository.findAll();
return new MemberListResponse(members, 0);
}

public MemberListResponse getMembersByClassNum(int classNum) {
List<Member> members = memberRepository.findByClassNum(classNum);
return new MemberListResponse(members, classNum);
}

public MemberInfoResponse getMemberInfoByKoreaName(String koreaName) {
return new MemberInfoResponse(
getMember(koreaName));
}

@Transactional
public MemberInfoResponse updateMemberInfoByKoreaName(String koreaName, MemberInfoUpdateRequest request) {
Member member = getMember(koreaName);

member.updateInfo(request.getKoreaName(), request.getEnglishName(), request.getClassNum(), request.getCourse());

return new MemberInfoResponse(member);
}

public String getMemberIntroduceByKoreaName(String koreaName) {
return getMember(koreaName).getIntroduce();
}

@Transactional
public String updateMemberIntroduceByKoreaName(String koreaName, String request) {
Member member = getMember(koreaName);
member.updateIntroduce(request);

return member.getIntroduce();
}

private Member getMember(String koreaName) {
return memberRepository.findByKoreaName(koreaName)
.orElseThrow(() -> new IllegalStateException(koreaName + "을 찾을 수 없습니다. 다시 한번 확인해주세요."));
}
}
Loading

0 comments on commit ea9533e

Please sign in to comment.