Skip to content

Commit

Permalink
Merge pull request #15 from PawWithU/feat/13-entity-divide
Browse files Browse the repository at this point in the history
[Feature] 이동봉사자, 이동봉사 중개 Entity 분리
  • Loading branch information
kyeong-hyeok authored Oct 27, 2023
2 parents 433fd98 + 43dfdb0 commit 2812f46
Show file tree
Hide file tree
Showing 15 changed files with 144 additions and 105 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.pawwithu.connectdog.domain.auth.controller;

import com.pawwithu.connectdog.domain.auth.dto.request.SignUpRequest;
import com.pawwithu.connectdog.domain.auth.dto.request.VolunteerSignUpRequest;
import com.pawwithu.connectdog.domain.auth.service.AuthService;
import com.pawwithu.connectdog.error.dto.ErrorResponse;
import io.swagger.v3.oas.annotations.Operation;
Expand All @@ -9,7 +9,6 @@
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
Expand All @@ -31,8 +30,8 @@ public class SignUpController {
, content = @Content(schema = @Schema(implementation = ErrorResponse.class)))
})
@PostMapping
public ResponseEntity<Void> signUp(@RequestBody SignUpRequest signUpRequest) {
authService.signUp(signUpRequest);
public ResponseEntity<Void> signUp(@RequestBody VolunteerSignUpRequest volunteerSignUpRequest) {
authService.signUp(volunteerSignUpRequest);
return ResponseEntity.noContent().build();
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.pawwithu.connectdog.domain.auth.dto.request;

import com.pawwithu.connectdog.domain.volunteer.entity.Volunteer;
import com.pawwithu.connectdog.domain.volunteer.entity.VolunteerRole;

public record VolunteerSignUpRequest(String email, String password, String nickname, Boolean isOptionAgr) {

public Volunteer toEntity() {
return Volunteer.builder()
.email(email)
.password(password)
.nickname(nickname)
.isOptionAgr(isOptionAgr)
.role(VolunteerRole.USER)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.pawwithu.connectdog.domain.auth.service;

import com.pawwithu.connectdog.domain.auth.dto.request.SignUpRequest;
import com.pawwithu.connectdog.domain.member.entity.Member;
import com.pawwithu.connectdog.domain.member.repository.MemberRepository;
import com.pawwithu.connectdog.domain.auth.dto.request.VolunteerSignUpRequest;
import com.pawwithu.connectdog.domain.volunteer.entity.Volunteer;
import com.pawwithu.connectdog.domain.volunteer.repository.VolunteerRepository;
import com.pawwithu.connectdog.error.exception.custom.BadRequestException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -19,21 +19,21 @@
@RequiredArgsConstructor
public class AuthService {

private final MemberRepository memberRepository;
private final VolunteerRepository volunteerRepository;
private final PasswordEncoder passwordEncoder;

public void signUp(SignUpRequest signUpRequest) {
public void signUp(VolunteerSignUpRequest volunteerSignUpRequest) {

if (memberRepository.existsByEmail(signUpRequest.email())) {
if (volunteerRepository.existsByEmail(volunteerSignUpRequest.email())) {
throw new BadRequestException(ALREADY_EXIST_EMAIL);
}
if (memberRepository.existsByNickname(signUpRequest.nickname())) {
if (volunteerRepository.existsByNickname(volunteerSignUpRequest.nickname())) {
throw new BadRequestException(ALREADY_EXIST_NICKNAME);
}

Member member = signUpRequest.toEntity();
member.passwordEncode(passwordEncoder);
memberRepository.save(member);
Volunteer volunteer = volunteerSignUpRequest.toEntity();
volunteer.passwordEncode(passwordEncoder);
volunteerRepository.save(volunteer);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.pawwithu.connectdog.domain.intermediary.entity;

import jakarta.persistence.*;
import lombok.*;
import org.springframework.security.crypto.password.PasswordEncoder;

@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor
@Entity
public class Intermediary {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String email; // 이메일
private String password; // 비밀번호
@Column(length = 20, nullable = false)
private String name; // 중개자 이름/중개 단체명
private String url; // 이동봉사 계정 링크
private String authImage; // 인증 사진
@Enumerated(EnumType.STRING)
private IntermediaryRole role; // 권한
private Boolean isOptionAgr; // 선택 이용약관 체크 여부

@Builder
public Intermediary(String email, String password, String name, String url, String authImage, IntermediaryRole role, Boolean isOptionAgr) {
this.email = email;
this.password = password;
this.name = name;
this.url = url;
this.authImage = authImage;
this.role = role;
this.isOptionAgr = isOptionAgr;
}

public void passwordEncode(PasswordEncoder passwordEncoder) {
this.password = passwordEncoder.encode(this.password);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.pawwithu.connectdog.domain.intermediary.entity;

import lombok.Getter;
import lombok.RequiredArgsConstructor;

@Getter
@RequiredArgsConstructor
public enum IntermediaryRole {

INTERMEDIARY("ROLE_INTERMEDIARY"), AUTH_INTERMEDIARY("ROLE_AUTH_INTERMEDIARY");

private final String key;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.pawwithu.connectdog.domain.intermediary.repository;

import com.pawwithu.connectdog.domain.intermediary.entity.Intermediary;
import org.springframework.data.jpa.repository.JpaRepository;

public interface IntermediaryRepository extends JpaRepository<Intermediary, Long> {

Boolean existsByEmail(String email);
Boolean existsByName(String name);
}

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.pawwithu.connectdog.domain.volunteer.entity;

public enum SocialType {
KAKAO, NAVER
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.pawwithu.connectdog.domain.member.entity;
package com.pawwithu.connectdog.domain.volunteer.entity;

import jakarta.persistence.*;
import lombok.*;
Expand All @@ -8,46 +8,36 @@
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor
@Entity
public class Member {
public class Volunteer {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

private String email; // 이메일
private String password; // 비밀번호
@Column(length = 15, nullable = false)
private String nickname; // 닉네임
@Column(length = 10, nullable = false)
private String name; // 이름
private String phone; // 이동봉사자 휴대폰 번호
private String url; // 이동봉사 단체 링크
private Boolean isOptionAgr; // 선택 이용약관 체크 여부

@Enumerated(EnumType.STRING)
private Role role;

private VolunteerRole role; // 권한
@Enumerated(EnumType.STRING)
private SocialType socialType; // KAKAO, NAVER

private String socialId; // 로그인한 소셜 타입 식별자 값 (일반 로그인의 경우 null)
private Boolean isOptionAgr; // 선택 이용약관 체크 여부

@Builder
public Member(String email, String password, String nickname, String name, String phone, String url, Boolean isOptionAgr, Role role, SocialType socialType, String socialId) {
public Volunteer(String email, String password, String nickname, VolunteerRole role, Boolean isOptionAgr) {
this.email = email;
this.password = password;
this.nickname = nickname;
this.name = name;
this.phone = phone;
this.url = url;
this.isOptionAgr = isOptionAgr;
this.role = role;
this.socialType = socialType;
this.socialId = socialId;
this.isOptionAgr = isOptionAgr;
}

public void passwordEncode(PasswordEncoder passwordEncoder) {
this.password = passwordEncoder.encode(this.password);
}



}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.pawwithu.connectdog.domain.volunteer.entity;

import lombok.Getter;
import lombok.RequiredArgsConstructor;

@Getter
@RequiredArgsConstructor
public enum VolunteerRole {

USER("ROLE_USER"), AUTH_USER("ROLE_AUTH_USER");

private final String key;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.pawwithu.connectdog.domain.volunteer.repository;

import com.pawwithu.connectdog.domain.volunteer.entity.Volunteer;
import com.pawwithu.connectdog.domain.volunteer.entity.SocialType;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.Optional;

public interface VolunteerRepository extends JpaRepository<Volunteer, Long> {

// 소셜 타입과 소셜의 식별값으로 회원 찾는 메소드 -> 추가 정보를 입력받아 회원가입 진행 시 이용
Optional<Volunteer> findBySocialTypeAndSocialId(SocialType socialType, String socialId);
Boolean existsByEmail(String email);
Boolean existsByNickname(String nickname);

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.pawwithu.connectdog.domain.member.entity.Member;
import com.pawwithu.connectdog.domain.member.repository.MemberRepository;
import com.pawwithu.connectdog.domain.volunteer.entity.Volunteer;
import com.pawwithu.connectdog.domain.volunteer.repository.VolunteerRepository;
import com.pawwithu.connectdog.error.exception.custom.TokenException;
import com.pawwithu.connectdog.jwt.util.PasswordUtil;
import com.pawwithu.connectdog.util.RedisUtil;
Expand Down Expand Up @@ -53,7 +53,7 @@ public class JwtService {
private static final String REFRESH_TOKEN_SUBJECT = "RefreshToken";
private static final String ID_CLAIM = "id";
private static final String BEARER = "Bearer ";
private final MemberRepository memberRepository;
private final VolunteerRepository volunteerRepository;
// private final RedisTemplate<String, String> redisTemplate; // 빈 주입 충돌 -> 명시적으로 주입하면 되지만 여기선 쓰이지 않으므로 주석
private final RedisUtil redisUtil;
private GrantedAuthoritiesMapper authoritiesMapper = new NullAuthoritiesMapper();
Expand Down Expand Up @@ -213,25 +213,25 @@ public boolean isRefreshTokenMatch(Long id, String refreshToken) {
public void getAuthentication(String accessToken) {
log.info("인증 처리 메소드 getAuthentication() 호출");
extractId(accessToken)
.ifPresent(id -> memberRepository.findById(id)
.ifPresent(id -> volunteerRepository.findById(id)
.ifPresent(this::saveAuthentication));
}

/**
* [인증 허가 메소드]
* 파라미터의 유저 : 우리가 만든 회원 객체 / 빌더의 유저 : UserDetails의 User 객체
*/
public void saveAuthentication(Member member) {
public void saveAuthentication(Volunteer volunteer) {
log.info("인증 허가 메소드 saveAuthentication() 호출");
String password = member.getPassword();
String password = volunteer.getPassword();
if (password == null) { // 소셜 로그인 유저의 비밀번호 임의로 설정 하여 소셜 로그인 유저도 인증 되도록 설정
password = PasswordUtil.generateRandomPassword();
}

UserDetails userDetailsUser = org.springframework.security.core.userdetails.User.builder()
.username(member.getEmail())
.username(volunteer.getEmail())
.password(password)
.roles(member.getRole().name())
.roles(volunteer.getRole().name())
.build();

Authentication authentication =
Expand Down

0 comments on commit 2812f46

Please sign in to comment.