-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
[Chore] 이미지 세팅
- Loading branch information
Showing
19 changed files
with
318 additions
and
10 deletions.
There are no files selected for viewing
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
24 changes: 24 additions & 0 deletions
24
src/main/java/zerobibim/flory/domain/Image/controller/ImageConroller.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,24 @@ | ||
package zerobibim.flory.domain.Image.controller; | ||
|
||
import io.swagger.v3.oas.annotations.Operation; | ||
import io.swagger.v3.oas.annotations.tags.Tag; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.web.bind.annotation.*; | ||
import org.springframework.web.multipart.MultipartFile; | ||
import zerobibim.flory.domain.Image.dto.response.ImageIdResponse; | ||
import zerobibim.flory.domain.Image.service.ImageService; | ||
import zerobibim.flory.global.common.ApiPayload.ApiResponse; | ||
|
||
@Tag(name = "Image API", description = "이미지 관련 API") | ||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("/image") | ||
public class ImageConroller { | ||
private final ImageService imageService; | ||
|
||
@PostMapping | ||
@Operation(summary = "이미지 생성 API") | ||
public ApiResponse<ImageIdResponse> createImage(@RequestPart("flower_image") MultipartFile flowerImage) { | ||
return ApiResponse.onSuccess(imageService.createImage(flowerImage)); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/zerobibim/flory/domain/Image/dto/response/ImageIdResponse.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,12 @@ | ||
package zerobibim.flory.domain.Image.dto.response; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
public class ImageIdResponse { | ||
private Long ImageId; | ||
} |
41 changes: 41 additions & 0 deletions
41
src/main/java/zerobibim/flory/domain/Image/entity/Image.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,41 @@ | ||
package zerobibim.flory.domain.Image.entity; | ||
|
||
import jakarta.persistence.*; | ||
import lombok.*; | ||
import org.hibernate.annotations.Where; | ||
import zerobibim.flory.global.common.BaseTime; | ||
|
||
@Entity | ||
@Getter | ||
@Where(clause = "deleted_at is null") | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
@AllArgsConstructor | ||
public class Image extends BaseTime { | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long id; | ||
|
||
@Column(nullable = false) | ||
private String url; | ||
|
||
private Long senderId; | ||
|
||
private Long receiverId; | ||
|
||
private Boolean isNFT; | ||
|
||
@Builder | ||
public Image(String url) { | ||
this.url = url; | ||
this.senderId = null; | ||
this.receiverId = null; | ||
this.isNFT = Boolean.FALSE; | ||
} | ||
|
||
public void updateImage(Long senderId, Long receiverId) { | ||
this.senderId = senderId; | ||
this.receiverId = receiverId; | ||
this.isNFT = Boolean.TRUE; | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/zerobibim/flory/domain/Image/mapper/ImageMapper.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 zerobibim.flory.domain.Image.mapper; | ||
|
||
import org.springframework.stereotype.Component; | ||
import zerobibim.flory.domain.Image.entity.Image; | ||
|
||
@Component | ||
public class ImageMapper { | ||
public Image toEntity(String url) { | ||
return Image.builder() | ||
.url(url) | ||
.build(); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/zerobibim/flory/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,12 @@ | ||
package zerobibim.flory.domain.Image.repository; | ||
|
||
import zerobibim.flory.domain.Image.entity.Image; | ||
|
||
import java.util.Optional; | ||
|
||
public interface ImageRepository { | ||
|
||
Image save(Image image); | ||
|
||
Optional<Image> findImageById(Long id); | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/zerobibim/flory/domain/Image/repository/JpaImageRepository.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,7 @@ | ||
package zerobibim.flory.domain.Image.repository; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import zerobibim.flory.domain.Image.entity.Image; | ||
|
||
public interface JpaImageRepository extends JpaRepository<Image, Long>, ImageRepository { | ||
} |
54 changes: 54 additions & 0 deletions
54
src/main/java/zerobibim/flory/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,54 @@ | ||
package zerobibim.flory.domain.Image.service; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
import org.springframework.web.multipart.MultipartFile; | ||
import zerobibim.flory.domain.Image.dto.response.ImageIdResponse; | ||
import zerobibim.flory.domain.Image.entity.Image; | ||
import zerobibim.flory.domain.Image.mapper.ImageMapper; | ||
import zerobibim.flory.domain.Image.repository.ImageRepository; | ||
import zerobibim.flory.global.common.ApiPayload.code.status.ErrorStatus; | ||
import zerobibim.flory.global.common.EntityLoader; | ||
import zerobibim.flory.global.common.ExceptionHandler; | ||
import zerobibim.flory.utils.S3ImageComponent; | ||
|
||
import java.util.Optional; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class ImageService implements EntityLoader<Image, Long> { | ||
|
||
private final ImageMapper imageMapper; | ||
private final ImageRepository imageRepository; | ||
private final S3ImageComponent s3ImageComponent; | ||
|
||
@Transactional | ||
public ImageIdResponse createImage(final MultipartFile flowerImage) { | ||
String imageUrl = uploadImage(flowerImage); | ||
|
||
Image newImage = imageRepository.save( | ||
imageMapper.toEntity(imageUrl)); | ||
return new ImageIdResponse(newImage.getId()); | ||
} | ||
|
||
private String uploadImage(final MultipartFile flowerImage) { | ||
if(flowerImage.isEmpty()) { | ||
throw new ExceptionHandler(ErrorStatus.IMAGE_BLANK); | ||
} | ||
return s3ImageComponent.uploadImage("flower-image", flowerImage); | ||
} | ||
|
||
@Transactional | ||
public void makeNft(Long imageId, Long senderId, Long receiverId) { | ||
Image image = loadEntity(imageId); | ||
image.updateImage(senderId, receiverId); | ||
} | ||
|
||
@Override | ||
public Image loadEntity(Long id) { | ||
Optional<Image> image = imageRepository.findImageById(id); | ||
if(image.isEmpty()) throw new ExceptionHandler(ErrorStatus.IMAGE_NOT_FOUND); | ||
return image.get(); | ||
} | ||
} |
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
13 changes: 13 additions & 0 deletions
13
src/main/java/zerobibim/flory/domain/flower/dto/request/FlowerInsertImageRequest.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 zerobibim.flory.domain.flower.dto.request; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Getter | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public class FlowerInsertImageRequest { | ||
private Long flowerId; | ||
private Long imageId; | ||
} |
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
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
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
Oops, something went wrong.