Skip to content

Commit

Permalink
[feat] 배너 삭제 기능 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
sanghee0820 committed Nov 14, 2024
1 parent addec4e commit 2696dfa
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -31,4 +31,11 @@ public List<Detail> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -33,4 +35,9 @@ public ResponseEntity<List<BannerResponse.Info>> getBanners() {
.toList();
return new ResponseEntity<>(response, HttpStatus.OK);
}

@DeleteMapping("/{id}")
public void deleteBanner(@PathVariable Long id) {
bannerService.deleteBanner(id);
}
}
28 changes: 27 additions & 1 deletion src/main/resources/static/js/banner.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,30 @@ document.getElementById('imageUploadForm').addEventListener('submit', function (
alert(error.message || '업로드 중 오류가 발생했습니다.');
console.error('Error:', error);
});
});
});

// 이미지 삭제 요청
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);
});
}
}

0 comments on commit 2696dfa

Please sign in to comment.