Skip to content

Commit

Permalink
Merge pull request #105 from Me1tingPot/feature/#104
Browse files Browse the repository at this point in the history
feat : ์ฑ„ํŒ… ๋ฉ”์„ธ์ง€ ์กฐํšŒ ๊ธฐํš ๋ณ€๊ฒฝ ์ ์šฉ
  • Loading branch information
yewonahn authored Jun 21, 2024
2 parents 94d863f + 6ca3a4b commit d3191ec
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public ResponseEntity<ResponseData> updateAlarmStatus(

// [CHECK] PageResponse
@PostMapping("/chat/{chatRoomId}")
@Operation(summary = "์ฑ„ํŒ…๋ฐฉ ์ฑ„ํŒ… ๋‚ด์—ญ ์กฐํšŒ", description = "์ฑ„ํŒ…๋ฐฉ ์ž…์žฅ ํ›„, ์ฑ„ํŒ…๋ฐฉ ๋ฉ”์‹œ์ง€ ์กฐํšŒ. (์œ„์น˜ ๊ณ ์ •) ์ขŒ์ธก : ์ฃผ์ตœ์ž๊ฐ€ ์ „์†กํ•œ ๋ฉ”์„ธ์ง€ / ์šฐ์ธก : ์ฐธ์—ฌ์ž๊ฐ€ ์ „์†กํ•œ ๋ฉ”์„ธ์ง€")
@Operation(summary = "์ฑ„ํŒ…๋ฐฉ ์ฑ„ํŒ… ๋‚ด์—ญ ์กฐํšŒ", description = "์ฑ„ํŒ…๋ฐฉ ์ž…์žฅ ํ›„, ์ฑ„ํŒ…๋ฐฉ ๋ฉ”์‹œ์ง€ ์กฐํšŒ")
@ApiResponses(value = {
@ApiResponse(responseCode = "OK", description = "์ฑ„ํŒ…๋ฐฉ ์ฑ„ํŒ… ๋‚ด์—ญ ์กฐํšŒ ์„ฑ๊ณต"),
@ApiResponse(responseCode = "NOT_FOUND", description = "์ฑ„ํŒ…๋ฐฉ ์ •๋ณด๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค"),
Expand All @@ -68,17 +68,6 @@ public ResponseEntity<ResponseData<ChatMessagePageResponse>> getChatMessage(
@PathVariable("chatRoomId") Long chatRoomId,
@RequestBody @Valid PageGetRequest pageGetRequest
) {
return ResponseData.toResponseEntity(CHAT_MESSAGE_GET_SUCCESS, chatRoomQueryService.getChatMessage(chatRoomId, pageGetRequest));
}

@GetMapping("/chat/{chatRoomId}/detail")
@Operation(summary = "์ฑ„ํŒ…๋ฐฉ ์ƒ๋‹จ", description = "์ฑ„ํŒ…๋ฐฉ ์ƒ๋‹จ ์œ„์น˜. ์ฑ„ํŒ…๋ฐฉ ์ •๋ณด")
@ApiResponses(value = {
@ApiResponse(responseCode = "OK", description = "์ฑ„ํŒ…๋ฐฉ ์ƒ๋‹จ ์กฐํšŒ ์„ฑ๊ณต"),
@ApiResponse(responseCode = "NOT_FOUND", description = "์ฑ„ํŒ…๋ฐฉ ์ •๋ณด๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค"),
@ApiResponse(responseCode = "BAD_REQUEST", description = "์ฑ„ํŒ…๋ฐฉ ์ƒ๋‹จ ์กฐํšŒ ์‹คํŒจ")
})
public ResponseEntity<ResponseData<ChatRoomDetailGetResponse>> getChatRoomDetail(@PathVariable("chatRoomId") Long chatRoomId) {
return ResponseData.toResponseEntity(CHAT_DETAIL_GET_SUCCESS, chatRoomQueryService.getRoomDetail(chatRoomId));
return ResponseData.toResponseEntity(CHAT_MESSAGE_GET_SUCCESS, chatRoomQueryService.getChatMessages(chatRoomId, pageGetRequest));
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,30 @@
package meltingpot.server.chat.dto;

import meltingpot.server.domain.entity.AccountProfileImage;
import meltingpot.server.domain.entity.chat.ChatMessage;
import meltingpot.server.domain.entity.chat.enums.Role;
import org.springframework.data.domain.Slice;

public record ChatMessageGetResponse(
Long chatMessageId,
String content,
Role role
Role memberRole,
String memberName,
String imageKey
) {
public static ChatMessageGetResponse from(ChatMessage chatMessage) {
String thumbnailImageKey = chatMessage.getChatRoom().getParty().getAccount().getProfileImages().stream()
.filter(AccountProfileImage::isThumbnail)
.map(AccountProfileImage::getImageKey)
.findFirst()
.orElse(null);

return new ChatMessageGetResponse(
chatMessage.getId(),
chatMessage.getContent(),
chatMessage.getRole()
chatMessage.getRole(),
chatMessage.getAccount().getUsername(),
thumbnailImageKey
);
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,34 @@
package meltingpot.server.chat.dto;

import meltingpot.server.domain.entity.AccountProfileImage;
import meltingpot.server.domain.entity.chat.ChatMessage;
import meltingpot.server.domain.entity.chat.ChatRoom;
import org.springframework.data.domain.Slice;

import java.util.List;

public record ChatMessagePageResponse(
List<ChatMessageGetResponse> chatMessageGetResponseList,
Long chatRoomId,
String hostImageKey,
String title,
int memberCnt,
Boolean isFirst,
Boolean hasNext
) {
public static ChatMessagePageResponse from(Slice<ChatMessage> chatMessagesSlice) {
public static ChatMessagePageResponse from(Slice<ChatMessage> chatMessagesSlice, ChatRoom chatRoom) {
String thumbnailImageKey = chatRoom.getParty().getAccount().getProfileImages().stream()
.filter(AccountProfileImage::isThumbnail)
.map(AccountProfileImage::getImageKey)
.findFirst()
.orElse(null);

return new ChatMessagePageResponse(
chatMessagesSlice.stream().map(ChatMessageGetResponse::from).toList(),
chatRoom.getId(),
thumbnailImageKey,
chatRoom.getParty().getPartySubject(),
chatRoom.getParty().getPartyParticipants().size(),
chatMessagesSlice.isFirst(),
chatMessagesSlice.hasNext()
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import static meltingpot.server.util.ResponseCode.CHAT_ROOM_NOT_FOUND;
import static meltingpot.server.util.ResponseCode.PARTY_NOT_FOUND;

@Slf4j
Expand All @@ -30,22 +31,16 @@ public class ChatRoomQueryService {
private final ChatMessageRepository chatMessageRepository;
private final PartyRepository partyRepository;

public ChatRoomDetailGetResponse getRoomDetail(Long chatRoomId) {
Party party = partyRepository.findByChatRoomId(chatRoomId)
.orElseThrow(() -> new ResourceNotFoundException(PARTY_NOT_FOUND));
return ChatRoomDetailGetResponse.from(party);
}

// [CHECK] 1. slice or page or list 2. PageResponse api
public ChatMessagePageResponse getChatMessage(Long chatRoomId, PageGetRequest pageGetRequest) {
public ChatMessagePageResponse getChatMessages(Long chatRoomId, PageGetRequest pageGetRequest) {
ChatRoom chatRoom = chatRoomRepository.findById(chatRoomId)
.orElseThrow(() -> new IllegalArgumentException("ChatRoom not found"));
.orElseThrow(() -> new ResourceNotFoundException(CHAT_ROOM_NOT_FOUND));

PageRequest pageRequest = PageRequest.of(pageGetRequest.page(), pageGetRequest.size(), Sort.by(Sort.Direction.DESC, "id"));

Slice<ChatMessage> chatMessagesSlice = chatMessageRepository.findAllByChatRoomId(chatRoom.getId(), pageRequest);

return ChatMessagePageResponse.from(chatMessagesSlice);
return ChatMessagePageResponse.from(chatMessagesSlice, chatRoom);
}

public ChatRoomsPageResponse getChatRooms(Long userId, PageGetRequest pageGetRequest) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public ChatMessage createChatMessage(String sessionId, ChatMessageSendRequest ch
ChatMessage newChatMessage = ChatMessage.builder()
.chatRoom(chatRoom)
.content(chatMessageSendRequest.content())
.userId(account.getId())
.account(account)
.role(role)
.build();

Expand Down Expand Up @@ -90,7 +90,7 @@ private Account getAccount(ChatRoomUser chatRoomUser) {
}

private boolean isSender(Account account, ChatMessage chatMessage) {
return account.getId().equals(chatMessage.getUserId());
return account.getId().equals(chatMessage.getAccount().getId());
}

private void sendNotificationOrMessage(Account account, ChatMessage chatMessage) {
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/meltingpot/server/domain/entity/Account.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import jakarta.persistence.*;
import lombok.*;
import jakarta.validation.constraints.NotNull;
import meltingpot.server.domain.entity.chat.ChatMessage;
import meltingpot.server.domain.entity.comment.Comment;
import meltingpot.server.domain.entity.comment.CommentImage;
import meltingpot.server.domain.entity.common.BaseEntity;
Expand Down Expand Up @@ -72,6 +73,9 @@ public class Account extends BaseEntity {
@OneToMany(mappedBy = "account")
private List<Post> posts = new ArrayList<>();

@OneToMany(mappedBy = "account")
private List<ChatMessage> chatMessages = new ArrayList<>();

@Builder.Default
@OneToMany(mappedBy = "account")
private List<AccountRole> accountRoles = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import jakarta.persistence.*;
import jakarta.validation.constraints.NotNull;
import lombok.*;
import meltingpot.server.domain.entity.Account;
import meltingpot.server.domain.entity.chat.enums.Role;
import meltingpot.server.domain.entity.common.BaseEntity;

Expand All @@ -26,11 +27,17 @@ public class ChatMessage extends BaseEntity {
private String content;

@NotNull
@Column(name = "user_id")
private Long userId;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id")
private Account account;

@NotNull
@Column(name = "is_leader")
@Enumerated(EnumType.STRING)
private Role role;

public void setAccount(Account account) {
this.account = account;
account.getChatMessages().add(this);
}
}

0 comments on commit d3191ec

Please sign in to comment.