diff --git a/src/main/java/team7/inplace/admin/banner/application/BannerService.java b/src/main/java/team7/inplace/admin/banner/application/BannerService.java index 37159b7a..3ed24e78 100644 --- a/src/main/java/team7/inplace/admin/banner/application/BannerService.java +++ b/src/main/java/team7/inplace/admin/banner/application/BannerService.java @@ -4,9 +4,9 @@ import java.util.List; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; +import team7.inplace.admin.banner.application.command.BannerCommand.Create; import team7.inplace.admin.banner.application.dto.BannerInfo; import team7.inplace.admin.banner.application.dto.BannerInfo.Detail; -import team7.inplace.admin.banner.application.command.BannerCommand.Create; import team7.inplace.admin.banner.persistence.BannerRepository; import team7.inplace.admin.banner.persistence.BannerS3Repository; @@ -31,4 +31,11 @@ public List getBanners() { .map(BannerInfo.Detail::from) .toList(); } + + public void deleteBanner(Long id) { + var banner = bannerRepository.findById(id) + .orElseThrow(() -> new IllegalArgumentException("존재하지 않는 배너입니다.")); + bannerS3Repository.deleteBanner(banner.getImgPath()); + bannerRepository.delete(banner); + } } diff --git a/src/main/java/team7/inplace/admin/banner/persistence/BannerS3Repository.java b/src/main/java/team7/inplace/admin/banner/persistence/BannerS3Repository.java index 98e6102a..7277f069 100644 --- a/src/main/java/team7/inplace/admin/banner/persistence/BannerS3Repository.java +++ b/src/main/java/team7/inplace/admin/banner/persistence/BannerS3Repository.java @@ -4,11 +4,13 @@ import com.amazonaws.services.s3.model.ObjectMetadata; import java.util.UUID; import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Repository; import org.springframework.web.multipart.MultipartFile; import team7.inplace.infra.s3.AwsProperties; @Repository +@Slf4j @RequiredArgsConstructor public class BannerS3Repository { private final AmazonS3Client amazonS3Client; @@ -29,4 +31,15 @@ public String uploadBanner(MultipartFile banner) { throw new RuntimeException("Failed to upload banner", e); } } + + public void deleteBanner(String imgPath) { + var bucketName = awsProperties.bucketName(); + var key = imgPath.substring(imgPath.lastIndexOf("banner")); + + try { + amazonS3Client.deleteObject(bucketName, key); + } catch (Exception e) { + throw new RuntimeException("Failed to delete banner", e); + } + } } diff --git a/src/main/java/team7/inplace/admin/banner/presentation/BannerController.java b/src/main/java/team7/inplace/admin/banner/presentation/BannerController.java index 4b6c1bd0..b510a0fa 100644 --- a/src/main/java/team7/inplace/admin/banner/presentation/BannerController.java +++ b/src/main/java/team7/inplace/admin/banner/presentation/BannerController.java @@ -5,7 +5,9 @@ import lombok.extern.slf4j.Slf4j; 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; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @@ -33,4 +35,9 @@ public ResponseEntity> getBanners() { .toList(); return new ResponseEntity<>(response, HttpStatus.OK); } + + @DeleteMapping("/{id}") + public void deleteBanner(@PathVariable Long id) { + bannerService.deleteBanner(id); + } } diff --git a/src/main/resources/static/js/banner.js b/src/main/resources/static/js/banner.js index bc465854..60ef7036 100644 --- a/src/main/resources/static/js/banner.js +++ b/src/main/resources/static/js/banner.js @@ -89,4 +89,30 @@ document.getElementById('imageUploadForm').addEventListener('submit', function ( alert(error.message || '업로드 중 오류가 발생했습니다.'); console.error('Error:', error); }); -}); \ No newline at end of file +}); + +// 이미지 삭제 요청 +function deleteImage(element) { + const imageId = element.getAttribute('data-id'); + if (confirm('이미지를 삭제하시겠습니까?')) { + fetch(`/banner/${imageId}`, { + method: 'DELETE' + }) + .then(response => { + if (!response.ok) { + return response.text().then(text => { + throw new Error(text || '삭제 실패'); + }); + } + return response.text(); + }) + .then(message => { + alert(message || '이미지가 성공적으로 삭제되었습니다.'); + location.reload(); + }) + .catch(error => { + alert(error.message || '삭제 중 오류가 발생했습니다.'); + console.error('Error:', error); + }); + } +} \ No newline at end of file