Skip to content

Commit

Permalink
[FEAT] 상담사 마이페이지 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
gyurili committed Aug 5, 2024
1 parent 5c5f37c commit 4445c4c
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import lombok.Getter;
import lombok.NoArgsConstructor;

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

Expand All @@ -24,6 +25,8 @@ public class CounselorResponseDTO {
private List<String> career = new ArrayList<>();
// 상담 횟수
private Integer count;
// 가장 처음 보낸 채팅 날짜
private LocalDateTime start;
// 어드민 계정인지 여부
private String role;

Expand All @@ -34,6 +37,7 @@ public static CounselorResponseDTO toDto(CounselorEntity entity) {
.specialty(entity.getSpecialty())
.career(entity.getCareer())
.count(entity.getCount())
.start(entity.getStart())
.role(entity.getRole())
.build();
}
Expand Down
12 changes: 7 additions & 5 deletions src/main/java/com/viewmore/poksin/entity/CounselorEntity.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package com.viewmore.poksin.entity;

import jakarta.persistence.Entity;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.*;

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

@Entity
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class CounselorEntity extends MainUserEntity{
Expand All @@ -22,13 +21,16 @@ public class CounselorEntity extends MainUserEntity{
private List<String> career = new ArrayList<>();
// 상담 횟수
private Integer count;
// 첫 채팅 날짜
private LocalDateTime start;

@Builder(builderMethodName = "counselorEntityBuilder")
public CounselorEntity(String username, String password, String phoneNum, String specialty, List<String> career, Integer count, String role) {
public CounselorEntity(String username, String password, String phoneNum, String specialty, List<String> career, Integer count, LocalDateTime start, String role) {
super(username, password, role);
this.phoneNum = phoneNum;
this.specialty = specialty;
this.career = career;
this.count = count;
this.start = start;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ public interface ChatMessageRepository extends JpaRepository<ChatMessageEntity,
@Query("SELECT c FROM ChatMessageEntity c WHERE c.sender = :username ORDER BY c.timestamp DESC")
List<ChatMessageEntity> findTopBySenderOrderByTimestampDesc(@Param("username") String username);

List<ChatMessageEntity> findBySender(String username);

default Optional<ChatMessageEntity> findLatestBySender(String username) {
List<ChatMessageEntity> results = findTopBySenderOrderByTimestampDesc(username);
return results.isEmpty() ? Optional.empty() : Optional.of(results.get(0));
}
}
}
23 changes: 22 additions & 1 deletion src/main/java/com/viewmore/poksin/service/CounselorService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.viewmore.poksin.dto.user.CounselorRegisterDTO;
import com.viewmore.poksin.dto.user.CounselorResponseDTO;
import com.viewmore.poksin.entity.ChatMessageEntity;
import com.viewmore.poksin.repository.ChatMessageRepository;
import com.viewmore.poksin.entity.CounselorEntity;
import com.viewmore.poksin.exception.DuplicateUsernameException;
import com.viewmore.poksin.repository.CounselorRepository;
Expand All @@ -12,17 +14,24 @@
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Service
@RequiredArgsConstructor
public class CounselorService {

@Autowired
private final UserRepository userRepository;

@Autowired
private final CounselorRepository counselorRepository;

private final BCryptPasswordEncoder bCryptPasswordEncoder;
@Autowired
private final ChatMessageRepository chatMessageRepository;

private final BCryptPasswordEncoder bCryptPasswordEncoder;

@Transactional
public void registerCounselor(CounselorRegisterDTO counselorRegisterDTO) {
Expand Down Expand Up @@ -59,6 +68,18 @@ public CounselorResponseDTO counselorMypage(String username) {
CounselorEntity user = counselorRepository.findByUsername(username)
.orElseThrow(() -> new UsernameNotFoundException("해당 사용자 이름을 가진 사용자를 찾을 수 없습니다: " + username));

// 채팅 메시지 조회
List<ChatMessageEntity> messages = chatMessageRepository.findBySender(username);

int count = messages.size();
LocalDateTime start = messages.stream()
.map(ChatMessageEntity::getTimestamp)
.min(LocalDateTime::compareTo)
.orElse(null);

user.setCount(count);
user.setStart(start);

return CounselorResponseDTO.toDto(user);
}
}

0 comments on commit 4445c4c

Please sign in to comment.