Skip to content

Commit

Permalink
Merge pull request #152 from KUIT-Space/feat/#145/채팅방-테스트-코드-추가
Browse files Browse the repository at this point in the history
Feat/#145: 채팅방 테스트 코드 추가
  • Loading branch information
hyunn522 authored Oct 7, 2024
2 parents 38ea31d + ccbd06c commit 504d56d
Show file tree
Hide file tree
Showing 13 changed files with 825 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import space.space_spring.response.BaseResponse;
import space.space_spring.service.ChatRoomService;
import space.space_spring.service.S3Uploader;
import space.space_spring.util.userSpace.UserSpaceUtils;

import java.io.IOException;

Expand All @@ -30,7 +29,6 @@
public class ChatRoomController {
private final ChatRoomService chatRoomService;
private final S3Uploader s3Uploader;
private final UserSpaceUtils userSpaceUtils;

/**
* 모든 채팅방 정보 조회
Expand Down Expand Up @@ -71,7 +69,7 @@ public BaseResponse<ChatSuccessResponse> updateLastReadTime(
@JwtLoginAuth Long userId,
@PathVariable Long spaceId,
@PathVariable Long chatRoomId) {
return new BaseResponse<>(chatRoomService.updateLastReadTime(userId, spaceId, chatRoomId));
return new BaseResponse<>(chatRoomService.updateLastReadTime(userId, chatRoomId));
}

/**
Expand Down
4 changes: 0 additions & 4 deletions src/main/java/space/space_spring/dao/chat/ChatRoomDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@
import org.springframework.stereotype.Repository;
import space.space_spring.dao.chat.custom.ChatRoomDaoCustom;
import space.space_spring.entity.ChatRoom;
import space.space_spring.entity.Space;

import java.util.List;

@Repository
public interface ChatRoomDao extends JpaRepository<ChatRoom, Long>, ChatRoomDaoCustom {
List<ChatRoom> findBySpace(Space space);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Getter
@Setter
@NoArgsConstructor
@Builder
public class CreateChatRoomRequest {

@Size(min = 2, max = 15, message = "채팅방 이름은 2자 이상, 15자 이내의 문자열이어야 합니다.")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
package space.space_spring.dto.chat.request;

import jakarta.validation.constraints.NotEmpty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.util.List;

@Getter
@Builder
@NoArgsConstructor /* controller단 테스트 위해 적용*/
@AllArgsConstructor
public class JoinChatRoomRequest {

@NotEmpty(message = "1명 이상의 멤버를 초대해야 합니다.")
Expand Down
10 changes: 3 additions & 7 deletions src/main/java/space/space_spring/entity/ChatRoom.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.Comment;
import space.space_spring.dto.chat.request.CreateChatRoomRequest;

import java.time.LocalDateTime;
import java.time.ZoneId;

@Entity
@Getter
Expand All @@ -27,7 +23,7 @@ public class ChatRoom extends BaseEntity{
private Long id;

@Comment("채팅방이 속한 스페이스 ID")
@ManyToOne(fetch = FetchType.LAZY)
@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinColumn(name = "space_id")
private Space space;

Expand All @@ -40,10 +36,10 @@ public class ChatRoom extends BaseEntity{
@Column(name = "chat_room_img")
private String img;

public static ChatRoom of(Space space, CreateChatRoomRequest createChatRoomRequest, String chatRoomImgUrl) {
public static ChatRoom of(Space space, String chatRoomName, String chatRoomImgUrl) {
return ChatRoom.builder()
.space(space)
.name(createChatRoomRequest.getName())
.name(chatRoomName)
.img(chatRoomImgUrl)
.build();
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/space/space_spring/entity/UserChatRoom.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class UserChatRoom extends BaseEntity{
private ChatRoom chatRoom;

@Comment("사용자 ID")
@ManyToOne(fetch = FetchType.LAZY)
@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinColumn(name = "user_id")
private User user;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.TypeAlias;
import org.springframework.data.mongodb.core.mapping.Document;
import space.space_spring.dto.chat.request.ChatMessageRequest;
import space.space_spring.entity.enumStatus.ChatMessageType;

import java.time.LocalDateTime;
Expand Down Expand Up @@ -36,15 +35,15 @@ public class ChatMessage {

private LocalDateTime createdAt;

public static ChatMessage of(ChatMessageRequest chatMessageRequest, Long chatRoomId, Long senderId, String senderName, String senderImg) {
public static ChatMessage of(HashMap<String, String> content, Long chatRoomId, Long spaceId, Long senderId, String senderName, String senderImg, ChatMessageType messageType) {
return ChatMessage.builder()
.content(chatMessageRequest.getContent())
.content(content)
.chatRoomId(chatRoomId)
.spaceId(chatMessageRequest.getSpaceId())
.spaceId(spaceId)
.senderId(senderId)
.senderName(senderName)
.senderImg(senderImg)
.messageType(chatMessageRequest.getMessageType())
.messageType(messageType)
.createdAt(LocalDateTime.now(ZoneId.of("Asia/Seoul")))
.build();
}
Expand Down
10 changes: 7 additions & 3 deletions src/main/java/space/space_spring/service/ChatRoomService.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,14 @@ public CreateChatRoomResponse createChatRoom(Long userId, Long spaceId, CreateCh
Space spaceBySpaceId = spaceUtils.findSpaceBySpaceId(spaceId);

// TODO 3: chatRoom 생성 및 저장
ChatRoom chatRoom = chatRoomDao.save(ChatRoom.of(spaceBySpaceId, createChatRoomRequest, chatRoomImgUrl));
ChatRoom chatRoom = chatRoomDao.save(ChatRoom.of(spaceBySpaceId, createChatRoomRequest.getName(), chatRoomImgUrl));

// TODO 4: user_chatRoom 매핑 정보 저장
UserChatRoom userChatRoom = userChatRoomDao.save(UserChatRoom.of(chatRoom, userByUserId, LocalDateTime.now()));
userChatRoomDao.save(UserChatRoom.of(chatRoom, userByUserId, LocalDateTime.now()));
for (Long id : createChatRoomRequest.getMemberList()) {
User user = userUtils.findUserByUserId(id);
userChatRoomDao.save(UserChatRoom.of(chatRoom, user, LocalDateTime.now()));
}

// TODO 5: chatroom id 반환
return CreateChatRoomResponse.of(chatRoom.getId());
Expand Down Expand Up @@ -152,7 +156,7 @@ public ReadChatRoomMemberResponse readChatRoomMembers(Long spaceId, Long chatRoo
}

@Transactional
public ChatSuccessResponse updateLastReadTime(Long userId, Long spaceId, Long chatRoomId) {
public ChatSuccessResponse updateLastReadTime(Long userId, Long chatRoomId) {
User userByUserId = userUtils.findUserByUserId(userId);
ChatRoom chatRoomByChatRoomId = chatRoomDao.findById(chatRoomId)
.orElseThrow(() -> new CustomException(CHATROOM_NOT_EXIST));
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/space/space_spring/service/ChattingService.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,13 @@ public ChatMessageResponse sendChatMessage(Long senderId, ChatMessageRequest cha

// TODO 4: DB에 메시지 저장
ChatMessage message = chattingDao.insert(ChatMessage.of(
chatMessageRequest,
chatMessageRequest.getContent(),
chatRoomId,
chatMessageRequest.getSpaceId(),
senderId,
senderName,
senderProfileImg
senderProfileImg,
chatMessageRequest.getMessageType()
));

return ChatMessageResponse.of(message);
Expand Down
Loading

0 comments on commit 504d56d

Please sign in to comment.