-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c7b7f14
commit 3f7f19e
Showing
3 changed files
with
51 additions
and
7 deletions.
There are no files selected for viewing
7 changes: 2 additions & 5 deletions
7
src/main/java/com/wedit/weditapp/domain/image/domain/repository/ImageRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,12 @@ | ||
package com.wedit.weditapp.domain.image.domain.repository; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import com.wedit.weditapp.domain.image.domain.Image; | ||
import com.wedit.weditapp.domain.invitation.domain.Invitation; | ||
|
||
public interface ImageRepository extends JpaRepository<Image, Long> { | ||
// 특정 Invitation과 location으로 이미지 조회 | ||
Optional<Image> findByInvitationIdAndLocation(Long invitationId, int location); | ||
|
||
// 특정 Invitation의 모든 이미지를 location 순서대로 조회 | ||
List<Image> findByInvitationIdOrderByLocation(Long invitationId); | ||
List<Image> findByInvitation(Invitation invitation); | ||
} |
49 changes: 49 additions & 0 deletions
49
src/main/java/com/wedit/weditapp/domain/image/service/ImageService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package com.wedit.weditapp.domain.image.service; | ||
|
||
import java.util.List; | ||
|
||
import org.springframework.stereotype.Service; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
import com.wedit.weditapp.domain.image.domain.Image; | ||
import com.wedit.weditapp.domain.image.domain.repository.ImageRepository; | ||
import com.wedit.weditapp.domain.invitation.domain.Invitation; | ||
import com.wedit.weditapp.domain.shared.S3Service; | ||
|
||
import jakarta.transaction.Transactional; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class ImageService { | ||
private final S3Service s3Service; | ||
private final ImageRepository imageRepository; | ||
/** | ||
* S3에 이미지를 업로드하고 URL을 반환하며, DB에 저장. | ||
* | ||
* @param images 업로드할 MultipartFile 리스트 | ||
* @param invitation 연관된 Invitation 엔티티 | ||
*/ | ||
@Transactional | ||
public void saveImages(List<MultipartFile> images, Invitation invitation) { | ||
if (images.size() != 4) { | ||
throw new IllegalArgumentException("Exactly 4 images are required."); | ||
} | ||
|
||
int location = 1; // 이미지 위치 인덱스 | ||
for (MultipartFile image : images) { | ||
String imageUrl = s3Service.upload(image); // S3 업로드 후 URL 반환 | ||
|
||
// 이미지 엔티티 생성 및 저장 | ||
Image imageEntity = Image.builder() | ||
.url(imageUrl) | ||
.location(location++) | ||
.invitation(invitation) | ||
.build(); | ||
|
||
imageRepository.save(imageEntity); | ||
} | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters