Skip to content

Commit

Permalink
Merge pull request #90 from Dan-sup/feat/hard_delete
Browse files Browse the repository at this point in the history
Feat/hard delete
  • Loading branch information
ImTakGyun authored Nov 8, 2023
2 parents b49a783 + c2416c2 commit 7acbf93
Show file tree
Hide file tree
Showing 13 changed files with 137 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import com.dansup.server.api.auth.dto.request.SignUpDto;
import com.dansup.server.api.auth.dto.response.AccessTokenDto;
import com.dansup.server.api.auth.service.AuthService;
import com.dansup.server.api.danceclass.service.DanceClassService;
import com.dansup.server.api.profile.service.ProfileService;
import com.dansup.server.api.user.domain.User;
import com.dansup.server.common.AuthUser;
import com.dansup.server.common.response.Response;
Expand All @@ -26,6 +28,8 @@
public class AuthController {

private final AuthService authService;
private final ProfileService profileService;
private final DanceClassService danceClassService;

@ApiOperation(value = "Sign Up", notes = "회원 가입")
@PostMapping(value = "/sign-up", consumes = {MediaType.APPLICATION_JSON_VALUE, MediaType.MULTIPART_FORM_DATA_VALUE})
Expand All @@ -48,6 +52,18 @@ public Response<Void> signOut(@AuthUser User user, @RequestBody RefreshTokenDto
return Response.success(ResponseCode.SUCCESS_OK);
}

@ApiOperation(value = "Delete User", notes = "탈퇴하기")
@PostMapping(value = "/delete")
public Response<Void> deleteUser(@AuthUser User user, @RequestBody RefreshTokenDto refreshTokenDto) {
authService.signOut(user, refreshTokenDto);

danceClassService.deleteAllClass(user);
profileService.deleteProfile(user);

authService.deleteUser(user);
return Response.success(ResponseCode.SUCCESS_OK);
}

@ApiOperation(value = "Reissue Access Token by Refresh Token", notes = "리프레쉬 토큰을 통해 액세스 토큰 재발급")
@PostMapping(value = "/reissuance")
public Response<AccessTokenDto> reissueAccessToken(@RequestBody RefreshTokenDto refreshTokenDto) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,15 @@ public void signOut(User user, RefreshTokenDto refreshTokenDto) {
SecurityContextHolder.clearContext();
}

public void deleteUser(User user){

User findUser = userRepository.findByEmail(user.getEmail()).orElseThrow(
() -> new BaseException(ResponseCode.USER_NOT_FOUND)
);

userRepository.delete(findUser);
}

public AccessTokenDto reissueAccessToken(RefreshTokenDto refreshTokenDto) {

if(!jwtTokenProvider.validateToken(refreshTokenDto.getRefreshToken())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,14 @@ public class DanceClass extends BaseEntity {

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id")
@OnDelete(action = OnDeleteAction.CASCADE)
private User user;

@OneToOne(fetch = FetchType.LAZY)
@OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.REMOVE)
@JoinColumn(name = "cv_id")
@OnDelete(action = OnDeleteAction.CASCADE)
private ClassVideo classVideo;

@Builder.Default
@OneToMany(mappedBy = "danceClass")
@OneToMany(mappedBy = "danceClass", cascade = CascadeType.REMOVE)
private List<ClassGenre> classGenres = new ArrayList<>();

@Column(nullable = false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
public interface DanceClassRepository extends JpaRepository<DanceClass, Long>, DanceClassRepositoryCustom {
Optional<DanceClass> findById(Long dance_class_id);
List<DanceClass> findByState(State state);
List<DanceClass> findByUser(User user);

List<DanceClass> findByUserAndStateNot(User user, State state);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@
import java.util.List;
import java.util.stream.Collectors;

import static com.dansup.server.common.response.ResponseCode.CLASS_NOT_FOUND;
import static com.dansup.server.common.response.ResponseCode.FAIL_BAD_REQUEST;
import static com.dansup.server.common.response.ResponseCode.*;

@Service
@RequiredArgsConstructor
Expand Down Expand Up @@ -100,14 +99,26 @@ public List<GetDanceClassListDto> getAllClassList(){
return createDanceClassListDtos(danceClasses);
}

public void deleteAllClass(User user) throws BaseException {
List<DanceClass> danceClasses = danceClassRepository.findByUser(user);

for (DanceClass danceClass: danceClasses) {
deleteClass(user, danceClass.getId());
}
// danceClasses.stream().map(danceClass -> {
// deleteClass(user, danceClass.getId());
// return null;
// });
}

public void deleteClass(User user, Long classId) throws BaseException {

DanceClass danceClass = loadDanceClass(classId);

compareUser(danceClass, user);

danceClass.updateState(State.Delete);
danceClassRepository.save(danceClass);
deleteFile(danceClass.getClassVideo().getVideoUrl());
danceClassRepository.delete(danceClass);
}

public void closeClass(User user, Long classId) throws BaseException{
Expand Down Expand Up @@ -232,13 +243,21 @@ private DanceClass loadDanceClass(Long classId) {
private void compareUser(DanceClass danceClass, User user) {

if(!danceClass.getUser().getId().equals(user.getId())){
throw new BaseException(FAIL_BAD_REQUEST);
throw new BaseException(FAIL_NOT_POSTER);
}
}

private Profile loadProfile(User user){
public Profile loadProfile(User user){
return profileRepository.findByUser(user).orElseThrow(
() -> new BaseException(ResponseCode.PROFILE_NOT_FOUND)
);
}

public void deleteFile(String ImageURL) {
if(ImageURL == null){
return;
}
String ImageName = ImageURL.split("com/")[1].trim();
s3UploaderService.deleteFile(ImageName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class ClassGenre extends BaseEntity {

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "dance_class_id")
@OnDelete(action = OnDeleteAction.CASCADE)
// @OnDelete(action = OnDeleteAction.CASCADE)
private DanceClass danceClass;

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ public class Portfolio extends BaseEntity {

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "profile_id")
@OnDelete(action = OnDeleteAction.CASCADE)
private Profile profile;

@Column
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ public class PortfolioVideo extends BaseEntity {

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "profile_id")
@OnDelete(action = OnDeleteAction.CASCADE)
private Profile profile;

@Column(nullable = false)
Expand Down
11 changes: 5 additions & 6 deletions src/main/java/com/dansup/server/api/profile/domain/Profile.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ public class Profile extends BaseEntity {

@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id")
@OnDelete(action = OnDeleteAction.CASCADE)
private User user;

@Column(nullable = false, unique = true)
Expand All @@ -40,21 +39,21 @@ public class Profile extends BaseEntity {
@Column(length = 50)
private String intro;

@OneToMany(mappedBy = "profile")
@OneToMany(mappedBy = "profile", cascade = CascadeType.REMOVE)
private List<ProfileGenre> profileGenres = new ArrayList<>();

@OneToOne(fetch = FetchType.LAZY)
@OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.REMOVE)
@JoinColumn(name = "pv_id")
private ProfileVideo profileVideo;

@OneToOne(fetch = FetchType.LAZY)
@OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.REMOVE)
@JoinColumn(name = "pi_id")
private ProfileImage profileImage;

@OneToMany(mappedBy = "profile")
@OneToMany(mappedBy = "profile", cascade = CascadeType.REMOVE)
private List<Portfolio> portfolios = new ArrayList<>();

@OneToMany(mappedBy = "profile")
@OneToMany(mappedBy = "profile", cascade = CascadeType.REMOVE)
private List<PortfolioVideo> portfolioVideos = new ArrayList<>();

@Column(length = 5)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import com.dansup.server.api.danceclass.domain.State;
import com.dansup.server.api.danceclass.dto.response.GetDanceClassListDto;
import com.dansup.server.api.danceclass.repository.DanceClassRepository;
import com.dansup.server.api.danceclass.service.DanceClassService;
import com.dansup.server.api.profile.domain.PortfolioVideo;
import com.dansup.server.api.profile.domain.Profile;
import com.dansup.server.api.profile.dto.response.GetFileUrlDto;
import com.dansup.server.api.profile.dto.response.GetPortfolioDto;
Expand All @@ -15,6 +17,7 @@
import com.dansup.server.api.user.repository.UserRepository;
import com.dansup.server.common.exception.BaseException;
import com.dansup.server.common.response.ResponseCode;
import com.dansup.server.config.s3.S3UploaderService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
Expand All @@ -23,7 +26,7 @@
import java.util.List;
import java.util.stream.Collectors;

import static com.dansup.server.common.response.ResponseCode.FAIL_BAD_REQUEST;
import static com.dansup.server.common.response.ResponseCode.*;

@Service
@RequiredArgsConstructor
Expand All @@ -32,6 +35,7 @@ public class ProfileService {

private final ProfileRepository profileRepository;
private final DanceClassRepository danceClassRepository;
private final DanceClassService danceClassService;


public GetProfileDetailDto getProfileDetail(Long profileId) {
Expand Down Expand Up @@ -143,9 +147,39 @@ public List<GetDanceClassListDto> getProfileClassList(Long profileId) {
}

private Profile loadProfile(Long profileId) {

return profileRepository.findById(profileId).orElseThrow(
() -> new BaseException(ResponseCode.PROFILE_NOT_FOUND)
);
}

private Profile loadProfile(User user) {
return profileRepository.findByUser(user).orElseThrow(
() -> new BaseException(ResponseCode.PROFILE_NOT_FOUND)
);
}

public void compareUser(Profile profile, User user) {

if(!profile.getUser().getId().equals(user.getId())){
throw new BaseException(FAIL_NOT_POSTER);
}
}

public void deleteProfile(User user) throws BaseException {

Profile profile = loadProfile(user);

compareUser(profile, user);

danceClassService.deleteFile(profile.getProfileImage().getUrl());
danceClassService.deleteFile(profile.getProfileVideo().getUrl());

List<PortfolioVideo> portfolioVideos= profile.getPortfolioVideos();

for(PortfolioVideo portfolioVideo: portfolioVideos){
danceClassService.deleteFile(portfolioVideo.getUrl());
}

profileRepository.delete(profile);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,16 @@ public Response<Void> createPortfolioVideo(@AuthUser User user,
return Response.success(ResponseCode.SUCCESS_CREATED);
}

@ApiOperation(value = "Delete PortfolioVideo", notes = "마이페이지에서 포트폴리오 영상 삭제")
@DeleteMapping("/portfolio/video/{pvId}")
public Response<Void> deletePostPortfolioVideo(@AuthUser User user,
@PathVariable("pvId") Long pvId) {

myPageService.deletePortfolioVideo(user, pvId);

return Response.success(ResponseCode.SUCCESS_OK);
}

@ApiOperation(value = "Get Mypage Portfolio", notes = "마이페이지에서 포트폴리오(공연 및 활동 경력) 조회")
@GetMapping(value = "/portfolio")
public Response<List<GetPortfolioDto>> getPortfolioList(@AuthUser User user) {
Expand Down Expand Up @@ -101,10 +111,9 @@ public Response<Void> closeDanceClass( @AuthUser User user,

@ApiOperation(value = "Delete DanceClass", notes = "댄스 수업 삭제")
@DeleteMapping("/class/{danceclassId}")
public Response<Void> deletePost(@AuthUser User user,
@PathVariable("danceclassId") Long danceclassId) {
public Response<Void> deleteDanceClass(@AuthUser User user,
@PathVariable("danceclassId") Long danceclassId) {

// DanceClass 의 State 를 Deleted 로 변경
danceClassService.deleteClass(user, danceclassId);

return Response.success(ResponseCode.SUCCESS_OK);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.dansup.server.api.danceclass.domain.State;
import com.dansup.server.api.danceclass.dto.response.GetDanceClassListDto;
import com.dansup.server.api.danceclass.repository.DanceClassRepository;
import com.dansup.server.api.danceclass.service.DanceClassService;
import com.dansup.server.api.profile.domain.PortfolioVideo;
import com.dansup.server.api.profile.domain.Profile;
import com.dansup.server.api.profile.domain.ProfileVideo;
Expand All @@ -30,18 +31,18 @@
import java.util.List;
import java.util.stream.Collectors;

import static com.dansup.server.common.response.ResponseCode.FAIL_NOT_POSTER;

@Service
@RequiredArgsConstructor
@Slf4j
@Transactional
public class MyPageService {

private final S3UploaderService s3UploaderService;
private final DanceClassService danceClassService;
private final ProfileRepository profileRepository;

private final DanceClassRepository danceClassRepository;

private final S3UploaderService s3UploaderService;

private final PortfolioVideoRepository portfolioVideoRepository;

public GetProfileDetailDto getMyPage(User user) {
Expand Down Expand Up @@ -139,11 +140,31 @@ public void uploadPortfolioVideo(User user, MultipartFile multipartFile) throws
portfolioVideoRepository.save(portfolioVideo);
}

public void deletePortfolioVideo(User user, Long pvId) throws BaseException {

PortfolioVideo portfolioVideo = portfolioVideoRepository.findById(pvId).orElseThrow(
() -> new BaseException(ResponseCode.VIDEO_NOT_FOUND)
);;

compareUser(portfolioVideo, user);

danceClassService.deleteFile(portfolioVideo.getUrl());
portfolioVideoRepository.delete(portfolioVideo);
}

private Profile loadMyPage(User user) {

return profileRepository.findByUser(user).orElseThrow(
() -> new BaseException(ResponseCode.MY_PAGE_NOT_FOUND)
);
}

private void compareUser(PortfolioVideo portfolioVideo, User user) {

Profile profile = loadMyPage(user);

if(!portfolioVideo.getProfile().equals(profile)){
throw new BaseException(FAIL_NOT_POSTER);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public enum ResponseCode {

//fail
FAIL_BAD_REQUEST(BAD_REQUEST, "잘못된 요청입니다."),
FAIL_NOT_POSTER(BAD_REQUEST, "작성자가 아닙니다."),
FAIL_SERVER_ERROR(INTERNAL_SERVER_ERROR, "서버 에러가 발생했습니다."),

// Auth
Expand All @@ -36,7 +37,10 @@ public enum ResponseCode {
PROFILE_NOT_FOUND(NOT_FOUND, "존재하지 않는 프로필입니다."),

//class
CLASS_NOT_FOUND(NOT_FOUND, "존재하지 않는 클래스입니다.");
CLASS_NOT_FOUND(NOT_FOUND, "존재하지 않는 클래스입니다."),

//video
VIDEO_NOT_FOUND(NOT_FOUND, "존재하지 않는 영상입니다.");

private final HttpStatus httpStatus;
private final String message;
Expand Down

0 comments on commit 7acbf93

Please sign in to comment.