Skip to content

Commit

Permalink
feat: 리스트 삭제 API 구현 (#83)
Browse files Browse the repository at this point in the history
* feat: 리스트 삭제 API 구현 (#62)

* feat: 리스트 삭제 시, s3에 존재하는 이미지 삭제하는 로직 수정 (#62)
  • Loading branch information
kdkdhoho authored Feb 8, 2024
1 parent c123e8c commit 03a0e7d
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/main/java/com/listywave/ListywaveApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationPropertiesScan;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import org.springframework.scheduling.annotation.EnableAsync;

@EnableAsync
@EnableJpaAuditing
@SpringBootApplication
@ConfigurationPropertiesScan(basePackages = {"com.listywave"})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package com.listywave.collaborator.domain.repository;

import com.listywave.collaborator.domain.Collaborator;
import com.listywave.list.application.domain.Lists;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;

public interface CollaboratorRepository extends JpaRepository<Collaborator, Long> {

List<Collaborator> findAllByListId(Long listId);

void deleteAllByList(Lists lists);
}
6 changes: 5 additions & 1 deletion src/main/java/com/listywave/common/exception/ErrorCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ public enum ErrorCode {
INVALID_ACCESS_TOKEN(HttpStatus.UNAUTHORIZED, "유효하지 않은 AccessToken 입니다. 다시 로그인해주세요."),

// list
INVALID_ACCESS(HttpStatus.FORBIDDEN, "접근 권한이 존재하지 않습니다.");
INVALID_ACCESS(HttpStatus.FORBIDDEN, "접근 권한이 존재하지 않습니다."),

// S3
S3_DELETE_OBJECTS_EXCEPTION(HttpStatus.INTERNAL_SERVER_ERROR, "서버 오류, 관리자에게 문의하세요"),
;

private final HttpStatus status;
private final String detail;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
package com.listywave.image.application.service;

import static com.listywave.common.exception.ErrorCode.S3_DELETE_OBJECTS_EXCEPTION;

import com.amazonaws.AmazonServiceException;
import com.amazonaws.HttpMethod;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.Headers;
import com.amazonaws.services.s3.model.CannedAccessControlList;
import com.amazonaws.services.s3.model.DeleteObjectRequest;
import com.amazonaws.services.s3.model.GeneratePresignedUrlRequest;
import com.amazonaws.services.s3.model.ListObjectsV2Result;
import com.amazonaws.services.s3.model.S3ObjectSummary;
import com.listywave.common.exception.CustomException;
import com.listywave.common.exception.ErrorCode;
import com.listywave.common.util.UserUtil;
Expand All @@ -24,6 +30,7 @@
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand Down Expand Up @@ -195,4 +202,21 @@ public String getCurrentProfile() {
private boolean isActivateDev(String profile) {
return profile.equals(DEV);
}

@Async
public void deleteAllOfListImages(Long listId) {
String path = getCurrentProfile() + "/lists_item/" + listId + "/";
try {
ListObjectsV2Result listObjects;
do {
listObjects = amazonS3.listObjectsV2(bucket, path);
for (S3ObjectSummary object : listObjects.getObjectSummaries()) {
amazonS3.deleteObject(new DeleteObjectRequest(bucket, object.getKey()));
}
listObjects.setContinuationToken(listObjects.getNextContinuationToken());
} while (listObjects.isTruncated());
} catch (AmazonServiceException e) {
throw new CustomException(S3_DELETE_OBJECTS_EXCEPTION);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import com.listywave.common.exception.CustomException;
import com.listywave.common.exception.ErrorCode;
import com.listywave.common.util.UserUtil;
import com.listywave.image.application.service.ImageService;
import com.listywave.list.application.domain.Comment;
import com.listywave.list.application.domain.Item;
import com.listywave.list.application.domain.Lists;
import com.listywave.list.application.dto.ListCreateCommand;
Expand All @@ -14,6 +16,8 @@
import com.listywave.list.application.dto.response.ListRecentResponse;
import com.listywave.list.application.dto.response.ListTrandingResponse;
import com.listywave.list.presentation.dto.request.ItemCreateRequest;
import com.listywave.list.repository.CommentRepository;
import com.listywave.list.repository.ReplyRepository;
import com.listywave.list.repository.list.ListRepository;
import com.listywave.user.application.domain.Follow;
import com.listywave.user.application.domain.User;
Expand All @@ -32,8 +36,11 @@ public class ListService {

private final UserUtil userUtil;
private final JwtManager jwtManager;
private final ImageService imageService;
private final ListRepository listRepository;
private final UserRepository userRepository;
private final ReplyRepository replyRepository;
private final CommentRepository commentRepository;
private final CollaboratorRepository collaboratorRepository;
private final FollowRepository followRepository;

Expand Down Expand Up @@ -124,7 +131,7 @@ public ListDetailResponse getListDetail(Long listId, String accessToken) {
@Transactional(readOnly = true)
public List<ListTrandingResponse> getTrandingList() {
List<Lists> lists = listRepository.findTrandingLists();
lists.forEach(Lists::sortItems); // <-- 추가 !
lists.forEach(Lists::sortItems);
return lists.stream()
.map(list -> ListTrandingResponse.of(list, getImageUrlTopRankItem(list.getItems())))
.toList();
Expand All @@ -138,6 +145,18 @@ private String getImageUrlTopRankItem(List<Item> items) {
.orElse("");
}

public void deleteList(Long listId) {
imageService.deleteAllOfListImages(listId);

Lists list = listRepository.getById(listId);

collaboratorRepository.deleteAllByList(list);
List<Comment> comments = commentRepository.findAllByList(list);
replyRepository.deleteAllByCommentIn(comments);
commentRepository.deleteAllInBatch(comments);

listRepository.deleteById(listId);
}
@Transactional(readOnly = true)
public ListRecentResponse getRecentLists(String accessToken) {
if (isSignedIn(accessToken)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
Expand Down Expand Up @@ -53,6 +54,12 @@ ResponseEntity<List<ListTrandingResponse>> getTrandingList() {
return ResponseEntity.ok().body(trandingList);
}

@DeleteMapping("/{listId}")
ResponseEntity<Void> deleteList(@PathVariable(value = "listId") Long listId) {
listService.deleteList(listId);
return ResponseEntity.noContent().build();
}

@GetMapping()
ResponseEntity<ListRecentResponse> getRecentLists(
@RequestHeader(value = "Authorization", defaultValue = "") String accessToken
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.listywave.common.exception.CustomException;
import com.listywave.list.application.domain.Comment;
import com.listywave.list.application.domain.Lists;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;

public interface CommentRepository extends JpaRepository<Comment, Long>, CustomCommentRepository {
Expand All @@ -14,4 +15,6 @@ default Comment getById(Long id) {
}

Long countByList(Lists list);

List<Comment> findAllByList(Lists list);
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ public interface ReplyRepository extends JpaRepository<Reply, Long> {
boolean existsByComment(Comment comment);

List<Reply> getAllByComment(Comment comment);

void deleteAllByCommentIn(List<Comment> comments);
}

0 comments on commit 03a0e7d

Please sign in to comment.