Skip to content

Commit

Permalink
Feat: 채팅방 개별 조회 API (#122)
Browse files Browse the repository at this point in the history
* Feat: 채팅방 개별 조회 API

* Feat: chatRoom-service cors 설정

* Feat: category -> hashTag로 네이밍 변경
  • Loading branch information
leebuwon authored Feb 15, 2024
1 parent e1d2f48 commit 42de980
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.tadak.chatroomservice.domain.chatmember.dto.response;

import com.tadak.chatroomservice.domain.chatmember.entity.ChatMember;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class ChatMemberResponse {

private String username;

public static ChatMemberResponse from(ChatMember chatMember){
return ChatMemberResponse.builder()
.username(chatMember.getUsername())
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@

import com.tadak.chatroomservice.domain.chatmember.dto.response.EnterChatMemberResponse;
import com.tadak.chatroomservice.domain.chatroom.dto.request.ChatRoomRequest;
import com.tadak.chatroomservice.domain.chatroom.dto.response.ChangeOwnerResponse;
import com.tadak.chatroomservice.domain.chatroom.dto.response.ChatRoomResponse;
import com.tadak.chatroomservice.domain.chatroom.dto.response.KickMemberResponse;
import com.tadak.chatroomservice.domain.chatroom.dto.response.*;
import com.tadak.chatroomservice.domain.chatroom.service.ChatRoomService;
import com.tadak.chatroomservice.domain.chatroom.dto.request.CreateChatroomRequest;
import com.tadak.chatroomservice.domain.chatroom.dto.response.CreateChatroomResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
Expand Down Expand Up @@ -80,4 +77,13 @@ public ResponseEntity<ChangeOwnerResponse> changeOwner(@PathVariable Long roomId
ChangeOwnerResponse changeOwnerResponse = chatRoomService.changeOwner(roomId, username, chatRoomRequest.getUsername());
return ResponseEntity.status(HttpStatus.OK).body(changeOwnerResponse);
}

/**
* 개별 방 조회
*/
@GetMapping("/rooms/{roomId}")
public ResponseEntity<OneChatRoomResponse> getChatRoom(@PathVariable Long roomId){
OneChatRoomResponse getChatRoom = chatRoomService.findChatRoom(roomId);
return ResponseEntity.status(HttpStatus.OK).body(getChatRoom);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ public class CreateChatroomRequest {
private String roomName;
private String description;
private String owner;
private String category;
private String hashtag;
private Integer capacity;
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class CreateChatroomResponse {
private String roomName;
private String description;
private String owner;
private String category;
private String hashtag;
private Integer participation;
private Integer capacity;
private EnterChatMemberResponse chatMemberResponse;
Expand All @@ -27,7 +27,7 @@ public static CreateChatroomResponse of(ChatRoom chatRoom, EnterChatMemberRespon
.roomName(chatRoom.getRoomName())
.description(chatRoom.getDescription())
.owner(chatRoom.getOwner())
.category(chatRoom.getCategory())
.hashtag(chatRoom.getHashtag())
.participation(chatRoom.getParticipation())
.capacity(chatRoom.getCapacity())
.chatMemberResponse(chatMemberResponse)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.tadak.chatroomservice.domain.chatroom.dto.response;

import com.tadak.chatroomservice.domain.chatmember.dto.response.ChatMemberResponse;
import com.tadak.chatroomservice.domain.chatroom.entity.ChatRoom;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.util.List;

@Getter
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class OneChatRoomResponse {

private String roomName;
private Integer participation;
private String owner;
private List<ChatMemberResponse> chatMemberResponses;

public static OneChatRoomResponse of(ChatRoom chatRoom, List<ChatMemberResponse> chatMemberResponses){
return OneChatRoomResponse.builder()
.roomName(chatRoom.getRoomName())
.participation(chatRoom.getParticipation())
.owner(chatRoom.getOwner())
.chatMemberResponses(chatMemberResponses)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public class ChatRoom {
private String owner;
@NotNull
@Size(max = 10, message = "카테고리 글자는 10자를 넘길 수 없습니다.")
private String category;
private String hashtag;
@NotNull
private Integer participation;
@NotNull
Expand All @@ -58,7 +58,7 @@ public static ChatRoom toEntity(CreateChatroomRequest chatroomRequest){
.roomName(chatroomRequest.getRoomName())
.description(chatroomRequest.getDescription())
.owner(chatroomRequest.getOwner())
.category(chatroomRequest.getCategory())
.hashtag(chatroomRequest.getHashtag())
.participation(DEFAULT_PARTICIPATION)
.capacity(chatroomRequest.getCapacity())
.build();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
package com.tadak.chatroomservice.domain.chatroom.service;

import com.tadak.chatroomservice.domain.chatmember.dto.response.ChatMemberResponse;
import com.tadak.chatroomservice.domain.chatmember.dto.response.EnterChatMemberResponse;
import com.tadak.chatroomservice.domain.chatmember.entity.ChatMember;
import com.tadak.chatroomservice.domain.chatmember.service.ChatMemberService;
import com.tadak.chatroomservice.domain.chatroom.dto.request.ChatRoomRequest;
import com.tadak.chatroomservice.domain.chatroom.dto.response.ChangeOwnerResponse;
import com.tadak.chatroomservice.domain.chatroom.dto.response.ChatRoomResponse;
import com.tadak.chatroomservice.domain.chatroom.dto.response.KickMemberResponse;
import com.tadak.chatroomservice.domain.chatroom.dto.response.*;
import com.tadak.chatroomservice.domain.chatroom.exception.AlreadyKickedException;
import com.tadak.chatroomservice.domain.chatroom.exception.NotFoundChatRoomException;
import com.tadak.chatroomservice.domain.chatroom.exception.NotRoomOwnerException;
import com.tadak.chatroomservice.domain.chatroom.repository.ChatRoomRepository;
import com.tadak.chatroomservice.domain.chatroom.dto.request.CreateChatroomRequest;
import com.tadak.chatroomservice.domain.chatroom.dto.response.CreateChatroomResponse;
import com.tadak.chatroomservice.domain.chatroom.entity.ChatRoom;
import com.tadak.chatroomservice.global.error.ErrorCode;
import lombok.RequiredArgsConstructor;
Expand Down Expand Up @@ -127,4 +125,13 @@ public ChangeOwnerResponse changeOwner(Long roomId, String username, String owne

return ChangeOwnerResponse.from(chatRoom);
}

public OneChatRoomResponse findChatRoom(Long roomId) {
ChatRoom chatRoom = findByChatRoom(roomId);

List<ChatMemberResponse> chatMemberResponses = chatRoom.getChatMembers().stream()
.map(ChatMemberResponse::from).toList();

return OneChatRoomResponse.of(chatRoom, chatMemberResponses);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.tadak.chatroomservice.global.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

@Configuration
public class CorsConfig {
@Bean
public CorsFilter corsFilter() {
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
CorsConfiguration config = new CorsConfiguration();
config.setAllowCredentials(true);
config.addAllowedOriginPattern("*");
config.addAllowedHeader("*");
config.addAllowedMethod("*");
config.addExposedHeader("Accesstoken, Refreshtoken");

source.registerCorsConfiguration("/**", config);
return new CorsFilter(source);
}
}

0 comments on commit 42de980

Please sign in to comment.