Skip to content
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

Refactor/#44/스페이스 생성 api 에서 실제 이미지 데이터 저장 #47

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 26 additions & 4 deletions src/main/java/space/space_spring/controller/PayController.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.springframework.web.bind.annotation.RestController;
import space.space_spring.argument_resolver.jwtLogin.JwtLoginAuth;
import space.space_spring.dto.pay.GetPayViewResponse;
import space.space_spring.dto.pay.GetRequestPayViewResponse;
import space.space_spring.dto.pay.PayReceiveInfoDto;
import space.space_spring.dto.pay.PayRequestInfoDto;
import space.space_spring.entity.UserSpace;
Expand All @@ -26,16 +27,21 @@ public class PayController {
private final PayService payService;
private final UserSpaceUtils userSpaceUtils;

/**
* 정산 홈 view
*/
@GetMapping("/space/{spaceId}/pay")
public BaseResponse<GetPayViewResponse> showPayListForUser(@JwtLoginAuth Long userId, @PathVariable Long spaceId) {
// TODO 1. 유저가 스페이스에 속하는 지 검증
validateIsUserInSpace(userId, spaceId);

// TODO 2. 유저가 요청한 정산 리스트 get
List<PayRequestInfoDto> payRequestInfoDtoList = payService.getPayRequestInfoForUser(userId, spaceId);
// TODO 2. 유저가 요청한 정산 중 현재 진행중인 정산 리스트 get
// 현재 진행중인 정산 -> isComplete = false
List<PayRequestInfoDto> payRequestInfoDtoList = payService.getPayRequestInfoForUser(userId, spaceId, false);

// TODO 3. 유저가 요청받은 정산 리스트 get
List<PayReceiveInfoDto> payReceiveInfoDtoList = payService.getPayReceiveInfoForUser(userId, spaceId);
// TODO 3. 유저가 요청받은 정산 중 현재 진행중인 정산 리스트 get
// 현재 진행중인 정산 -> isComplete = false
List<PayReceiveInfoDto> payReceiveInfoDtoList = payService.getPayReceiveInfoForUser(userId, spaceId, false);

return new BaseResponse<>(new GetPayViewResponse(payRequestInfoDtoList, payReceiveInfoDtoList));
}
Expand All @@ -46,4 +52,20 @@ private void validateIsUserInSpace(Long userId, Long spaceId) {
userSpaceUtils.isUserInSpace(userId, spaceId);
}

/**
* 내가 요청한 정산 view
*/
@GetMapping("/space/{spaceId}/pay/request")
public BaseResponse<GetRequestPayViewResponse> showRequestPayListForUser(@JwtLoginAuth Long userId, @PathVariable Long spaceId) {
// TODO 1. 유저가 스페이스에 속하는 지 검증 -> 추후에 인터셉터에서 처리하게끔 리펙토링 필요
validateIsUserInSpace(userId, spaceId);

// TODO 2. 유저가 요청한 정산 중 현재 진행중인 정산 리스트 get -> 아직 완료되지 않은 정산 : isComplete = false
List<PayRequestInfoDto> payRequestInfoDtoListInComplete = payService.getPayRequestInfoForUser(userId, spaceId, false);

// TODO 3. 유저가 요청한 정산 중 완료한 정산 리스트 get -> 완료된 정산 : isComplete = true
List<PayRequestInfoDto> payRequestInfoDtoListComplete = payService.getPayRequestInfoForUser(userId, spaceId, true);

return new BaseResponse<>(new GetRequestPayViewResponse(payRequestInfoDtoListInComplete, payRequestInfoDtoListComplete));
}
}
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
12 changes: 6 additions & 6 deletions src/main/java/space/space_spring/dao/PayDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ public class PayDao {
@PersistenceContext
private EntityManager em;

public List<PayRequest> findPayRequestListByUser(User user, Space space) {
public List<PayRequest> findPayRequestListByUser(User user, Space space, boolean isComplete) {
// 유저가 해당 스페이스에서 요청한 정산 목록만을 select
// 아직 정산이 완료되지 않은 payRequest 엔티티만 select
String jpql = "SELECT pr FROM PayRequest pr WHERE pr.payCreateUser = :user AND pr.space = :space AND pr.isComplete = false";
String jpql = "SELECT pr FROM PayRequest pr WHERE pr.payCreateUser = :user AND pr.space = :space AND pr.isComplete = :isComplete";

return em.createQuery(jpql, PayRequest.class)
.setParameter("user", user)
.setParameter("space", space)
.setParameter("isComplete", isComplete)
.getResultList();
}

Expand All @@ -39,16 +39,16 @@ public List<PayRequestTarget> findPayRequestTargetListByPayRequest(PayRequest pa
.getResultList();
}

public List<PayRequestTarget> findPayRequestTargetListByUser(User user, Space space) {
public List<PayRequestTarget> findPayRequestTargetListByUser(User user, Space space, boolean isComplete) {
// 유저가 해당 스페이스에서 요청받은 정산 목록만을 select
// 유저가 요청받은 정산 중 아직 완료되지 않은 payRequestTarget 엔티티만 select
String jpql = "SELECT prt FROM PayRequestTarget prt " +
"JOIN prt.payRequest pr " +
"WHERE prt.targetUserId = :userId AND pr.space = :space AND pr.isComplete = false";
"WHERE prt.targetUserId = :userId AND pr.space = :space AND pr.isComplete = :isComplete";

return em.createQuery(jpql, PayRequestTarget.class)
.setParameter("userId", user.getUserId())
.setParameter("space", space)
.setParameter("isComplete", isComplete)
.getResultList();
}

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
@@ -0,0 +1,16 @@
package space.space_spring.dto.pay;

import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.ArrayList;
import java.util.List;

@Getter
@AllArgsConstructor
public class GetRequestPayViewResponse {

private List<PayRequestInfoDto> payRequestInfoDtoListInComplete = new ArrayList<>();

private List<PayRequestInfoDto> payRequestInfoDtoListComplete = new ArrayList<>();
}
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이 잘 생성됐는지 확인하는 용도
}
12 changes: 6 additions & 6 deletions src/main/java/space/space_spring/service/PayService.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ public class PayService {
private final UserUtils userUtils;
private final SpaceUtils spaceUtils;

public List<PayRequestInfoDto> getPayRequestInfoForUser(Long userId, Long spaceId) {
public List<PayRequestInfoDto> getPayRequestInfoForUser(Long userId, Long spaceId, boolean isComplete) {
// TODO 1. userId에 해당하는 user find
User userByUserId = userUtils.findUserByUserId(userId);

// TODO 2. spaceId에 해당하는 space find
Space spaceBySpaceId = spaceUtils.findSpaceBySpaceId(spaceId);

// TODO 3. 유저가 요청한 정산 중 진행 중인 정산 리스트 select
List<PayRequest> payRequestListByUser = payDao.findPayRequestListByUser(userByUserId, spaceBySpaceId);
// TODO 3. 유저가 요청한 정산 리스트 select
List<PayRequest> payRequestListByUser = payDao.findPayRequestListByUser(userByUserId, spaceBySpaceId, isComplete);

// TODO 4. return 타입 구성
// 3-1. 각 payRequest 에 해당하는 모든 payRequestTarget 을 loop로 돌면서 데이터 수집
Expand Down Expand Up @@ -62,15 +62,15 @@ public List<PayRequestInfoDto> getPayRequestInfoForUser(Long userId, Long spaceI
return payRequestInfoDtoList;
}

public List<PayReceiveInfoDto> getPayReceiveInfoForUser(Long userId, Long spaceId) {
public List<PayReceiveInfoDto> getPayReceiveInfoForUser(Long userId, Long spaceId, boolean isComplete) {
// TODO 1. userId에 해당하는 유저 find
User userByUserId = userUtils.findUserByUserId(userId);

// TODO 2. spaceId에 해당하는 space find
Space spaceBySpaceId = spaceUtils.findSpaceBySpaceId(spaceId);

// TODO 3. 유저가 요청받은 정산 중 진행 중인 정산 리스트 select
List<PayRequestTarget> payRequestTargetListByUser = payDao.findPayRequestTargetListByUser(userByUserId, spaceBySpaceId);
// TODO 3. 유저가 요청받은 정산 리스트 select
List<PayRequestTarget> payRequestTargetListByUser = payDao.findPayRequestTargetListByUser(userByUserId, spaceBySpaceId, isComplete);

// TODO 4. return 타입 구성
// 3-1. 각 payRequestTarget 에 해당하는 정산 요청자, 정산 요청 금액 을 loop를 돌면서 데이터 수집
Expand Down
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();
}
}
Loading
Loading