Skip to content

Commit

Permalink
[Refactor] member delete ref & issue api ref(refresh in cookie ref)
Browse files Browse the repository at this point in the history
  • Loading branch information
Han-Jeong committed Jan 20, 2024
1 parent 79a01b4 commit 4b820c4
Show file tree
Hide file tree
Showing 20 changed files with 36 additions and 53 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/example/waggle/domain/board/Board.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public abstract class Board extends BaseEntity {
protected List<Media> medias = new ArrayList<>();

@Builder.Default
@OneToMany(mappedBy = "board")
@OneToMany(mappedBy = "board", cascade = CascadeType.REMOVE)
protected List<Comment> comments = new ArrayList<>();


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ public interface AnswerRepository extends JpaRepository<Answer, Long> {
Page<Answer> findPagedAnswerByMemberUsername(String username, Pageable pageable);

void deleteAllByMemberUsername(String username);

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,10 @@
public class AnswerCommandServiceImpl implements AnswerCommandService {
//TODO user는 question 내에 하나의 answer만 사용할 수 있도록 제어

//REPOSITORY
private final AnswerRepository answerRepository;
private final QuestionRepository questionRepository;
private final RecommendRepository recommendRepository;
//QUERY_SERVICE
private final MemberQueryService memberQueryService;
//COMMAND_SERVICE
private final CommentCommandService commentCommandService;
private final BoardService boardService;
private final MediaCommandService mediaCommandService;
Expand Down Expand Up @@ -88,10 +85,6 @@ public Long updateAnswerV2(Long boardId,

mediaCommandService.updateMediaV2(mediaUpdateDto, multipartFiles, answer);

answer.getBoardHashtags().clear();
for (String hashtag : answerUpdateDto.getHashtags()) {
boardService.saveHashtag(answer, hashtag);
}
return answer.getId();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,10 @@
@Service
public class QuestionCommandServiceImpl implements QuestionCommandService {

//REPOSITORY
private final QuestionRepository questionRepository;
private final AnswerRepository answerRepository;
private final RecommendRepository recommendRepository;
//QUERY_SERVICE
private final MemberQueryService memberQueryService;
//COMMAND_SERVICE
private final AnswerCommandService answerCommandService;
private final BoardService boardService;
private final MediaCommandService mediaCommandService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,12 @@
@RequiredArgsConstructor
public class BoardServiceImpl implements BoardService {

//REPOSITORY
private final StoryRepository storyRepository;
private final QuestionRepository questionRepository;
private final AnswerRepository answerRepository;
private final SirenRepository sirenRepository;
private final ScheduleRepository scheduleRepository;
private final HashtagRepository hashtagRepository;
//QUERY_SERVICE
private final MemberQueryService memberQueryService;


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,10 @@
@Transactional
@Service
public class SirenCommandServiceImpl implements SirenCommandService {
//REPOSITORY

private final SirenRepository sirenRepository;
private final RecommendRepository recommendRepository;
//QUERY_SERVICE
private final MemberQueryService memberQueryService;
//COMMAND_SERVICE
private final BoardService boardService;
private final CommentCommandService commentCommandService;
private final MediaCommandService mediaCommandService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,9 @@
@Service
public class StoryCommandServiceImpl implements StoryCommandService {

//REPOSITORY
private final StoryRepository storyRepository;
private final RecommendRepository recommendRepository;
//QUERY_SERVICE
private final MemberQueryService memberQueryService;
//COMMAND_SERVICE
private final BoardService boardService;
private final MediaCommandService mediaCommandService;
private final CommentCommandService commentCommandService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,9 @@
@Service
public class CommentCommandServiceImpl implements CommentCommandService {

//REPOSITORY
private final CommentRepository commentRepository;
private final ReplyRepository replyRepository;
//QUERY_SERVICE
private final MemberQueryService memberQueryService;
//COMMAND_SERVICE
private final BoardService boardService;

@Override
Expand Down Expand Up @@ -82,11 +79,12 @@ public void deleteComment(Long commentId) {

@Override
public void deleteCommentForHardReset(Long commentId) {
Comment comment = commentRepository.findById(commentId)
.orElseThrow(() -> new CommentHandler(ErrorStatus.COMMENT_NOT_FOUND));

replyRepository.deleteAllByCommentId(commentId);
commentRepository.delete(comment);
commentRepository.findById(commentId).ifPresent(
comment -> {
replyRepository.deleteAllByCommentId(commentId);
commentRepository.delete(comment);
}
);
}

public boolean validateMember(Long commentId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,9 @@
@Service
public class ReplyCommandServiceImpl implements ReplyCommandService {

//REPOSITORY
private final MemberRepository memberRepository;
private final CommentRepository commentRepository;
private final ReplyRepository replyRepository;
//QUERY_SERVICE
private final MemberQueryService memberQueryService;

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@
@Transactional
@Service
public class FollowCommandServiceImpl implements FollowCommandService {
//REPOSITORY
private final FollowRepository followRepository;
//QUERY_SERVICE
private final MemberQueryService memberQueryService;

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
import com.example.waggle.domain.member.repository.MemberRepository;
import com.example.waggle.domain.recommend.repository.RecommendRepository;
import com.example.waggle.domain.schedule.entity.Schedule;
import com.example.waggle.domain.schedule.entity.Team;
import com.example.waggle.domain.schedule.repository.ScheduleRepository;
import com.example.waggle.domain.schedule.repository.TeamRepository;
import com.example.waggle.domain.schedule.service.ScheduleCommandService;
import com.example.waggle.global.exception.handler.MemberHandler;
import com.example.waggle.global.payload.code.ErrorStatus;
Expand All @@ -41,7 +43,6 @@
@Service
public class MemberCommandServiceImpl implements MemberCommandService {

//REPOSITORY
private final MemberRepository memberRepository;
private final CommentRepository commentRepository;
private final ReplyRepository replyRepository;
Expand All @@ -52,9 +53,8 @@ public class MemberCommandServiceImpl implements MemberCommandService {
private final AnswerRepository answerRepository;
private final SirenRepository sirenRepository;
private final ScheduleRepository scheduleRepository;
//QUERY_SERVICE
private final TeamRepository teamRepository;
private final MemberQueryService memberQueryService;
//COMMAND_SERVICE
private final AwsS3Service awsS3Service;
private final RedisService redisService;
private final StoryCommandService storyCommandService;
Expand Down Expand Up @@ -133,6 +133,14 @@ public void deleteMember() {
sirens.forEach(siren -> sirenCommandService.deleteSiren(siren.getId()));
schedules.forEach(schedule -> scheduleCommandService.deleteSchedule(schedule.getId()));

List<Team> teamsByLeader = teamRepository.findListByTeamMembers_Member_Username(username);
teamsByLeader.stream()
.forEach(team -> {
team.getSchedules().stream()
.forEach(schedule -> scheduleCommandService.deleteScheduleForHardReset(schedule.getId()));
teamRepository.deleteById(team.getId());
});

memberRepository.delete(member);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,8 @@
@Service
public class PetCommandServiceImpl implements PetCommandService {

//REPOSITORY
private final MemberRepository memberRepository;
private final PetRepository petRepository;
//QUERY_SERVICE
private final MemberQueryService memberQueryService;

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@
@Transactional
@Service
public class RecommendCommandServiceImpl implements RecommendCommandService {
//REPOSITORY
private final RecommendRepository recommendRepository;
//SERVICE
private final BoardService boardService;
private final MemberQueryService memberQueryService;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ public interface TeamMemberRepository extends JpaRepository<TeamMember, Long> {

Optional<TeamMember> findTeamMemberByMemberUsernameAndTeamId(String username, Long teamId);

void deleteAllByMemberUsername(String username);

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ public interface ScheduleCommandService {

void deleteSchedule(Long scheduleId);

void deleteScheduleForHardReset(Long scheduleId);

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,18 @@
import com.example.waggle.global.exception.handler.TeamHandler;
import com.example.waggle.global.payload.code.ErrorStatus;
import com.example.waggle.web.dto.schedule.ScheduleRequest.Post;
import groovy.util.logging.Slf4j;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Slf4j
@RequiredArgsConstructor
@Transactional
@Service
public class ScheduleCommandServiceImpl implements ScheduleCommandService {

//REPOSITORY
private final ScheduleRepository scheduleRepository;
private final TeamRepository teamRepository;
private final MemberRepository memberRepository;
//SERVICE
private final CommentCommandService commentCommandService;
private final BoardService boardService;

Expand Down Expand Up @@ -80,6 +76,16 @@ public void deleteSchedule(Long scheduleId) {
scheduleRepository.delete(schedule);
}

@Override
public void deleteScheduleForHardReset(Long scheduleId) {
scheduleRepository.findById(scheduleId).ifPresent(
schedule -> {
schedule.getComments().forEach(comment -> commentCommandService.deleteCommentForHardReset(comment.getId()));
scheduleRepository.delete(schedule);
}
);
}

private static void validateTeamMember(Team team, Member member) {
boolean isWriterInTeam = team.getTeamMembers().stream()
.anyMatch(teamMember -> teamMember.getMember().equals(member));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,12 @@
@Service
public class TeamCommandServiceImpl implements TeamCommandService {

//REPOSITORY
private final MemberRepository memberRepository;
private final TeamRepository teamRepository;
private final TeamMemberRepository teamMemberRepository;
private final ScheduleRepository scheduleRepository;
private final ParticipationRepository participationRepository;
//QUERY_SERVICE
private final MemberQueryService memberQueryService;
//COMMAND_SERVICE
private final ScheduleCommandService scheduleCommandService;

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public ApiResponseDto<JwtToken> loginByOAuth2(HttpServletRequest request, HttpSe
public ApiResponseDto<JwtToken> refreshToken(HttpServletRequest request, HttpServletResponse response) {
String refreshToken = getRefreshInCookie(request, response);
JwtToken newTokens = tokenService.issueTokens(refreshToken);
CookieUtil.addCookie(response, "refresh_token", newTokens.getRefreshToken(), week);

return ApiResponseDto.onSuccess(newTokens);
}
Expand All @@ -73,7 +74,7 @@ private static String getRefreshInCookie(HttpServletRequest request, HttpServlet
String refreshToken = CookieUtil.getCookie(request, "refresh_token")
.map(cookie -> cookie.getValue())
.orElseThrow(() -> new GeneralException(ErrorStatus.AUTH_REFRESH_NOT_EXIST_IN_COOKIE));
CookieUtil.deleteCookie(request, response, refreshToken);
CookieUtil.deleteCookie(request, response, "refresh_token");
return refreshToken;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ public static AnswerResponse.ViewDto toViewDto(Answer answer) {
.username(answer.getMember().getUsername())
.profileImg(MediaUtil.getProfile(answer.getMember()))
.createDate(answer.getCreatedDate())
.hashtags(answer.getBoardHashtags().stream()
.map(bh -> bh.getHashtag().getContent()).collect(Collectors.toList()))
.medias(MediaUtil.getBoardMedias(answer))
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ public static class Post {
@NotNull(message = "답변 내용을 작성해주세요.")
@Max(1500)
private String content;

@Builder.Default
private List<String> hashtags = new ArrayList<>();
}

@Getter
Expand All @@ -36,8 +33,6 @@ public static class Put {
private String content;
@Builder.Default
private List<String> medias = new ArrayList<>();
@Builder.Default
private List<String> hashtags = new ArrayList<>();

private String username;
private String profileImg;
Expand Down

0 comments on commit 4b820c4

Please sign in to comment.