Real-World

https://github.com/choomi1217/real-world.git

Real-World 구현하기

🔧사용한 기술 버전

  • 자바 17
  • 스프링 3.1.1
    • 스프링 시큐리티
  • h2

🔧 느낀점

4-Tier Layered Architecture를 사용했습니다.

이번 프로젝트로 JPA를 처음 사용하다보니 여러가지 문제가 있었습니다.

  • 순환참조

아래와 같이 순환참조를 해결하기 위한 클래스가 하나 생겼습니다.

@Service
@RequiredArgsConstructor
public class AccountUsercase {

    private final AccountService accountService;
    private final JwtService jwtService;
    private final MemberService memberService;
    private final PasswordEncoder passwordEncoder;

	// ... ... ...

}
  • Entity가 그대로 Service에 노출

Ropository에서 꺼내온 Entity가 바로 Service Layer에서 사용되었습니다.

Service Layer에서 Entity를 비즈니스 처리 로직을 위해 처리하는 과정이 계속 되었습니다.

그리고 이런 기능이 Service Layer에 있어도 되나 싶었습니다.

@Service
@RequiredArgsConstructor
public class MemberFollowService {

    private final FollowService followService;
    private final MemberService memberService;

    public ProfileResponse follow(String followerEmail, String followedName) {
        Member follower = memberService.findByEmail(followerEmail).orElseThrow(() -> new UsernameNotFoundException("User Not Found"));
        Member followed = memberService.findByUsername(followedName).orElseThrow(() -> new UsernameNotFoundException("User Not Found"));
        followService.follow(follower.getUsername(), followed.getUsername());
        return new ProfileResponse(followed, true);
    }

    @Transactional
    public ProfileResponse unfollow(String followerEmail, String followedName) throws NotFollowingException {
        Member follower = memberService.findByEmail(followerEmail).orElseThrow(() -> new UsernameNotFoundException("User Not Found"));
        Member followed = memberService.findByUsername(followedName).orElseThrow(() -> new UsernameNotFoundException("User Not Found"));

        if( !isItFollow(follower, followed) ){
            throw new NotFollowingException( String.format("%s is not following %s.", follower.getUsername(), followed.getUsername()));
        }

        followService.unfollow(follower.getUsername(), followed.getUsername());
        return new ProfileResponse(followed, false);
    }

    private boolean isItFollow(Member follower, Member followed) {
        return followService.exists(follower.getUsername(), followed.getUsername());
    }
}

🔧 참고

4-Tier Layered Architecture 참고

댓글남기기