Skip to content

Commit

Permalink
chore: 클래스 병합(memberservice1, memberrepository1) 및 패키지 이동(querydlscon…
Browse files Browse the repository at this point in the history
…fig)
  • Loading branch information
Bellroute committed May 26, 2024
1 parent c151441 commit 504a0a0
Show file tree
Hide file tree
Showing 8 changed files with 137 additions and 141 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.climingo.climingoApi.config;
package com.climingo.climingoApi.global.config;

import com.querydsl.jpa.impl.JPAQueryFactory;
import jakarta.persistence.EntityManager;
Expand All @@ -7,7 +7,7 @@
import org.springframework.context.annotation.Configuration;

@Configuration
public class QuerydslConfiguration {
public class QuerydslConfig {

@PersistenceContext
private EntityManager entityManager;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.climingo.climingoApi.member.application;
package com.climingo.climingoApi.member.api;

import com.climingo.climingoApi.global.auth.LoginMember;
import com.climingo.climingoApi.member.application.MemberService;
import com.climingo.climingoApi.member.application.response.MemberInfoResponse;
import com.climingo.climingoApi.member.application.response.ProfileResponse;
import com.climingo.climingoApi.member.domain.Member;
Expand All @@ -16,7 +17,7 @@
@RestController
public class MemberController {

private final MemberService1 memberService;
private final MemberService memberService;

@GetMapping("/members")
public ResponseEntity<ProfileResponse> findMyInfo() {
Expand All @@ -31,7 +32,6 @@ public ResponseEntity<MemberInfoResponse> findMemberInfo(@PathVariable(value = "
}

@PatchMapping("/members/{memberId}/nickname")
// TODO
public ResponseEntity<Void> updateNickname(@LoginMember Member member, @PathVariable(value = "memberId") Long memberId, @RequestBody String nickname) {
memberService.updateNickname(member, memberId, nickname);
return ResponseEntity.ok().build();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,64 +1,14 @@
package com.climingo.climingoApi.member.application;

import com.climingo.climingoApi.auth.api.request.SignUpRequest;
import com.climingo.climingoApi.auth.api.response.MemberInfo;
import com.climingo.climingoApi.auth.application.SignInService;
import com.climingo.climingoApi.auth.application.SignUpService;
import com.climingo.climingoApi.member.application.response.MemberInfoResponse;
import com.climingo.climingoApi.member.application.response.ProfileResponse;
import com.climingo.climingoApi.member.domain.Member;
import com.climingo.climingoApi.member.domain.MemberRepository;
import java.util.NoSuchElementException;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
public class MemberService implements SignUpService, SignInService {
public interface MemberService {

private final MemberRepository memberRepository;
ProfileResponse findMyInfo(Long memberId);

@Transactional
@Override
public MemberInfo signUp(SignUpRequest request) {
Member member = Member.builder()
.authId(request.getAuthId())
.providerType(request.getProviderType())
.nickname(request.getNickname())
.profileUrl(request.getProfileUrl())
.physicalInfo(request.getPhysicalInfo())
.build();
MemberInfoResponse findMemberInfo(Long memberId);

validateAbleToSignUp(member);

member = memberRepository.save(member);
return new MemberInfo(member);
}

private void validateAbleToSignUp(Member member) {
// 중복 가입 여부 확인
if (isDuplicatedMember(member.getAuthId(), member.getProviderType())) {
throw new IllegalArgumentException("이미 존재하는 회원입니다.");
}

if (isDuplicatedNickname(member.getNickname())) {
throw new IllegalArgumentException("이미 존재하는 닉네임입니다.");
}
}

private boolean isDuplicatedNickname(String nickname) {
return memberRepository.existsByNickname(nickname);
}

private boolean isDuplicatedMember(String authId, String providerType) {
return memberRepository.existsByAuthIdAndProviderType(authId, providerType);
}

@Override
public MemberInfo findEnrolledMemberInfoByAuthIdAndProviderType(String authId,
String providerType) {
Member member = memberRepository.findByAuthIdAndProviderType(authId, providerType)
.orElseThrow(() -> new NoSuchElementException("등록되지 않은 사용자. 회원가입을 먼저 진행하세요."));

return new MemberInfo(member);
}
void updateNickname(Member member, Long memberId, String nickname);
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package com.climingo.climingoApi.member.application;

import com.climingo.climingoApi.auth.api.request.SignUpRequest;
import com.climingo.climingoApi.auth.api.response.MemberInfo;
import com.climingo.climingoApi.auth.application.SignInService;
import com.climingo.climingoApi.auth.application.SignUpService;
import com.climingo.climingoApi.member.application.response.MemberInfoResponse;
import com.climingo.climingoApi.member.application.response.ProfileResponse;
import com.climingo.climingoApi.member.domain.Member;
import com.climingo.climingoApi.member.domain.MemberRepository;
import com.climingo.climingoApi.record.api.response.RecordResponse;
import com.climingo.climingoApi.record.domain.Record;
import jakarta.persistence.EntityNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.NoSuchElementException;
import lombok.RequiredArgsConstructor;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
public class MemberServiceImpl implements SignUpService, SignInService, MemberService {

private final MemberRepository memberRepository;

@Transactional
@Override
public MemberInfo signUp(SignUpRequest request) {
Member member = Member.builder()
.authId(request.getAuthId())
.providerType(request.getProviderType())
.nickname(request.getNickname())
.profileUrl(request.getProfileUrl())
.physicalInfo(request.getPhysicalInfo())
.build();

validateAbleToSignUp(member);

member = memberRepository.save(member);
return new MemberInfo(member);
}

private void validateAbleToSignUp(Member member) {
// 중복 가입 여부 확인
if (isDuplicatedMember(member.getAuthId(), member.getProviderType())) {
throw new IllegalArgumentException("이미 존재하는 회원입니다.");
}

if (isDuplicatedNickname(member.getNickname())) {
throw new IllegalArgumentException("이미 존재하는 닉네임입니다.");
}
}

private boolean isDuplicatedNickname(String nickname) {
return memberRepository.existsByNickname(nickname);
}

private boolean isDuplicatedMember(String authId, String providerType) {
return memberRepository.existsByAuthIdAndProviderType(authId, providerType);
}

@Override
public MemberInfo findEnrolledMemberInfoByAuthIdAndProviderType(String authId,
String providerType) {
Member member = memberRepository.findByAuthIdAndProviderType(authId, providerType)
.orElseThrow(() -> new NoSuchElementException("등록되지 않은 사용자. 회원가입을 먼저 진행하세요."));

return new MemberInfo(member);
}

@Override

@Transactional(readOnly = true)
public ProfileResponse findMyInfo(Long memberId) {
Member member = memberRepository.findMemberWithRecords(memberId);

List<RecordResponse> recordResponses = new ArrayList<>();
for (Record record : member.getRecords()) {
recordResponses.add(new RecordResponse(member, record, record.getGym(), record.getLevel()));
}

ProfileResponse profileResponse = ProfileResponse.builder()
.myInfo(new MemberInfoResponse(member))
.records(recordResponses)
.build();
return profileResponse;
}

@Override
@Transactional(readOnly = true)
public MemberInfoResponse findMemberInfo(Long memberId) {
Member member = memberRepository.findById(memberId)
.orElseThrow(() -> new EntityNotFoundException(memberId + "is not found"));

return new MemberInfoResponse(member);
}

@Override
public void updateNickname(Member member, Long memberId, String nickname) {
if (!member.isSameMember(memberId)) {
throw new AccessDeniedException("다른 사용자의 닉네임은 변경할 수 없음");
}

if (isDuplicated(nickname)) {
throw new DataIntegrityViolationException("Nickname already exists: " + nickname);
}


member.updateNickname(nickname);
memberRepository.save(member);
}

private boolean isDuplicated(String nickname) {
return memberRepository.existsByNickname(nickname);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

@Repository
Expand All @@ -12,4 +14,7 @@ public interface MemberRepository extends JpaRepository<Member, Long> {
boolean existsByNickname(String nickname);

Optional<Member> findByAuthIdAndProviderType(String authId, String providerType);

@Query("SELECT m FROM Member m JOIN FETCH m.records WHERE m.id = :memberId")
Member findMemberWithRecords(@Param("memberId") Long memberId);
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@
import org.junit.jupiter.api.Test;

@DisplayName("MemberService Unit Tests")
class MemberServiceTest {
class MemberServiceImplTest {

private SignUpService signUpService;
private MemberRepository mockedMemberRepository;

@BeforeEach
void setUp() {
mockedMemberRepository = mock(MemberRepository.class);
signUpService = new MemberService(mockedMemberRepository);
signUpService = new MemberServiceImpl(mockedMemberRepository);
}

@Test
Expand Down

0 comments on commit 504a0a0

Please sign in to comment.