Skip to content

Commit

Permalink
Merge pull request #47 from KUIT-Space/refactor/#44/스페이스-생성-api-에서-실제…
Browse files Browse the repository at this point in the history
…-이미지-데이터-저장

Refactor/#44/스페이스 생성 api 에서 실제 이미지 데이터 저장
  • Loading branch information
seongjunnoh authored Jul 28, 2024
2 parents daa15fd + 243841e commit b559fd5
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 9 deletions.
12 changes: 10 additions & 2 deletions src/main/java/space/space_spring/controller/SpaceController.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@
import space.space_spring.entity.UserSpace;
import space.space_spring.exception.SpaceException;
import space.space_spring.response.BaseResponse;
import space.space_spring.service.S3Uploader;
import space.space_spring.service.SpaceService;
import space.space_spring.util.userSpace.UserSpaceUtils;

import java.io.IOException;
import java.util.Optional;

import static space.space_spring.response.status.BaseExceptionResponseStatus.INVALID_SPACE_CREATE;
Expand All @@ -28,14 +30,20 @@ public class SpaceController {

private final SpaceService spaceService;
private final UserSpaceUtils userSpaceUtils;
private final S3Uploader s3Uploader;
private final String spaceImgDirName = "spaceImg";

@PostMapping("/create")
public BaseResponse<PostSpaceCreateResponse> createSpace(@JwtLoginAuth Long userId, @Validated @RequestBody PostSpaceCreateRequest postSpaceCreateRequest, BindingResult bindingResult) {
public BaseResponse<PostSpaceCreateResponse> createSpace(@JwtLoginAuth Long userId, @Validated @ModelAttribute PostSpaceCreateRequest postSpaceCreateRequest, BindingResult bindingResult) throws IOException {
if (bindingResult.hasErrors()) {
throw new SpaceException(INVALID_SPACE_CREATE, getErrorMessage(bindingResult));
}

return new BaseResponse<>(spaceService.createSpace(userId, postSpaceCreateRequest));
// TODO 1. 스페이스 썸네일을 s3에 upload
String spaceImgUrl = s3Uploader.upload(postSpaceCreateRequest.getSpaceProfileImg(), spaceImgDirName);

// TODO 2. s3에 저장하고 받은 이미지 url 정보와 spaceName 정보로 space create 작업 수행
return new BaseResponse<>(spaceService.createSpace(userId, postSpaceCreateRequest.getSpaceName(), spaceImgUrl));
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/space/space_spring/dao/SpaceDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ public class SpaceDao {
@PersistenceContext
private EntityManager em;

public Space saveSpace(PostSpaceCreateRequest postSpaceCreateRequest) {
public Space saveSpace(String spaceName, String spaceImgUrl) {
Space space = new Space();
space.saveSpace(postSpaceCreateRequest.getSpaceName(), postSpaceCreateRequest.getSpaceProfileImg());
space.saveSpace(spaceName, spaceImgUrl);

em.persist(space);
return space;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.hibernate.validator.constraints.Length;
import org.springframework.web.multipart.MultipartFile;
import space.space_spring.validator.ValidFile;

@Getter
@Setter
Expand All @@ -15,6 +17,6 @@ public class PostSpaceCreateRequest {
@NotBlank(message = "스페이스 이름은 공백일 수 없습니다.")
private String spaceName;

@NotBlank(message = "스페이스 프로필 이미지는 공백일 수 없습니다.")
private String spaceProfileImg; // 스페이스 프로필 이미지 (썸네일)
@ValidFile(message = "스페이스 프로필 이미지는 공백일 수 없습니다.")
private MultipartFile spaceProfileImg; // 스페이스 프로필 이미지 (썸네일)
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@
public class PostSpaceCreateResponse {

private Long spaceId;
private String spaceImgUrl; // 사진 url이 잘 생성됐는지 확인하는 용도
}
6 changes: 3 additions & 3 deletions src/main/java/space/space_spring/service/SpaceService.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ public class SpaceService {
private final UserSpaceDao userSpaceDao;

@Transactional
public PostSpaceCreateResponse createSpace(Long userId, PostSpaceCreateRequest postSpaceCreateRequest) {
public PostSpaceCreateResponse createSpace(Long userId, String spaceName, String spaceImgUrl) {

// TODO 1. 스페이스 생성 정보 db insert
Space saveSpace = spaceDao.saveSpace(postSpaceCreateRequest);
Space saveSpace = spaceDao.saveSpace(spaceName, spaceImgUrl);

// TODO 2. 유저_스페이스 매핑 정보 db insert
User manager = userDao.findUserByUserId(userId);
UserSpace userSpace = userSpaceDao.createUserSpace(manager, saveSpace);

return new PostSpaceCreateResponse(saveSpace.getSpaceId());
return new PostSpaceCreateResponse(saveSpace.getSpaceId(), spaceImgUrl);
}
}
18 changes: 18 additions & 0 deletions src/main/java/space/space_spring/validator/ValidFile.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package space.space_spring.validator;

import jakarta.validation.Constraint;
import jakarta.validation.Payload;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = ValidFileValidator.class)
public @interface ValidFile {
String message() default "유효하지 않은 파일입니다.";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
14 changes: 14 additions & 0 deletions src/main/java/space/space_spring/validator/ValidFileValidator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package space.space_spring.validator;

import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;
import org.springframework.web.multipart.MultipartFile;

public class ValidFileValidator implements ConstraintValidator<ValidFile, MultipartFile> {

// multipartFile이 null 이 아니거나 비어있지 않으면 검증로직 통과
@Override
public boolean isValid(MultipartFile multipartFile, ConstraintValidatorContext constraintValidatorContext) {
return multipartFile != null && !multipartFile.isEmpty();
}
}

0 comments on commit b559fd5

Please sign in to comment.