[개발] 회원 가입 기능 구조 개발
필기자
2022-10-25 18:22
5,480
0
본문
kr/hull/shop/constant/Role.java
public enum Role {
USER, ADMIN
}
kr/hull/shop/dto/MemberFormDto.java
@Getter
@Setter
public class MemberFormDto {
private String name;
private String email;
private String password;
private String address;
}
kr/hull/shop/entity/Member.java
@Entity
@Table(name="member")
@Getter
@Setter
@ToString
public class Member extends BaseEntity {
@Id
@Column(name="member_id")
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
@Column(unique = true)
private String email;
private String password;
private String address;
@Enumerated(EnumType.STRING)
private Role role;
public static Member createMember(MemberFormDto memberFormDto, PasswordEncoder passwordEncoder){
Member member = new Member();
member.setName(memberFormDto.getName());
member.setEmail(memberFormDto.getEmail());
member.setAddress(memberFormDto.getAddress());
String password = passwordEncoder.encode(memberFormDto.getPassword());
member.setPassword(password);
member.setRole(Role.ADMIN);
return member;
}
}
kr/hull/shop/repository/MemberRepository.java
public interface MemberRepository extends JpaRepository<Member, Long> {
Member findByEmail(String email);
}
kr/hull/shop/service/MemberService.java
@Service
@Transactional
@RequiredArgsConstructor //bean 주입방법 생성자 final member, @NonNull member 생성자 생성함
public class MemberService{
private final MemberRepository memberRepository;
public Member saveMember(Member member){
validateDuplicateMember(member);
return memberRepository.save(member);
}
private void validateDuplicateMember(Member member){
Member findMember = memberRepository.findByEmail(member.getEmail());
if(findMember != null){
throw new IllegalStateException("이미 가입된 회원입니다."); // 예외 처리
}
}
}
댓글목록0