Skip to content

Commit

Permalink
Merge pull request #168 from theMomentTeam/feature/answer-service-ref…
Browse files Browse the repository at this point in the history
…actoring

Feature/answer service refactoring
  • Loading branch information
jyeonjyan authored Jun 19, 2021
2 parents f011e43 + 98208cb commit c555e73
Show file tree
Hide file tree
Showing 3 changed files with 251 additions and 271 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/moment/the/service/AdminServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public void withdrawal(SignInDto signInDto) throws Exception {
}

//현재 로그인 된 사용자의 ID를 Return
public String getUserEmail() {
static public String getUserEmail() {
String userEmail;
Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
if(principal instanceof UserDetails) {
Expand Down
28 changes: 10 additions & 18 deletions src/main/java/com/moment/the/service/AnswerService.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,33 +23,37 @@ public class AnswerService {
final private TableRepository tableRepo;

// 답변 작성하기
public void save(AnswerDto answerDto, Long boardIdx) {
public AnswerDomain save(AnswerDto answerDto, Long boardIdx) {
//예외 처리
TableDomain tableDomain = tableFindBy(boardIdx); // table 번호로 찾고 없으면 Exception
boolean existAnswer = tableDomain.getAnswerDomain() != null ? true : false;
if(existAnswer) throw new AnswerAlreadyExistsException(); //이미 답변이 있으면 Exception

AdminDomain adminDomain = adminRepo.findByAdminId(getLoginAdminEmail());
AdminDomain adminDomain = adminRepo.findByAdminId(AdminServiceImpl.getUserEmail());

// AnswerDomain 생성 및 Table 과의 연관관계 맻음
answerDto.setAdminDomain(adminDomain);
AnswerDomain saveAnswerDomain = answerDto.toEntity();
saveAnswerDomain.updateTableDomain(tableDomain);

answerRepo.save(saveAnswerDomain);
AnswerDomain savedAnswerDomain = answerRepo.save(saveAnswerDomain);

return savedAnswerDomain;
}

// 답변 수정하기
@Transactional
public void update(AnswerDto answerDto, Long answerIdx) {
public AnswerDomain update(AnswerDto answerDto, Long answerIdx) {
AnswerDomain answerDomain = answerFindBy(answerIdx); // 해당하는 answer 찾기
AdminDomain answerAdmin = answerDomain.getAdminDomain();
AdminDomain loginAdmin = adminRepo.findByAdminId(getLoginAdminEmail());
AdminDomain loginAdmin = adminRepo.findByAdminId(AdminServiceImpl.getUserEmail());

answerOwnerCheck(answerAdmin, loginAdmin); // 자신이 작성한 답변인지 확인

// 답변 업데이트하기
answerDomain.update(answerDto);

return answerDomain;
}

public AnswerResDto view(Long boardIdx) {
Expand All @@ -73,7 +77,7 @@ public void delete(Long answerIdx) {
AnswerDomain answerDomain = answerFindBy(answerIdx);
AdminDomain answerAdmin = answerDomain.getAdminDomain();

AdminDomain loginAdmin = adminRepo.findByAdminId(getLoginAdminEmail());
AdminDomain loginAdmin = adminRepo.findByAdminId(AdminServiceImpl.getUserEmail());
answerOwnerCheck(answerAdmin, loginAdmin); // 자신이 작성한 답변인지 확인

// answer 삭제하기
Expand All @@ -95,18 +99,6 @@ public TableDomain tableFindBy(Long tableId){
return tableRepo.findById(tableId).orElseThrow(NoPostException::new);
}

// Current userEmail 을 가져오기.
public String getLoginAdminEmail() {
String userEmail;
Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
if(principal instanceof UserDetails) {
userEmail = ((UserDetails)principal).getUsername();
} else {
userEmail = principal.toString();
}
return userEmail;
}

public void deleteAnswer(AnswerDomain answerDomain){
Long answerIdx = answerDomain.getAnswerIdx();
answerDomain.getTableDomain().updateAnswerDomain(null); // 외래키 제약조건으로 인한 오류 해결
Expand Down
Loading

0 comments on commit c555e73

Please sign in to comment.