Skip to content

Commit

Permalink
feat: 회원 탈퇴 API 추가 완료 (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
limehee authored Sep 26, 2024
1 parent e9fc044 commit 1e29eec
Show file tree
Hide file tree
Showing 32 changed files with 311 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.stempo.api.domain.application.event;

import com.stempo.api.domain.domain.model.Board;
import com.stempo.api.domain.domain.repository.BoardRepository;
import jakarta.annotation.PostConstruct;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Component
@RequiredArgsConstructor
public class BoardEventProcessor implements UserEventProcessor {

private final BoardRepository boardRepository;
private final UserEventProcessorRegistry processorRegistry;

@PostConstruct
public void init() {
processorRegistry.register(this);
}

@Override
@Transactional
public void processUserDeleted(String deviceTag) {
List<Board> boards = boardRepository.findByDeviceTag(deviceTag);
boardRepository.deleteAll(boards);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.stempo.api.domain.application.event;

import com.stempo.api.domain.domain.model.Homework;
import com.stempo.api.domain.domain.repository.HomeworkRepository;
import jakarta.annotation.PostConstruct;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Component
@RequiredArgsConstructor
public class HomeworkEventProcessor implements UserEventProcessor {

private final HomeworkRepository homeworkRepository;
private final UserEventProcessorRegistry processorRegistry;

@PostConstruct
public void init() {
processorRegistry.register(this);
}

@Override
@Transactional
public void processUserDeleted(String deviceTag) {
List<Homework> records = homeworkRepository.findByDeviceTag(deviceTag);
homeworkRepository.deleteAll(records);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.stempo.api.domain.application.event;

import com.stempo.api.domain.domain.model.Record;
import com.stempo.api.domain.domain.repository.RecordRepository;
import jakarta.annotation.PostConstruct;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Component
@RequiredArgsConstructor
public class RecordEventProcessor implements UserEventProcessor {

private final RecordRepository recordRepository;
private final UserEventProcessorRegistry processorRegistry;

@PostConstruct
public void init() {
processorRegistry.register(this);
}

@Override
@Transactional
public void processUserDeleted(String deviceTag) {
List<Record> records = recordRepository.findByDeviceTag(deviceTag);
recordRepository.deleteAll(records);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.stempo.api.domain.application.event;

import lombok.Getter;
import org.springframework.context.ApplicationEvent;

@Getter
public class UserDeletedEvent extends ApplicationEvent {

private final String deviceTag;

public UserDeletedEvent(Object source, String deviceTag) {
super(source);
this.deviceTag = deviceTag;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.stempo.api.domain.application.event;

import lombok.RequiredArgsConstructor;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@RequiredArgsConstructor
public class UserEventDispatcher {

private final List<UserEventProcessor> processors;

@EventListener
public void handleUserDeletedEvent(UserDeletedEvent event) {
processors.forEach(processor -> processor.processUserDeleted(event.getDeviceTag()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.stempo.api.domain.application.event;

public interface UserEventProcessor {

void processUserDeleted(String deviceTag);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.stempo.api.domain.application.event;

import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Component
public class UserEventProcessorRegistry {

private final List<UserEventProcessor> processors = new ArrayList<>();

public void register(UserEventProcessor processor) {
processors.add(processor);
}

public List<UserEventProcessor> getProcessors() {
return Collections.unmodifiableList(processors);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
Expand All @@ -22,18 +23,21 @@ public class AchievementServiceImpl implements AchievementService {
private final ApplicationEventPublisher eventPublisher;

@Override
@Transactional
public Long registerAchievement(AchievementRequestDto requestDto) {
Achievement achievement = AchievementRequestDto.toDomain(requestDto);
return repository.save(achievement).getId();
}

@Override
@Transactional(readOnly = true)
public PagedResponseDto<AchievementResponseDto> getAchievements(Pageable pageable) {
Page<Achievement> achievements = repository.findAll(pageable);
return new PagedResponseDto<>(achievements.map(AchievementResponseDto::toDto));
}

@Override
@Transactional
public Long updateAchievement(Long achievementId, AchievementUpdateRequestDto requestDto) {
Achievement achievement = repository.findByIdOrThrow(achievementId);
achievement.update(requestDto);
Expand All @@ -43,6 +47,7 @@ public Long updateAchievement(Long achievementId, AchievementUpdateRequestDto re
}

@Override
@Transactional
public Long deleteAchievement(Long achievementId) {
Achievement achievement = repository.findByIdOrThrow(achievementId);
achievement.delete();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
Expand All @@ -19,31 +20,36 @@ public class ArticleServiceImpl implements ArticleService {
private final ArticleRepository repository;

@Override
@Transactional
public Long registerArticle(ArticleRequestDto requestDto) {
Article article = ArticleRequestDto.toDomain(requestDto);
return repository.save(article).getId();
}

@Override
@Transactional(readOnly = true)
public PagedResponseDto<ArticleResponseDto> getArticles(Pageable pageable) {
Page<Article> articles = repository.findAll(pageable);
return new PagedResponseDto<>(articles.map(ArticleResponseDto::toDto));
}

@Override
@Transactional(readOnly = true)
public ArticleDetailsResponseDto getArticle(Long articleId) {
Article article = repository.findByIdOrThrow(articleId);
return ArticleDetailsResponseDto.toDto(article);
}

@Override
@Transactional
public Long updateArticle(Long articleId, ArticleUpdateRequestDto requestDto) {
Article article = repository.findByIdOrThrow(articleId);
article.update(requestDto);
return repository.save(article).getId();
}

@Override
@Transactional
public Long deleteArticle(Long articleId) {
Article article = repository.findByIdOrThrow(articleId);
article.delete();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ public interface AuthService {

TokenInfo registerUser(AuthRequestDto requestDto);

String unregisterUser();

TokenInfo login(AuthRequestDto requestDto);

TokenInfo reissueToken(HttpServletRequest request);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.stempo.api.domain.application.service;

import com.stempo.api.domain.application.event.UserDeletedEvent;
import com.stempo.api.domain.application.exception.UserAlreadyExistsException;
import com.stempo.api.domain.domain.model.User;
import com.stempo.api.domain.presentation.dto.request.AuthRequestDto;
Expand All @@ -11,10 +12,12 @@
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
Expand All @@ -25,8 +28,10 @@ public class AuthServiceImpl implements AuthService {
private final JwtTokenProvider jwtTokenProvider;
private final CustomAuthenticationProvider authenticationManager;
private final PasswordEncoder passwordEncoder;
private final ApplicationEventPublisher eventPublisher;

@Override
@Transactional
public TokenInfo registerUser(AuthRequestDto requestDto) {
String deviceTag = requestDto.getDeviceTag();
String password = requestDto.getPassword();
Expand All @@ -42,6 +47,16 @@ public TokenInfo registerUser(AuthRequestDto requestDto) {
}

@Override
@Transactional
public String unregisterUser() {
User user = userService.getCurrentUser();
userService.delete(user);
eventPublisher.publishEvent(new UserDeletedEvent(this, user.getDeviceTag()));
return user.getDeviceTag();
}

@Override
@Transactional
public TokenInfo login(AuthRequestDto requestDto) {
String deviceTag = requestDto.getDeviceTag();
String password = requestDto.getPassword();
Expand All @@ -52,6 +67,7 @@ public TokenInfo login(AuthRequestDto requestDto) {
}

@Override
@Transactional
public TokenInfo reissueToken(HttpServletRequest request) {
String refreshToken = jwtTokenProvider.resolveToken(request);
validateRefreshToken(refreshToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
Expand All @@ -22,6 +23,7 @@ public class BoardServiceImpl implements BoardService {
private final BoardRepository repository;

@Override
@Transactional
public Long registerBoard(BoardRequestDto requestDto) {
User user = userService.getCurrentUser();
Board board = BoardRequestDto.toDomain(requestDto, user.getDeviceTag());
Expand All @@ -30,13 +32,15 @@ public Long registerBoard(BoardRequestDto requestDto) {
}

@Override
@Transactional(readOnly = true)
public PagedResponseDto<BoardResponseDto> getBoardsByCategory(BoardCategory category, Pageable pageable) {
validateAccessPermissionForSuggestion(category);
Page<Board> boards = repository.findByCategory(category, pageable);
return new PagedResponseDto<>(boards.map(BoardResponseDto::toDto));
}

@Override
@Transactional
public Long updateBoard(Long boardId, BoardUpdateRequestDto requestDto) {
User user = userService.getCurrentUser();
Board board = repository.findByIdOrThrow(boardId);
Expand All @@ -46,6 +50,7 @@ public Long updateBoard(Long boardId, BoardUpdateRequestDto requestDto) {
}

@Override
@Transactional
public Long deleteBoard(Long boardId) {
User user = userService.getCurrentUser();
Board board = repository.findByIdOrThrow(boardId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,31 @@ public class HomeworkServiceImpl implements HomeworkService {
private final UserService userService;
private final HomeworkRepository repository;

@Transactional
@Override
@Transactional
public Long addHomework(HomeworkRequestDto requestDto) {
String deviceTag = userService.getCurrentDeviceTag();
Homework homework = HomeworkRequestDto.toDomain(requestDto, deviceTag);
return repository.save(homework).getId();
}

@Transactional(readOnly = true)
@Override
@Transactional(readOnly = true)
public PagedResponseDto<HomeworkResponseDto> getHomeworks(Boolean completed, Pageable pageable) {
Page<Homework> homeworks = repository.findByCompleted(completed, pageable);
return new PagedResponseDto<>(homeworks.map(HomeworkResponseDto::toDto));
}

@Transactional
@Override
@Transactional
public Long updateHomework(Long homeworkId, HomeworkUpdateRequestDto requestDto) {
Homework homework = repository.findByIdOrThrow(homeworkId);
homework.update(requestDto);
return repository.save(homework).getId();
}

@Transactional
@Override
@Transactional
public Long deleteHomework(Long homeworkId) {
Homework homework = repository.findByIdOrThrow(homeworkId);
repository.delete(homework);
Expand Down
Loading

0 comments on commit 1e29eec

Please sign in to comment.