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

회원가입 시 프로필 사진 선택 기능 구현 완료 #207

Merged
merged 2 commits into from
Mar 16, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import balancetalk.global.exception.ErrorCode;
import balancetalk.global.jwt.JwtTokenProvider;
import balancetalk.global.redis.application.RedisService;
import balancetalk.module.file.domain.File;
import balancetalk.module.file.domain.FileRepository;
import balancetalk.module.member.domain.Member;
import balancetalk.module.member.domain.MemberRepository;
import balancetalk.module.member.dto.*;
Expand All @@ -22,6 +24,8 @@
import java.util.List;
import java.util.stream.Collectors;

import static balancetalk.global.exception.ErrorCode.NOT_FOUND_FILE;

@Slf4j
@Service
@RequiredArgsConstructor
Expand All @@ -30,22 +34,24 @@ public class MemberService {
private final JwtTokenProvider jwtTokenProvider;
private final AuthenticationManager authenticationManager;
private final MemberRepository memberRepository;
private final FileRepository fileRepository;
private final PasswordEncoder passwordEncoder;
private final RedisService redisService;

@Transactional
public Long join(final JoinRequest joinRequest) {
joinRequest.setPassword(passwordEncoder.encode(joinRequest.getPassword()));
if (memberRepository.existsByNickname(joinRequest.getNickname())) {
throw new BalanceTalkException(ErrorCode.ALREADY_REGISTERED_NICKNAME);
}
if (memberRepository.existsByEmail(joinRequest.getEmail())) {
throw new BalanceTalkException(ErrorCode.ALREADY_REGISTERED_EMAIL);
File profilePhoto = null;
if (joinRequest.getProfilePhoto() != null && !joinRequest.getProfilePhoto().isEmpty()) {
profilePhoto = fileRepository.findByStoredName(joinRequest.getProfilePhoto())
.orElseThrow(() -> new BalanceTalkException(NOT_FOUND_FILE));
}
Member member = joinRequest.toEntity();

Member member = joinRequest.toEntity(profilePhoto);
return memberRepository.save(member).getId();
}


@Transactional
public TokenDto login(final LoginRequest loginRequest) {
Member member = memberRepository.findByEmail(loginRequest.getEmail())
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/balancetalk/module/member/domain/Member.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import balancetalk.module.bookmark.domain.Bookmark;
import balancetalk.module.comment.domain.Comment;
import balancetalk.module.comment.domain.CommentLike;
import balancetalk.module.file.domain.File;
import balancetalk.module.notice.domain.Notice;
import balancetalk.module.post.domain.Post;
import balancetalk.module.post.domain.PostLike;
Expand Down Expand Up @@ -77,6 +78,10 @@ public class Member extends BaseTimeEntity implements UserDetails {
@OneToMany(mappedBy = "reporter")
private List<Report> reports = new ArrayList<>(); // 신고한 기록

@OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinColumn(name = "file_id")
private File profilePhoto;

@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
return null;
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/balancetalk/module/member/dto/JoinRequest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package balancetalk.module.member.dto;

import balancetalk.module.file.domain.File;
import balancetalk.module.member.domain.Member;
import balancetalk.module.member.domain.Role;
import io.swagger.v3.oas.annotations.media.Schema;
Expand Down Expand Up @@ -29,14 +30,16 @@ public class JoinRequest {
@Schema(description = "회원 비밀번호", example = "Test1234test!")
private String password;

// TODO: profilePhoto 추가
@Schema(description = "회원 프로필 사진", example = "4df23447-2355-45h2-8783-7f6gd2ceb848_프로필사진.jpg")
private String profilePhoto;

public Member toEntity() {
public Member toEntity(File profilePhoto) {
return Member.builder()
.nickname(nickname)
.email(email)
.password(password)
.role(Role.USER)
.profilePhoto(profilePhoto)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package balancetalk.module.member.dto;

import balancetalk.module.file.domain.File;
import balancetalk.module.member.domain.Member;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.time.LocalDateTime;
import java.util.Optional;

@Data
@Builder
Expand Down Expand Up @@ -36,10 +38,14 @@ public class MemberResponse {
private int level;

public static MemberResponse fromEntity(Member member) {
String profilePhotoName = Optional.ofNullable(member.getProfilePhoto())
.map(File::getStoredName)
.orElse(null);

return MemberResponse.builder()
.id(member.getId())
.nickname(member.getNickname())
//.profilePhoto(file.getPath())
.profilePhoto(profilePhotoName)
.createdAt(member.getCreatedAt())
.postsCount(member.getPostCount())
.totalPostLike(member.getPostLikes())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ void changeNicknameSuccess() {

// when
joinRequest.setNickname(newNickname);
member = joinRequest.toEntity();
member = joinRequest.toEntity(null);
memberService.updateNickname(newNickname, request);

// then
Expand Down Expand Up @@ -236,7 +236,7 @@ void changePasswordSuccess() {

// when
joinRequest.setPassword(newPassword);
member = joinRequest.toEntity();
member = joinRequest.toEntity(null);
memberService.updatePassword(newPassword, request);

// then
Expand Down