Skip to content

Commit

Permalink
Merge pull request #47 from inu-appcenter/wonjeong#44-improve-image-d…
Browse files Browse the repository at this point in the history
…eletion

[Fix] 이미지가 제대로 수정되지 않던 문제를 해결합니다.
  • Loading branch information
NARUBROWN authored Jan 29, 2024
2 parents 46ede8e + 6c669c4 commit 6e3f4dc
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import server.inuappcenter.kr.common.data.dto.CommonResponseDto;
import server.inuappcenter.kr.service.ImageService;

import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/image")
Expand All @@ -25,4 +25,15 @@ public class ImageController {
public ResponseEntity<?> getPhoto (final @PathVariable("id") Long id) {
return ResponseEntity.status(HttpStatus.OK).contentType(MediaType.valueOf("image/png")).body(imageService.getImage(id));
}

@Parameter(name = "image_id", description = "사진 ID", required = true)
@Parameter(name = "board_id", description = "사진 ID", required = true)
@DeleteMapping("/photo")
public ResponseEntity<?> deleteMultiplePhotoByIds (
final @RequestParam(name = "image_id") List<Long> image_id,
final @RequestParam(name = "board_id") Long board_id
) {
imageService.deleteMultipleImages(board_id, image_id);
return ResponseEntity.status(HttpStatus.OK).body(new CommonResponseDto("successfully deleted."));
}
}
30 changes: 30 additions & 0 deletions src/main/java/server/inuappcenter/kr/service/ImageService.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,25 @@
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import server.inuappcenter.kr.data.domain.board.Board;
import server.inuappcenter.kr.data.domain.board.Image;
import server.inuappcenter.kr.data.repository.BoardRepository;
import server.inuappcenter.kr.data.redis.domain.ImageRedis;
import server.inuappcenter.kr.data.redis.repository.ImageRedisRepository;
import server.inuappcenter.kr.data.repository.ImageRepository;
import server.inuappcenter.kr.data.utils.ImageUtils;
import server.inuappcenter.kr.exception.customExceptions.CustomNotFoundException;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;

@Service
@RequiredArgsConstructor
public class ImageService {
private final ImageRepository imageRepository;
private final BoardRepository<Board> boardRepository;
private final ImageRedisRepository imageRedisRepository;

@Transactional(readOnly = true)
Expand All @@ -33,4 +41,26 @@ public void deleteByImageId(Long id) {
imageRedisRepository.deleteById(id);
imageRepository.deleteById(id);
}

@Transactional
public void deleteMultipleImages(Long board_id, List<Long> image_ids) {
Board foundBoard = boardRepository.findById(board_id).orElseThrow(() -> new CustomNotFoundException("The requested ID was not found."));
List<Long> foundImageIds = new ArrayList<>();
for (Image image: foundBoard.getImages()) {
foundImageIds.add(image.getId());
}
if (!new HashSet<>(foundImageIds).containsAll(image_ids)) {
throw new CustomNotFoundException("The requested ID was not found.");
}

int imagesListSize = foundBoard.getImages().size();
for (int i = 0; i < imagesListSize; i++) {
for (Long imageId : image_ids) {
if (Objects.equals(foundBoard.getImages().get(i).getId(), imageId)) {
foundBoard.getImages().remove(i);
imageRepository.deleteById(imageId);
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import server.inuappcenter.kr.common.data.dto.CommonResponseDto;
import server.inuappcenter.kr.data.domain.board.Board;
import server.inuappcenter.kr.data.domain.board.Image;
Expand Down Expand Up @@ -65,12 +64,12 @@ public CommonResponseDto updateBoard(Long board_id, List<Long> image_id, BoardRe
if (boardRequestDto.getMultipartFiles() != null || image_id != null) {
// 이미지 레포지토리에서 사용자가 보낸 ID로 조회를 먼저 진행하여, 찾아진 이미지 목록을 가짐
List<Image> foundImageList = imageRepository.findByImageIdsAndBoard(image_id, foundBoard);

// multipart에서 이미지를 가져와 데이터와 정보를 해당 image에 업데이트
for (Image image: foundImageList) {
for (MultipartFile multipartFile: boardRequestDto.getMultipartFiles()) {
image.updateImage(multipartFile);
}
for (int i = 0; i < foundImageList.size(); i++) {
foundImageList.get(i).updateImage(boardRequestDto.getMultipartFiles().get(i));
}

// DB에 저장되지 않은 이미지에 대한 처리들을 진행해야 함
// 먼저 DB에서 찾아진 ID에 대한 목록을 만들어줌
List<Long> foundImageIds = new ArrayList<>();
Expand All @@ -81,7 +80,7 @@ public CommonResponseDto updateBoard(Long board_id, List<Long> image_id, BoardRe

// 캐시에서 이미지를 삭제한다.
imageRedisRepository.deleteAllById(foundImageIds);

// 찾아진 ID 목록에 존재하지 않는 ID를 얻어야 하기 때문에 없는 이미지 ID 목록을 만들어줌
List<Long> missingImageIds = new ArrayList<>();
for (Long id : image_id) {
Expand Down

0 comments on commit 6e3f4dc

Please sign in to comment.