Skip to content

Commit

Permalink
Merge pull request #56 from tukcomCD2024/RIngo-145
Browse files Browse the repository at this point in the history
R ingo 145
  • Loading branch information
KangJiSseok authored May 16, 2024
2 parents f578042 + 5b42ab0 commit f0bda12
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@

@Data
@Entity
@Table(name = "friendship",
uniqueConstraints = {
@UniqueConstraint(columnNames = {"user_id", "friend_id"})
})
public class Friendship {

public enum FriendshipStatus {
Expand All @@ -20,11 +24,11 @@ public enum FriendshipStatus {
private Long id;

@ManyToOne
@JoinColumn(name = "user_id", nullable = false)
@JoinColumn(name = "user_id", referencedColumnName = "id", nullable = false)
private Member userId;

@ManyToOne
@JoinColumn(name = "friend_id", nullable = false)
@JoinColumn(name = "friend_id", referencedColumnName = "id", nullable = false)
private Member friendId;

@Column(name = "status")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public enum ErrorCode {
EMAIL_ALREADY_TAKEN(400, "이미 존재하는 이메일입니다."),
LOGIN_FAIL_TAKEN(400,"로그인 실패"),
FIND_TARGETUSER_TAKEN(400,"상대방이 존재하지 않습니다."),
FIND_TAKEN(400,"이메일과 패스워드를 다시 한번 검토해보시오."),
FIND_EMAIL_TAKEN(400,"이메일이 존재하지 않습니다"),

//Friend
FRIEND_ALREADT_TAKEN(400,"이미 친구 요청을 보낸 상대입니다.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,14 @@
import org.springframework.web.bind.annotation.*;
import springwebsocket.webchat.global.response.ApiResponse;
import springwebsocket.webchat.member.dto.request.EmailRequest;
import springwebsocket.webchat.member.dto.request.LoginRequest;
import springwebsocket.webchat.member.dto.request.EmailPasswordRequest;
import springwebsocket.webchat.member.dto.request.SignUpRequest;
import springwebsocket.webchat.member.dto.response.TokenMessage;
import springwebsocket.webchat.member.dto.response.UserResponse;
import springwebsocket.webchat.member.entity.Member;
import springwebsocket.webchat.member.dto.MemberUpdataDto;
import springwebsocket.webchat.member.service.MemberService;

import java.util.Optional;

@Tag(name = "Member API")
@RestController
@Slf4j
Expand All @@ -35,13 +33,15 @@ public ApiResponse<?> register(final @RequestBody SignUpRequest request) {
}

@PostMapping("/update")
public void update(Long userId, MemberUpdataDto updateParam) {
userService.update(userId, updateParam);
public ApiResponse<?> update(@RequestBody MemberUpdataDto updateParam) {
userService.update(updateParam);
return ApiResponse.createSuccessWithNoContent();
}

@PostMapping("/find")
public Optional<Member> findById(Long id) {
return userService.findById(id);
public ApiResponse<Member> findById(@RequestBody EmailRequest email) {
Member member = userService.findById(email);
return ApiResponse.createSuccess(member);
}

@PostMapping("/findtarget")
Expand All @@ -52,13 +52,14 @@ public ApiResponse<UserResponse> findByTarget(@RequestBody EmailRequest email){
}

@PostMapping("/delete")
public void delete(Long id) {
userService.delete(id);
public ApiResponse<?> delete(@RequestBody EmailPasswordRequest request) {
userService.delete(request);
return ApiResponse.createSuccessWithNoContent();
}

@PostMapping("/login")
public ApiResponse<?> login(@RequestBody LoginRequest request) {
TokenMessage message = userService.login(request.getLoginEmail(), request.getPassword());
public ApiResponse<?> login(@RequestBody EmailPasswordRequest request) {
TokenMessage message = userService.login(request.getEmail(), request.getPassword());
return ApiResponse.createSuccess(message);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,24 @@


import lombok.Data;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Data
@Getter
@NoArgsConstructor
public class MemberUpdataDto {

private String email;
private String password;
private String newPassword;
private String name;
private String language;


public MemberUpdataDto() {
}

public MemberUpdataDto(String email, String password, String name) {
public MemberUpdataDto(String email, String password, String newPassword, String name, String language) {
this.email = email;
this.password = password;
this.newPassword = newPassword;
this.name = name;
this.language = language;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
@Getter
@NoArgsConstructor
@AllArgsConstructor
public class LoginRequest {
public class EmailPasswordRequest {

private String loginEmail;
private String email;
private String password;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package springwebsocket.webchat.member.exception;

import springwebsocket.webchat.global.error.BusinessException;
import springwebsocket.webchat.global.response.ErrorCode;

public class FindEmailException extends BusinessException {

public FindEmailException() {
super(ErrorCode.FIND_EMAIL_TAKEN);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package springwebsocket.webchat.member.exception;

import springwebsocket.webchat.global.error.BusinessException;
import springwebsocket.webchat.global.response.ErrorCode;

public class FindEmailPasswordException extends BusinessException {
public FindEmailPasswordException() {
super(ErrorCode.FIND_TAKEN);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,24 @@
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.http.ResponseEntity;
import springwebsocket.webchat.global.response.ApiResponse;
import springwebsocket.webchat.member.dto.MemberUpdataDto;
import springwebsocket.webchat.member.dto.request.EmailRequest;
import springwebsocket.webchat.member.dto.request.EmailPasswordRequest;
import springwebsocket.webchat.member.dto.request.SignUpRequest;
import springwebsocket.webchat.member.dto.response.TokenMessage;
import springwebsocket.webchat.member.dto.response.UserResponse;
import springwebsocket.webchat.member.entity.Member;

import java.util.Optional;

public interface MemberService {
UserResponse signUp(SignUpRequest signUpRequest);

void update(Long userId, MemberUpdataDto updateParam);
void update(MemberUpdataDto updateParam);

Optional<Member> findById(Long id);
Member findById(EmailRequest emailRequest);

UserResponse findByTarget(EmailRequest email);

void delete(Long id);
void delete(EmailPasswordRequest request);

TokenMessage login(String loginEmail, String password);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,14 @@
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import springwebsocket.webchat.global.jwt.TokenProvider;
import springwebsocket.webchat.global.response.ApiResponse;
import springwebsocket.webchat.member.dto.MemberUpdataDto;
import springwebsocket.webchat.member.dto.request.EmailRequest;
import springwebsocket.webchat.member.dto.request.EmailPasswordRequest;
import springwebsocket.webchat.member.dto.request.SignUpRequest;
import springwebsocket.webchat.member.dto.response.TokenMessage;
import springwebsocket.webchat.member.dto.response.UserResponse;
import springwebsocket.webchat.member.entity.Member;
import springwebsocket.webchat.member.exception.EmailDuplicatedException;
import springwebsocket.webchat.member.exception.FindTargetException;
import springwebsocket.webchat.member.exception.LoginFailException;
import springwebsocket.webchat.member.exception.*;
import springwebsocket.webchat.member.repository.MemberRepository;
import springwebsocket.webchat.member.repository.RefreshMemberRepository;
import springwebsocket.webchat.member.repository.springdata.SpringDataJpaMemberRepository;
Expand All @@ -41,7 +39,7 @@ public class MemberServiceImpl implements MemberService {
private final RefreshMemberRepository refreshMemberRepository;


public MemberServiceImpl(SpringDataJpaMemberRepository jpaMemberRepository,MemberRepository memberRepository, BCryptPasswordEncoder bCryptPasswordEncoder, TokenProvider tokenProvider, RefreshMemberRepository refreshMemberRepository) {
public MemberServiceImpl(SpringDataJpaMemberRepository jpaMemberRepository, MemberRepository memberRepository, BCryptPasswordEncoder bCryptPasswordEncoder, TokenProvider tokenProvider, RefreshMemberRepository refreshMemberRepository) {
this.jpaMemberRepository = jpaMemberRepository;
this.memberRepository = memberRepository;
this.bCryptPasswordEncoder = bCryptPasswordEncoder;
Expand Down Expand Up @@ -69,13 +67,43 @@ public UserResponse signUp(SignUpRequest signUpRequest) {
}

@Override
public void update(Long userId, MemberUpdataDto updateParam) {
memberRepository.update(userId, updateParam);
public void update(MemberUpdataDto updateParam) {
String email = updateParam.getEmail();
String password = updateParam.getPassword();
String newPassword = updateParam.getNewPassword();
String language = updateParam.getLanguage();
String name = updateParam.getName();
Optional<Member> user = jpaMemberRepository.findByEmail(email)
.filter(m -> bCryptPasswordEncoder.matches(password, m.getPassword()));

if(user.isEmpty()) throw new FindEmailPasswordException();

Member member = user.get();

if (language != null) {
member.setLanguage(language);
}
if (name != null) {
member.setName(name);
}
if (newPassword != null) {
member.setPassword(bCryptPasswordEncoder.encode(newPassword));
}

jpaMemberRepository.save(member);
}


@Override
public Optional<Member> findById(Long id) {
return memberRepository.findById(id);
public Member findById(EmailRequest emailRequest) {
String email = emailRequest.getEmail();
Optional<Member> findMember = jpaMemberRepository.findByEmail(email);

if(findMember.isEmpty()) throw new FindEmailException();
Member member = findMember.get();
member.setPassword(null);
member.setFirebaseToken(null);
return member;
}

@Override
Expand All @@ -85,13 +113,19 @@ public UserResponse findByTarget(EmailRequest email) {
throw new FindTargetException();
}

UserResponse userResponse = new UserResponse(findEmail.get().getName(),findEmail.get().getEmail());
UserResponse userResponse = new UserResponse(findEmail.get().getName(), findEmail.get().getEmail());
return userResponse;
}

@Override
public void delete(Long id) {
memberRepository.delete(id);
public void delete(EmailPasswordRequest request) {
String email = request.getEmail();
String password = request.getPassword();
Optional<Member> user = memberRepository.findByLoginEmail(email)
.filter(m -> bCryptPasswordEncoder.matches(password, m.getPassword()));
if(user.isEmpty()) throw new FindEmailPasswordException();

jpaMemberRepository.delete(user.get());
}

public TokenMessage login(String loginEmail, String password) {
Expand Down

0 comments on commit f0bda12

Please sign in to comment.