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

Feat/#73/스페이스 별 유저 프로필 관련 api 개발 #80

Merged
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
97 changes: 95 additions & 2 deletions src/main/java/space/space_spring/controller/SpaceController.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,14 @@
import org.springframework.web.multipart.MultipartFile;
import space.space_spring.argument_resolver.jwtLogin.JwtLoginAuth;
import space.space_spring.dto.space.GetSpaceJoinDto;
import space.space_spring.dto.space.PostSpaceJoinDto;
import space.space_spring.dto.space.response.GetUserInfoBySpaceResponse;
import space.space_spring.dto.space.request.PostSpaceCreateRequest;

import space.space_spring.dto.userSpace.GetUserProfileInSpaceDto;
import space.space_spring.dto.userSpace.PutUserProfileInSpaceDto;
import space.space_spring.entity.UserSpace;
import space.space_spring.exception.CustomException;
import space.space_spring.exception.MultipartFileException;
import space.space_spring.exception.SpaceException;
import space.space_spring.response.BaseResponse;
Expand All @@ -19,9 +24,9 @@
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;
import static space.space_spring.response.status.BaseExceptionResponseStatus.IS_NOT_IMAGE_FILE;
import static space.space_spring.response.status.BaseExceptionResponseStatus.*;
import static space.space_spring.util.bindingResult.BindingResultUtils.getErrorMessage;

@RestController
Expand All @@ -33,6 +38,7 @@ public class SpaceController {
private final SpaceService spaceService;
private final S3Uploader s3Uploader;
private final String spaceImgDirName = "spaceImg";
private final String userProfileImgDirName = "userProfileImg";
private final UserSpaceUtils userSpaceUtils;

@PostMapping("")
Expand Down Expand Up @@ -93,4 +99,91 @@ private void validateIsUserAlreadySpaceMember(Long userId, Long spaceId) {
userSpaceUtils.isUserAlreadySpaceMember(userId, spaceId);
}

/**
* 스페이스 별 유저 프로필 정보 조회
* 본인의 프로필 조회하는 경우 -> requestParam으로 userId 받지 X
* 다른 멤버의 프로필 조회하는 경우 -> requestParam으로 userId 받음
*/
@GetMapping("/{spaceId}/member-profile")
public BaseResponse<GetUserProfileInSpaceDto.Response> getUserProfileInSpace(@JwtLoginAuth Long userId, @PathVariable Long spaceId, @RequestParam(name = "userId", required = false) Long targetUserId) {

log.info("targetUserId={}", targetUserId);

// TODO 1. 요청보내는 유저가 스페이스에 가입되어 있는지 검증
validateIsUserInSpace(userId, spaceId);

// TODO 2. 본인의 프로필을 조회하는지 다른 멤버의 프로필을 조회하는지 체크
if (targetUserId == null) {
targetUserId = userId;
}

log.info("targetUserId={}", targetUserId);

// TODO 3. 유저 프로필 정보 return
return new BaseResponse<>(spaceService.getUserProfileInSpace(targetUserId, spaceId));
}

private void validateIsUserInSpace(Long userId, Long spaceId) {
userSpaceUtils.isUserInSpace(userId, spaceId);
}

/**
* 스페이스 별 유저 프로필 정보 수정
*/
@PutMapping("/{spaceId}/member-profile")
public BaseResponse<PutUserProfileInSpaceDto.Response> updateUserProfileInSpace(@JwtLoginAuth Long userId, @PathVariable Long spaceId, @Validated @ModelAttribute PutUserProfileInSpaceDto.Request request, BindingResult bindingResult) throws IOException {
if (bindingResult.hasErrors()) {
throw new SpaceException(INVALID_USER_SPACE_PROFILE, getErrorMessage(bindingResult));
}

// TODO 1. 유저가 스페이스에 가입되어 있는지 검증
validateIsUserInSpace(userId, spaceId);

// TODO 2. 유저 프로필 썸네일을 s3에 upload
String userProfileImgUrl = processUserProfileImage(request.getUserProfileImg());

// TODO 3. 유저 프로필 정보 update
PutUserProfileInSpaceDto putUserProfileInSpaceDto = new PutUserProfileInSpaceDto(
request.getUserName(),
userProfileImgUrl,
request.getUserProfileMsg()
);

return new BaseResponse<>(spaceService.changeUserProfileInSpace(userId, spaceId, putUserProfileInSpaceDto));
}

private String processUserProfileImage(MultipartFile userProfileImg) throws IOException {
if (userProfileImg == null) {
return null;
}
validateImageFile(userProfileImg);
return s3Uploader.upload(userProfileImg, userProfileImgDirName);
}

/**
* 유저의 스페이스 가입 처리
*/
@PostMapping("/{spaceId}/join")
BaseResponse<String> joinUserToSpace(@JwtLoginAuth Long userId, @PathVariable Long spaceId, @Validated @ModelAttribute PostSpaceJoinDto.Request request, BindingResult bindingResult) throws IOException {
if (bindingResult.hasErrors()) {
throw new CustomException(INVALID_SPACE_JOIN_REQUEST, getErrorMessage(bindingResult));
}

// TODO 1. 유저가 스페이스에 가입되어 있는지 검증
validateIsUserAlreadySpaceMember(userId, spaceId);

// TODO 2. 유저 프로필 썸네일을 s3에 upload
String userProfileImgUrl = processUserProfileImage(request.getUserProfileImg());

// TODO 3. 유저의 스페이스 가입 처리
PostSpaceJoinDto postSpaceJoinDto = new PostSpaceJoinDto(
userProfileImgUrl,
request.getUserName(),
request.getUserProfileMsg()
);

spaceService.createUserSpace(userId, spaceId, postSpaceJoinDto);

return new BaseResponse<>("유저의 스페이스 가입 처리 성공");
}
}
5 changes: 3 additions & 2 deletions src/main/java/space/space_spring/dao/UserSpaceDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import space.space_spring.entity.Space;
import space.space_spring.entity.User;
import space.space_spring.entity.UserSpace;
import space.space_spring.entity.enumStatus.UserSpaceAuth;

import java.util.*;

Expand All @@ -21,9 +22,9 @@ public class UserSpaceDao {
@PersistenceContext
private EntityManager em;

public UserSpace createUserSpace(User manager, Space saveSpace) {
public UserSpace createUserSpace(User manager, Space saveSpace, UserSpaceAuth userSpaceAuth) {
UserSpace userSpace = new UserSpace();
userSpace.createUserSpace(manager, saveSpace, MANAGER);
userSpace.createUserSpace(manager, saveSpace, userSpaceAuth);

em.persist(userSpace);
return userSpace;
Expand Down
38 changes: 38 additions & 0 deletions src/main/java/space/space_spring/dto/space/PostSpaceJoinDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package space.space_spring.dto.space;

import jakarta.annotation.Nullable;
import jakarta.validation.constraints.NotBlank;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.hibernate.validator.constraints.Length;
import org.springframework.web.multipart.MultipartFile;

@Getter
@AllArgsConstructor
public class PostSpaceJoinDto {

private String userProfileImg;

private String userName;

private String userProfileMsg;

@Getter
@Setter
@NoArgsConstructor
public static class Request {

@Nullable
private MultipartFile userProfileImg;

@NotBlank(message = "유저 이름은 공백일 수 없습니다.")
@Length(min = 1, max = 10, message = "이름은 10자이내의 문자열이어야 합니다.")
private String userName;

@Nullable
@Length(max = 50, message = "프로필 상태메시지는 50자이내의 문자열이어야 합니다.")
private String userProfileMsg;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package space.space_spring.dto.userSpace;

import com.amazonaws.services.ec2.model.CpuOptionsRequest;
import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
public class GetUserProfileInSpaceDto {

@Getter
@AllArgsConstructor
public static class Response {

private String userProfileImg;

private String userName;

private String userAuth;

private String userProfileMsg;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package space.space_spring.dto.userSpace;

import jakarta.annotation.Nullable;
import jakarta.validation.constraints.NotBlank;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.hibernate.validator.constraints.Length;
import org.springframework.web.multipart.MultipartFile;

@Getter
@AllArgsConstructor
public class PutUserProfileInSpaceDto {

private String userName;

private String userProfileImg;

private String userProfileMsg;

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public static class Request {

@Nullable
private MultipartFile userProfileImg;

@NotBlank(message = "유저 이름은 공백일 수 없습니다.")
@Length(min = 1, max = 10, message = "이름은 10자이내의 문자열이어야 합니다.")
private String userName;

@Nullable
@Length(max = 50, message = "프로필 상태메시지는 50자이내의 문자열이어야 합니다.")
private String userProfileMsg;
}

@Getter
@AllArgsConstructor
public static class Response {

private String userProfileImg;

private String userAuth;

private String userName;

private String userProfileMsg;
}
}
15 changes: 15 additions & 0 deletions src/main/java/space/space_spring/entity/UserSpace.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,20 @@ public void createUserSpace(User user, Space space, UserSpaceAuth userSpaceAuth)
this.userSpaceAuth = userSpaceAuth.getAuth();
}

public void changeUserName(String userName) {
this.userName = userName;
}

public void changeUserProfileImg(String profileImg) {
this.userProfileImg = profileImg;
}

public void changeUserProfileMsg(String profileMsg) {
this.userProfileMsg = profileMsg;
}

// 유저 권한 변경 기능 아직 개발 X
public void changeUserSpaceAuth(String auth) {
this.userSpaceAuth = auth;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,23 +49,20 @@ public enum BaseExceptionResponseStatus implements ResponseStatus {
EMAIL_NOT_FOUND(4006, HttpStatus.BAD_REQUEST, "존재하지 않는 이메일입니다."),
INVALID_USER_LOGIN(4007, HttpStatus.BAD_REQUEST, "로그인 요청에서 잘못된 값이 존재합니다."),


/**
* 6000: Space 오류
*/
INVALID_SPACE_CREATE(6000, HttpStatus.BAD_REQUEST, "스페이스 생성 요청에서 잘못된 값이 존재합니다."),
SPACE_NOT_FOUND(6001, HttpStatus.BAD_REQUEST, "존재하지 않는 스페이스입니다."),
adff(6002, HttpStatus.BAD_REQUEST, "이미 존재하는 닉네임입니다."),
baab(6003, HttpStatus.BAD_REQUEST, "존재하지 않는 회원입니다."),
INVALID_USER_SPACE_PROFILE(6002, HttpStatus.BAD_REQUEST, "스페이스 별 유저 프로필 정보 수정 요청에서 잘못된 값이 존재합니다."),
INVALID_SPACE_JOIN_REQUEST(6003, HttpStatus.BAD_REQUEST, "스페이스 가입 요청에서 잘못된 값이 존재합니다."),
nff(6004, HttpStatus.BAD_REQUEST, "비밀번호가 일치하지 않습니다."),
gnf(6005, HttpStatus.BAD_REQUEST, "잘못된 회원 status 값입니다."),
fb(6006, HttpStatus.BAD_REQUEST, "존재하지 않는 이메일입니다."),


/**
* 7000: UserSpace 오류
*/

USER_IS_NOT_IN_SPACE(7000, HttpStatus.BAD_REQUEST, "해당 스페이스에 속하지 않는 유저입니다."),
UNAUTHORIZED_USER(7001, HttpStatus.UNAUTHORIZED, "해당 스페이스에 관리자 권한이 없는 유저입니다."),
USER_IS_ALREADY_IN_SPACE(7002, HttpStatus.BAD_REQUEST, "해당 스페이스에 이미 가입되어 있는 유저입니다"),
Expand All @@ -74,10 +71,6 @@ public enum BaseExceptionResponseStatus implements ResponseStatus {
F(7005, HttpStatus.BAD_REQUEST, "잘못된 회원 status 값입니다."),
G(7006, HttpStatus.BAD_REQUEST, "존재하지 않는 이메일입니다."),





/**
* 8000: Chat 오류
*/
Expand Down
Loading
Loading