Skip to content

Commit

Permalink
feat: create image api 생성
Browse files Browse the repository at this point in the history
  • Loading branch information
junseokkim committed Nov 14, 2023
1 parent 5ab045e commit 304b94e
Show file tree
Hide file tree
Showing 9 changed files with 110 additions and 0 deletions.
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));
}
}
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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public class Image extends BaseTime {
@Builder
public Image(String url) {
this.url = url;
this.senderId = null;
this.isNFT = Boolean.FALSE;
}
}
13 changes: 13 additions & 0 deletions src/main/java/zerobibim/flory/domain/Image/mapper/ImageMapper.java
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();
}
}
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);
}
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 {
}
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,8 @@ public void update(String description, Long price) {
this.description = description;
this.price = price;
}

public void updateFlowerImage(Image image) {
this.image = image;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ public enum ErrorStatus implements BaseErrorCode {

// 테스트 관련 응답
TEST_EXCEPTION(HttpStatus.BAD_REQUEST, "TEST4001", "테스트를 위한 에러 코드"),

// 이미지 관련 응답
IMAGE_BLANK(HttpStatus.BAD_REQUEST, "IMAGE4001", "이미지 파일이 없습니다."),
;


Expand Down

0 comments on commit 304b94e

Please sign in to comment.