Skip to content

Commit

Permalink
Refactor/#62 optimization field and method (#63)
Browse files Browse the repository at this point in the history
* refactor: member 및 작가 마이페이지에 나오는 북토크 관련 숫자 필드 없애고 list.size()를 이용해 구현

* add: 북토크 참가 인원이 최대를 넘었을 때의 예외처리 추가

* refactor: 완료된 북토크 연관관계 설정 메서드 추가
  • Loading branch information
dong2ast authored Jul 21, 2023
1 parent 301ce83 commit b168f8b
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 48 deletions.
17 changes: 0 additions & 17 deletions src/main/java/org/sophy/sophy/InitDb.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,24 +42,19 @@ public void dbInit() {
.isOperator(false)
.authority(Authority.ROLE_USER)
.build();
citizen.setCompleteBookTalkCount(8);
citizen.setWaitingBookTalkCount(5);
em.persist(citizen);

AuthorProperty memauthor1 = AuthorProperty.builder()
.myBookTalkList(new ArrayList<>())
.myBookList(new ArrayList<>())
.expectedBookTalkCount(10)
.build();
AuthorProperty memauthor2 = AuthorProperty.builder()
.myBookTalkList(new ArrayList<>())
.myBookList(new ArrayList<>())
.expectedBookTalkCount(10)
.build();
AuthorProperty memauthor3 = AuthorProperty.builder()
.myBookTalkList(new ArrayList<>())
.myBookList(new ArrayList<>())
.expectedBookTalkCount(7)
.build();

em.persist(memauthor1);
Expand All @@ -77,8 +72,6 @@ public void dbInit() {
.authority(Authority.ROLE_USER)
.build();
author1.setAuthorProperty(memauthor1);
author1.setCompleteBookTalkCount(1);
author1.setWaitingBookTalkCount(0);

Member author2 = Member.builder()
.name("이승희")
Expand All @@ -91,8 +84,6 @@ public void dbInit() {
.authority(Authority.ROLE_USER)
.build();
author2.setAuthorProperty(memauthor2);
author2.setCompleteBookTalkCount(2);
author2.setWaitingBookTalkCount(1);

Member author3 = Member.builder()
.name("김상욱")
Expand All @@ -105,8 +96,6 @@ public void dbInit() {
.authority(Authority.ROLE_USER)
.build();
author3.setAuthorProperty(memauthor3);
author3.setCompleteBookTalkCount(1);
author3.setWaitingBookTalkCount(0);

em.persist(author1);
em.persist(author2);
Expand Down Expand Up @@ -992,19 +981,13 @@ public void dbInit() {
em.persist(completedBooktalk1);
em.persist(completedBooktalk2);
em.persist(completedBooktalk3);
citizen.getCompletedBookTalkList().add(completedBooktalk1);
citizen.getCompletedBookTalkList().add(completedBooktalk2);
citizen.getCompletedBookTalkList().add(completedBooktalk3);

completedBooktalk4.setMember(author1);
completedBooktalk5.setMember(author1);
completedBooktalk6.setMember(author1);
em.persist(completedBooktalk4);
em.persist(completedBooktalk5);
em.persist(completedBooktalk6);
author1.getCompletedBookTalkList().add(completedBooktalk4);
author1.getCompletedBookTalkList().add(completedBooktalk5);
author1.getCompletedBookTalkList().add(completedBooktalk6);
}
}
}
6 changes: 0 additions & 6 deletions src/main/java/org/sophy/sophy/domain/AuthorProperty.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,8 @@ public class AuthorProperty {

@OneToMany(mappedBy = "authorProperty", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.LAZY)
private List<Book> myBookList;
private Integer expectedBookTalkCount; // 개최 예정 북토크

public void deleteBooktalk(Booktalk booktalk) {
myBookTalkList.remove(booktalk);
}

public void setExpectedBookTalkCount(Integer count) {
this.expectedBookTalkCount = count;
}
public void increaseBooktalkCount(){ this.expectedBookTalkCount +=1; }
}
3 changes: 0 additions & 3 deletions src/main/java/org/sophy/sophy/domain/Booktalk.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,6 @@ public void setAuthor(Member member) {
this.member = member;
this.authorProperty = member.getAuthorProperty();
member.getAuthorProperty().getMyBookTalkList().add(this);
// if (!member.getAuthorProperty().getMyBookTalkList().contains(this)) {
// member.getAuthorProperty().getMyBookTalkList().add(this);
// }
}

@Builder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,6 @@ public CompletedBooktalk(String title, String bookName, String authorName, Local

public void setMember(Member member) {
this.member = member;
member.getCompletedBookTalkList().add(this);
}
}
16 changes: 2 additions & 14 deletions src/main/java/org/sophy/sophy/domain/Member.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,11 @@ public class Member extends AuditingTimeEntity {
@Enumerated(EnumType.STRING)
private Authority authority;

private Integer waitingBookTalkCount; // 참여 예정 북토크
private Integer completeBookTalkCount; // 참여 완료 북토크 ( 북토크 complete 시킬 때 숫자 증가시키는거 필요함)

public void increaseWaitingBooktalkCount(){ this.waitingBookTalkCount +=1; }

@OneToMany(mappedBy = "member")
private List<MemberBooktalk> userBookTalkList;
private List<MemberBooktalk> userBookTalkList; // -> 참여 예정 북토크 수

@OneToMany(mappedBy = "member")
private List<CompletedBooktalk> completedBookTalkList; //이거 길이로 북토크 수 보여줄 수 있지 않을까?
private List<CompletedBooktalk> completedBookTalkList; //이거 길이로 북토크 수 보여줄 수 있지 않을까? -> 참여 완료 북토크 수

@OneToOne
@JoinColumn(name = "author_property_id")
Expand Down Expand Up @@ -94,13 +89,6 @@ public void setOperatorProperty(OperatorProperty operatorProperty) {
this.operatorProperty = operatorProperty;
}

public void setWaitingBookTalkCount(int count) {
this.waitingBookTalkCount = count;
}

public void setCompleteBookTalkCount(int count) {
this.completeBookTalkCount = count;
}

public void setAdditionalInfo(MemberAdditionalInfoDto memberAdditionalInfoDto) {
this.gender = memberAdditionalInfoDto.getGender();
Expand Down
1 change: 1 addition & 0 deletions src/main/java/org/sophy/sophy/exception/ErrorStatus.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public enum ErrorStatus {
* 409 CONFLICT
*/
ALREADY_EXIST_USER_EXCEPTION(HttpStatus.CONFLICT, "이미 존재하는 유저입니다"),
OVER_MAX_PARTICIPATION_EXCEPTION(HttpStatus.CONFLICT, "북토크 참여 가능 인원을 초과하였습니다."),

/**
* 500 INTERNAL SERVER ERROR
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.sophy.sophy.exception.model;

import org.sophy.sophy.exception.ErrorStatus;

public class OverMaxParticipationException extends SophyException{
public OverMaxParticipationException(ErrorStatus errorStatus, String message) {
super(errorStatus, message);
}
}
8 changes: 5 additions & 3 deletions src/main/java/org/sophy/sophy/service/BooktalkService.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.sophy.sophy.domain.enumerate.City;
import org.sophy.sophy.exception.ErrorStatus;
import org.sophy.sophy.exception.model.ForbiddenException;
import org.sophy.sophy.exception.model.OverMaxParticipationException;
import org.sophy.sophy.infrastructure.*;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand Down Expand Up @@ -38,7 +39,6 @@ public BooktalkCreateResponseDto createBooktalk(BooktalkRequestDto booktalkReque
Booktalk booktalk = booktalkRequestDto.toBooktalk(place, member);
//작가 정보에 북토크 업데이트
member.getAuthorProperty().getMyBookTalkList().add(booktalk);
member.getAuthorProperty().increaseBooktalkCount();
return BooktalkCreateResponseDto.of(booktalkRepository.save(booktalk));
}

Expand Down Expand Up @@ -71,15 +71,17 @@ public BooktalkDetailResponseDto getBooktalkDetail(Long booktalkId) {
// 북토크 참여 신청
@Transactional
public void postBooktalkParticipation(BooktalkParticipationRequestDto booktalkParticipationRequestDto) {
//북토크 현재 인원이 최대인원을 넘지 않았는지 체크하는 메서드 필요할듯
Member member = memberRepository.getMemberById(booktalkParticipationRequestDto.getMemberId());
Booktalk booktalk = booktalkRepository.getBooktalkById(booktalkParticipationRequestDto.getBooktalkId());
//북토크 현재 인원이 최대인원을 넘지 않았는지 체크하는 메서드 필요할듯
if (booktalk.getMaximum() == booktalk.getParticipantList().size()) {
throw new OverMaxParticipationException(ErrorStatus.OVER_MAX_PARTICIPATION_EXCEPTION, ErrorStatus.OVER_MAX_PARTICIPATION_EXCEPTION.getMessage());
}
// 복합키?
MemberBooktalk memberBooktalk = booktalkParticipationRequestDto.toMemberBooktalk(booktalk, member);
//연관 객체 변경 ( member 객체 북토크 수 표시하는 메서드 리팩터 필요 )
booktalk.getParticipantList().add(memberBooktalk);
member.getUserBookTalkList().add(memberBooktalk);
member.increaseWaitingBooktalkCount();
memberBooktalkRepository.save(memberBooktalk);
}

Expand Down
10 changes: 5 additions & 5 deletions src/main/java/org/sophy/sophy/service/MemberService.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,17 @@ public MyPageDto getMyPage(Long memberId) {
if(member.getIsAuthor()){
return MyPageDto.builder()
.name(member.getName())
.expectedBookTalkCount(member.getAuthorProperty().getExpectedBookTalkCount())
.waitingBookTalkCount(member.getWaitingBookTalkCount())
.completeBookTalkCount(member.getCompleteBookTalkCount())
.expectedBookTalkCount(member.getAuthorProperty().getMyBookTalkList().size())
.waitingBookTalkCount(member.getUserBookTalkList().size())
.completeBookTalkCount(member.getCompletedBookTalkList().size())
.myPageBooktalkDtos(getBooktalksByMemberId(memberId))
.myBookDtos(getAuthorBooksByMemberId(memberId))
.build();
} else {
return MyPageDto.builder()
.name(member.getName())
.waitingBookTalkCount(member.getWaitingBookTalkCount())
.completeBookTalkCount(member.getCompleteBookTalkCount())
.waitingBookTalkCount(member.getUserBookTalkList().size())
.completeBookTalkCount(member.getCompletedBookTalkList().size())
.myPageBooktalkDtos(getBooktalksByMemberId(memberId))
.build();
}
Expand Down

0 comments on commit b168f8b

Please sign in to comment.