Skip to content

Commit

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

[Feat] 이미지 삭제 기능 추가, 수정 기능 개선
  • Loading branch information
NARUBROWN authored Jan 29, 2024
2 parents ac82b53 + 21a19b9 commit 0ca06af
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: CI & CD with Gradle
name: CD with Gradle

on:
push:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ public ResponseEntity<?> getPhoto (final @PathVariable("id") Long id) {
return ResponseEntity.status(HttpStatus.OK).contentType(MediaType.valueOf("image/png")).body(imageService.getImage(id));
}

@Operation(summary = "사진 여러장 삭제하기", description = "삭제할 사진들의 ID와 게시판의 ID를 입력해주세요")
@Parameter(name = "image_id", description = "사진 ID", required = true)
@Parameter(name = "board_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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
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.redis.domain.ImageRedis;
import server.inuappcenter.kr.data.repository.ImageRepository;
import server.inuappcenter.kr.data.utils.ImageUtils;
import server.inuappcenter.kr.exception.customExceptions.CustomNotFoundException;
Expand Down Expand Up @@ -36,12 +36,6 @@ public byte[] getImage(Long id) {
});
}

@Transactional
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."));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@
import server.inuappcenter.kr.data.dto.request.BoardRequestDto;
import server.inuappcenter.kr.data.dto.response.BoardResponseDto;
import server.inuappcenter.kr.data.redis.repository.ImageRedisRepository;
import server.inuappcenter.kr.data.redis.repository.BoardResponseRedisRepository;
import server.inuappcenter.kr.data.redis.repository.ImageRedisRepository;
import server.inuappcenter.kr.data.repository.BoardRepository;
import server.inuappcenter.kr.data.repository.ImageRepository;
import server.inuappcenter.kr.data.redis.repository.BoardResponseRedisRepository;
import server.inuappcenter.kr.exception.customExceptions.CustomNotFoundException;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
@RequiredArgsConstructor
Expand Down Expand Up @@ -65,11 +67,6 @@ public CommonResponseDto updateBoard(Long board_id, List<Long> image_id, BoardRe
// 이미지 레포지토리에서 사용자가 보낸 ID로 조회를 먼저 진행하여, 찾아진 이미지 목록을 가짐
List<Image> foundImageList = imageRepository.findByImageIdsAndBoard(image_id, foundBoard);

// multipart에서 이미지를 가져와 데이터와 정보를 해당 image에 업데이트
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 @@ -78,9 +75,21 @@ public CommonResponseDto updateBoard(Long board_id, List<Long> image_id, BoardRe
foundImageIds.add(image.getId());
}


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


// image_id와 일치하는 ImageList를 순회함
for (Image image : foundImageList) {
// image_id와 ImageList의 id가 일치하는 값만 변경처리해줌
for (int j = 0; j < image_id.size(); j++) {
if (Objects.equals(image.getId(), image_id.get(j))) {
image.updateImage(boardRequestDto.getMultipartFiles().get(j));
}
}
}

// 찾아진 ID 목록에 존재하지 않는 ID를 얻어야 하기 때문에 없는 이미지 ID 목록을 만들어줌
List<Long> missingImageIds = new ArrayList<>();
for (Long id : image_id) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ spring:
password: test1234
jpa:
hibernate:
ddl-auto: none
ddl-auto: create
show-sql: true
open-in-view: false
properties:
Expand Down

0 comments on commit 0ca06af

Please sign in to comment.