Skip to content

Commit

Permalink
Merge branch 'Ringo-143'
Browse files Browse the repository at this point in the history
  • Loading branch information
KangJiSseok committed May 12, 2024
2 parents d3c2808 + 8c4c627 commit cd8c17f
Show file tree
Hide file tree
Showing 14 changed files with 103 additions and 41 deletions.
8 changes: 3 additions & 5 deletions .idea/modules/webChat.main.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 13 additions & 8 deletions BackEnd/src/main/java/springwebsocket/webchat/error/ErrorCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,19 @@ public enum ErrorCode {

// Member
EMAIL_ALREADY_TAKEN(400, "M001", "이미 존재하는 이메일입니다."),
USER_NAME_NOT_FOUND(400, "M004", "해당 이름의 사용자를 찾을수 없습니다."),
EMAIL_USER_NOT_FOUND(400, "M005", "해당 이메일의 사용자가 존재하지 않습니다."),
EMAIL_EMPTY(400, "M007", "이메일을 입력해주세요."),
PASSWORD_EMPTY(400, "M008", "비밀번호를 입력해주세요."),
INVALID_LOGIN_INFO(400, "M009", "이메일 또는 비밀번호를 다시 확인해 주세요."),
UNAUTHORIZED_USER(401, "M010", "로그인 후 이용가능합니다."),
USER_EMPTY(400, "M011", "존재하지 않는 사용자입니다."),
LOGIN_AGAIN(400, "M012", "로그인을 다시 시도해 주세요");
// USER_NAME_NOT_FOUND(400, "M004", "해당 이름의 사용자를 찾을수 없습니다."),
// EMAIL_USER_NOT_FOUND(400, "M005", "해당 이메일의 사용자가 존재하지 않습니다."),
// EMAIL_EMPTY(400, "M007", "이메일을 입력해주세요."),


//Friend
FRIEND_ALREADT_TAKEN(400, "M011", "이미 친구 요청을 보낸 상대입니다.");
// INVALID_LOGIN_INFO(400, "M009", "이메일 또는 비밀번호를 다시 확인해 주세요."),
// UNAUTHORIZED_USER(401, "M010", "로그인 후 이용가능합니다."),
// USER_EMPTY(400, "M011", "존재하지 않는 사용자입니다."),
// LOGIN_AGAIN(400, "M012", "로그인을 다시 시도해 주세요");
//



private final int httpStatus;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ protected InvalidValueException(String message, ErrorCode errorCode) {
super(message, errorCode);
}

public InvalidValueException(ErrorCode errorCode) {
protected InvalidValueException(ErrorCode errorCode) {
super(errorCode);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package springwebsocket.webchat.friend.controller;


import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import springwebsocket.webchat.friend.exception.FriendDuplicationException;
import springwebsocket.webchat.global.exception.ExceptionResponse;


@RestControllerAdvice
@Slf4j
public class FriendExceptionController {

@ExceptionHandler(FriendDuplicationException.class)
public ResponseEntity<ExceptionResponse> DataIntegrityViolationException(FriendDuplicationException e){
log.info("DataIntegrityViolationException");
ExceptionResponse exceptionResponse = new ExceptionResponse(e.getErrorCode());
return ResponseEntity.status(exceptionResponse.getStatus()).body(exceptionResponse);
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package springwebsocket.webchat.friend.controller;

import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
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.RestController;
import springwebsocket.webchat.friend.dto.request.UserIdRequest;
import springwebsocket.webchat.friend.dto.request.UserEmailRequest;
import springwebsocket.webchat.friend.dto.response.friendMessageResponse;
import springwebsocket.webchat.friend.entity.Friendship;
import springwebsocket.webchat.friend.repository.springdata.UserInfoMapping;
import springwebsocket.webchat.friend.service.FriendshipService;
Expand All @@ -21,18 +23,13 @@ public class FriendshipController {
private final FriendshipService friendshipService;

@PostMapping("/sendFriendRequest")
public String sendFriendRequest(@RequestBody UserEmailRequest userEmailRequest) {
public ResponseEntity<?> sendFriendRequest(@RequestBody UserEmailRequest userEmailRequest) {

String senderEmail = userEmailRequest.getSenderEmail();
String receiverEmail = userEmailRequest.getReceiverEmail();

Friendship friendship = friendshipService.sendFriendRequest(senderEmail, receiverEmail);
return friendshipService.sendFriendRequest(senderEmail, receiverEmail);

if (friendship == null) {
return "fail";
} else {
return "success";
}
}

@PostMapping("/acceptFriendRequestById")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package springwebsocket.webchat.friend.dto.response;

import lombok.Getter;

@Getter
public class friendMessageResponse {
String message;

public friendMessageResponse(String message) {
this.message = message;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package springwebsocket.webchat.friend.exception;

import springwebsocket.webchat.error.ErrorCode;
import springwebsocket.webchat.error.exception.InvalidValueException;

public class FriendDuplicationException extends InvalidValueException {
public FriendDuplicationException() {
super(ErrorCode.FRIEND_ALREADT_TAKEN);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package springwebsocket.webchat.friend.repository;

import org.springframework.http.ResponseEntity;
import springwebsocket.webchat.friend.dto.response.friendMessageResponse;
import springwebsocket.webchat.friend.entity.Friendship;
import springwebsocket.webchat.friend.repository.springdata.UserInfoMapping;
import springwebsocket.webchat.member.entity.Member;
Expand All @@ -9,7 +11,7 @@
public interface FriendshipRepository{

// 친구 요청 보내기
Friendship sendFriendRequest(String senderEmail, String receiverEmail);
ResponseEntity<?> sendFriendRequest(String senderEmail, String receiverEmail);

// 친구 요청 수락
String acceptFriendRequestById(String senderEmail, String receiverEmail);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,21 @@

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import springwebsocket.webchat.friend.dto.response.friendMessageResponse;
import springwebsocket.webchat.friend.entity.Friendship;
import springwebsocket.webchat.friend.exception.FriendDuplicationException;
import springwebsocket.webchat.friend.repository.springdata.UserInfoMapping;
import springwebsocket.webchat.member.entity.Member;
import springwebsocket.webchat.friend.repository.springdata.SpringDataJpaFriendshipRepository;
import springwebsocket.webchat.member.exception.EmailDuplicatedException;
import springwebsocket.webchat.member.repository.springdata.SpringDataJpaMemberRepository;

import java.sql.SQLException;
import java.sql.SQLIntegrityConstraintViolationException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
Expand All @@ -26,7 +33,7 @@ public class JpaFriendshipRepository implements FriendshipRepository {
private final SpringDataJpaMemberRepository memberRepository;

@Override
public Friendship sendFriendRequest(String senderEmail, String receiverEmail) {
public ResponseEntity<?> sendFriendRequest(String senderEmail, String receiverEmail) {

Optional<Member> senderMember = memberRepository.findByEmail(senderEmail);
Optional<Member> receiverMember = memberRepository.findByEmail(receiverEmail);
Expand All @@ -40,11 +47,19 @@ public Friendship sendFriendRequest(String senderEmail, String receiverEmail) {
friendship.setUserId(sender);
friendship.setFriendId(receiver);
friendship.setStatus(Friendship.FriendshipStatus.PENDING);
friendshipRepository.save(friendship);

return friendship;
try {
friendshipRepository.save(friendship);
} catch (DataIntegrityViolationException ex) {
// DataIntegrityViolationException으로 수정
throw new FriendDuplicationException();
}
friendMessageResponse message = new friendMessageResponse("success");
return ResponseEntity.ok().body(message);

} else {
return null;
friendMessageResponse message = new friendMessageResponse("not exist");
return ResponseEntity.ok().body(message);
}

}
Expand All @@ -62,7 +77,7 @@ public String acceptFriendRequestById(String senderEmail, String receiverEmail)
// 기존의 Friendship 엔터티를 찾는다.
Optional<Friendship> existingFriendship = friendshipRepository.findByFriendIdAndUserId(sender, receiver);

if(existingFriendship.isEmpty()) return "fail";
if (existingFriendship.isEmpty()) return "fail";

existingFriendship.ifPresent(friendship -> {
// Friendship 엔터티의 상태를 FRIENDS로 update.
Expand All @@ -83,9 +98,9 @@ public String rejectFriendRequestById(String senderEmail, String receiverEmail)
// 기존의 Friendship 엔터티를 찾는다.
Optional<Friendship> existingFriendship = friendshipRepository.findByFriendIdAndUserId(memberMe.get(), memberYou.get());

if(existingFriendship.isEmpty()) return "fail";
if (existingFriendship.isEmpty()) return "fail";

existingFriendship.ifPresent(friendship ->{
existingFriendship.ifPresent(friendship -> {
// Friendship 엔터티를 삭제
friendshipRepository.delete(existingFriendship.get());
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package springwebsocket.webchat.friend.service;

import org.springframework.http.ResponseEntity;
import springwebsocket.webchat.friend.entity.Friendship;
import springwebsocket.webchat.friend.repository.springdata.UserInfoMapping;
import springwebsocket.webchat.member.entity.Member;
Expand All @@ -9,7 +10,7 @@
public interface FriendshipService {

// 친구 요청 보내기
Friendship sendFriendRequest(String senderEmail, String receiverEmail);
ResponseEntity<?> sendFriendRequest(String senderEmail, String receiverEmail);

// 친구 요청 수락
String acceptFriendRequestById(String senderEmail, String receiverEmail);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package springwebsocket.webchat.friend.service;

import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import springwebsocket.webchat.friend.entity.Friendship;
import springwebsocket.webchat.friend.repository.springdata.UserInfoMapping;
Expand All @@ -16,7 +17,7 @@ public class FriendshipServiceV1 implements FriendshipService{
private final FriendshipRepository friendshipRepository;

@Override
public Friendship sendFriendRequest(String senderEmail, String receiverEmail) {
public ResponseEntity<?> sendFriendRequest(String senderEmail, String receiverEmail) {
return friendshipRepository.sendFriendRequest(senderEmail, receiverEmail);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package springwebsocket.webchat.member.exception.response;
package springwebsocket.webchat.global.exception;

import org.springframework.http.HttpStatus;
import springwebsocket.webchat.error.ErrorCode;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package springwebsocket.webchat.member.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import springwebsocket.webchat.member.exception.EmailDuplicatedException;
import springwebsocket.webchat.member.exception.response.ExceptionResponse;
import springwebsocket.webchat.global.exception.ExceptionResponse;

@RestControllerAdvice
@Slf4j
Expand Down
12 changes: 6 additions & 6 deletions BackEnd/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,16 @@ spring:
url: ${MYSQL_DATABASE_URL}
username: ${MYSQL_DATABASE_USERNAME}
password: ${MYSQL_DATABASE_PASSWORD}
# url: "jdbc:mysql://localhost:3306/JPA?characterEncoding=UTF-8&serverTimezone=UTC"
# username: "springDB"
# password:
# url: "jdbc:mysql://localhost:3306/JPA?characterEncoding=UTF-8&serverTimezone=UTC"
# username: "springDB"
# password:
jpa:
show-sql: true
hibernate:
ddl-auto: update
# properties:
# hibernate:
# format_sql: true
# properties:
# hibernate:
# format_sql: true
jwt:
secret: ${JWT_KEY}
naver :
Expand Down

0 comments on commit cd8c17f

Please sign in to comment.