Skip to content

Commit

Permalink
feat: 유저 로직 구현 #8
Browse files Browse the repository at this point in the history
  • Loading branch information
PgmJun committed Jan 18, 2024
1 parent eeace2b commit 6245e57
Show file tree
Hide file tree
Showing 7 changed files with 142 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.nice.petudio.api.controller.member.dto;

import com.nice.petudio.domain.member.SocialType;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.ToString;

@ToString
@Getter
@AllArgsConstructor(access = AccessLevel.PRIVATE)
@NoArgsConstructor(access = AccessLevel.PRIVATE)
@Builder(access = AccessLevel.PRIVATE)
public class CreateMemberRequest {

private String socialId;
private SocialType socialType;
private String nickname;
private String profileImageUrl;

public static CreateMemberRequest of(String socialId, SocialType socialType, String nickname, String profileImageUrl) {
return CreateMemberRequest.builder()
.socialId(socialId)
.socialType(socialType)
.nickname(nickname)
.profileImageUrl(profileImageUrl)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.nice.petudio.api.controller.member.service;

import com.nice.petudio.api.controller.member.dto.CreateMemberRequest;
import com.nice.petudio.domain.member.Member;
import com.nice.petudio.domain.member.repository.MemberRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class MemberService {
private final MemberRepository memberRepository;

public Long registerMember(CreateMemberRequest request) {
MemberServiceUtils.validateNotExistsMember(memberRepository, request.getSocialId(), request.getSocialType());
Member member = memberRepository.save(
Member.newInstance(request.getSocialId(), request.getSocialType()));
return member.getId();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.nice.petudio.api.controller.member.service;

import com.nice.petudio.domain.member.Member;
import com.nice.petudio.domain.member.SocialType;
import com.nice.petudio.domain.member.repository.MemberRepository;
import com.nice.petudio.global.exception.ConflictException;
import com.nice.petudio.global.exception.NotFoundException;
import com.nice.petudio.global.exception.error.ErrorCode;
import java.util.Optional;

public class MemberServiceUtils {

public static Member findMemberById(MemberRepository memberRepository, Long memberId) {
return memberRepository.findById(memberId)
.orElseThrow(() -> new NotFoundException(ErrorCode.NOT_FOUND_MEMBER_EXCEPTION,
String.format("존재하지 않는 memberId(%d) 입니다.", memberId)));
}

static void validateNotExistsMember(MemberRepository memberRepository, String socialId,
SocialType socialType) {
if (memberRepository.existsBySocialIdAndSocialType(socialId, socialType)) {
throw new ConflictException(ErrorCode.CONFLICT_MEMBER_EXCEPTION, String.format("이미 존재하는 회원 소셜정보 (%s - %s) 입니다", socialId, socialType));
}
}

public static Member findMemberBySocialIdAndSocialType(MemberRepository memberRepository, String socialId,
SocialType socialType) {
Optional<Member> member = memberRepository.findMemberBySocialIdAndSocialType(socialId, socialType);
return member.orElseThrow(() -> new NotFoundException(
ErrorCode.NOT_FOUND_MEMBER_EXCEPTION,
String.format("존재하지 않는 회원 소셜정보 (%s - %s) 입니다", socialType, socialId)));
}
}

This file was deleted.

9 changes: 9 additions & 0 deletions src/main/java/com/nice/petudio/domain/member/Member.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@
import jakarta.persistence.Table;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Entity
@Table(name = "members")
@Getter
@Builder
@AllArgsConstructor(access = AccessLevel.PROTECTED)
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Member extends BaseEntity {
Expand All @@ -35,4 +37,11 @@ public class Member extends BaseEntity {

@Embedded
private SocialInfo socialInfo;

public static Member newInstance(String socialId, SocialType socialType) {
return Member.builder()
.socialInfo(SocialInfo.of(socialId, socialType))
.role(MemberRole.MEMBER)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
package com.nice.petudio.domain.member.repository;

import com.nice.petudio.domain.member.Member;
import com.nice.petudio.domain.member.SocialType;
import java.util.Optional;

public interface MemberRepositoryCustom {
boolean existsBySocialIdAndSocialType(String socialId, SocialType socialType);

Optional<Member> findMemberById(Long id);

Optional<Member> findMemberBySocialIdAndSocialType(String socialId, SocialType socialType);
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,44 @@
package com.nice.petudio.domain.member.repository;

import static com.nice.petudio.domain.member.QMember.member;

import com.nice.petudio.domain.member.Member;
import com.nice.petudio.domain.member.SocialType;
import com.querydsl.jpa.impl.JPAQueryFactory;
import java.util.Optional;
import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
public class MemberRepositoryImpl implements MemberRepositoryCustom {
private final JPAQueryFactory queryFactory;

@Override
public boolean existsBySocialIdAndSocialType(String socialId, SocialType socialType) {
return Optional.ofNullable(queryFactory.selectOne()
.from(member)
.where(
member.socialInfo.socialId.eq(socialId),
member.socialInfo.socialType.eq(socialType)
)
.fetchFirst()).isPresent();
}

@Override
public Optional<Member> findMemberById(Long id) {
return Optional.ofNullable(queryFactory
.selectFrom(member)
.where(member.id.eq(id))
.fetchOne());
}

@Override
public Optional<Member> findMemberBySocialIdAndSocialType(String socialId, SocialType socialType) {
return Optional.ofNullable(queryFactory
.selectFrom(member)
.where(
member.socialInfo.socialId.eq(socialId),
member.socialInfo.socialType.eq(socialType)
)
.fetchOne());
}
}

0 comments on commit 6245e57

Please sign in to comment.