-
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.
- Loading branch information
1 parent
5ab045e
commit 304b94e
Showing
9 changed files
with
110 additions
and
0 deletions.
There are no files selected for viewing
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; | ||
} |
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/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(); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
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,8 @@ | ||
package zerobibim.flory.domain.Image.repository; | ||
|
||
import zerobibim.flory.domain.Image.entity.Image; | ||
|
||
public interface ImageRepository { | ||
|
||
Image save(Image image); | ||
} |
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 { | ||
} |
38 changes: 38 additions & 0 deletions
38
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,38 @@ | ||
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.ExceptionHandler; | ||
import zerobibim.flory.utils.S3ImageComponent; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class ImageService { | ||
|
||
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); | ||
} | ||
} |
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