Skip to content

Commit

Permalink
Merge pull request #111 from gague-jinsim-in-jadeul/fix/110_change_ti…
Browse files Browse the repository at this point in the history
…mezone

🐛 ($chat) fix chat time bug
  • Loading branch information
MinkeySon authored Jan 16, 2025
2 parents bbcd149 + febcee8 commit 55656a3
Show file tree
Hide file tree
Showing 12 changed files with 202 additions and 69 deletions.
4 changes: 2 additions & 2 deletions src/main/java/org/gagu/gagubackend/chat/dao/ChatDAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,13 @@ public interface ChatDAO {
* @param nickname
* @return
*/
ResponseChatDto askEstimate(RequestChatContentsDto message, String nickname);
ResponseChatDto askEstimate(RequestChatContentsDto message, Long roomNumber, String nickname);

/**
* 견적서 완료
* @param message
* @param nickname
* @return
*/
ResponseChatDto completeEstimate(RequestChatContentsDto message, String nickname);
ResponseChatDto completeEstimate(RequestChatContentsDto message, Long roomNumber, String nickname);
}
88 changes: 49 additions & 39 deletions src/main/java/org/gagu/gagubackend/chat/dao/impl/ChatDAOImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.google.firebase.messaging.Notification;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.gagu.gagubackend.chat.config.FCMConfig;
import org.gagu.gagubackend.chat.dao.ChatDAO;
import org.gagu.gagubackend.chat.domain.ChatContents;
import org.gagu.gagubackend.chat.domain.ChatRoom;
Expand All @@ -28,16 +27,13 @@
import org.gagu.gagubackend.auth.domain.User;
import org.gagu.gagubackend.auth.dto.request.RequestUserInfoDto;
import org.gagu.gagubackend.auth.repository.UserRepository;
import org.gagu.gagubackend.global.service.TimeService;
import org.springframework.beans.factory.annotation.Value;
import org.gagu.gagubackend.global.util.TimeUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
Expand All @@ -52,7 +48,7 @@ public class ChatDAOImpl implements ChatDAO {
private final ChatContentsRepository chatContentsRepository;
private final EstimateRepository estimateRepository;
private final FirebaseMessaging firebaseMessaging;
private final TimeService timeService;
private final TimeUtil timeUtil;

@Override
public ResponseEntity<?> createChatRoom(RequestUserInfoDto userInfoDto, RequestCreateChatRoomDto requestCreateChatRoomDto) {
Expand Down Expand Up @@ -161,9 +157,10 @@ public ResponseChatDto saveMessage(RequestChatContentsDto requestChatContentsDto
String contents = requestChatContentsDto.getChatContentsInfo().getContents();
User user = userOptional.get();
// 저장되는 채팅 내역
ChatContents chatContents = new ChatContents(timeService.makeTimeTemplate(),
ChatContents chatContents = new ChatContents(timeUtil.makeTimeTemplate(),
contents,
roomId,
requestChatContentsDto.getType().toString(),
user);

// 실제 전송되는 메세지
Expand All @@ -184,33 +181,39 @@ public ResponseChatDto saveMessage(RequestChatContentsDto requestChatContentsDto

@Override
public Page<ResponseChatContentsDto> getChatContents(String nickname, Pageable pageable, Long roomNumber) {
log.info("[chat] get chat contents room number : {}",roomNumber);
User user = userRepository.findByNickName(nickname);
Optional<ChatRoom> chatRoomList = chatRoomRepository.findById(roomNumber);
if(!chatRoomList.isEmpty()){
ChatRoom chatRoom = chatRoomList.get();
if(checkMember(chatRoom,user)){
Page<ChatContents> contentsList = chatContentsRepository.findByChatRoomId(roomNumber,pageable);
log.info("[GET-CHAT-CONTENTS] room number : {}", roomNumber);
Page<ChatContents> contentsList = chatContentsRepository.pageChatContents(roomNumber,pageable);

List<ResponseChatContentsDto> contentsDtoList = contentsList.stream()
List<ResponseChatContentsDto> contentsDtoList = contentsList.stream()
.map(v -> {
String messageType = v.getMessageType();
ResponseChatContentsDto dto = new ResponseChatContentsDto();
dto.setSendTime(v.getSendTime());
dto.setSender(v.getSender().getNickName());
dto.setMessage(v.getMessage());
dto.setChatRoomId(v.getChatRoomId());
return dto;

switch (messageType) {
case "SEND":
dto.generalBuilder(v);
break;
case "RESPONSE_ESTIMATE":
Optional<Estimate> optionalEstimate = estimateRepository.findEstimateById(v.getEstimateId());
if(optionalEstimate.isPresent()){
dto.resEstimateBuilder(v, optionalEstimate.get());
break;
}else{
dto.generalBuilder(v);
break;
}
case "REQUEST_ESTIMATE":
optionalEstimate = estimateRepository.findEstimateById(v.getChatRoomId());
if(optionalEstimate.isPresent()){
dto.reqEstimateBuilder(v, optionalEstimate.get());
break;
}else{
dto.generalBuilder(v);
break;
}
}
return dto;
}).collect(Collectors.toList());

return new PageImpl<>(contentsDtoList, pageable, contentsList.getTotalElements());

}else{
throw new NotMemberException("채팅방 조회 권한이 없습니다.");
}
}else{
throw new ChatRoomNotFoundException("채팅방이 존재하지 않습니다.");
}
return new PageImpl<>(contentsDtoList, pageable, contentsList.getTotalElements());
}

@Override
Expand Down Expand Up @@ -284,27 +287,37 @@ public Optional<ChatRoomMember> getChatRoomMember(String nickname, Long id) {
}

@Override
public ResponseChatDto askEstimate(RequestChatContentsDto message, String nickname) {
Optional<Estimate> estimateOptional = estimateRepository.findEstimateById(message.getEstimateInfo().getEstimateId());
public ResponseChatDto askEstimate(RequestChatContentsDto message, Long roomNumber, String nickname) {
Long estimateId = message.getEstimateInfo().getEstimateId();
Optional<Estimate> estimateOptional = estimateRepository.findEstimateById(estimateId);
if(estimateOptional.isPresent()){
User user = userRepository.findUserByNickname(nickname).get();
String time = timeUtil.makeTimeTemplate();

chatContentsRepository.save(new ChatContents(time, " ", roomNumber, message.getType().toString(), estimateId, user));

return ResponseChatDto.builder()
.type(message.getType())
.estimateInfo(new ResponseChatDto.EstimateInfo(estimateOptional.get()))
.nickName(nickname)
.time(timeService.makeTimeTemplate())
.time(time)
.build();
}else{
throw new NullPointerException("견적서를 찾을 수 없습니다.");
}
}

@Override
public ResponseChatDto completeEstimate(RequestChatContentsDto message, String nickname) {
public ResponseChatDto completeEstimate(RequestChatContentsDto message, Long roomNumber, String nickname) {
RequestChatContentsDto.EstimateInfo estimateInfo = message.getEstimateInfo();
Optional<Estimate> estimateOptional = estimateRepository.findEstimateById(estimateInfo.getEstimateId());
Long estimateId = estimateInfo.getEstimateId();
Optional<Estimate> estimateOptional = estimateRepository.findEstimateById(estimateId);

if(estimateOptional.isPresent()){
String time = timeUtil.makeTimeTemplate();
User user = userRepository.findUserByNickname(nickname).get();
chatContentsRepository.save(new ChatContents(time, " ", roomNumber, message.getType().toString(), estimateId, user));

Estimate estimate = estimateOptional.get();
estimate.setMakerName(nickname);

Expand All @@ -313,7 +326,7 @@ public ResponseChatDto completeEstimate(RequestChatContentsDto message, String n
.type(message.getType())
.estimateInfo(new ResponseChatDto.EstimateInfo(estimateOptional.get()))
.nickName(nickname)
.time(timeService.makeTimeTemplate())
.time(time)
.build();
}else{
log.error("[CHAT-DAO-IMPL] fail to get estimate info!");
Expand All @@ -333,9 +346,6 @@ private boolean checkWorkshopExist(String workShopName){
private String createChatRoomName(String buyerName, String sellerName){
return buyerName + "님과 " + sellerName + "과의 채팅방";
}
public boolean checkMember(ChatRoom roomId, User member){
return chatRoomMemberRepository.existsChatRoomMemberByRoomIdAndMember(roomId,member);
}
public boolean areUsersInSameRoom(User user, User workshop) {
return chatRoomMemberRepository.existsChatRoomByMembers(user, workshop);
}
Expand Down
18 changes: 16 additions & 2 deletions src/main/java/org/gagu/gagubackend/chat/domain/ChatContents.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import java.util.List;

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder
Expand All @@ -28,14 +27,29 @@ public class ChatContents {
@Column(nullable = false)
private Long chatRoomId;

@Column(nullable = false)
private String MessageType;

@Column(nullable = true)
private Long estimateId;

@ManyToOne
@JoinColumn
private User sender;

public ChatContents(String sendTime, String message, Long chatRoomId, User sender){
public ChatContents(String sendTime, String message, Long chatRoomId, String messageType, User sender){
this.sendTime = sendTime;
this.message = message;
this.chatRoomId = chatRoomId;
this.MessageType = messageType;
this.sender = sender;
}
public ChatContents(String sendTime, String message, Long chatRoomId, String messageType, Long estimateId, User sender){
this.sendTime = sendTime;
this.message = message;
this.chatRoomId = chatRoomId;
this.MessageType = messageType;
this.estimateId = estimateId;
this.sender = sender;
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package org.gagu.gagubackend.chat.dto.response;

import lombok.*;
import org.gagu.gagubackend.chat.domain.ChatContents;
import org.gagu.gagubackend.estimate.domain.Estimate;

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@ToString
Expand All @@ -12,5 +13,60 @@ public class ResponseChatContentsDto {
private String sendTime;
private String sender;
private String message;
private EstimateInfo estimateInfo;
private Long chatRoomId;

public void generalBuilder(ChatContents contents){
this.sendTime = contents.getSendTime();
this.sender = contents.getSender().getNickName();
this.message = contents.getMessage();
this.chatRoomId = contents.getChatRoomId();
}

public void reqEstimateBuilder(ChatContents contents, Estimate estimate){
generalBuilder(contents);
this.estimateInfo = new EstimateInfo();
this.estimateInfo.reqEstimateBuilder(estimate);
}
public void resEstimateBuilder(ChatContents contents, Estimate estimate){
generalBuilder(contents);
this.estimateInfo = new EstimateInfo();
this.estimateInfo.repEstimateBuilder(estimate);
}

@Getter
@AllArgsConstructor
@Builder
public static class EstimateInfo{
private Long estimateId;
private String furnitureName;
private String furniture2DUrl;
private String furnitureGlbUrl;
private String furnitureGltfUrl;
private String createdDate;
private String description;
private String price;
private String makerName;

public void repEstimateBuilder(Estimate estimate){
this.estimateId = estimate.getId();
this.furnitureName = estimate.getFurnitureName();
this.furniture2DUrl = estimate.getFurniture2DUrl();
this.furnitureGlbUrl = estimate.getFurnitureGlbUrl();
this.furnitureGltfUrl = estimate.getFurnitureGltfUrl();
this.createdDate = estimate.getCreatedDate();
this.price = estimate.getPrice();
this.makerName = estimate.getMakerName();
this.description = estimate.getDescription();
}
public void reqEstimateBuilder(Estimate estimate){
this.estimateId = estimate.getId();
this.furnitureName = estimate.getFurnitureName();
this.furniture2DUrl = estimate.getFurniture2DUrl();
this.furnitureGlbUrl = estimate.getFurnitureGlbUrl();
this.furnitureGltfUrl = estimate.getFurnitureGltfUrl();
this.createdDate = estimate.getCreatedDate();
}
public EstimateInfo(){}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

import jakarta.transaction.Transactional;
import org.gagu.gagubackend.chat.domain.ChatContents;
import org.gagu.gagubackend.chat.repository.custom.ChatContentsRepositoryCustom;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;

public interface ChatContentsRepository extends JpaRepository<ChatContents, Long> {
Page<ChatContents> findByChatRoomId(Long roomNumber, Pageable pageable);
public interface ChatContentsRepository extends JpaRepository<ChatContents, Long>, ChatContentsRepositoryCustom {
@Transactional
void deleteAllByChatRoomId(Long roomNumber);
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ public interface ChatRoomMemberRepository extends JpaRepository<ChatRoomMember,
boolean existsChatRoomMemberByRoomId(ChatRoom chatRoom);
List<ChatRoomMember> findAllByRoomId(ChatRoom chatRoom);
List<ChatRoomMember> findAllByMember(User user);
boolean existsChatRoomMemberByRoomIdAndMember(ChatRoom id, User member);
@Query("SELECT CASE WHEN COUNT(c1) > 0 THEN true ELSE false END FROM ChatRoomMember c1 " +
"JOIN ChatRoomMember c2 ON c1.roomId = c2.roomId " +
"WHERE c1.member = :userA AND c2.member = :userB")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package org.gagu.gagubackend.chat.repository.custom.impl;

import com.querydsl.jpa.JPQLQuery;
import com.querydsl.jpa.impl.JPAQueryFactory;
import jakarta.persistence.EntityManager;
import org.gagu.gagubackend.chat.domain.ChatContents;
import org.gagu.gagubackend.chat.domain.ChatRoomMember;
import org.gagu.gagubackend.chat.domain.QChatContents;
import org.gagu.gagubackend.chat.repository.custom.ChatContentsRepositoryCustom;
import org.gagu.gagubackend.chat.repository.custom.ChatRoomMemberRepositoryCustom;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.support.PageableExecutionUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class ChatContentsRepositoryCustomImpl implements ChatContentsRepositoryCustom {
private final JPAQueryFactory jpaQueryFactory;

public ChatContentsRepositoryCustomImpl(EntityManager em) {
this.jpaQueryFactory = new JPAQueryFactory(em);
}

@Override
public Page<ChatContents> pageChatContents(Long roomNumber, Pageable pageable) {
QChatContents qChatContents = QChatContents.chatContents;

int page = pageable.getPageNumber();
int pageSize = pageable.getPageSize();

long offset = (long) page * pageSize;

List<ChatContents> chatContentsList = jpaQueryFactory.select(qChatContents)
.from(qChatContents)
.where(qChatContents.chatRoomId.eq(roomNumber))
.orderBy(qChatContents.sendTime.desc())
.limit(pageable.getPageSize())
.offset(offset)
.fetch();

JPQLQuery<Long> count = jpaQueryFactory.select(qChatContents.count())
.from(qChatContents)
.where(qChatContents.chatRoomId.eq(roomNumber));

return PageableExecutionUtils.getPage(chatContentsList, pageable, count::fetchOne);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ public ResponseChatDto sendContents(RequestChatContentsDto message, Long roomNum
case "SEND":
return chatDAO.saveMessage(message,roomNumber,nickname);
case "REQUEST_ESTIMATE":
return chatDAO.askEstimate(message,nickname);
return chatDAO.askEstimate(message, roomNumber, nickname);
case "RESPONSE_ESTIMATE":
return chatDAO.completeEstimate(message,nickname);
return chatDAO.completeEstimate(message, roomNumber, nickname);
}
throw new RuntimeException();
}
Expand Down

This file was deleted.

Loading

0 comments on commit 55656a3

Please sign in to comment.