Skip to content

Commit

Permalink
refactor/#54 (#60)
Browse files Browse the repository at this point in the history
* ♻️ 크리에이터 등록 수정

* ♻️ 크리에이터 등록 수정 (기본 이미지 포함)
  • Loading branch information
Jindongleee authored Nov 6, 2024
1 parent bf2cf19 commit 92f41e9
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
public interface BucketService {

// 프로필 이미지 업로드
UploadProfileImgRes uploadProfileImg(MultipartFile file, Long userId) throws Exception;
void uploadProfileImg(MultipartFile file, Long userId) throws Exception;

// 파일 경로 받아오기
Long getPublicImgUrl(Long userId) throws Exception;

// 파일 다운로드
MultipartFile downloadFile(Long userId, String type) throws Exception;

String defaultProfileImgUrl();
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ private UploadManager getManager(ObjectStorage client) {
}

@Override
public UploadProfileImgRes uploadProfileImg(MultipartFile file, Long userId) throws Exception {
public void uploadProfileImg(MultipartFile file, Long userId) throws Exception {

User user = userRepository.getById(userId);

Expand Down Expand Up @@ -85,7 +85,6 @@ public UploadProfileImgRes uploadProfileImg(MultipartFile file, Long userId) thr

log.info("이미지 업로드 완료: {}", profileImgUrl);
client.close();
return UploadProfileImgRes.from(profileImgUrl);
}

@Override
Expand All @@ -105,4 +104,9 @@ private File convertMultiPartToFile(MultipartFile file) throws Exception {
fos.close();
return convFile;
}

@Override
public String defaultProfileImgUrl() {
return DEFAULT_URI_PREFIX + BUCKET_NAME_SPACE + "/b/" + BUCKET_NAME + "/o/" + "default_profile.png";
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.example.gather_back_end.creator.controller;

import lombok.RequiredArgsConstructor;
import org.example.gather_back_end.bucket.service.BucketService;
import org.example.gather_back_end.creator.dto.CreateCreatorReq;
import org.example.gather_back_end.creator.dto.GetCreatorRes;
import org.example.gather_back_end.creator.service.CreatorService;
Expand All @@ -16,7 +17,9 @@
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequiredArgsConstructor
Expand All @@ -27,9 +30,10 @@ public class CreatorController {
private final PortfolioService portfolioService;
private final UserRepository userRepository;
private final WorkService workService;
private final BucketService bucketService;

@PostMapping()
public SuccessResponse<?> createCreator(Authentication authentication, @RequestBody CreateCreatorReq req) {
public SuccessResponse<?> createCreator(Authentication authentication, @RequestBody CreateCreatorReq req) throws Exception {

CustomOAuth2User customOAuth2User = (CustomOAuth2User) authentication.getPrincipal();

Expand All @@ -41,6 +45,10 @@ public SuccessResponse<?> createCreator(Authentication authentication, @RequestB
portfolioService.createPortfolio(user.getId(), req.createPortfolioReqList());
workService.createWork(user.getId(), req.createWorkReqList());

if (req.profileImgUrl() != null) {
bucketService.uploadProfileImg(req.profileImgUrl(), user.getId());
}

creatorService.createCreator(
user.getId(),
req.nickname(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
import org.example.gather_back_end.domain.User;
import org.example.gather_back_end.portfolio.dto.CreatePortfolioReq;
import org.example.gather_back_end.work.dto.CreateWorkReq;
import org.springframework.web.multipart.MultipartFile;

import java.util.*;

@Builder
public record CreateCreatorReq(
MultipartFile profileImgUrl,
String nickname,
String introductionTitle,
String introductionContent,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public void createCreator(
){

User user = userRepository.getById(userId);
user.createCreatorInfo(user,nickname, introductionTitle, introductionContent, contactKakaoId, contactEmail);
user.createCreatorInfo(user, nickname, introductionTitle, introductionContent, contactKakaoId, contactEmail);
userRepository.save(user);
};

Expand Down
10 changes: 6 additions & 4 deletions src/main/java/org/example/gather_back_end/domain/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,10 @@ public class User extends BaseEntity {
@OneToMany(mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Work> workList = new ArrayList<>();

public static User createUserInfo(String username, String name, String email, String role, String nickname) {
// 유저 생성
public static User createUserInfo(String profileImgUrl, String username, String name, String email, String role, String nickname) {
return User.builder()
.profileImgUrl(profileImgUrl)
.username(username)
.name(name)
.email(email)
Expand All @@ -101,9 +103,6 @@ public void updateUserInfo(String name, String email) {
this.email = email;
}

public static void updateProfileImgUrl(User user, String profileImgUrl) {
user.profileImgUrl = profileImgUrl;
}

// 대학생 인증 시 유저 정보 업데이트
public static void updateStudentAuthInfo(User user) {
Expand Down Expand Up @@ -135,4 +134,7 @@ public void createCreatorInfo(
user.contactEmail = contactEmail;
}

public static void updateProfileImgUrl(User user, String profileImgUrl) {
user.profileImgUrl = profileImgUrl;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,4 @@
@RequestMapping("/api/users")
public class UserController implements UserControllerApi {

private final BucketService bucketService;

@PostMapping("/{userId}/profileImg")
public SuccessResponse<UploadProfileImgRes> uploadProfileImg(
@PathVariable Long userId, // TODO: 나중에 Authentication 으로 바꾸기
@RequestParam("file") MultipartFile file) throws Exception {
UploadProfileImgRes res = bucketService.uploadProfileImg(file, userId);
return SuccessResponse.of(res);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,17 @@
@Tag(name = "User 관련", description = "유저와 관련된 API")
public interface UserControllerApi {

@Operation(summary = "프로필 사진 업로드", description = "유저의 프로필 사진을 업로드하는 API")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "성공",
content = @Content(mediaType = "application/json",
examples = @ExampleObject(value = "{\n"
+ " \"timestamp\": \"2024-10-22T21:35:03.755865\",\n"
+ " \"isSuccess\": true,\n"
+ " \"code\": \"200\",\n"
+ " \"message\": \"호출에 성공하였습니다.\",\n"
+ " \"data\": null\n"
+ "}"),
schema = @Schema(implementation = SuccessResponse.class)))
})
@PostMapping("/{userId}/profileImg")
SuccessResponse<UploadProfileImgRes> uploadProfileImg(@PathVariable Long userId, @RequestParam("file") MultipartFile file) throws Exception;
// @Operation(summary = "프로필 사진 업로드", description = "유저의 프로필 사진을 업로드하는 API")
// @ApiResponses(value = {
// @ApiResponse(responseCode = "200", description = "성공",
// content = @Content(mediaType = "application/json",
// examples = @ExampleObject(value = "{\n"
// + " \"timestamp\": \"2024-10-22T21:35:03.755865\",\n"
// + " \"isSuccess\": true,\n"
// + " \"code\": \"200\",\n"
// + " \"message\": \"호출에 성공하였습니다.\",\n"
// + " \"data\": null\n"
// + "}"),
// schema = @Schema(implementation = SuccessResponse.class)))
// })
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import javax.crypto.SecretKey;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.example.gather_back_end.bucket.service.BucketService;
import org.example.gather_back_end.domain.User;
import org.example.gather_back_end.repository.UserRepository;
import org.example.gather_back_end.util.jwt.dto.CustomOAuth2User;
Expand All @@ -25,6 +26,7 @@ public class CustomOAuth2UserServiceImpl extends DefaultOAuth2UserService implem

private final UserRepository userRepository;
private final LocalDateTimeNumericEncryption localDateTimeNumericEncryption;
private final BucketService bucketService;

@Override
public OAuth2User loadUser(OAuth2UserRequest userRequest) throws OAuth2AuthenticationException {
Expand Down Expand Up @@ -65,9 +67,11 @@ public OAuth2User loadUser(OAuth2UserRequest userRequest) throws OAuth2Authentic

User existData = userRepository.findByUsernameCustom(username);

// Default로 기본 이미지 넣기 (default_profile.png <- OCI)
if (existData == null) {

userRepository.save(User.createUserInfo(
bucketService.defaultProfileImgUrl(),
username,
oAuth2Response.getName(),
oAuth2Response.getEmail(),
Expand All @@ -85,10 +89,13 @@ public OAuth2User loadUser(OAuth2UserRequest userRequest) throws OAuth2Authentic

} else {

// 유저 정보 업데이트
existData.updateUserInfo(oAuth2Response.getName(), oAuth2Response.getEmail());

// 업데이트 된 유저 정보
userRepository.save(existData);

// 수정된 이름과 이메일을 다시 토큰에 넣기
UserDto userDto = UserDto.builder()
.username(existData.getUsername())
.name(existData.getName())
Expand Down

0 comments on commit 92f41e9

Please sign in to comment.