Skip to content

Commit

Permalink
[feat] #15 - ImageService 생성
Browse files Browse the repository at this point in the history
  • Loading branch information
leeseulgi0208 committed Jan 18, 2025
1 parent c7b7f14 commit 3f7f19e
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 7 deletions.
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);
}
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);
}
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,4 @@ public class InvitationCreateRequestDTO {

// BankAccountDTO 생성 후 활성화
//private List<BankAccountDTO> bankAccounts; // 계좌 정보 리스트

private List<MultipartFile> images; // 이미지 파일 리스트 (4장)
}

0 comments on commit 3f7f19e

Please sign in to comment.