Skip to content

Commit

Permalink
refactor: controller 단에서 레포에 접근하지 않고 service 단에서 접근하게 접근하게 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
dong2ast committed Jul 27, 2023
1 parent 57319fa commit 6723a45
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import org.sophy.sophy.domain.dto.booktalk.response.BooktalkDeleteResponseDto;
import org.sophy.sophy.domain.dto.mypage.MyPageBooktalkDto;
import org.sophy.sophy.exception.SuccessStatus;
import org.sophy.sophy.infrastructure.MemberRepository;
import org.sophy.sophy.service.BooktalkService;
import org.sophy.sophy.service.MemberService;
import org.springframework.http.HttpStatus;
Expand All @@ -23,16 +22,13 @@
@RestController
@RequiredArgsConstructor
@RequestMapping("/author")
public class AuthorController {
private final MemberRepository memberRepository;
private final MemberService memberService;
public class AuthorController { private final MemberService memberService;
private final BooktalkService booktalkService;

@GetMapping("/my-booktalks") //개설한 북토크 조회
@ResponseStatus(HttpStatus.OK)
public ApiResponseDto<List<MyPageBooktalkDto>> getAuthorBooktalks(@AuthenticationPrincipal User user) {
Long memberId = memberRepository.getMemberByEmail(user.getUsername()).getId();
return ApiResponseDto.success(SuccessStatus.GET_AUTHOR_BOOKTALKS_SUCCESS, memberService.getAuthorBooktalksByMemberId(memberId));
return ApiResponseDto.success(SuccessStatus.GET_AUTHOR_BOOKTALKS_SUCCESS, memberService.getAuthorBooktalksByEmail(user.getUsername()));
}

@PostMapping("/booktalk") //북토크 생성
Expand Down
22 changes: 4 additions & 18 deletions src/main/java/org/sophy/sophy/controller/MemberController.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import org.sophy.sophy.domain.dto.mypage.MyPageDto;
import org.sophy.sophy.domain.dto.mypage.MyInfoDto;
import org.sophy.sophy.exception.SuccessStatus;
import org.sophy.sophy.infrastructure.MemberRepository;
import org.sophy.sophy.service.MemberService;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
Expand All @@ -20,42 +19,29 @@
@RequestMapping("/member")
public class MemberController {
private final MemberService memberService;
private final MemberRepository memberRepository;

@GetMapping("/my-page") // 마이페이지 조회
@ResponseStatus(HttpStatus.OK)
public ApiResponseDto<MyPageDto> getMyPage(@AuthenticationPrincipal User user) {
Long memberId = memberRepository.getMemberByEmail(user.getUsername()).getId();
return ApiResponseDto.success(SuccessStatus.GET_MYPAGE_SUCCESS, memberService.getMyPage(memberId));
return ApiResponseDto.success(SuccessStatus.GET_MYPAGE_SUCCESS, memberService.getMyPage(user.getUsername()));
}

@GetMapping("/my-info") //내 정보 조회
@ResponseStatus(HttpStatus.OK)
public ApiResponseDto<MyInfoDto> getInfo(@AuthenticationPrincipal User user) {
Long memberId = memberRepository.getMemberByEmail(user.getUsername()).getId();
return ApiResponseDto.success(SuccessStatus.GET_MYPAGE_SUCCESS, memberService.getMyInfo(memberId));
return ApiResponseDto.success(SuccessStatus.GET_MYPAGE_SUCCESS, memberService.getMyInfo(user.getUsername()));
}

@PostMapping("/my-info") //추가 정보 입력
@ResponseStatus(HttpStatus.OK)
public ApiResponseDto<MemberAdditionalInfoDto> postAdditionalInfo(@AuthenticationPrincipal User user, @RequestBody @Valid MemberAdditionalInfoDto memberAdditionalInfoDto) {
Long memberId = memberRepository.getMemberByEmail(user.getUsername()).getId();
return ApiResponseDto.success(SuccessStatus.POST_ADDITIONALINFO_SUCCESS, memberService.postAdditionalInfo(memberId, memberAdditionalInfoDto));
return ApiResponseDto.success(SuccessStatus.POST_ADDITIONALINFO_SUCCESS, memberService.postAdditionalInfo(user.getUsername(), memberAdditionalInfoDto));
}

@PatchMapping("/my-info") //내 정보 업데이트
@ResponseStatus(HttpStatus.OK)
public ApiResponseDto<MyInfoDto> patchInfo(@AuthenticationPrincipal User user, @RequestBody @Valid MyInfoDto myInfoDto) {
Long memberId = memberRepository.getMemberByEmail(user.getUsername()).getId();
return ApiResponseDto.success(SuccessStatus.PATCH_MYINFO_SUCCESS, memberService.patchMyInfo(memberId, myInfoDto));
return ApiResponseDto.success(SuccessStatus.PATCH_MYINFO_SUCCESS, memberService.patchMyInfo(user.getUsername(), myInfoDto));
}

// @GetMapping("/my-booktalks") //예정된 북토크 조회 (신청)
// @ResponseStatus(HttpStatus.OK)
// public ApiResponseDto<List<MyPageBooktalkDto>> getMyBooktalks(@AuthenticationPrincipal User user) {
// Long memberId = memberRepository.getMemberByEmail(user.getUsername()).getId();
// return ApiResponseDto.success(SuccessStatus.GET_MY_BOOKTALKS_SUCCESS, memberService.getBooktalksByMemberId(memberId));
// }


}

This file was deleted.

34 changes: 17 additions & 17 deletions src/main/java/org/sophy/sophy/service/MemberService.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,30 +26,30 @@ public class MemberService {
private final MemberRepository memberRepository;

@Transactional
public MyPageDto getMyPage(Long memberId) {
Member member = memberRepository.getMemberById(memberId);
public MyPageDto getMyPage(String email) {
Member member = memberRepository.getMemberByEmail(email);
//여기에 추가로 member에 있는 userBookTalk 리스트를 시간순으로 정렬해 가장 마감이 임박한 booktalk도 보여줌
if(member.getAuthority().equals(Authority.ROLE_AUTHOR)){
return MyPageDto.builder()
.name(member.getName())
.expectedBookTalkCount(member.getAuthorProperty().getMyBookTalkList().size())
.waitingBookTalkCount(member.getUserBookTalkList().size())
.completeBookTalkCount(member.getCompletedBookTalkList().size())
.myPageBooktalkDtos(getBooktalksByMemberId(memberId))
.myBookDtos(getAuthorBooksByMemberId(memberId))
.myPageBooktalkDtos(getBooktalksByMember(member))
.myBookDtos(getAuthorBooksByMember(member))
.build();
} else {
return MyPageDto.builder()
.name(member.getName())
.waitingBookTalkCount(member.getUserBookTalkList().size())
.completeBookTalkCount(member.getCompletedBookTalkList().size())
.myPageBooktalkDtos(getBooktalksByMemberId(memberId))
.myPageBooktalkDtos(getBooktalksByMember(member))
.build();
}
}
@Transactional
public MyInfoDto getMyInfo(Long memberId) {
Member member = memberRepository.getMemberById(memberId);
public MyInfoDto getMyInfo(String email) {
Member member = memberRepository.getMemberByEmail(email);
return MyInfoDto.builder()
.email(member.getEmail())
.name(member.getName())
Expand All @@ -61,22 +61,22 @@ public MyInfoDto getMyInfo(Long memberId) {
.build();
}
@Transactional
public MemberAdditionalInfoDto postAdditionalInfo(Long memberId, MemberAdditionalInfoDto memberAdditionalInfoDto) {
Member member = memberRepository.getMemberById(memberId);
public MemberAdditionalInfoDto postAdditionalInfo(String email, MemberAdditionalInfoDto memberAdditionalInfoDto) {
Member member = memberRepository.getMemberByEmail(email);
member.setAdditionalInfo(memberAdditionalInfoDto);
return memberAdditionalInfoDto;
}

@Transactional
public MyInfoDto patchMyInfo(Long memberId, MyInfoDto myInfoDto) {
Member member = memberRepository.getMemberById(memberId);
public MyInfoDto patchMyInfo(String email, MyInfoDto myInfoDto) {
Member member = memberRepository.getMemberByEmail(email);
member.patchMyInfo(myInfoDto);
return myInfoDto;
}

@Transactional
public List<MyPageBooktalkDto> getBooktalksByMemberId(Long memberId) { //예정된 북토크 조회 메서드
List<MemberBooktalk> userBookTalkList = memberRepository.getMemberById(memberId).getUserBookTalkList();
public List<MyPageBooktalkDto> getBooktalksByMember(Member member) { //예정된 북토크 조회 메서드
List<MemberBooktalk> userBookTalkList = member.getUserBookTalkList();
List<MyPageBooktalkDto> booktalkResponseDtoList = new ArrayList<>();
userBookTalkList.forEach(memberBooktalk -> {
Booktalk booktalk = memberBooktalk.getBooktalk();
Expand All @@ -99,8 +99,8 @@ public List<MyPageBooktalkDto> getBooktalksByMemberId(Long memberId) { //예정
}

@Transactional
public List<MyPageBooktalkDto> getAuthorBooktalksByMemberId(Long memberId) { //작가가 개최한 북토크 조회 메서드
List<Booktalk> authorBookTalkList = memberRepository.getMemberById(memberId).getAuthorProperty().getMyBookTalkList();
public List<MyPageBooktalkDto> getAuthorBooktalksByEmail(String email) { //작가가 개최한 북토크 조회 메서드
List<Booktalk> authorBookTalkList = memberRepository.getMemberByEmail(email).getAuthorProperty().getMyBookTalkList();
List<MyPageBooktalkDto> booktalkResponseDtoList = new ArrayList<>();
authorBookTalkList.forEach(booktalk -> {
booktalkResponseDtoList.add(MyPageBooktalkDto.builder()
Expand All @@ -120,8 +120,8 @@ public List<MyPageBooktalkDto> getAuthorBooktalksByMemberId(Long memberId) { //
}

@Transactional
public List<MyBookDto> getAuthorBooksByMemberId(Long memberId) { //작가가 쓴 책 조회 메서드
List<Book> authorBookList = memberRepository.getMemberById(memberId).getAuthorProperty().getMyBookList();
public List<MyBookDto> getAuthorBooksByMember(Member member) { //작가가 쓴 책 조회 메서드
List<Book> authorBookList = member.getAuthorProperty().getMyBookList();
List<MyBookDto> bookResponseDtoList = new ArrayList<>();
authorBookList.forEach(book -> {
bookResponseDtoList.add(MyBookDto.builder()
Expand Down

0 comments on commit 6723a45

Please sign in to comment.