-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #81 from Att-ies/Feature/55-get-chat-room-id
Feature/55 post chat room
- Loading branch information
Showing
10 changed files
with
431 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
src/main/java/com/sptp/backend/chat_room/service/ChatRoomService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package com.sptp.backend.chat_room.service; | ||
|
||
import com.sptp.backend.art_work.repository.ArtWorkRepository; | ||
import com.sptp.backend.chat_room.repository.ChatRoom; | ||
import com.sptp.backend.chat_room.repository.ChatRoomRepository; | ||
import com.sptp.backend.chat_room.web.dto.ChatRoomDetailResponse; | ||
import com.sptp.backend.common.exception.CustomException; | ||
import com.sptp.backend.common.exception.ErrorCode; | ||
import com.sptp.backend.member.repository.MemberRepository; | ||
import com.sptp.backend.message.repository.Message; | ||
import com.sptp.backend.message.repository.MessageRepository; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.stream.Collectors; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
@Transactional | ||
public class ChatRoomService { | ||
|
||
private final ChatRoomRepository chatRoomRepository; | ||
private final MemberRepository memberRepository; | ||
private final ArtWorkRepository artWorkRepository; | ||
private final MessageRepository messageRepository; | ||
|
||
public long createChatRoom(Long loginMemberId, Long artistId, Long artWorkId) { | ||
|
||
Optional<ChatRoom> chatRoomOptional = | ||
chatRoomRepository.findByMemberIdAndArtistIdAndArtWorkId(loginMemberId, artistId, artWorkId); | ||
|
||
// 기존 채팅방이 존재하는 경우 해당 id 반환 | ||
if (chatRoomOptional.isPresent()) { | ||
return chatRoomOptional.get().getId(); | ||
} | ||
|
||
ChatRoom chatRoom = ChatRoom.builder() | ||
.member(memberRepository.findById(loginMemberId) | ||
.orElseThrow(() -> new CustomException(ErrorCode.NOT_FOUND_MEMBER))) | ||
.artist(memberRepository.findById(artistId) | ||
.orElseThrow(() -> new CustomException(ErrorCode.NOT_FOUND_ARTIST))) | ||
.artWork(artWorkRepository.findById(artWorkId) | ||
.orElseThrow(() -> new CustomException(ErrorCode.NOT_FOUND_ARTWORK))) | ||
.build(); | ||
|
||
return chatRoomRepository.save(chatRoom).getId(); | ||
} | ||
|
||
public ChatRoomDetailResponse getChatRoomDetail(Long chatRoomId) { | ||
|
||
ChatRoom chatRoom = chatRoomRepository.findById(chatRoomId) | ||
.orElseThrow(() -> new CustomException(ErrorCode.NOT_FOUND_CHAT_ROOM)); | ||
List<Message> messages = messageRepository.findAllByChatRoomOrderByIdAsc(chatRoom); | ||
|
||
return ChatRoomDetailResponse.builder() | ||
.chatRoomId(chatRoomId) | ||
.artist(ChatRoomDetailResponse.MemberDto.from(chatRoom.getArtist())) | ||
.member(ChatRoomDetailResponse.MemberDto.from(chatRoom.getMember())) | ||
.messages(messages.stream() | ||
.map(ChatRoomDetailResponse.MessageDto::from) | ||
.collect(Collectors.toList())) | ||
.build(); | ||
} | ||
|
||
} |
39 changes: 39 additions & 0 deletions
39
src/main/java/com/sptp/backend/chat_room/web/ChatRoomController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.sptp.backend.chat_room.web; | ||
|
||
import com.sptp.backend.chat_room.service.ChatRoomService; | ||
import com.sptp.backend.chat_room.web.dto.ChatRoomCreateRequest; | ||
import com.sptp.backend.chat_room.web.dto.ChatRoomDetailResponse; | ||
import com.sptp.backend.jwt.service.dto.CustomUserDetails; | ||
import lombok.RequiredArgsConstructor; | ||
import org.apache.http.HttpHeaders; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.security.core.annotation.AuthenticationPrincipal; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
import javax.validation.Valid; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("/chat-rooms") | ||
public class ChatRoomController { | ||
|
||
private final ChatRoomService chatRoomService; | ||
|
||
@PostMapping | ||
public ResponseEntity<Void> createChatRoom(@Valid @RequestBody ChatRoomCreateRequest chatRoomCreateRequest, | ||
@AuthenticationPrincipal CustomUserDetails userDetails) { | ||
|
||
long chatRoomId = chatRoomService.createChatRoom(userDetails.getMember().getId(), | ||
chatRoomCreateRequest.getArtistId(), chatRoomCreateRequest.getArtWorkId()); | ||
|
||
return ResponseEntity.status(HttpStatus.MOVED_PERMANENTLY) | ||
.header(HttpHeaders.LOCATION, "/chat-rooms/" + chatRoomId).build(); | ||
} | ||
|
||
@GetMapping("/{chatRoomId}") | ||
public ResponseEntity<ChatRoomDetailResponse> getChatRoomDetail(@PathVariable Long chatRoomId) { | ||
|
||
return ResponseEntity.ok(chatRoomService.getChatRoomDetail(chatRoomId)); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/com/sptp/backend/chat_room/web/dto/ChatRoomCreateRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.sptp.backend.chat_room.web.dto; | ||
|
||
import com.sptp.backend.art_work.repository.ArtWork; | ||
import com.sptp.backend.member.repository.Member; | ||
import lombok.*; | ||
|
||
import javax.validation.constraints.NotNull; | ||
|
||
@Getter | ||
@Setter | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
@Builder | ||
public class ChatRoomCreateRequest { | ||
|
||
@NotNull(message = "작가 고유 번호는 필수입니다.") | ||
private Long artistId; | ||
|
||
@NotNull(message = "작품 고유 번호는 필수입니다.") | ||
private Long artWorkId; | ||
} |
52 changes: 52 additions & 0 deletions
52
src/main/java/com/sptp/backend/chat_room/web/dto/ChatRoomDetailResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package com.sptp.backend.chat_room.web.dto; | ||
|
||
import com.fasterxml.jackson.annotation.JsonFormat; | ||
import com.sptp.backend.member.repository.Member; | ||
import com.sptp.backend.message.repository.Message; | ||
import lombok.*; | ||
|
||
import java.time.LocalDateTime; | ||
import java.util.List; | ||
|
||
@Data | ||
@Builder | ||
public class ChatRoomDetailResponse { | ||
|
||
private Long chatRoomId; | ||
private MemberDto artist; | ||
private MemberDto member; | ||
private List<MessageDto> messages; | ||
|
||
@Data | ||
@Builder | ||
public static class MemberDto { | ||
private Long id; | ||
private String name; | ||
// private String responseTime; 추후 구현 예정 | ||
|
||
public static MemberDto from(Member member) { | ||
return MemberDto.builder() | ||
.id(member.getId()) | ||
.name(member.getNickname()) | ||
.build(); | ||
} | ||
} | ||
|
||
@Data | ||
@Builder | ||
public static class MessageDto { | ||
private Long senderId; | ||
private String message; | ||
|
||
@JsonFormat(pattern = "yyyy-MM-dd-HH-mm-ss") | ||
private LocalDateTime sendTime; | ||
|
||
public static MessageDto from(Message message) { | ||
return MessageDto.builder() | ||
.senderId(message.getSender().getId()) | ||
.message(message.getMessage()) | ||
.sendTime(message.getCreatedDate()) | ||
.build(); | ||
} | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/com/sptp/backend/message/repository/MessageRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,13 @@ | ||
package com.sptp.backend.message.repository; | ||
|
||
import com.sptp.backend.chat_room.repository.ChatRoom; | ||
import org.springframework.data.jpa.repository.EntityGraph; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
import java.util.List; | ||
|
||
public interface MessageRepository extends JpaRepository<Message, Long> { | ||
|
||
@EntityGraph(attributePaths = {"sender"}) | ||
List<Message> findAllByChatRoomOrderByIdAsc(ChatRoom chatRom); | ||
} |
Oops, something went wrong.