-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
이미지 관리 테이블 작성한다 #67
Open
jjt4515
wants to merge
19
commits into
week7
Choose a base branch
from
feature/ISSUE-33
base: week7
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
이미지 관리 테이블 작성한다 #67
Changes from 12 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
98cfa15
feat: S3PresignedUrl
stopmin 9953a35
refactor: AwsProperties 네이밍 수정
stopmin 5d724bb
refactor: 패키지 분리
stopmin 2b13d19
S3 presignedUrl 테스트 코드 작성 #32
jjt4515 ab050b5
fix: aws 자격 증명 명시 #33
jjt4515 b8eea9a
refactor: PresignedUrlPut 파라미터 수정 #33
jjt4515 0e13d8e
feat: 이미지 정보 저장 로직 구현 #33
jjt4515 9233f32
refactor: EncryptionUtil 인코딩 변수 추가 #33
jjt4515 f2cd491
refactor: presigned-url-put을 PostMapping으로 변경 #33
jjt4515 78b9a6f
feat: 이미지 정보 관리 CRUD 작성 #33
jjt4515 99d7b2b
refactor: 이미지 key 형식 수정 #33
jjt4515 060a05b
fix: S3PresignedUrlController 오류 해결 #33
jjt4515 243e398
feat: 이미지 삭제 url 수정
jjt4515 6cd5126
feat: 이미지 delete 문 오타 수정 #33
jjt4515 0d06716
fix: 이미지 소프트 삭제 반영 #33
jjt4515 bdcebeb
refactor: 이미지 서비스 리팩토링 #33
jjt4515 d87d201
refactor: 소프트 삭제된 이미지 저장 시 복구시키는 로직 추가 #33
jjt4515 5b6e19b
feat: 이미지 복구 로직 추가 #33
jjt4515 6fcc401
refactor: 이미지 복구 로직 수정 #33
jjt4515 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
61 changes: 61 additions & 0 deletions
61
src/main/java/poomasi/domain/image/controller/ImageController.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,61 @@ | ||
package poomasi.domain.image.controller; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.*; | ||
import poomasi.domain.image.dto.ImageRequest; | ||
import poomasi.domain.image.entity.Image; | ||
import poomasi.domain.image.entity.ImageType; | ||
import poomasi.domain.image.service.ImageService; | ||
|
||
import java.util.List; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("/api/image") | ||
public class ImageController { | ||
private final ImageService imageService; | ||
|
||
// 이미지 정보 저장 | ||
@PostMapping | ||
public ResponseEntity<?> saveImageInfo(@RequestBody ImageRequest imageRequest) { | ||
Image savedImage = imageService.saveImage(imageRequest); | ||
return ResponseEntity.ok(savedImage); | ||
} | ||
|
||
// 여러 이미지 정보 저장 | ||
@PostMapping("/multiple") | ||
public ResponseEntity<List<Image>> saveMultipleImages(@RequestBody List<ImageRequest> imageRequests) { | ||
List<Image> savedImages = imageService.saveMultipleImages(imageRequests); | ||
return ResponseEntity.ok(savedImages); | ||
} | ||
|
||
// 특정 이미지 삭제 | ||
@DeleteMapping("delete/{id}") | ||
public ResponseEntity<Void> deleteImage(@PathVariable Long id) { | ||
imageService.deleteImage(id); | ||
return ResponseEntity.noContent().build(); | ||
} | ||
|
||
// 특정 이미지 조회 | ||
@GetMapping("/{id}") | ||
public ResponseEntity<Image> getImage(@PathVariable Long id) { | ||
return ResponseEntity.ok(imageService.getImageById(id)); | ||
} | ||
|
||
// 모든 이미지 조회 (특정 referenceId에 따라) | ||
@GetMapping("/reference/{type}/{referenceId}") | ||
public ResponseEntity<List<Image>> getImagesByTypeAndReference(@PathVariable ImageType type, @PathVariable Long referenceId) { | ||
List<Image> images = imageService.getImagesByTypeAndReferenceId(type, referenceId); | ||
return ResponseEntity.ok(images); | ||
} | ||
|
||
// 이미지 정보 수정 | ||
@PutMapping("/{id}") | ||
public ResponseEntity<?> updateImageInfo(@PathVariable Long id, @RequestBody ImageRequest imageRequest) { | ||
Image updatedImage = imageService.updateImage(id, imageRequest); | ||
return ResponseEntity.ok(updatedImage); | ||
} | ||
|
||
|
||
} |
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,16 @@ | ||
package poomasi.domain.image.dto; | ||
|
||
import poomasi.domain.image.entity.Image; | ||
import poomasi.domain.image.entity.ImageType; | ||
|
||
public record ImageRequest(String objectKey, String imageUrl, ImageType type, Long referenceId) { | ||
public Image toEntity(ImageRequest imageRequest){ | ||
return new Image( | ||
imageRequest.objectKey, | ||
imageRequest.imageUrl, | ||
imageRequest.type, | ||
imageRequest.referenceId | ||
); | ||
} | ||
} | ||
|
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,54 @@ | ||
package poomasi.domain.image.entity; | ||
|
||
import jakarta.persistence.*; | ||
import lombok.*; | ||
import org.hibernate.annotations.SQLDelete; | ||
import poomasi.domain.image.dto.ImageRequest; | ||
|
||
import java.util.Date; | ||
|
||
@Entity | ||
@Table(name = "images", uniqueConstraints = { | ||
@UniqueConstraint(columnNames = {"type", "reference_id", "object_key"}) | ||
}) | ||
@Getter | ||
@Setter | ||
@NoArgsConstructor | ||
@SQLDelete(sql = "UPDATE Image SET deleted_at = current_timestamp WHERE id = ?") | ||
public class Image { | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long id; | ||
|
||
@Column(nullable = false) | ||
private String objectKey; | ||
|
||
@Column(nullable = false) | ||
private String imageUrl; | ||
|
||
@Enumerated(EnumType.STRING) | ||
@Column(nullable = false) | ||
private ImageType type; | ||
|
||
@Column(name = "reference_id", nullable = false) | ||
private Long referenceId; | ||
|
||
@Column(name = "created_at", nullable = false, updatable = false) | ||
@Temporal(TemporalType.TIMESTAMP) | ||
private Date createdAt = new Date(); | ||
|
||
public Image(String objectKey, String imageUrl, ImageType type, Long referenceId) { | ||
this.objectKey = objectKey; | ||
this.imageUrl = imageUrl; | ||
this.type = type; | ||
this.referenceId = referenceId; | ||
} | ||
|
||
public void update(ImageRequest request) { | ||
this.objectKey = request.objectKey(); | ||
this.imageUrl = request.imageUrl(); | ||
this.type = request.type(); | ||
this.referenceId = request.referenceId(); | ||
} | ||
} |
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,5 @@ | ||
package poomasi.domain.image.entity; | ||
|
||
public enum ImageType { | ||
FARM, FARM_REVIEW, PRODUCT, PRODUCT_REVIEW | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/poomasi/domain/image/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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package poomasi.domain.image.repository; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import poomasi.domain.image.entity.Image; | ||
import poomasi.domain.image.entity.ImageType; | ||
|
||
import java.util.List; | ||
|
||
public interface ImageRepository extends JpaRepository<Image, Long> { | ||
long countByTypeAndReferenceId(ImageType type, Long referenceId); | ||
boolean existsByObjectKeyAndReferenceId(String objectKey, Long referenceId); | ||
List<Image> findByTypeAndReferenceId(ImageType type, Long referenceId); | ||
} |
74 changes: 74 additions & 0 deletions
74
src/main/java/poomasi/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,74 @@ | ||
package poomasi.domain.image.service; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
import poomasi.domain.image.dto.ImageRequest; | ||
import poomasi.domain.image.entity.Image; | ||
import poomasi.domain.image.entity.ImageType; | ||
import poomasi.domain.image.repository.ImageRepository; | ||
import poomasi.global.error.BusinessException; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
import static poomasi.global.error.BusinessError.*; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
@Transactional(readOnly = true) | ||
public class ImageService { | ||
private final ImageRepository imageRepository; | ||
|
||
@Transactional | ||
public Image saveImage(ImageRequest imageRequest) { | ||
if (imageRepository.countByTypeAndReferenceId(imageRequest.type(), imageRequest.referenceId()) >= 5) { | ||
throw new BusinessException(IMAGE_LIMIT_EXCEED); | ||
} | ||
|
||
if (imageRepository.existsByObjectKeyAndReferenceId(imageRequest.objectKey(), imageRequest.referenceId())) { | ||
throw new BusinessException(IMAGE_ALREADY_EXISTS); | ||
} | ||
|
||
Image imageEntity = imageRequest.toEntity(imageRequest); | ||
return imageRepository.save(imageEntity); | ||
} | ||
|
||
// 여러 이미지 저장 | ||
@Transactional | ||
public List<Image> saveMultipleImages(List<ImageRequest> imageRequests) { | ||
return imageRequests.stream() | ||
.map(this::saveImage) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
@Transactional | ||
public void deleteImage(Long id) { | ||
imageRepository.deleteById(id); | ||
} | ||
|
||
public Image getImageById(Long id) { | ||
return imageRepository.findById(id) | ||
.orElseThrow(() -> new BusinessException(IMAGE_NOT_FOUND)); | ||
} | ||
|
||
public List<Image> getImagesByTypeAndReferenceId(ImageType type, Long referenceId) { | ||
return imageRepository.findByTypeAndReferenceId(type, referenceId); | ||
} | ||
|
||
// 이미지 수정 | ||
@Transactional | ||
public Image updateImage(Long id, ImageRequest imageRequest) { | ||
Image image = imageRepository.findById(id) | ||
.orElseThrow(() -> new BusinessException(IMAGE_NOT_FOUND)); | ||
|
||
if (imageRepository.countByTypeAndReferenceId(imageRequest.type(), imageRequest.referenceId()) >= 5 && | ||
!image.getType().equals(imageRequest.type())) { | ||
throw new BusinessException(IMAGE_LIMIT_EXCEED); | ||
} | ||
|
||
image.update(imageRequest); | ||
|
||
return imageRepository.save(image); | ||
} | ||
} |
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
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
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
6 changes: 6 additions & 0 deletions
6
src/main/java/poomasi/global/config/s3/dto/request/PresignedUrlPutRequest.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,6 @@ | ||
package poomasi.global.config.s3.dto.request; | ||
|
||
import java.util.Map; | ||
|
||
public record PresignedUrlPutRequest(String keyPrefix, Map<String, String> metadata) { | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
여기 /빠진 것 같습니당~~