Skip to content

Commit

Permalink
feat: 챗봇 대화 조회 API 구현 (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
Haewonny committed Feb 7, 2024
1 parent a15162b commit a001a38
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import ewha.lux.once.global.common.ResponseCode;
import ewha.lux.once.global.common.UserAccount;
import lombok.RequiredArgsConstructor;
import org.springframework.data.repository.query.Param;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
Expand All @@ -27,4 +28,14 @@ public CommonResponse<?> mypageInfo(@AuthenticationPrincipal UserAccount user) {
return new CommonResponse<>(e.getStatus());
}
}

// [Get] 챗봇 대화 조회
@GetMapping("/chathistory")
public CommonResponse<?> chatHistory(@AuthenticationPrincipal UserAccount user, @Param("month") String month) {
try {
return new CommonResponse<>(ResponseCode.SUCCESS, mypageService.getChatHistory(user.getUsers(), month));
} catch (CustomException e) {
return new CommonResponse<>(e.getStatus());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package ewha.lux.once.domain.mypage.dto;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;

public class ChatHistoryResponseDto {

@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public static class ChatHistoryDto {
private int chatCount;
List<ChatListDto> chatList;
}

@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public static class ChatListDto {
private Long chatId;
private String keyword;
private String cardName;
private String chatDate;
private String chatTime;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,19 @@

import ewha.lux.once.domain.card.entity.OwnedCard;
import ewha.lux.once.domain.home.entity.ChatHistory;
import ewha.lux.once.domain.mypage.dto.ChatHistoryResponseDto;
import ewha.lux.once.domain.mypage.dto.MypageResponseDto;
import ewha.lux.once.domain.user.entity.Users;
import ewha.lux.once.global.common.CustomException;
import ewha.lux.once.global.common.ResponseCode;
import ewha.lux.once.global.repository.ChatHistoryRepository;
import ewha.lux.once.global.repository.OwnedCardRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.List;

Expand Down Expand Up @@ -54,4 +57,37 @@ public MypageResponseDto getMypageInfo(Users nowUser) throws CustomException {
return mypageResponseDto;

}

public ChatHistoryResponseDto.ChatHistoryDto getChatHistory(Users nowUser, String month) throws CustomException {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

LocalDateTime localDateTime = LocalDate.parse(month + "-01", formatter).atStartOfDay();
LocalDateTime startDate = localDateTime.withDayOfMonth(1);
LocalDateTime endDate = localDateTime.withDayOfMonth(localDateTime.toLocalDate().lengthOfMonth()).plusDays(1);

List<ChatHistory> chatList = chatHistoryRepository.findByUsersAndCreatedAtBetween(nowUser, startDate, endDate);

List<ChatHistoryResponseDto.ChatListDto> chatListDto = chatList.stream()
.map(chatHistory -> {
LocalDateTime createdAt = chatHistory.getCreatedAt();

String chatDate = createdAt.format(DateTimeFormatter.ofPattern("dd.MM"));
String chatTime = createdAt.format(DateTimeFormatter.ofPattern("HH:mm"));

return new ChatHistoryResponseDto.ChatListDto(
chatHistory.getId(),
chatHistory.getKeyword(),
chatHistory.getCardName(),
chatDate,
chatTime
);
}).toList();

ChatHistoryResponseDto.ChatHistoryDto chatHistoryDto = ChatHistoryResponseDto.ChatHistoryDto.builder()
.chatCount(chatList.size())
.chatList(chatListDto)
.build();

return chatHistoryDto;
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package ewha.lux.once.global.repository;

import ewha.lux.once.domain.home.entity.ChatHistory;
import ewha.lux.once.domain.mypage.dto.ChatHistoryResponseDto;
import ewha.lux.once.domain.user.entity.Users;
import org.springframework.data.jpa.repository.JpaRepository;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

public interface ChatHistoryRepository extends JpaRepository<ChatHistory, Long> {
List<ChatHistory> findByUsers(Users users);
List<ChatHistory> findByUsersAndHasPaidIsTrueAndCreatedAtBetween(Users nowUser, LocalDateTime startOfMonth, LocalDateTime endOfMonth);
List<ChatHistory> findByUsersAndCreatedAtBetween(Users nowUser, LocalDateTime startDate, LocalDateTime endDate);
}

0 comments on commit a001a38

Please sign in to comment.