Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.example.team4backend.controller;

import com.example.team4backend.common.response.ApiResult;
import com.example.team4backend.dto.MessageListResponse;
import com.example.team4backend.security.CustomUserDetails;
import com.example.team4backend.service.MessageService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Tag(name = "Message", description = "메시지 관리 API")
@RestController
@RequestMapping("/messages")
@RequiredArgsConstructor
public class MessageController {

private final MessageService messageService;

@Operation(summary = "메시지 리스트 조회", description = "대상자별 이름, 추천 서두, 마지막 연락 날짜를 리스트로 조회합니다.")
@GetMapping
public ResponseEntity<ApiResult<List<MessageListResponse>>> getMessageList(
@AuthenticationPrincipal CustomUserDetails userDetails
) {
List<MessageListResponse> messageList = messageService.getMessageList(userDetails.getId());
return ResponseEntity.ok(ApiResult.ok(messageList));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ public class TargetPerson extends BaseTimeEntity {

private Instant lastMessageDate;

@Column(columnDefinition = "TEXT")
private String recommendedOpening;

private Instant deletedAt;

@Builder
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.example.team4backend.dto;

import com.example.team4backend.domain.TargetPerson;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.v3.oas.annotations.media.Schema;

import java.time.Instant;

@Schema(description = "메시지 리스트 응답")
public record MessageListResponse(
@Schema(description = "대상자 ID", example = "1")
Long targetId,

@Schema(description = "대상자 이름", example = "홍길동")
String name,

@Schema(description = "추천 서두", example = "안녕하세요! 오랜만이에요")
String recommendedOpening,

@Schema(description = "마지막 연락 날짜", example = "2024-01-15T10:30:00Z")
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss'Z'", timezone = "UTC")
Instant lastMessageDate
) {
public static MessageListResponse of(TargetPerson target, String recommendedOpening) {
return new MessageListResponse(
target.getId(),
target.getName(),
recommendedOpening,
target.getLastMessageDate()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,9 @@ public interface TargetPersonRepository extends JpaRepository<TargetPerson, Long
int updateLastMessageDate(@Param("targetId") Long targetId,
@Param("userId") Long userId,
@Param("date") Instant date);

@Modifying
@Query("UPDATE TargetPerson t SET t.recommendedOpening = :message WHERE t.id = :targetId AND t.deletedAt IS NULL")
int updateRecommendedOpening(@Param("targetId") Long targetId,
@Param("message") String message);
}
26 changes: 26 additions & 0 deletions src/main/java/com/example/team4backend/service/MessageService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.example.team4backend.service;

import com.example.team4backend.domain.TargetPerson;
import com.example.team4backend.dto.MessageListResponse;
import com.example.team4backend.repository.TargetPersonRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@RequiredArgsConstructor
public class MessageService {

private final TargetPersonRepository targetPersonRepository;

@Transactional(readOnly = true)
public List<MessageListResponse> getMessageList(Long userId) {
List<TargetPerson> targets = targetPersonRepository.findAllByUserIdWithDetailsAndDeletedAtIsNull(userId);

return targets.stream()
.map(target -> MessageListResponse.of(target, target.getRecommendedOpening()))
.toList();
}
}